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