fix segfault on opening a mime part
[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_save_as            (MimeView       *mimeview);
104 static void mimeview_launch             (MimeView       *mimeview);
105 static void mimeview_open_with          (MimeView       *mimeview);
106 static void mimeview_view_file          (const gchar    *filename,
107                                          MimeInfo       *partinfo,
108                                          const gchar    *cmdline);
109 #if USE_GPGME
110 static void mimeview_check_signature    (MimeView       *mimeview);
111 #endif
112
113 static GtkItemFactoryEntry mimeview_popup_entries[] =
114 {
115         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
116         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
117         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
118         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL}
119 #if USE_GPGME
120         ,
121         {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
122 #endif
123 };
124
125 static GtkTargetEntry mimeview_mime_types[] =
126 {
127         {"text/uri-list", 0, 0}
128 };
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         n_entries = sizeof(mimeview_popup_entries) /
202                 sizeof(mimeview_popup_entries[0]);
203         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
204                                       "<MimeView>", &popupfactory, mimeview);
205
206         mimeview->notebook     = notebook;
207         mimeview->vbox         = vbox;
208         mimeview->paned        = paned;
209         mimeview->scrolledwin  = scrolledwin;
210         mimeview->ctree        = ctree;
211         mimeview->mime_vbox    = mime_vbox;
212         mimeview->popupmenu    = popupmenu;
213         mimeview->popupfactory = popupfactory;
214         mimeview->type         = -1;
215
216         return mimeview;
217 }
218
219 void mimeview_init(MimeView *mimeview)
220 {
221 }
222
223 /* 
224  * Check whether the message is OpenPGP signed
225  */
226 #if USE_GPGME
227 static gboolean mimeview_is_signed(MimeView *mimeview)
228 {
229         MimeInfo *partinfo = NULL;
230         MsgInfo *msginfo = NULL;
231
232         debug_print("mimeview_is signed of %p\n", mimeview);
233
234         if (!mimeview) return FALSE;
235         if (!mimeview->opened) return FALSE;
236
237         debug_print("mimeview_is_signed: open\n" );
238
239         if (!mimeview->file) return FALSE;
240
241         debug_print("mimeview_is_signed: file\n" );
242
243         partinfo = gtk_ctree_node_get_row_data
244                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
245         g_return_val_if_fail(partinfo != NULL, FALSE);
246
247         /* walk the tree and see whether there is a signature somewhere */
248         do {
249                 if (rfc2015_has_signature(partinfo))
250                         return TRUE;
251                 if (pgptext_has_signature(msginfo, 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, "r")) == 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                 rfc2015_check_signature(mimeinfo, fp);
310         else
311                 set_unchecked_signature(mimeinfo);
312 #endif
313
314         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
315                                          mimeview);
316
317         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
318
319         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
320                                            mimeview_selected, mimeview);
321
322         /* search first text part */
323         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
324              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
325                 MimeInfo *partinfo;
326
327                 partinfo = gtk_ctree_node_get_row_data(ctree, node);
328                 if (partinfo &&
329                     (partinfo->mime_type == MIME_TEXT ||
330                      partinfo->mime_type == MIME_TEXT_HTML)) {
331                         if (fseek(fp, partinfo->fpos, SEEK_SET) < 0)
332                                 perror("fseek");
333                         else
334                                 textview_show_part
335                                         (mimeview->messageview->textview,
336                                          partinfo, fp);
337                         break;
338                 }
339         }
340         fclose(fp);
341
342         if (!node)
343                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
344
345         if (node) {
346                 gtk_ctree_select(ctree, node);
347                 gtkut_ctree_set_focus_row(ctree, node);
348                 gtk_widget_grab_focus(mimeview->ctree);
349         }
350 }
351
352 void mimeview_destroy(MimeView *mimeview)
353 {
354         procmime_mimeinfo_free_all(mimeview->mimeinfo);
355         g_free(mimeview->file);
356         g_free(mimeview);
357 }
358
359 static void mimeview_set_multipart_tree(MimeView *mimeview,
360                                         MimeInfo *mimeinfo,
361                                         GtkCTreeNode *parent)
362 {
363         GtkCTreeNode *current = parent;
364
365         g_return_if_fail(mimeinfo != NULL);
366
367         if (!mimeinfo->sub && mimeinfo->parent)
368                 current = mimeview_append_part(mimeview, mimeinfo, parent);
369
370         if (mimeinfo->sub)
371                 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
372
373         if (mimeinfo->children)
374                 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
375
376         if (mimeinfo->next)
377                 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
378 }
379
380 static gchar *get_part_name(MimeInfo *partinfo)
381 {
382 #if USE_GPGME
383         if (partinfo->sigstatus)
384                 return partinfo->sigstatus;
385         else
386 #endif
387         if (partinfo->name)
388                 return partinfo->name;
389         else if (partinfo->filename)
390                 return partinfo->filename;
391         else
392                 return "";
393 }
394
395 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
396                                           MimeInfo *partinfo,
397                                           GtkCTreeNode *parent)
398 {
399         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
400         GtkCTreeNode *node;
401         gchar *str[N_MIMEVIEW_COLS];
402
403         str[COL_MIMETYPE] =
404                 partinfo->content_type ? partinfo->content_type : "";
405         str[COL_SIZE] = to_human_readable(partinfo->size);
406         str[COL_NAME] = get_part_name(partinfo);
407
408         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
409                                      NULL, NULL, NULL, NULL,
410                                      FALSE, TRUE);
411         gtk_ctree_node_set_row_data(ctree, node, partinfo);
412
413         return node;
414 }
415
416 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
417 {
418         FILE *fp;
419         const gchar *fname;
420 #if USE_GPGME
421         MimeInfo *pi;
422 #endif
423
424         if (!partinfo) return;
425
426 #if USE_GPGME
427         for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
428                 ;
429         fname = pi ? pi->plaintextfile : mimeview->file;
430 #else
431         fname = mimeview->file;
432 #endif /* USE_GPGME */
433         if (!fname) return;
434
435         if ((fp = fopen(fname, "r")) == NULL) {
436                 FILE_OP_ERROR(fname, "fopen");
437                 return;
438         }
439
440         if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
441                 FILE_OP_ERROR(mimeview->file, "fseek");
442                 fclose(fp);
443                 return;
444         }
445
446         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
447         textview_show_part(mimeview->textview, partinfo, fp);
448
449         fclose(fp);
450 }
451
452 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
453 {
454         gchar *filename;
455
456         if (!partinfo) return;
457
458         filename = procmime_get_tmp_file_name(partinfo);
459
460         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
461                 alertpanel_error
462                         (_("Can't get the part of multipart message."));
463         else {
464                 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
465                 /* Workaround for the GTK+ bug with handling scroll adjustments
466                  * in GtkViewport */
467                 imageview_clear(mimeview->imageview);
468                 imageview_show_image(mimeview->imageview, partinfo, filename);
469                 unlink(filename);
470         }
471
472         g_free(filename);
473 }
474
475 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
476 {
477         TextView  *textview  = mimeview->textview;
478         ImageView *imageview = mimeview->imageview;
479         GList *children;
480
481         if (mimeview->type == type) return;
482
483         children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
484         if (children) {
485                 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
486                                        GTK_WIDGET(children->data));
487                 g_list_free(children);
488         }
489
490         switch (type) {
491         case MIMEVIEW_IMAGE:
492                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
493                                   GTK_WIDGET_PTR(imageview));
494                 break;
495         case MIMEVIEW_TEXT:
496                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
497                                   GTK_WIDGET_PTR(textview));
498                 break;
499         default:
500                 return;
501         }
502
503         mimeview->type = type;
504 }
505
506 static void mimeview_clear(MimeView *mimeview)
507 {
508         GtkCList *clist = GTK_CLIST(mimeview->ctree);
509
510         procmime_mimeinfo_free_all(mimeview->mimeinfo);
511         mimeview->mimeinfo = NULL;
512
513         gtk_clist_clear(clist);
514         textview_clear(mimeview->textview);
515         imageview_clear(mimeview->imageview);
516
517         mimeview->opened = NULL;
518
519         g_free(mimeview->file);
520         mimeview->file = NULL;
521
522         /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
523 }
524
525 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
526                               MimeView *mimeview)
527 {
528         MimeInfo *partinfo;
529
530         if (mimeview->opened == node) return;
531         mimeview->opened = node;
532         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
533
534         partinfo = gtk_ctree_node_get_row_data(ctree, node);
535         if (!partinfo) return;
536
537         /* ungrab the mouse event */
538         if (GTK_WIDGET_HAS_GRAB(ctree)) {
539                 gtk_grab_remove(GTK_WIDGET(ctree));
540                 if (gdk_pointer_is_grabbed())
541                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
542         }
543
544         switch (partinfo->mime_type) {
545         case MIME_TEXT:
546         case MIME_TEXT_HTML:
547         case MIME_TEXT_ENRICHED:
548         case MIME_MESSAGE_RFC822:
549         case MIME_MULTIPART:
550                 mimeview_show_message_part(mimeview, partinfo);
551                 break;
552 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
553         case MIME_IMAGE:
554                 mimeview_show_image_part(mimeview, partinfo);
555                 break;
556 #endif
557         default:
558                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
559 #if USE_GPGME
560                 if (g_strcasecmp(partinfo->content_type,
561                                  "application/pgp-signature") == 0)
562                         textview_show_signature_part(mimeview->textview,
563                                                      partinfo);
564                 else
565 #endif
566                         textview_show_mime_part(mimeview->textview, partinfo);
567                 break;
568         }
569 }
570
571 static void mimeview_start_drag(GtkWidget *widget, gint button,
572                                 GdkEvent *event, MimeView *mimeview)
573 {
574         GtkTargetList *list;
575         GdkDragContext *context;
576         MimeInfo *partinfo;
577         GtkCTree *ctree;
578
579         g_return_if_fail(mimeview != NULL);
580         ctree = GTK_CTREE(mimeview->ctree);
581         partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
582         if (partinfo->filename == NULL && partinfo->name == NULL) return;
583
584         list = gtk_target_list_new(mimeview_mime_types, 1);
585         context = gtk_drag_begin(widget, list,
586                                  GDK_ACTION_COPY, button, event);
587         gtk_drag_set_icon_default(context);
588 }
589
590 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
591                                     MimeView *mimeview)
592 {
593         GtkCList *clist = GTK_CLIST(widget);
594         MimeInfo *partinfo;
595         gint row, column;
596
597         if (!event) return FALSE;
598
599         if (event->button == 2 || event->button == 3) {
600                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
601                                                   &row, &column))
602                         return FALSE;
603                 gtk_clist_unselect_all(clist);
604                 gtk_clist_select_row(clist, row, column);
605                 gtkut_clist_set_focus_row(clist, row);
606         }
607
608         if (event->button == 2 ||
609             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
610                 /* call external program for image, audio or html */
611                 mimeview_launch(mimeview);
612         } else if (event->button == 3) {
613                 partinfo = gtk_ctree_node_get_row_data
614                         (GTK_CTREE(mimeview->ctree), mimeview->opened);
615                 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
616                                  partinfo->mime_type == MIME_TEXT_HTML ||
617                                  partinfo->mime_type == MIME_TEXT_ENRICHED ||
618                                  partinfo->mime_type == MIME_MESSAGE_RFC822 ||
619                                  partinfo->mime_type == MIME_IMAGE ||
620                                  partinfo->mime_type == MIME_MULTIPART))
621                         menu_set_sensitive(mimeview->popupfactory,
622                                            "/Display as text", FALSE);
623                 else
624                         menu_set_sensitive(mimeview->popupfactory,
625                                            "/Display as text", TRUE);
626                 if (partinfo &&
627                     partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
628                         menu_set_sensitive(mimeview->popupfactory,
629                                            "/Open", FALSE);
630                 else
631                         menu_set_sensitive(mimeview->popupfactory,
632                                            "/Open", TRUE);
633 #if USE_GPGME
634                 menu_set_sensitive(mimeview->popupfactory,
635                                    "/Check signature",
636                                    mimeview_is_signed(mimeview));
637 #endif
638
639                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
640                                NULL, NULL, NULL, NULL,
641                                event->button, event->time);
642         }
643
644         return TRUE;
645 }
646
647 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
648 {
649         mimeview_key_pressed(mimeview->ctree, event, mimeview);
650 }
651
652 #define BREAK_ON_MODIFIER_KEY() \
653         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
654
655 #define KEY_PRESS_EVENT_STOP() \
656         if (gtk_signal_n_emissions_by_name \
657                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
658                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
659                                              "key_press_event"); \
660         }
661
662 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
663                                  MimeView *mimeview)
664 {
665         SummaryView *summaryview;
666         GtkCTree *ctree = GTK_CTREE(widget);
667         GtkCTreeNode *node;
668
669         if (!event) return FALSE;
670         if (!mimeview->opened) return FALSE;
671
672         switch (event->keyval) {
673         case GDK_space:
674                 if (textview_scroll_page(mimeview->textview, FALSE))
675                         return TRUE;
676
677                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
678                 if (node) {
679                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
680                         gtk_sctree_select(GTK_SCTREE(ctree), node);
681                         return TRUE;
682                 }
683                 break;
684         case GDK_BackSpace:
685                 textview_scroll_page(mimeview->textview, TRUE);
686                 return TRUE;
687         case GDK_Return:
688                 textview_scroll_one_line(mimeview->textview,
689                                          (event->state & GDK_MOD1_MASK) != 0);
690                 return TRUE;
691         case GDK_n:
692         case GDK_N:
693                 BREAK_ON_MODIFIER_KEY();
694                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
695                 KEY_PRESS_EVENT_STOP();
696
697                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
698                                         GTK_SCROLL_STEP_FORWARD, 0.0);
699                 return TRUE;
700         case GDK_p:
701         case GDK_P:
702                 BREAK_ON_MODIFIER_KEY();
703                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
704                 KEY_PRESS_EVENT_STOP();
705
706                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
707                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
708                 return TRUE;
709         case GDK_y:
710                 BREAK_ON_MODIFIER_KEY();
711                 KEY_PRESS_EVENT_STOP();
712                 mimeview_save_as(mimeview);
713                 return TRUE;
714         case GDK_t:
715                 BREAK_ON_MODIFIER_KEY();
716                 KEY_PRESS_EVENT_STOP();
717                 mimeview_display_as_text(mimeview);
718                 return TRUE;
719         case GDK_l:
720                 BREAK_ON_MODIFIER_KEY();
721                 KEY_PRESS_EVENT_STOP();
722                 mimeview_launch(mimeview);
723                 return TRUE;
724         default:
725         }
726
727         if (!mimeview->messageview->mainwin) return FALSE;
728         summaryview = mimeview->messageview->mainwin->summaryview;
729         summary_pass_key_press_event(summaryview, event);
730         return TRUE;
731 }
732
733 static void mimeview_drag_data_get(GtkWidget        *widget,
734                                    GdkDragContext   *drag_context,
735                                    GtkSelectionData *selection_data,
736                                    guint             info,
737                                    guint             time,
738                                    MimeView         *mimeview)
739 {
740         gchar *filename, *uriname;
741         MimeInfo *partinfo;
742
743         if (!mimeview->opened) return;
744         if (!mimeview->file) return;
745
746         partinfo = gtk_ctree_node_get_row_data
747                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
748         if (!partinfo) return;
749         if (!partinfo->filename && !partinfo->name) return;
750
751         filename = partinfo->filename ? partinfo->filename : partinfo->name;
752         filename = g_basename(filename);
753         if (*filename == '\0') return;
754
755         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
756                                filename, NULL);
757
758         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
759                 alertpanel_error
760                         (_("Can't save the part of multipart message."));
761
762         uriname = g_strconcat("file:/", filename, NULL);
763         gtk_selection_data_set(selection_data, selection_data->target, 8,
764                                uriname, strlen(uriname));
765
766         g_free(uriname);
767         g_free(filename);
768 }
769
770 static void mimeview_display_as_text(MimeView *mimeview)
771 {
772         MimeInfo *partinfo;
773
774         if (!mimeview->opened) return;
775
776         partinfo = gtk_ctree_node_get_row_data
777                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
778         mimeview_show_message_part(mimeview, partinfo);
779 }
780
781 static void mimeview_save_as(MimeView *mimeview)
782 {
783         gchar *filename;
784         gchar *defname = NULL;
785         MimeInfo *partinfo;
786
787         if (!mimeview->opened) return;
788         if (!mimeview->file) return;
789
790         partinfo = gtk_ctree_node_get_row_data
791                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
792         g_return_if_fail(partinfo != NULL);
793
794         if (partinfo->filename)
795                 defname = partinfo->filename;
796         else if (partinfo->name) {
797                 Xstrdup_a(defname, partinfo->name, return);
798                 subst_for_filename(defname);
799         }
800
801         filename = filesel_select_file(_("Save as"), defname);
802         if (!filename) return;
803         if (is_file_exist(filename)) {
804                 AlertValue aval;
805
806                 aval = alertpanel(_("Overwrite"),
807                                   _("Overwrite existing file?"),
808                                   _("OK"), _("Cancel"), NULL);
809                 if (G_ALERTDEFAULT != aval) return;
810         }
811
812         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
813                 alertpanel_error
814                         (_("Can't save the part of multipart message."));
815 }
816
817 static void mimeview_launch(MimeView *mimeview)
818 {
819         MimeInfo *partinfo;
820         gchar *filename;
821
822         if (!mimeview->opened) return;
823         if (!mimeview->file) return;
824
825         partinfo = gtk_ctree_node_get_row_data
826                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
827         g_return_if_fail(partinfo != NULL);
828
829         filename = procmime_get_tmp_file_name(partinfo);
830
831         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
832                 alertpanel_error
833                         (_("Can't save the part of multipart message."));
834         else
835                 mimeview_view_file(filename, partinfo, NULL);
836
837         g_free(filename);
838 }
839
840 static void mimeview_open_with(MimeView *mimeview)
841 {
842         MimeInfo *partinfo;
843         gchar *filename;
844         gchar *cmd;
845
846         if (!mimeview->opened) return;
847         if (!mimeview->file) return;
848
849         partinfo = gtk_ctree_node_get_row_data
850                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
851         g_return_if_fail(partinfo != NULL);
852
853         filename = procmime_get_tmp_file_name(partinfo);
854
855         if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
856                 alertpanel_error
857                         (_("Can't save the part of multipart message."));
858                 g_free(filename);
859                 return;
860         }
861
862         if (!prefs_common.mime_open_cmd_history)
863                 prefs_common.mime_open_cmd_history =
864                         add_history(NULL, prefs_common.mime_open_cmd);
865
866         cmd = input_dialog_combo
867                 (_("Open with"),
868                  _("Enter the command line to open file:\n"
869                    "(`%s' will be replaced with file name)"),
870                  prefs_common.mime_open_cmd,
871                  prefs_common.mime_open_cmd_history);
872         if (cmd) {
873                 mimeview_view_file(filename, partinfo, cmd);
874                 g_free(prefs_common.mime_open_cmd);
875                 prefs_common.mime_open_cmd = cmd;
876                 prefs_common.mime_open_cmd_history =
877                         add_history(prefs_common.mime_open_cmd_history, cmd);
878         }
879
880         g_free(filename);
881 }
882
883 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
884                                const gchar *cmdline)
885 {
886         static gchar *default_image_cmdline = "display '%s'";
887         static gchar *default_audio_cmdline = "play '%s'";
888         static gchar *default_html_cmdline =
889                 "netscape -remote 'openURL(%s,raise)'";
890         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
891         gchar buf[1024];
892         gchar m_buf[1024];
893         const gchar *cmd;
894         const gchar *def_cmd;
895         const gchar *p;
896
897         if (cmdline) {
898                 cmd = cmdline;
899                 def_cmd = NULL;
900         } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
901                 return;
902         } else if (MIME_IMAGE == partinfo->mime_type) {
903                 cmd = prefs_common.mime_image_viewer;
904                 def_cmd = default_image_cmdline;
905         } else if (MIME_AUDIO == partinfo->mime_type) {
906                 cmd = prefs_common.mime_audio_player;
907                 def_cmd = default_audio_cmdline;
908         } else if (MIME_TEXT_HTML == partinfo->mime_type) {
909                 cmd = prefs_common.uri_cmd;
910                 def_cmd = default_html_cmdline;
911         } else {
912                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
913                            partinfo->content_type, "%s");
914                 cmd = m_buf;
915                 def_cmd = NULL;
916         }
917
918         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
919             !strchr(p + 2, '%'))
920                 g_snprintf(buf, sizeof(buf), cmd, filename);
921         else {
922                 if (cmd)
923                         g_warning(_("MIME viewer command line is invalid: `%s'"), cmd);
924                 if (def_cmd)
925                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
926                 else
927                         return;
928         }
929
930         execute_command_line(buf, TRUE);
931 }
932
933 #if USE_GPGME
934 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
935                              gpointer data)
936 {
937         MimeInfo *partinfo;
938         gchar *part_name;
939
940         partinfo = gtk_ctree_node_get_row_data(ctree, node);
941         g_return_if_fail(partinfo != NULL);
942
943         part_name = get_part_name(partinfo);
944         gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
945 }
946
947 static void mimeview_update_names(MimeView *mimeview)
948 {
949         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
950
951         gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
952 }
953
954 static void mimeview_update_signature_info(MimeView *mimeview)
955 {
956         MimeInfo *partinfo;
957
958         if (!mimeview) return;
959         if (!mimeview->opened) return;
960
961         partinfo = gtk_ctree_node_get_row_data
962                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
963         if (!partinfo) return;
964
965         if (g_strcasecmp(partinfo->content_type,
966                          "application/pgp-signature") == 0) {
967                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
968                 textview_show_signature_part(mimeview->textview, partinfo);
969         }
970 }
971
972 static void mimeview_check_signature(MimeView *mimeview)
973 {
974         MimeInfo *mimeinfo;
975         gchar buf[BUFFSIZE];
976         FILE *fp;
977
978         g_return_if_fail (mimeview_is_signed(mimeview));
979
980         mimeinfo = gtk_ctree_node_get_row_data
981                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
982         g_return_if_fail(mimeinfo != NULL);
983         g_return_if_fail(mimeview->file != NULL);
984
985         while (mimeinfo->parent)
986                 mimeinfo = mimeinfo->parent;
987
988         if ((fp = fopen(mimeview->file, "r")) == NULL) {
989                 FILE_OP_ERROR(mimeview->file, "fopen");
990                 return;
991         }
992
993         /* skip headers */
994         if (mimeinfo->mime_type == MIME_MULTIPART) {
995                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
996                 FILE_OP_ERROR(mimeview->file, "fseek");
997                 while (fgets(buf, sizeof(buf), fp) != NULL)
998                         if (buf[0] == '\r' || buf[0] == '\n') break;
999         }
1000
1001         procmime_scan_multipart_message(mimeinfo, fp);
1002         rfc2015_check_signature(mimeinfo, fp);
1003         fclose(fp);
1004
1005         mimeview_update_names(mimeview);
1006         mimeview_update_signature_info(mimeview);
1007 }
1008 #endif /* USE_GPGME */