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