cb8db9ce60c75678a7e2bbd8b03238a7b4a8cdad
[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
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                                      prefs_common.resize_image);
471                 unlink(filename);
472         }
473
474         g_free(filename);
475 }
476
477 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
478 {
479         TextView  *textview  = mimeview->textview;
480         ImageView *imageview = mimeview->imageview;
481         GList *children;
482
483         if (mimeview->type == type) return;
484
485         children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
486         if (children) {
487                 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
488                                        GTK_WIDGET(children->data));
489                 g_list_free(children);
490         }
491
492         switch (type) {
493         case MIMEVIEW_IMAGE:
494                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
495                                   GTK_WIDGET_PTR(imageview));
496                 break;
497         case MIMEVIEW_TEXT:
498                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
499                                   GTK_WIDGET_PTR(textview));
500                 break;
501         default:
502                 return;
503         }
504
505         mimeview->type = type;
506 }
507
508 static void mimeview_clear(MimeView *mimeview)
509 {
510         GtkCList *clist = GTK_CLIST(mimeview->ctree);
511
512         procmime_mimeinfo_free_all(mimeview->mimeinfo);
513         mimeview->mimeinfo = NULL;
514
515         gtk_clist_clear(clist);
516         textview_clear(mimeview->textview);
517         imageview_clear(mimeview->imageview);
518
519         mimeview->opened = NULL;
520
521         g_free(mimeview->file);
522         mimeview->file = NULL;
523
524         /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
525 }
526
527 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
528                               MimeView *mimeview)
529 {
530         MimeInfo *partinfo;
531
532         if (mimeview->opened == node) return;
533         mimeview->opened = node;
534         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
535
536         partinfo = gtk_ctree_node_get_row_data(ctree, node);
537         if (!partinfo) return;
538
539         /* ungrab the mouse event */
540         if (GTK_WIDGET_HAS_GRAB(ctree)) {
541                 gtk_grab_remove(GTK_WIDGET(ctree));
542                 if (gdk_pointer_is_grabbed())
543                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
544         }
545         
546         mimeview->textview->default_text = FALSE;
547         
548         switch (partinfo->mime_type) {
549         case MIME_TEXT:
550         case MIME_TEXT_HTML:
551         case MIME_TEXT_ENRICHED:
552         case MIME_MESSAGE_RFC822:
553         case MIME_MULTIPART:
554                 mimeview_show_message_part(mimeview, partinfo);
555                 
556                 break;
557 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
558         case MIME_IMAGE:
559                 mimeview->textview->default_text = TRUE;        
560                 if (prefs_common.display_img)
561                         mimeview_show_image_part(mimeview, partinfo);
562                 else {
563                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
564                         textview_show_mime_part(mimeview->textview, partinfo);
565                 }
566                 break;
567 #endif
568         default:
569                 mimeview->textview->default_text = TRUE;        
570                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
571 #if USE_GPGME
572                 if (g_strcasecmp(partinfo->content_type,
573                                  "application/pgp-signature") == 0)
574                         textview_show_signature_part(mimeview->textview,
575                                                      partinfo);
576                 else
577 #endif
578                         textview_show_mime_part(mimeview->textview, partinfo);
579                 break;
580         }
581 }
582
583 static void mimeview_start_drag(GtkWidget *widget, gint button,
584                                 GdkEvent *event, MimeView *mimeview)
585 {
586         GtkTargetList *list;
587         GdkDragContext *context;
588         MimeInfo *partinfo;
589         GtkCTree *ctree;
590
591         g_return_if_fail(mimeview != NULL);
592         ctree = GTK_CTREE(mimeview->ctree);
593         partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
594         if (partinfo->filename == NULL && partinfo->name == NULL) return;
595
596         list = gtk_target_list_new(mimeview_mime_types, 1);
597         context = gtk_drag_begin(widget, list,
598                                  GDK_ACTION_COPY, button, event);
599         gtk_drag_set_icon_default(context);
600 }
601
602 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
603                                     MimeView *mimeview)
604 {
605         GtkCList *clist = GTK_CLIST(widget);
606         MimeInfo *partinfo;
607         gint row, column;
608
609         if (!event) return FALSE;
610
611         if (event->button == 2 || event->button == 3) {
612                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
613                                                   &row, &column))
614                         return FALSE;
615                 gtk_clist_unselect_all(clist);
616                 gtk_clist_select_row(clist, row, column);
617                 gtkut_clist_set_focus_row(clist, row);
618         }
619
620         if (event->button == 2 ||
621             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
622                 /* call external program for image, audio or html */
623                 mimeview_launch(mimeview);
624         } else if (event->button == 3) {
625                 partinfo = gtk_ctree_node_get_row_data
626                         (GTK_CTREE(mimeview->ctree), mimeview->opened);
627                 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
628                                  partinfo->mime_type == MIME_TEXT_HTML ||
629                                  partinfo->mime_type == MIME_TEXT_ENRICHED ||
630                                  partinfo->mime_type == MIME_MESSAGE_RFC822 ||
631                                  partinfo->mime_type == MIME_IMAGE ||
632                                  partinfo->mime_type == MIME_MULTIPART))
633                         menu_set_sensitive(mimeview->popupfactory,
634                                            "/Display as text", FALSE);
635                 else
636                         menu_set_sensitive(mimeview->popupfactory,
637                                            "/Display as text", TRUE);
638                 if (partinfo &&
639                     partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
640                         menu_set_sensitive(mimeview->popupfactory,
641                                            "/Open", FALSE);
642                 else
643                         menu_set_sensitive(mimeview->popupfactory,
644                                            "/Open", TRUE);
645
646 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
647                 if (partinfo && (partinfo->mime_type == MIME_IMAGE))
648                         menu_set_sensitive(mimeview->popupfactory,
649                                            "/Display image", TRUE);
650 #endif                                     
651 #if USE_GPGME
652                 menu_set_sensitive(mimeview->popupfactory,
653                                    "/Check signature",
654                                    mimeview_is_signed(mimeview));
655 #endif
656
657                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
658                                NULL, NULL, NULL, NULL,
659                                event->button, event->time);
660         }
661
662         return TRUE;
663 }
664
665 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
666 {
667         mimeview_key_pressed(mimeview->ctree, event, mimeview);
668 }
669
670 #define BREAK_ON_MODIFIER_KEY() \
671         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
672
673 #define KEY_PRESS_EVENT_STOP() \
674         if (gtk_signal_n_emissions_by_name \
675                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
676                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
677                                              "key_press_event"); \
678         }
679
680 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
681                                  MimeView *mimeview)
682 {
683         SummaryView *summaryview;
684         GtkCTree *ctree = GTK_CTREE(widget);
685         GtkCTreeNode *node;
686
687         if (!event) return FALSE;
688         if (!mimeview->opened) return FALSE;
689
690         switch (event->keyval) {
691         case GDK_space:
692                 if (textview_scroll_page(mimeview->textview, FALSE))
693                         return TRUE;
694
695                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
696                 if (node) {
697                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
698                         gtk_sctree_select(GTK_SCTREE(ctree), node);
699                         return TRUE;
700                 }
701                 break;
702         case GDK_BackSpace:
703                 textview_scroll_page(mimeview->textview, TRUE);
704                 return TRUE;
705         case GDK_Return:
706                 textview_scroll_one_line(mimeview->textview,
707                                          (event->state & GDK_MOD1_MASK) != 0);
708                 return TRUE;
709         case GDK_n:
710         case GDK_N:
711                 BREAK_ON_MODIFIER_KEY();
712                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
713                 KEY_PRESS_EVENT_STOP();
714
715                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
716                                         GTK_SCROLL_STEP_FORWARD, 0.0);
717                 return TRUE;
718         case GDK_p:
719         case GDK_P:
720                 BREAK_ON_MODIFIER_KEY();
721                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
722                 KEY_PRESS_EVENT_STOP();
723
724                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
725                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
726                 return TRUE;
727         case GDK_y:
728                 BREAK_ON_MODIFIER_KEY();
729                 KEY_PRESS_EVENT_STOP();
730                 mimeview_save_as(mimeview);
731                 return TRUE;
732         case GDK_t:
733                 BREAK_ON_MODIFIER_KEY();
734                 KEY_PRESS_EVENT_STOP();
735                 mimeview_display_as_text(mimeview);
736                 return TRUE;    
737         case GDK_i:
738                 BREAK_ON_MODIFIER_KEY();
739                 KEY_PRESS_EVENT_STOP();
740                 mimeview_show_image(mimeview);
741                 return TRUE;
742         case GDK_l:
743                 BREAK_ON_MODIFIER_KEY();
744                 KEY_PRESS_EVENT_STOP();
745                 mimeview_launch(mimeview);
746                 return TRUE;
747         default:
748                 break;
749         }
750
751         if (!mimeview->messageview->mainwin) return FALSE;
752         summaryview = mimeview->messageview->mainwin->summaryview;
753         summary_pass_key_press_event(summaryview, event);
754         return TRUE;
755 }
756
757 static void mimeview_drag_data_get(GtkWidget        *widget,
758                                    GdkDragContext   *drag_context,
759                                    GtkSelectionData *selection_data,
760                                    guint             info,
761                                    guint             time,
762                                    MimeView         *mimeview)
763 {
764         gchar *filename, *uriname;
765         MimeInfo *partinfo;
766
767         if (!mimeview->opened) return;
768         if (!mimeview->file) return;
769
770         partinfo = gtk_ctree_node_get_row_data
771                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
772         if (!partinfo) return;
773         if (!partinfo->filename && !partinfo->name) return;
774
775         filename = partinfo->filename ? partinfo->filename : partinfo->name;
776         filename = g_basename(filename);
777         if (*filename == '\0') return;
778
779         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
780                                filename, NULL);
781
782         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
783                 alertpanel_error
784                         (_("Can't save the part of multipart message."));
785
786         uriname = g_strconcat("file:/", filename, NULL);
787         gtk_selection_data_set(selection_data, selection_data->target, 8,
788                                uriname, strlen(uriname));
789
790         g_free(uriname);
791         g_free(filename);
792 }
793
794 static void mimeview_save_all(MimeView *mimeview)
795 {
796         gchar *dirname;
797         gchar *defname = NULL;
798         MimeInfo *partinfo;
799         MimeInfo *attachment;
800         gchar buf[1024];
801
802         if (!mimeview->opened) return;
803         if (!mimeview->file) return;
804
805         partinfo = gtk_ctree_node_get_row_data
806                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
807         g_return_if_fail(partinfo != NULL);
808
809         dirname = filesel_select_file(_("Save as"), defname);
810         if (!dirname) return;
811
812         /* return to first children */
813         attachment = partinfo->parent->children->next;
814         /* for each attachment, extract it in the selected dir. */
815         while (attachment != NULL) {
816                 static guint subst_cnt = 1;
817                 gchar *attachdir;
818                 gchar *attachname = (attachment->filename)
819                         ? g_strdup(attachment->filename)
820                         : g_strdup_printf("noname.%d",subst_cnt++);
821                 AlertValue aval;
822                 gchar *res;
823
824                 subst_chars(attachname, "/\\", G_DIR_SEPARATOR);
825                 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
826                 g_snprintf(buf, sizeof(buf), "%s%s",
827                            dirname,
828                            (attachname[0] == G_DIR_SEPARATOR)
829                            ? &attachname[1]
830                            : attachname);
831                 attachdir = g_dirname(buf);
832                 make_dir_hier(attachdir);
833                 g_free(attachdir);
834
835                 if (is_file_exist(buf)) {
836                         res = g_strdup_printf(_("Overwrite existing file '%s'?"),
837                                               attachname);
838                         aval = alertpanel(_("Overwrite"), res, _("OK"), 
839                                           _("Cancel"), NULL);
840                         g_free(res);                                      
841                 }
842                 g_free(attachname);
843
844                 if (G_ALERTDEFAULT == aval && procmime_get_part(buf, mimeview->file, attachment) < 0)
845                         alertpanel_error(_("Can't save the part of multipart message."));
846                 attachment = attachment->next;
847         }
848 }
849
850 static void mimeview_display_as_text(MimeView *mimeview)
851 {
852         MimeInfo *partinfo;
853
854         if (!mimeview->opened) return;
855
856         partinfo = gtk_ctree_node_get_row_data
857                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
858         mimeview_show_message_part(mimeview, partinfo);
859 }
860
861 static void mimeview_show_image(MimeView *mimeview)
862 {
863         MimeInfo *partinfo;
864
865         if (!mimeview->opened) return;
866
867         partinfo = gtk_ctree_node_get_row_data
868                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
869         mimeview_show_image_part(mimeview, partinfo);
870 }
871
872 static void mimeview_save_as(MimeView *mimeview)
873 {
874         gchar *filename;
875         gchar *defname = NULL;
876         MimeInfo *partinfo;
877
878         if (!mimeview->opened) return;
879         if (!mimeview->file) return;
880
881         partinfo = gtk_ctree_node_get_row_data
882                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
883         g_return_if_fail(partinfo != NULL);
884
885         if (partinfo->filename)
886                 defname = partinfo->filename;
887         else if (partinfo->name) {
888                 Xstrdup_a(defname, partinfo->name, return);
889                 subst_for_filename(defname);
890         }
891
892         filename = filesel_select_file(_("Save as"), defname);
893         if (!filename) return;
894         if (is_file_exist(filename)) {
895                 AlertValue aval;
896
897                 aval = alertpanel(_("Overwrite"),
898                                   _("Overwrite existing file?"),
899                                   _("OK"), _("Cancel"), NULL);
900                 if (G_ALERTDEFAULT != aval) return;
901         }
902
903         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
904                 alertpanel_error
905                         (_("Can't save the part of multipart message."));
906 }
907
908 static void mimeview_launch(MimeView *mimeview)
909 {
910         MimeInfo *partinfo;
911         gchar *filename;
912
913         if (!mimeview->opened) return;
914         if (!mimeview->file) return;
915
916         partinfo = gtk_ctree_node_get_row_data
917                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
918         g_return_if_fail(partinfo != NULL);
919
920         filename = procmime_get_tmp_file_name(partinfo);
921
922         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
923                 alertpanel_error
924                         (_("Can't save the part of multipart message."));
925         else
926                 mimeview_view_file(filename, partinfo, NULL);
927
928         g_free(filename);
929 }
930
931 static void mimeview_open_with(MimeView *mimeview)
932 {
933         MimeInfo *partinfo;
934         gchar *filename;
935         gchar *cmd;
936
937         if (!mimeview->opened) return;
938         if (!mimeview->file) return;
939
940         partinfo = gtk_ctree_node_get_row_data
941                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
942         g_return_if_fail(partinfo != NULL);
943
944         filename = procmime_get_tmp_file_name(partinfo);
945
946         if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
947                 alertpanel_error
948                         (_("Can't save the part of multipart message."));
949                 g_free(filename);
950                 return;
951         }
952
953         if (!prefs_common.mime_open_cmd_history)
954                 prefs_common.mime_open_cmd_history =
955                         add_history(NULL, prefs_common.mime_open_cmd);
956
957         cmd = input_dialog_combo
958                 (_("Open with"),
959                  _("Enter the command line to open file:\n"
960                    "(`%s' will be replaced with file name)"),
961                  prefs_common.mime_open_cmd,
962                  prefs_common.mime_open_cmd_history,
963                  TRUE);
964         if (cmd) {
965                 mimeview_view_file(filename, partinfo, cmd);
966                 g_free(prefs_common.mime_open_cmd);
967                 prefs_common.mime_open_cmd = cmd;
968                 prefs_common.mime_open_cmd_history =
969                         add_history(prefs_common.mime_open_cmd_history, cmd);
970         }
971
972         g_free(filename);
973 }
974
975 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
976                                const gchar *cmdline)
977 {
978         static gchar *default_image_cmdline = "display '%s'";
979         static gchar *default_audio_cmdline = "play '%s'";
980         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
981         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
982         gchar buf[1024];
983         gchar m_buf[1024];
984         const gchar *cmd;
985         const gchar *def_cmd;
986         const gchar *p;
987
988         if (cmdline) {
989                 cmd = cmdline;
990                 def_cmd = NULL;
991         } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
992                 return;
993         } else if (MIME_IMAGE == partinfo->mime_type) {
994                 cmd = prefs_common.mime_image_viewer;
995                 def_cmd = default_image_cmdline;
996         } else if (MIME_AUDIO == partinfo->mime_type) {
997                 cmd = prefs_common.mime_audio_player;
998                 def_cmd = default_audio_cmdline;
999         } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1000                 cmd = prefs_common.uri_cmd;
1001                 def_cmd = default_html_cmdline;
1002         } else {
1003                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1004                            partinfo->content_type, "%s");
1005                 cmd = m_buf;
1006                 def_cmd = NULL;
1007         }
1008
1009         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1010             !strchr(p + 2, '%'))
1011                 g_snprintf(buf, sizeof(buf), cmd, filename);
1012         else {
1013                 if (cmd)
1014                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1015                 if (def_cmd)
1016                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1017                 else
1018                         return;
1019         }
1020
1021         execute_command_line(buf, TRUE);
1022 }
1023
1024 #if USE_GPGME
1025 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1026                              gpointer data)
1027 {
1028         MimeInfo *partinfo;
1029         gchar *part_name;
1030
1031         partinfo = gtk_ctree_node_get_row_data(ctree, node);
1032         g_return_if_fail(partinfo != NULL);
1033
1034         part_name = get_part_name(partinfo);
1035         gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1036 }
1037
1038 static void mimeview_update_names(MimeView *mimeview)
1039 {
1040         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1041
1042         gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1043 }
1044
1045 static void mimeview_update_signature_info(MimeView *mimeview)
1046 {
1047         MimeInfo *partinfo;
1048
1049         if (!mimeview) return;
1050         if (!mimeview->opened) return;
1051
1052         partinfo = gtk_ctree_node_get_row_data
1053                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1054         if (!partinfo) return;
1055
1056         if (g_strcasecmp(partinfo->content_type,
1057                          "application/pgp-signature") == 0) {
1058                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1059                 textview_show_signature_part(mimeview->textview, partinfo);
1060         }
1061 }
1062
1063 void mimeview_check_signature(MimeView *mimeview)
1064 {
1065         MimeInfo *mimeinfo;
1066         FILE *fp;
1067
1068         g_return_if_fail (mimeview_is_signed(mimeview));
1069         g_return_if_fail (gpg_started);
1070
1071         mimeinfo = gtk_ctree_node_get_row_data
1072                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1073         g_return_if_fail(mimeinfo != NULL);
1074         g_return_if_fail(mimeview->file != NULL);
1075
1076         while (mimeinfo->parent)
1077                 mimeinfo = mimeinfo->parent;
1078
1079         if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1080                 FILE_OP_ERROR(mimeview->file, "fopen");
1081                 return;
1082         }
1083
1084         rfc2015_check_signature(mimeinfo, fp);
1085         fclose(fp);
1086
1087         mimeview_update_names(mimeview);
1088         mimeview_update_signature_info(mimeview);
1089
1090         textview_show_message(mimeview->messageview->textview, mimeinfo,
1091                               mimeview->file);
1092 }
1093 #endif /* USE_GPGME */