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