d186a53965e5d61e8ff55aea128ba6a9da95bd3c
[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_launch             (MimeView       *mimeview);
106 static void mimeview_open_with          (MimeView       *mimeview);
107 static void mimeview_view_file          (const gchar    *filename,
108                                          MimeInfo       *partinfo,
109                                          const gchar    *cmdline);
110 #if USE_GPGME
111 static void mimeview_check_signature    (MimeView       *mimeview);
112 #endif
113
114 static GtkItemFactoryEntry mimeview_popup_entries[] =
115 {
116         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
117         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
118         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
119         {N_("/_Display image"),   NULL, mimeview_show_image,      0, NULL},
120         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL}
121 #if USE_GPGME
122         ,
123         {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
124 #endif
125 };
126
127 static GtkTargetEntry mimeview_mime_types[] =
128 {
129         {"text/uri-list", 0, 0}
130 };
131
132 MimeView *mimeview_create(void)
133 {
134         MimeView *mimeview;
135
136         GtkWidget *notebook;
137         GtkWidget *vbox;
138         GtkWidget *paned;
139         GtkWidget *scrolledwin;
140         GtkWidget *ctree;
141         GtkWidget *mime_vbox;
142         GtkWidget *popupmenu;
143         GtkItemFactory *popupfactory;
144         gchar *titles[N_MIMEVIEW_COLS];
145         gint n_entries;
146         gint i;
147
148         debug_print(_("Creating MIME view...\n"));
149         mimeview = g_new0(MimeView, 1);
150
151         titles[COL_MIMETYPE] = _("MIME Type");
152         titles[COL_SIZE]     = _("Size");
153         titles[COL_NAME]     = _("Name");
154
155         notebook = gtk_notebook_new();
156         gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
157
158         vbox = gtk_vbox_new(FALSE, 0);
159         gtk_container_add(GTK_CONTAINER(notebook), vbox);
160         gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
161                                         _("Text"));
162
163         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
164         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
165                                        GTK_POLICY_AUTOMATIC,
166                                        GTK_POLICY_ALWAYS);
167         gtk_widget_set_usize(scrolledwin, -1, 80);
168
169         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
170         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
171         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
172         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
173                                            GTK_JUSTIFY_RIGHT);
174         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
175         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
176         for (i = 0; i < N_MIMEVIEW_COLS; i++)
177                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
178                                        GTK_CAN_FOCUS);
179         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
180
181         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
182                            GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
183         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
184                            GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
185         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
186                            GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
187         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
188                            GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
189         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
190                            GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
191     
192         mime_vbox = gtk_vbox_new(FALSE, 0);
193
194         paned = gtk_vpaned_new();
195         gtk_paned_add1(GTK_PANED(paned), scrolledwin);
196         gtk_paned_add2(GTK_PANED(paned), mime_vbox);
197         gtk_container_add(GTK_CONTAINER(notebook), paned);
198         gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
199                                         _("Attachments"));
200
201         gtk_widget_show_all(notebook);
202
203         gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
204
205         n_entries = sizeof(mimeview_popup_entries) /
206                 sizeof(mimeview_popup_entries[0]);
207         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
208                                       "<MimeView>", &popupfactory, mimeview);
209
210         mimeview->notebook     = notebook;
211         mimeview->vbox         = vbox;
212         mimeview->paned        = paned;
213         mimeview->scrolledwin  = scrolledwin;
214         mimeview->ctree        = ctree;
215         mimeview->mime_vbox    = mime_vbox;
216         mimeview->popupmenu    = popupmenu;
217         mimeview->popupfactory = popupfactory;
218         mimeview->type         = -1;
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                 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
393                 return "";
394 }
395
396 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
397                                           MimeInfo *partinfo,
398                                           GtkCTreeNode *parent)
399 {
400         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
401         GtkCTreeNode *node;
402         gchar *str[N_MIMEVIEW_COLS];
403
404         str[COL_MIMETYPE] =
405                 partinfo->content_type ? partinfo->content_type : "";
406         str[COL_SIZE] = to_human_readable(partinfo->size);
407         str[COL_NAME] = get_part_name(partinfo);
408
409         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
410                                      NULL, NULL, NULL, NULL,
411                                      FALSE, TRUE);
412         gtk_ctree_node_set_row_data(ctree, node, partinfo);
413
414         return node;
415 }
416
417 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
418 {
419         FILE *fp;
420         const gchar *fname;
421 #if USE_GPGME
422         MimeInfo *pi;
423 #endif
424
425         if (!partinfo) return;
426
427 #if USE_GPGME
428         for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
429                 ;
430         fname = pi ? pi->plaintextfile : mimeview->file;
431 #else
432         fname = mimeview->file;
433 #endif /* USE_GPGME */
434         if (!fname) return;
435
436         if ((fp = fopen(fname, "rb")) == NULL) {
437                 FILE_OP_ERROR(fname, "fopen");
438                 return;
439         }
440
441         if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
442                 FILE_OP_ERROR(mimeview->file, "fseek");
443                 fclose(fp);
444                 return;
445         }
446
447         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
448         textview_show_part(mimeview->textview, partinfo, fp);
449
450         fclose(fp);
451 }
452
453 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
454 {
455         gchar *filename;
456
457         if (!partinfo) return;
458
459         filename = procmime_get_tmp_file_name(partinfo);
460
461         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
462                 alertpanel_error
463                         (_("Can't get the part of multipart message."));
464         else {
465                 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
466                 /* Workaround for the GTK+ bug with handling scroll adjustments
467                  * in GtkViewport */
468                 imageview_clear(mimeview->imageview);
469                 imageview_show_image(mimeview->imageview, partinfo, filename);
470                 unlink(filename);
471         }
472
473         g_free(filename);
474 }
475
476 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
477 {
478         TextView  *textview  = mimeview->textview;
479         ImageView *imageview = mimeview->imageview;
480         GList *children;
481
482         if (mimeview->type == type) return;
483
484         children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
485         if (children) {
486                 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
487                                        GTK_WIDGET(children->data));
488                 g_list_free(children);
489         }
490
491         switch (type) {
492         case MIMEVIEW_IMAGE:
493                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
494                                   GTK_WIDGET_PTR(imageview));
495                 break;
496         case MIMEVIEW_TEXT:
497                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
498                                   GTK_WIDGET_PTR(textview));
499                 break;
500         default:
501                 return;
502         }
503
504         mimeview->type = type;
505 }
506
507 static void mimeview_clear(MimeView *mimeview)
508 {
509         GtkCList *clist = GTK_CLIST(mimeview->ctree);
510
511         procmime_mimeinfo_free_all(mimeview->mimeinfo);
512         mimeview->mimeinfo = NULL;
513
514         gtk_clist_clear(clist);
515         textview_clear(mimeview->textview);
516         imageview_clear(mimeview->imageview);
517
518         mimeview->opened = NULL;
519
520         g_free(mimeview->file);
521         mimeview->file = NULL;
522
523         /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
524 }
525
526 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
527                               MimeView *mimeview)
528 {
529         MimeInfo *partinfo;
530
531         if (mimeview->opened == node) return;
532         mimeview->opened = node;
533         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
534
535         partinfo = gtk_ctree_node_get_row_data(ctree, node);
536         if (!partinfo) return;
537
538         /* ungrab the mouse event */
539         if (GTK_WIDGET_HAS_GRAB(ctree)) {
540                 gtk_grab_remove(GTK_WIDGET(ctree));
541                 if (gdk_pointer_is_grabbed())
542                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
543         }
544
545         switch (partinfo->mime_type) {
546         case MIME_TEXT:
547         case MIME_TEXT_HTML:
548         case MIME_TEXT_ENRICHED:
549         case MIME_MESSAGE_RFC822:
550         case MIME_MULTIPART:
551                 mimeview_show_message_part(mimeview, partinfo);
552                 break;
553 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
554         case MIME_IMAGE:
555                 if (prefs_common.display_img)
556                         mimeview_show_image_part(mimeview, partinfo);
557                 else {
558                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
559                         textview_show_mime_part(mimeview->textview, partinfo);
560                 }
561                 break;
562 #endif
563         default:
564                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
565 #if USE_GPGME
566                 if (g_strcasecmp(partinfo->content_type,
567                                  "application/pgp-signature") == 0)
568                         textview_show_signature_part(mimeview->textview,
569                                                      partinfo);
570                 else
571 #endif
572                         textview_show_mime_part(mimeview->textview, partinfo);
573                 break;
574         }
575 }
576
577 static void mimeview_start_drag(GtkWidget *widget, gint button,
578                                 GdkEvent *event, MimeView *mimeview)
579 {
580         GtkTargetList *list;
581         GdkDragContext *context;
582         MimeInfo *partinfo;
583         GtkCTree *ctree;
584
585         g_return_if_fail(mimeview != NULL);
586         ctree = GTK_CTREE(mimeview->ctree);
587         partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
588         if (partinfo->filename == NULL && partinfo->name == NULL) return;
589
590         list = gtk_target_list_new(mimeview_mime_types, 1);
591         context = gtk_drag_begin(widget, list,
592                                  GDK_ACTION_COPY, button, event);
593         gtk_drag_set_icon_default(context);
594 }
595
596 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
597                                     MimeView *mimeview)
598 {
599         GtkCList *clist = GTK_CLIST(widget);
600         MimeInfo *partinfo;
601         gint row, column;
602
603         if (!event) return FALSE;
604
605         if (event->button == 2 || event->button == 3) {
606                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
607                                                   &row, &column))
608                         return FALSE;
609                 gtk_clist_unselect_all(clist);
610                 gtk_clist_select_row(clist, row, column);
611                 gtkut_clist_set_focus_row(clist, row);
612         }
613
614         if (event->button == 2 ||
615             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
616                 /* call external program for image, audio or html */
617                 mimeview_launch(mimeview);
618         } else if (event->button == 3) {
619                 partinfo = gtk_ctree_node_get_row_data
620                         (GTK_CTREE(mimeview->ctree), mimeview->opened);
621                 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
622                                  partinfo->mime_type == MIME_TEXT_HTML ||
623                                  partinfo->mime_type == MIME_TEXT_ENRICHED ||
624                                  partinfo->mime_type == MIME_MESSAGE_RFC822 ||
625                                  partinfo->mime_type == MIME_IMAGE ||
626                                  partinfo->mime_type == MIME_MULTIPART))
627                         menu_set_sensitive(mimeview->popupfactory,
628                                            "/Display as text", FALSE);
629                 else
630                         menu_set_sensitive(mimeview->popupfactory,
631                                            "/Display as text", TRUE);
632                 if (partinfo &&
633                     partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
634                         menu_set_sensitive(mimeview->popupfactory,
635                                            "/Open", FALSE);
636                 else
637                         menu_set_sensitive(mimeview->popupfactory,
638                                            "/Open", TRUE);
639
640 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
641                 if (partinfo && (partinfo->mime_type == MIME_IMAGE))
642                         menu_set_sensitive(mimeview->popupfactory,
643                                            "/Display image", TRUE);
644 #endif                                     
645 #if USE_GPGME
646                 menu_set_sensitive(mimeview->popupfactory,
647                                    "/Check signature",
648                                    mimeview_is_signed(mimeview));
649 #endif
650
651                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
652                                NULL, NULL, NULL, NULL,
653                                event->button, event->time);
654         }
655
656         return TRUE;
657 }
658
659 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
660 {
661         mimeview_key_pressed(mimeview->ctree, event, mimeview);
662 }
663
664 #define BREAK_ON_MODIFIER_KEY() \
665         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
666
667 #define KEY_PRESS_EVENT_STOP() \
668         if (gtk_signal_n_emissions_by_name \
669                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
670                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
671                                              "key_press_event"); \
672         }
673
674 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
675                                  MimeView *mimeview)
676 {
677         SummaryView *summaryview;
678         GtkCTree *ctree = GTK_CTREE(widget);
679         GtkCTreeNode *node;
680
681         if (!event) return FALSE;
682         if (!mimeview->opened) return FALSE;
683
684         switch (event->keyval) {
685         case GDK_space:
686                 if (textview_scroll_page(mimeview->textview, FALSE))
687                         return TRUE;
688
689                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
690                 if (node) {
691                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
692                         gtk_sctree_select(GTK_SCTREE(ctree), node);
693                         return TRUE;
694                 }
695                 break;
696         case GDK_BackSpace:
697                 textview_scroll_page(mimeview->textview, TRUE);
698                 return TRUE;
699         case GDK_Return:
700                 textview_scroll_one_line(mimeview->textview,
701                                          (event->state & GDK_MOD1_MASK) != 0);
702                 return TRUE;
703         case GDK_n:
704         case GDK_N:
705                 BREAK_ON_MODIFIER_KEY();
706                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
707                 KEY_PRESS_EVENT_STOP();
708
709                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
710                                         GTK_SCROLL_STEP_FORWARD, 0.0);
711                 return TRUE;
712         case GDK_p:
713         case GDK_P:
714                 BREAK_ON_MODIFIER_KEY();
715                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
716                 KEY_PRESS_EVENT_STOP();
717
718                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
719                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
720                 return TRUE;
721         case GDK_y:
722                 BREAK_ON_MODIFIER_KEY();
723                 KEY_PRESS_EVENT_STOP();
724                 mimeview_save_as(mimeview);
725                 return TRUE;
726         case GDK_t:
727                 BREAK_ON_MODIFIER_KEY();
728                 KEY_PRESS_EVENT_STOP();
729                 mimeview_display_as_text(mimeview);
730                 return TRUE;    
731         case GDK_i:
732                 BREAK_ON_MODIFIER_KEY();
733                 KEY_PRESS_EVENT_STOP();
734                 mimeview_show_image(mimeview);
735                 return TRUE;
736         case GDK_l:
737                 BREAK_ON_MODIFIER_KEY();
738                 KEY_PRESS_EVENT_STOP();
739                 mimeview_launch(mimeview);
740                 return TRUE;
741         default:
742                 break;
743         }
744
745         if (!mimeview->messageview->mainwin) return FALSE;
746         summaryview = mimeview->messageview->mainwin->summaryview;
747         summary_pass_key_press_event(summaryview, event);
748         return TRUE;
749 }
750
751 static void mimeview_drag_data_get(GtkWidget        *widget,
752                                    GdkDragContext   *drag_context,
753                                    GtkSelectionData *selection_data,
754                                    guint             info,
755                                    guint             time,
756                                    MimeView         *mimeview)
757 {
758         gchar *filename, *uriname;
759         MimeInfo *partinfo;
760
761         if (!mimeview->opened) return;
762         if (!mimeview->file) return;
763
764         partinfo = gtk_ctree_node_get_row_data
765                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
766         if (!partinfo) return;
767         if (!partinfo->filename && !partinfo->name) return;
768
769         filename = partinfo->filename ? partinfo->filename : partinfo->name;
770         filename = g_basename(filename);
771         if (*filename == '\0') return;
772
773         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
774                                filename, NULL);
775
776         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
777                 alertpanel_error
778                         (_("Can't save the part of multipart message."));
779
780         uriname = g_strconcat("file:/", filename, NULL);
781         gtk_selection_data_set(selection_data, selection_data->target, 8,
782                                uriname, strlen(uriname));
783
784         g_free(uriname);
785         g_free(filename);
786 }
787
788 static void mimeview_display_as_text(MimeView *mimeview)
789 {
790         MimeInfo *partinfo;
791
792         if (!mimeview->opened) return;
793
794         partinfo = gtk_ctree_node_get_row_data
795                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
796         mimeview_show_message_part(mimeview, partinfo);
797 }
798
799 static void mimeview_show_image(MimeView *mimeview)
800 {
801         MimeInfo *partinfo;
802
803         if (!mimeview->opened) return;
804
805         partinfo = gtk_ctree_node_get_row_data
806                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
807         mimeview_show_image_part(mimeview, partinfo);
808 }
809
810 static void mimeview_save_as(MimeView *mimeview)
811 {
812         gchar *filename;
813         gchar *defname = NULL;
814         MimeInfo *partinfo;
815
816         if (!mimeview->opened) return;
817         if (!mimeview->file) return;
818
819         partinfo = gtk_ctree_node_get_row_data
820                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
821         g_return_if_fail(partinfo != NULL);
822
823         if (partinfo->filename)
824                 defname = partinfo->filename;
825         else if (partinfo->name) {
826                 Xstrdup_a(defname, partinfo->name, return);
827                 subst_for_filename(defname);
828         }
829
830         filename = filesel_select_file(_("Save as"), defname);
831         if (!filename) return;
832         if (is_file_exist(filename)) {
833                 AlertValue aval;
834
835                 aval = alertpanel(_("Overwrite"),
836                                   _("Overwrite existing file?"),
837                                   _("OK"), _("Cancel"), NULL);
838                 if (G_ALERTDEFAULT != aval) return;
839         }
840
841         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
842                 alertpanel_error
843                         (_("Can't save the part of multipart message."));
844 }
845
846 static void mimeview_launch(MimeView *mimeview)
847 {
848         MimeInfo *partinfo;
849         gchar *filename;
850
851         if (!mimeview->opened) return;
852         if (!mimeview->file) return;
853
854         partinfo = gtk_ctree_node_get_row_data
855                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
856         g_return_if_fail(partinfo != NULL);
857
858         filename = procmime_get_tmp_file_name(partinfo);
859
860         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
861                 alertpanel_error
862                         (_("Can't save the part of multipart message."));
863         else
864                 mimeview_view_file(filename, partinfo, NULL);
865
866         g_free(filename);
867 }
868
869 static void mimeview_open_with(MimeView *mimeview)
870 {
871         MimeInfo *partinfo;
872         gchar *filename;
873         gchar *cmd;
874
875         if (!mimeview->opened) return;
876         if (!mimeview->file) return;
877
878         partinfo = gtk_ctree_node_get_row_data
879                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
880         g_return_if_fail(partinfo != NULL);
881
882         filename = procmime_get_tmp_file_name(partinfo);
883
884         if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
885                 alertpanel_error
886                         (_("Can't save the part of multipart message."));
887                 g_free(filename);
888                 return;
889         }
890
891         if (!prefs_common.mime_open_cmd_history)
892                 prefs_common.mime_open_cmd_history =
893                         add_history(NULL, prefs_common.mime_open_cmd);
894
895         cmd = input_dialog_combo
896                 (_("Open with"),
897                  _("Enter the command line to open file:\n"
898                    "(`%s' will be replaced with file name)"),
899                  prefs_common.mime_open_cmd,
900                  prefs_common.mime_open_cmd_history,
901                  TRUE);
902         if (cmd) {
903                 mimeview_view_file(filename, partinfo, cmd);
904                 g_free(prefs_common.mime_open_cmd);
905                 prefs_common.mime_open_cmd = cmd;
906                 prefs_common.mime_open_cmd_history =
907                         add_history(prefs_common.mime_open_cmd_history, cmd);
908         }
909
910         g_free(filename);
911 }
912
913 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
914                                const gchar *cmdline)
915 {
916         static gchar *default_image_cmdline = "display '%s'";
917         static gchar *default_audio_cmdline = "play '%s'";
918         static gchar *default_html_cmdline =
919                 "netscape -remote 'openURL(%s,raise)'";
920         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
921         gchar buf[1024];
922         gchar m_buf[1024];
923         const gchar *cmd;
924         const gchar *def_cmd;
925         const gchar *p;
926
927         if (cmdline) {
928                 cmd = cmdline;
929                 def_cmd = NULL;
930         } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
931                 return;
932         } else if (MIME_IMAGE == partinfo->mime_type) {
933                 cmd = prefs_common.mime_image_viewer;
934                 def_cmd = default_image_cmdline;
935         } else if (MIME_AUDIO == partinfo->mime_type) {
936                 cmd = prefs_common.mime_audio_player;
937                 def_cmd = default_audio_cmdline;
938         } else if (MIME_TEXT_HTML == partinfo->mime_type) {
939                 cmd = prefs_common.uri_cmd;
940                 def_cmd = default_html_cmdline;
941         } else {
942                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
943                            partinfo->content_type, "%s");
944                 cmd = m_buf;
945                 def_cmd = NULL;
946         }
947
948         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
949             !strchr(p + 2, '%'))
950                 g_snprintf(buf, sizeof(buf), cmd, filename);
951         else {
952                 if (cmd)
953                         g_warning(_("MIME viewer command line is invalid: `%s'"), cmd);
954                 if (def_cmd)
955                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
956                 else
957                         return;
958         }
959
960         execute_command_line(buf, TRUE);
961 }
962
963 #if USE_GPGME
964 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
965                              gpointer data)
966 {
967         MimeInfo *partinfo;
968         gchar *part_name;
969
970         partinfo = gtk_ctree_node_get_row_data(ctree, node);
971         g_return_if_fail(partinfo != NULL);
972
973         part_name = get_part_name(partinfo);
974         gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
975 }
976
977 static void mimeview_update_names(MimeView *mimeview)
978 {
979         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
980
981         gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
982 }
983
984 static void mimeview_update_signature_info(MimeView *mimeview)
985 {
986         MimeInfo *partinfo;
987
988         if (!mimeview) return;
989         if (!mimeview->opened) return;
990
991         partinfo = gtk_ctree_node_get_row_data
992                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
993         if (!partinfo) return;
994
995         if (g_strcasecmp(partinfo->content_type,
996                          "application/pgp-signature") == 0) {
997                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
998                 textview_show_signature_part(mimeview->textview, partinfo);
999         }
1000 }
1001
1002 static void mimeview_check_signature(MimeView *mimeview)
1003 {
1004         MimeInfo *mimeinfo;
1005         gchar buf[BUFFSIZE];
1006         FILE *fp;
1007
1008         g_return_if_fail (mimeview_is_signed(mimeview));
1009
1010         mimeinfo = gtk_ctree_node_get_row_data
1011                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1012         g_return_if_fail(mimeinfo != NULL);
1013         g_return_if_fail(mimeview->file != NULL);
1014
1015         while (mimeinfo->parent)
1016                 mimeinfo = mimeinfo->parent;
1017
1018         if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1019                 FILE_OP_ERROR(mimeview->file, "fopen");
1020                 return;
1021         }
1022
1023         /* skip headers */
1024         if (mimeinfo->mime_type == MIME_MULTIPART) {
1025                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
1026                 FILE_OP_ERROR(mimeview->file, "fseek");
1027                 while (fgets(buf, sizeof(buf), fp) != NULL)
1028                         if (buf[0] == '\r' || buf[0] == '\n') break;
1029         }
1030
1031         procmime_scan_multipart_message(mimeinfo, fp);
1032         rfc2015_check_signature(mimeinfo, fp);
1033         fclose(fp);
1034
1035         mimeview_update_names(mimeview);
1036         mimeview_update_signature_info(mimeview);
1037 }
1038 #endif /* USE_GPGME */