eee0f61b0744c7f306d90fa141df9dd82bd14858
[claws.git] / src / mimeview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtknotebook.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkctree.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkvpaned.h>
33 #include <gtk/gtktext.h>
34 #include <gtk/gtksignal.h>
35 #include <gtk/gtkmenu.h>
36 #include <gtk/gtkdnd.h>
37 #include <gtk/gtkselection.h>
38 #include <stdio.h>
39
40 #ifndef HAVE_APACHE_FNMATCH
41 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
42  * fnmatch.h */
43 #include <fnmatch.h>
44 #endif
45
46 #include "intl.h"
47 #include "main.h"
48 #include "mimeview.h"
49 #include "textview.h"
50 #include "procmime.h"
51 #include "summaryview.h"
52 #include "menu.h"
53 #include "filesel.h"
54 #include "alertpanel.h"
55 #include "inputdialog.h"
56 #include "utils.h"
57 #include "gtkutils.h"
58 #include "prefs_common.h"
59 #include "rfc2015.h"
60 #include "stock_pixmap.h"
61
62 typedef enum
63 {
64         COL_MIMETYPE = 0,
65         COL_SIZE     = 1,
66         COL_NAME     = 2
67 } MimeViewColumnPos;
68
69 #define N_MIMEVIEW_COLS 3
70
71 static void mimeview_set_multipart_tree         (MimeView       *mimeview,
72                                                  MimeInfo       *mimeinfo,
73                                                  GtkCTreeNode   *parent);
74 static GtkCTreeNode *mimeview_append_part       (MimeView       *mimeview,
75                                                  MimeInfo       *partinfo,
76                                                  GtkCTreeNode   *parent);
77 static void mimeview_show_message_part          (MimeView       *mimeview,
78                                                  MimeInfo       *partinfo);
79 static void mimeview_change_view_type           (MimeView       *mimeview,
80                                                  MimeViewType    type);
81 static void mimeview_clear                      (MimeView       *mimeview);
82
83 static void mimeview_selected           (GtkCTree       *ctree,
84                                          GtkCTreeNode   *node,
85                                          gint            column,
86                                          MimeView       *mimeview);
87 static void mimeview_start_drag         (GtkWidget      *widget,
88                                          gint            button,
89                                          GdkEvent       *event,
90                                          MimeView       *mimeview);
91 static gint mimeview_button_pressed     (GtkWidget      *widget,
92                                          GdkEventButton *event,
93                                          MimeView       *mimeview);
94 static gint mimeview_key_pressed        (GtkWidget      *widget,
95                                          GdkEventKey    *event,
96                                          MimeView       *mimeview);
97
98 static void mimeview_drag_data_get      (GtkWidget        *widget,
99                                          GdkDragContext   *drag_context,
100                                          GtkSelectionData *selection_data,
101                                          guint             info,
102                                          guint             time,
103                                          MimeView         *mimeview);
104
105 static void mimeview_display_as_text    (MimeView       *mimeview);
106 static void mimeview_save_as            (MimeView       *mimeview);
107 static void mimeview_save_all           (MimeView       *mimeview);
108 static void mimeview_launch             (MimeView       *mimeview);
109 static void mimeview_open_with          (MimeView       *mimeview);
110 static void mimeview_view_file          (const gchar    *filename,
111                                          MimeInfo       *partinfo,
112                                          const gchar    *cmdline);
113
114 static GtkItemFactoryEntry mimeview_popup_entries[] =
115 {
116         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
117         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
118         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
119         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL},
120         {N_("/Save _all..."),     NULL, mimeview_save_all,        0, NULL}
121 #if USE_GPGME
122         ,
123         {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
124 #endif
125 };
126
127 static GtkTargetEntry mimeview_mime_types[] =
128 {
129         {"text/uri-list", 0, 0}
130 };
131
132 GSList *mimeviewer_factories;
133 GSList *mimeviews;
134
135 MimeView *mimeview_create(MainWindow *mainwin)
136 {
137         MimeView *mimeview;
138
139         GtkWidget *notebook;
140         GtkWidget *vbox;
141         GtkWidget *paned;
142         GtkWidget *scrolledwin;
143         GtkWidget *ctree;
144         GtkWidget *mime_notebook;
145         GtkWidget *popupmenu;
146         GtkWidget *icon;
147         GtkItemFactory *popupfactory;
148         gchar *titles[N_MIMEVIEW_COLS];
149         gint n_entries;
150         gint i;
151
152         debug_print("Creating MIME view...\n");
153         mimeview = g_new0(MimeView, 1);
154
155         titles[COL_MIMETYPE] = _("MIME Type");
156         titles[COL_SIZE]     = _("Size");
157         titles[COL_NAME]     = _("Name");
158
159         notebook = gtk_notebook_new();
160         gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
161
162         vbox = gtk_vbox_new(FALSE, 0);
163         gtk_container_add(GTK_CONTAINER(notebook), vbox);
164         icon = stock_pixmap_widget(mainwin->window, STOCK_PIXMAP_MESSAGEVIEW_TEXT); 
165         gtk_widget_show(icon);
166         gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), vbox, icon);
167         
168         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
169         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
170                                        GTK_POLICY_AUTOMATIC,
171                                        GTK_POLICY_ALWAYS);
172         gtk_widget_set_usize(scrolledwin, -1, 80);
173
174         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
175         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
176         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
177         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
178                                            GTK_JUSTIFY_RIGHT);
179         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
180         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
181         for (i = 0; i < N_MIMEVIEW_COLS; i++)
182                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
183                                        GTK_CAN_FOCUS);
184         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
185
186         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
187                            GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
188         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
189                            GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
190         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
191                            GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
192         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
193                            GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
194         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
195                            GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
196
197         mime_notebook = gtk_notebook_new();
198         gtk_widget_show(mime_notebook);
199         GTK_WIDGET_UNSET_FLAGS(mime_notebook, GTK_CAN_FOCUS);
200         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
201         gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
202
203         paned = gtk_vpaned_new();
204         gtk_paned_add1(GTK_PANED(paned), scrolledwin);
205         gtk_paned_add2(GTK_PANED(paned), mime_notebook);
206         gtk_container_add(GTK_CONTAINER(notebook), paned);
207         icon = stock_pixmap_widget(mainwin->window, STOCK_PIXMAP_CLIP); 
208         gtk_widget_show(icon);
209         gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), paned, icon);
210         gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_RIGHT);
211
212         gtk_widget_show_all(notebook);
213
214         gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
215
216         n_entries = sizeof(mimeview_popup_entries) /
217                 sizeof(mimeview_popup_entries[0]);
218         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
219                                       "<MimeView>", &popupfactory, mimeview);
220
221         mimeview->notebook      = notebook;
222         mimeview->vbox          = vbox;
223         mimeview->paned         = paned;
224         mimeview->scrolledwin   = scrolledwin;
225         mimeview->ctree         = ctree;
226         mimeview->mime_notebook = mime_notebook;
227         mimeview->popupmenu     = popupmenu;
228         mimeview->popupfactory  = popupfactory;
229         mimeview->type          = -1;
230
231         mimeview->target_list   = gtk_target_list_new(mimeview_mime_types, 1); 
232         
233         mimeviews = g_slist_prepend(mimeviews, mimeview);
234
235         return mimeview;
236 }
237
238 void mimeview_init(MimeView *mimeview)
239 {
240         textview_init(mimeview->textview);
241
242         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
243                 GTK_WIDGET_PTR(mimeview->textview));
244 }
245
246 /* 
247  * Check whether the message is OpenPGP signed
248  */
249 #if USE_GPGME
250 static gboolean mimeview_is_signed(MimeView *mimeview)
251 {
252         MimeInfo *partinfo = NULL;
253
254         debug_print("mimeview_is signed of %p\n", mimeview);
255
256         if (!mimeview) return FALSE;
257         if (!mimeview->opened) return FALSE;
258
259         debug_print("mimeview_is_signed: open\n" );
260
261         if (!mimeview->file) return FALSE;
262
263         debug_print("mimeview_is_signed: file\n" );
264
265         partinfo = gtk_ctree_node_get_row_data
266                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
267         g_return_val_if_fail(partinfo != NULL, FALSE);
268
269         /* walk the tree and see whether there is a signature somewhere */
270         do {
271                 if (rfc2015_has_signature(partinfo))
272                         return TRUE;
273         } while ((partinfo = partinfo->parent) != NULL);
274
275         debug_print("mimeview_is_signed: FALSE\n" );
276
277         return FALSE;
278 }
279
280 static void set_unchecked_signature(MimeInfo *mimeinfo)
281 {
282         MimeInfo *sig_partinfo;
283
284         sig_partinfo = rfc2015_find_signature(mimeinfo);
285         if (sig_partinfo == NULL) return;
286
287         g_free(sig_partinfo->sigstatus);
288         sig_partinfo->sigstatus =
289                 g_strdup(_("Right-click here to verify the signature"));
290
291         g_free(sig_partinfo->sigstatus_full);
292         sig_partinfo->sigstatus_full = NULL;
293 }
294 #endif /* USE_GPGME */
295
296 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
297                            const gchar *file)
298 {
299         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
300         GtkCTreeNode *node;
301         FILE *fp;
302
303         mimeview_clear(mimeview);
304         textview_clear(mimeview->messageview->textview);
305
306         g_return_if_fail(file != NULL);
307         g_return_if_fail(mimeinfo != NULL);
308
309         mimeview->mimeinfo = mimeinfo;
310
311         mimeview->file = g_strdup(file);
312
313 #if USE_GPGME
314         if (prefs_common.auto_check_signatures && gpg_started) {
315                 if ((fp = fopen(file, "rb")) == NULL) {
316                         FILE_OP_ERROR(file, "fopen");
317                         return;
318                 }
319                 rfc2015_check_signature(mimeinfo, fp);
320                 fclose(fp);
321         } else
322                 set_unchecked_signature(mimeinfo);
323 #endif
324
325         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
326                                          mimeview);
327
328         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
329
330         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
331                                            mimeview_selected, mimeview);
332
333         /* search first text part */
334         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
335              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
336                 MimeInfo *partinfo;
337
338                 partinfo = gtk_ctree_node_get_row_data(ctree, node);
339                 if (partinfo &&
340                     (partinfo->mime_type == MIME_TEXT ||
341                      partinfo->mime_type == MIME_TEXT_HTML))
342                         break;
343         }
344         textview_show_message(mimeview->messageview->textview, mimeinfo, file);
345
346         if (!node)
347                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
348
349         if (node) {
350                 gtk_ctree_select(ctree, node);
351                 gtkut_ctree_set_focus_row(ctree, node);
352                 gtk_widget_grab_focus(mimeview->ctree);
353         }
354 }
355
356 void mimeview_destroy(MimeView *mimeview)
357 {
358         GSList *cur;
359         
360         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
361                 MimeViewer *viewer = (MimeViewer *) cur->data;
362                 gtk_container_remove(GTK_CONTAINER(mimeview->mime_notebook),
363                         GTK_WIDGET(viewer->get_widget(viewer)));
364                 viewer->destroy_viewer(viewer);
365         }
366         g_slist_free(mimeview->viewers);
367         gtk_target_list_unref(mimeview->target_list);
368
369         procmime_mimeinfo_free_all(mimeview->mimeinfo);
370         g_free(mimeview->file);
371         g_free(mimeview);
372
373         mimeviews = g_slist_remove(mimeviews, mimeview);
374 }
375
376 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
377 {
378         if (gtk_notebook_get_current_page
379                 (GTK_NOTEBOOK(mimeview->notebook)) == 0)
380                 return NULL;
381
382         return gtk_ctree_node_get_row_data
383                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
384 }
385
386 static void mimeview_set_multipart_tree(MimeView *mimeview,
387                                         MimeInfo *mimeinfo,
388                                         GtkCTreeNode *parent)
389 {
390         GtkCTreeNode *node;
391
392         g_return_if_fail(mimeinfo != NULL);
393
394         if (mimeinfo->children)
395                 mimeinfo = mimeinfo->children;
396
397         while (mimeinfo != NULL) {
398                 node = mimeview_append_part(mimeview, mimeinfo, parent);
399
400                 if (mimeinfo->children)
401                         mimeview_set_multipart_tree(mimeview, mimeinfo, node);
402                 else if (mimeinfo->sub &&
403                          mimeinfo->sub->mime_type != MIME_TEXT &&
404                          mimeinfo->sub->mime_type != MIME_TEXT_HTML)
405                         mimeview_set_multipart_tree(mimeview, mimeinfo->sub,
406                                                     node);
407                 mimeinfo = mimeinfo->next;
408         }
409 }
410
411 static gchar *get_part_name(MimeInfo *partinfo)
412 {
413 #if USE_GPGME
414         if (partinfo->sigstatus)
415                 return partinfo->sigstatus;
416         else
417 #endif
418         if (partinfo->name)
419                 return partinfo->name;
420         else if (partinfo->filename)
421                 return partinfo->filename;
422         else if (partinfo->description)
423                 return partinfo->description;
424         else
425                 return "";
426 }
427
428 static gchar *get_part_description(MimeInfo *partinfo)
429 {
430         if (partinfo->description)
431                 return partinfo->description;
432         else if (partinfo->name)
433                 return partinfo->name;
434         else if (partinfo->filename)
435                 return partinfo->filename;
436         else
437                 return "";
438 }
439
440 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
441                                           MimeInfo *partinfo,
442                                           GtkCTreeNode *parent)
443 {
444         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
445         GtkCTreeNode *node;
446         gchar *str[N_MIMEVIEW_COLS];
447
448         str[COL_MIMETYPE] =
449                 partinfo->content_type ? partinfo->content_type : "";
450         str[COL_SIZE] = to_human_readable(partinfo->size);
451         if (prefs_common.attach_desc)
452                 str[COL_NAME] = get_part_description(partinfo);
453         else
454                 str[COL_NAME] = get_part_name(partinfo);
455
456         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
457                                      NULL, NULL, NULL, NULL,
458                                      FALSE, TRUE);
459         gtk_ctree_node_set_row_data(ctree, node, partinfo);
460
461         return node;
462 }
463
464 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
465 {
466         FILE *fp;
467         const gchar *fname;
468 #if USE_GPGME
469         MimeInfo *pi;
470 #endif
471
472         if (!partinfo) return;
473
474 #if USE_GPGME
475         for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
476                 ;
477         fname = pi ? pi->plaintextfile : mimeview->file;
478 #else
479         fname = mimeview->file;
480 #endif /* USE_GPGME */
481         if (!fname) return;
482
483         if ((fp = fopen(fname, "rb")) == NULL) {
484                 FILE_OP_ERROR(fname, "fopen");
485                 return;
486         }
487
488         if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
489                 FILE_OP_ERROR(mimeview->file, "fseek");
490                 fclose(fp);
491                 return;
492         }
493
494         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
495         textview_show_part(mimeview->textview, partinfo, fp);
496
497         fclose(fp);
498 }
499
500 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
501 {
502         GSList *cur;
503         MimeViewerFactory *factory = NULL;
504         MimeViewer *viewer = NULL;
505
506 /*
507  * FNM_CASEFOLD is a GNU extension
508  * if its not defined copy the string to the stack and
509  * convert the copy to lower case
510  */
511 #ifndef FNM_CASEFOLD
512 #define FNM_CASEFOLD 0
513         Xstrdup_a(content_type, content_type, return NULL);
514         g_strdown((gchar *)content_type);
515 #endif
516         
517         for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
518                 MimeViewerFactory *curfactory = cur->data;
519                 gint i = 0;
520
521                 while (curfactory->content_types[i] != NULL) {
522                         debug_print("%s\n", curfactory->content_types[i]);
523                         if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
524                                 factory = curfactory;
525                                 break;
526                         }
527                         i++;
528                 }
529                 if (factory != NULL)
530                         break;
531         }
532         if (factory == NULL)
533                 return NULL;
534
535         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
536                 MimeViewer *curviewer = cur->data;
537                 
538                 if (curviewer->factory == factory)
539                         return curviewer;
540         }
541         viewer = factory->create_viewer();
542         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
543                 GTK_WIDGET(viewer->get_widget(viewer)));
544                 
545         mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
546
547         return viewer;
548 }
549
550 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
551 {
552         gchar *content_type = NULL;
553         MimeViewer *viewer = NULL;
554
555         if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
556             (partinfo->name != NULL)) {
557                 content_type = procmime_get_mime_type(partinfo->name);
558         } else {
559                 content_type = g_strdup(partinfo->content_type);
560         }
561
562         if (content_type != NULL) {
563                 viewer = get_viewer_for_content_type(mimeview, content_type);
564                 g_free(content_type);
565         }
566
567         return viewer;
568 }
569
570 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
571 {
572         MimeViewer *viewer;
573         
574         viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
575         if (viewer == NULL) {
576                 if (mimeview->mimeviewer != NULL)
577                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
578                 mimeview->mimeviewer = NULL;
579                 return FALSE;
580         }
581
582         if (mimeview->mimeviewer != viewer) {
583                 if (mimeview->mimeviewer != NULL)
584                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
585                 mimeview->mimeviewer = viewer;
586                 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
587         }
588         viewer->show_mimepart(viewer, mimeview->file, partinfo);
589
590         return TRUE;
591 }
592
593 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
594 {
595         TextView  *textview  = mimeview->textview;
596
597         if ((mimeview->type != MIMEVIEW_VIEWER) && 
598             (mimeview->type == type)) return;
599
600         switch (type) {
601         case MIMEVIEW_TEXT:
602                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
603                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
604                         GTK_WIDGET_PTR(textview)));
605                 break;
606         case MIMEVIEW_VIEWER:
607                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
608                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
609                         GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
610                 break;
611         default:
612                 return;
613         }
614
615         mimeview->type = type;
616 }
617
618 static void mimeview_clear(MimeView *mimeview)
619 {
620         GtkCList *clist = GTK_CLIST(mimeview->ctree);
621
622         procmime_mimeinfo_free_all(mimeview->mimeinfo);
623         mimeview->mimeinfo = NULL;
624
625         gtk_clist_clear(clist);
626         textview_clear(mimeview->textview);
627         if (mimeview->mimeviewer != NULL)
628                 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
629
630         mimeview->opened = NULL;
631
632         g_free(mimeview->file);
633         mimeview->file = NULL;
634
635         /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
636 }
637
638 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
639                               MimeView *mimeview)
640 {
641         MimeInfo *partinfo;
642
643         if (mimeview->opened == node) return;
644         mimeview->opened = node;
645         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
646
647         partinfo = gtk_ctree_node_get_row_data(ctree, node);
648         if (!partinfo) return;
649
650         /* ungrab the mouse event */
651         if (GTK_WIDGET_HAS_GRAB(ctree)) {
652                 gtk_grab_remove(GTK_WIDGET(ctree));
653                 if (gdk_pointer_is_grabbed())
654                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
655         }
656         
657         mimeview->textview->default_text = FALSE;
658         
659         if (!mimeview_show_part(mimeview, partinfo)) {
660                 switch (partinfo->mime_type) {
661                 case MIME_TEXT:
662                 case MIME_TEXT_HTML:
663                 case MIME_TEXT_ENRICHED:
664                 case MIME_MESSAGE_RFC822:
665                 case MIME_MULTIPART:
666                         mimeview_show_message_part(mimeview, partinfo);
667                 
668                         break;
669                 default:
670                         mimeview->textview->default_text = TRUE;        
671                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
672 #if USE_GPGME
673                         if (g_strcasecmp(partinfo->content_type,
674                                          "application/pgp-signature") == 0)
675                                 textview_show_signature_part(mimeview->textview,
676                                                              partinfo);
677                         else
678 #endif
679                                 textview_show_mime_part(mimeview->textview, partinfo);
680                         break;
681                 }
682         }
683 }
684
685 static void mimeview_start_drag(GtkWidget *widget, gint button,
686                                 GdkEvent *event, MimeView *mimeview)
687 {
688         GdkDragContext *context;
689         MimeInfo *partinfo;
690
691         g_return_if_fail(mimeview != NULL);
692
693         partinfo = mimeview_get_selected_part(mimeview);
694         if (partinfo->filename == NULL && partinfo->name == NULL) return;
695
696         context = gtk_drag_begin(widget, mimeview->target_list,
697                                  GDK_ACTION_COPY, button, event);
698         gtk_drag_set_icon_default(context);
699 }
700
701 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
702                                     MimeView *mimeview)
703 {
704         GtkCList *clist = GTK_CLIST(widget);
705         MimeInfo *partinfo;
706         gint row, column;
707
708         if (!event) return FALSE;
709
710         if (event->button == 2 || event->button == 3) {
711                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
712                                                   &row, &column))
713                         return FALSE;
714                 gtk_clist_unselect_all(clist);
715                 gtk_clist_select_row(clist, row, column);
716                 gtkut_clist_set_focus_row(clist, row);
717         }
718
719         if (event->button == 2 ||
720             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
721                 /* call external program for image, audio or html */
722                 mimeview_launch(mimeview);
723         } else if (event->button == 3) {
724                 partinfo = mimeview_get_selected_part(mimeview);
725                 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
726                                  partinfo->mime_type == MIME_TEXT_HTML ||
727                                  partinfo->mime_type == MIME_TEXT_ENRICHED ||
728                                  partinfo->mime_type == MIME_MESSAGE_RFC822 ||
729                                  partinfo->mime_type == MIME_IMAGE ||
730                                  partinfo->mime_type == MIME_MULTIPART))
731                         menu_set_sensitive(mimeview->popupfactory,
732                                            "/Display as text", FALSE);
733                 else
734                         menu_set_sensitive(mimeview->popupfactory,
735                                            "/Display as text", TRUE);
736                 if (partinfo &&
737                     partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
738                         menu_set_sensitive(mimeview->popupfactory,
739                                            "/Open", FALSE);
740                 else
741                         menu_set_sensitive(mimeview->popupfactory,
742                                            "/Open", TRUE);
743
744 #if USE_GPGME
745                 menu_set_sensitive(mimeview->popupfactory,
746                                    "/Check signature",
747                                    mimeview_is_signed(mimeview));
748 #endif
749
750                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
751                                NULL, NULL, NULL, NULL,
752                                event->button, event->time);
753         }
754
755         return TRUE;
756 }
757
758 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
759 {
760         mimeview_key_pressed(mimeview->ctree, event, mimeview);
761 }
762
763 #define BREAK_ON_MODIFIER_KEY() \
764         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
765
766 #define KEY_PRESS_EVENT_STOP() \
767         if (gtk_signal_n_emissions_by_name \
768                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
769                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
770                                              "key_press_event"); \
771         }
772
773 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
774                                  MimeView *mimeview)
775 {
776         SummaryView *summaryview;
777         GtkCTree *ctree = GTK_CTREE(widget);
778         GtkCTreeNode *node;
779
780         if (!event) return FALSE;
781         if (!mimeview->opened) return FALSE;
782
783         switch (event->keyval) {
784         case GDK_space:
785                 if (textview_scroll_page(mimeview->textview, FALSE))
786                         return TRUE;
787
788                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
789                 if (node) {
790                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
791                         gtk_sctree_select(GTK_SCTREE(ctree), node);
792                         return TRUE;
793                 }
794                 break;
795         case GDK_BackSpace:
796                 textview_scroll_page(mimeview->textview, TRUE);
797                 return TRUE;
798         case GDK_Return:
799                 textview_scroll_one_line(mimeview->textview,
800                                          (event->state & GDK_MOD1_MASK) != 0);
801                 return TRUE;
802         case GDK_n:
803         case GDK_N:
804                 BREAK_ON_MODIFIER_KEY();
805                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
806                 KEY_PRESS_EVENT_STOP();
807
808                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
809                                         GTK_SCROLL_STEP_FORWARD, 0.0);
810                 return TRUE;
811         case GDK_p:
812         case GDK_P:
813                 BREAK_ON_MODIFIER_KEY();
814                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
815                 KEY_PRESS_EVENT_STOP();
816
817                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
818                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
819                 return TRUE;
820         case GDK_y:
821                 BREAK_ON_MODIFIER_KEY();
822                 KEY_PRESS_EVENT_STOP();
823                 mimeview_save_as(mimeview);
824                 return TRUE;
825         case GDK_t:
826                 BREAK_ON_MODIFIER_KEY();
827                 KEY_PRESS_EVENT_STOP();
828                 mimeview_display_as_text(mimeview);
829                 return TRUE;    
830         case GDK_l:
831                 BREAK_ON_MODIFIER_KEY();
832                 KEY_PRESS_EVENT_STOP();
833                 mimeview_launch(mimeview);
834                 return TRUE;
835         default:
836                 break;
837         }
838
839         if (!mimeview->messageview->mainwin) return FALSE;
840         summaryview = mimeview->messageview->mainwin->summaryview;
841         summary_pass_key_press_event(summaryview, event);
842         return TRUE;
843 }
844
845 static void mimeview_drag_data_get(GtkWidget        *widget,
846                                    GdkDragContext   *drag_context,
847                                    GtkSelectionData *selection_data,
848                                    guint             info,
849                                    guint             time,
850                                    MimeView         *mimeview)
851 {
852         gchar *filename, *uriname;
853         MimeInfo *partinfo;
854
855         if (!mimeview->opened) return;
856         if (!mimeview->file) return;
857
858         partinfo = mimeview_get_selected_part(mimeview);
859         if (!partinfo) return;
860         if (!partinfo->filename && !partinfo->name) return;
861
862         filename = partinfo->filename ? partinfo->filename : partinfo->name;
863         filename = g_basename(filename);
864         if (*filename == '\0') return;
865
866         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
867                                filename, NULL);
868
869         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
870                 alertpanel_error
871                         (_("Can't save the part of multipart message."));
872
873         uriname = g_strconcat("file:/", filename, NULL);
874         gtk_selection_data_set(selection_data, selection_data->target, 8,
875                                uriname, strlen(uriname));
876
877         g_free(uriname);
878         g_free(filename);
879 }
880
881 static void mimeview_save_all(MimeView *mimeview)
882 {
883         gchar *dirname;
884         gchar *defname = NULL;
885         MimeInfo *partinfo;
886         MimeInfo *attachment;
887         gchar buf[1024];
888
889         if (!mimeview->opened) return;
890         if (!mimeview->file) return;
891
892         partinfo = mimeview_get_selected_part(mimeview);
893         g_return_if_fail(partinfo != NULL);
894
895         dirname = filesel_select_file(_("Save as"), defname);
896         if (!dirname) return;
897
898         /* return to first children */
899         if (!partinfo->parent->children) return;  /* multipart container? */
900         attachment = partinfo->parent->children->next;
901         /* for each attachment, extract it in the selected dir. */
902         while (attachment != NULL) {
903                 static guint subst_cnt = 1;
904                 gchar *attachdir;
905                 gchar *attachname = g_strdup(get_part_name(attachment));
906                 AlertValue aval = G_ALERTDEFAULT;
907                 gchar *res;
908
909                 if (!attachname || !strlen(attachname))
910                         attachname = g_strdup_printf("noname.%d",subst_cnt++);
911                 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
912                 g_snprintf(buf, sizeof(buf), "%s%s",
913                            dirname,
914                            (attachname[0] == G_DIR_SEPARATOR)
915                            ? &attachname[1]
916                            : attachname);
917                 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
918                 attachdir = g_dirname(buf);
919                 make_dir_hier(attachdir);
920                 g_free(attachdir);
921
922                 if (is_file_exist(buf)) {
923                         res = g_strdup_printf(_("Overwrite existing file '%s'?"),
924                                               attachname);
925                         aval = alertpanel(_("Overwrite"), res, _("OK"), 
926                                           _("Cancel"), NULL);
927                         g_free(res);                                      
928                 }
929                 g_free(attachname);
930
931                 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
932                         alertpanel_error(_("Can't save the part of multipart message."));
933                 attachment = attachment->next;
934         }
935 }
936
937 static void mimeview_display_as_text(MimeView *mimeview)
938 {
939         MimeInfo *partinfo;
940
941         if (!mimeview->opened) return;
942
943         partinfo = mimeview_get_selected_part(mimeview);
944         g_return_if_fail(partinfo != NULL);
945         mimeview_show_message_part(mimeview, partinfo);
946 }
947
948 static void mimeview_save_as(MimeView *mimeview)
949 {
950         gchar *filename;
951         gchar *defname = NULL;
952         MimeInfo *partinfo;
953         gchar *res;
954
955         if (!mimeview->opened) return;
956         if (!mimeview->file) return;
957
958         partinfo = mimeview_get_selected_part(mimeview);
959         g_return_if_fail(partinfo != NULL);
960
961         if (partinfo->filename)
962                 defname = partinfo->filename;
963         else if (partinfo->name) {
964                 Xstrdup_a(defname, partinfo->name, return);
965                 subst_for_filename(defname);
966         }
967
968         filename = filesel_select_file(_("Save as"), defname);
969         if (!filename) return;
970         if (is_file_exist(filename)) {
971                 AlertValue aval;
972                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
973                                       filename);
974                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
975                                   _("Cancel"), NULL);
976                 g_free(res);                                      
977                 if (G_ALERTDEFAULT != aval) return;
978         }
979
980         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
981                 alertpanel_error
982                         (_("Can't save the part of multipart message."));
983 }
984
985 static void mimeview_launch(MimeView *mimeview)
986 {
987         MimeInfo *partinfo;
988         gchar *filename;
989
990         if (!mimeview->opened) return;
991         if (!mimeview->file) return;
992
993         partinfo = mimeview_get_selected_part(mimeview);
994         g_return_if_fail(partinfo != NULL);
995
996         filename = procmime_get_tmp_file_name(partinfo);
997
998         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
999                 alertpanel_error
1000                         (_("Can't save the part of multipart message."));
1001         else
1002                 mimeview_view_file(filename, partinfo, NULL);
1003
1004         g_free(filename);
1005 }
1006
1007 static void mimeview_open_with(MimeView *mimeview)
1008 {
1009         MimeInfo *partinfo;
1010         gchar *filename;
1011         gchar *cmd;
1012
1013         if (!mimeview->opened) return;
1014         if (!mimeview->file) return;
1015
1016         partinfo = mimeview_get_selected_part(mimeview);
1017         g_return_if_fail(partinfo != NULL);
1018
1019         filename = procmime_get_tmp_file_name(partinfo);
1020
1021         if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1022                 alertpanel_error
1023                         (_("Can't save the part of multipart message."));
1024                 g_free(filename);
1025                 return;
1026         }
1027
1028         if (!prefs_common.mime_open_cmd_history)
1029                 prefs_common.mime_open_cmd_history =
1030                         add_history(NULL, prefs_common.mime_open_cmd);
1031
1032         cmd = input_dialog_combo
1033                 (_("Open with"),
1034                  _("Enter the command line to open file:\n"
1035                    "(`%s' will be replaced with file name)"),
1036                  prefs_common.mime_open_cmd,
1037                  prefs_common.mime_open_cmd_history,
1038                  TRUE);
1039         if (cmd) {
1040                 mimeview_view_file(filename, partinfo, cmd);
1041                 g_free(prefs_common.mime_open_cmd);
1042                 prefs_common.mime_open_cmd = cmd;
1043                 prefs_common.mime_open_cmd_history =
1044                         add_history(prefs_common.mime_open_cmd_history, cmd);
1045         }
1046
1047         g_free(filename);
1048 }
1049
1050 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1051                                const gchar *cmdline)
1052 {
1053         static gchar *default_image_cmdline = "display '%s'";
1054         static gchar *default_audio_cmdline = "play '%s'";
1055         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1056         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1057         gchar buf[1024];
1058         gchar m_buf[1024];
1059         const gchar *cmd;
1060         const gchar *def_cmd;
1061         const gchar *p;
1062
1063         if (cmdline) {
1064                 cmd = cmdline;
1065                 def_cmd = NULL;
1066         } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1067                 return;
1068         } else if (MIME_IMAGE == partinfo->mime_type) {
1069                 cmd = prefs_common.mime_image_viewer;
1070                 def_cmd = default_image_cmdline;
1071         } else if (MIME_AUDIO == partinfo->mime_type) {
1072                 cmd = prefs_common.mime_audio_player;
1073                 def_cmd = default_audio_cmdline;
1074         } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1075                 cmd = prefs_common.uri_cmd;
1076                 def_cmd = default_html_cmdline;
1077         } else {
1078                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1079                            partinfo->content_type, "%s");
1080                 cmd = m_buf;
1081                 def_cmd = NULL;
1082         }
1083
1084         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1085             !strchr(p + 2, '%'))
1086                 g_snprintf(buf, sizeof(buf), cmd, filename);
1087         else {
1088                 if (cmd)
1089                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1090                 if (def_cmd)
1091                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1092                 else
1093                         return;
1094         }
1095
1096         execute_command_line(buf, TRUE);
1097 }
1098
1099 #if USE_GPGME
1100 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1101                              gpointer data)
1102 {
1103         MimeInfo *partinfo;
1104         gchar *part_name;
1105
1106         partinfo = gtk_ctree_node_get_row_data(ctree, node);
1107         g_return_if_fail(partinfo != NULL);
1108
1109         part_name = get_part_name(partinfo);
1110         gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1111 }
1112
1113 static void mimeview_update_names(MimeView *mimeview)
1114 {
1115         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1116
1117         gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1118 }
1119
1120 static void mimeview_update_signature_info(MimeView *mimeview)
1121 {
1122         MimeInfo *partinfo;
1123
1124         if (!mimeview) return;
1125         if (!mimeview->opened) return;
1126
1127         partinfo = mimeview_get_selected_part(mimeview);
1128         if (!partinfo) return;
1129
1130         if (g_strcasecmp(partinfo->content_type,
1131                          "application/pgp-signature") == 0) {
1132                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1133                 textview_show_signature_part(mimeview->textview, partinfo);
1134         }
1135 }
1136
1137 void mimeview_check_signature(MimeView *mimeview)
1138 {
1139         MimeInfo *mimeinfo;
1140         FILE *fp;
1141
1142         g_return_if_fail (mimeview_is_signed(mimeview));
1143         g_return_if_fail (gpg_started);
1144
1145         mimeinfo = gtk_ctree_node_get_row_data
1146                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1147         g_return_if_fail(mimeinfo != NULL);
1148         g_return_if_fail(mimeview->file != NULL);
1149
1150         while (mimeinfo->parent)
1151                 mimeinfo = mimeinfo->parent;
1152
1153         if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1154                 FILE_OP_ERROR(mimeview->file, "fopen");
1155                 return;
1156         }
1157
1158         rfc2015_check_signature(mimeinfo, fp);
1159         fclose(fp);
1160
1161         mimeview_update_names(mimeview);
1162         mimeview_update_signature_info(mimeview);
1163
1164         textview_show_message(mimeview->messageview->textview, mimeinfo,
1165                               mimeview->file);
1166 }
1167 #endif /* USE_GPGME */
1168
1169 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1170 {
1171         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1172 }
1173
1174 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1175 {
1176         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1177 }
1178
1179 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1180 {
1181         GSList *mimeview_list, *viewer_list;
1182
1183         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1184                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1185                 
1186                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1187                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1188                         mimeview->mimeviewer = NULL;
1189                 }
1190
1191                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1192                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1193
1194                         mimeviewer->destroy_viewer(mimeviewer);
1195                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1196                 }
1197         }
1198
1199         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1200 }