fix open attachment with whitespace bug
[claws.git] / src / mimeview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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/gtkscrolledwindow.h>
29 #include <gtk/gtkctree.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkvpaned.h>
32 #include <gtk/gtktext.h>
33 #include <gtk/gtksignal.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkdnd.h>
36 #include <gtk/gtkselection.h>
37 #include <stdio.h>
38 #include <unistd.h>
39
40 #include "intl.h"
41 #include "main.h"
42 #include "mimeview.h"
43 #include "textview.h"
44 #include "procmime.h"
45 #include "summaryview.h"
46 #include "menu.h"
47 #include "filesel.h"
48 #include "alertpanel.h"
49 #include "inputdialog.h"
50 #include "utils.h"
51 #include "gtkutils.h"
52 #include "prefs_common.h"
53 #include "rfc2015.h"
54
55 typedef enum
56 {
57         COL_MIMETYPE = 0,
58         COL_SIZE     = 1,
59         COL_NAME     = 2
60 } MimeViewColumnPos;
61
62 #define N_MIMEVIEW_COLS 3
63
64 static void mimeview_set_multipart_tree         (MimeView       *mimeview,
65                                                  MimeInfo       *mimeinfo,
66                                                  GtkCTreeNode   *parent);
67 static GtkCTreeNode *mimeview_append_part       (MimeView       *mimeview,
68                                                  MimeInfo       *partinfo,
69                                                  GtkCTreeNode   *parent);
70 static void mimeview_show_message_part          (MimeView       *mimeview,
71                                                  MimeInfo       *partinfo);
72 static void mimeview_show_image_part            (MimeView       *mimeview,
73                                                  MimeInfo       *partinfo);
74 static void mimeview_change_view_type           (MimeView       *mimeview,
75                                                  MimeViewType    type);
76 static void mimeview_clear                      (MimeView       *mimeview);
77
78 static void mimeview_selected           (GtkCTree       *ctree,
79                                          GtkCTreeNode   *node,
80                                          gint            column,
81                                          MimeView       *mimeview);
82 static void mimeview_start_drag         (GtkWidget      *widget,
83                                          gint            button,
84                                          GdkEvent       *event,
85                                          MimeView       *mimeview);
86 static void mimeview_button_pressed     (GtkWidget      *widget,
87                                          GdkEventButton *event,
88                                          MimeView       *mimeview);
89 static void mimeview_key_pressed        (GtkWidget      *widget,
90                                          GdkEventKey    *event,
91                                          MimeView       *mimeview);
92
93 static void mimeview_drag_data_get      (GtkWidget        *widget,
94                                          GdkDragContext   *drag_context,
95                                          GtkSelectionData *selection_data,
96                                          guint             info,
97                                          guint             time,
98                                          MimeView         *mimeview);
99
100 static void mimeview_display_as_text    (MimeView       *mimeview);
101 static void mimeview_save_as            (MimeView       *mimeview);
102 static void mimeview_launch             (MimeView       *mimeview);
103 static void mimeview_open_with          (MimeView       *mimeview);
104 static void mimeview_view_file          (const gchar    *filename,
105                                          MimeInfo       *partinfo,
106                                          const gchar    *cmdline);
107 #if USE_GPGME
108 static void mimeview_check_signature    (MimeView       *mimeview);
109 #endif
110
111 static GtkItemFactoryEntry mimeview_popup_entries[] =
112 {
113         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
114         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
115         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
116         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL}
117 #if USE_GPGME
118         ,
119         {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
120 #endif
121 };
122
123 static GtkTargetEntry mimeview_mime_types[] =
124 {
125         {"text/uri-list", 0, 0}
126 };
127
128 MimeView *mimeview_create(void)
129 {
130         MimeView *mimeview;
131
132         GtkWidget *paned;
133         GtkWidget *scrolledwin;
134         GtkWidget *ctree;
135         GtkWidget *vbox;
136         GtkWidget *popupmenu;
137         GtkItemFactory *popupfactory;
138
139         gchar *titles[] = {_("MIME Type"), _("Size"), _("Name")};
140         gint n_entries;
141         gint i;
142
143         debug_print(_("Creating MIME view...\n"));
144         mimeview = g_new0(MimeView, 1);
145
146         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
147         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
148                                        GTK_POLICY_AUTOMATIC,
149                                        GTK_POLICY_ALWAYS);
150         gtk_widget_set_usize(scrolledwin, -1, 80);
151
152         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
153         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
154         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
155         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
156                                            GTK_JUSTIFY_RIGHT);
157         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
158         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
159         for (i = 0; i < N_MIMEVIEW_COLS; i++)
160                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
161                                        GTK_CAN_FOCUS);
162         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
163
164         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
165                            GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
166         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
167                            GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
168         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
169                            GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
170         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
171                            GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
172         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
173                            GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
174     
175         vbox = gtk_vbox_new(FALSE, 0);
176
177         paned = gtk_vpaned_new();
178         gtk_paned_add1(GTK_PANED(paned), scrolledwin);
179         gtk_paned_add2(GTK_PANED(paned), vbox);
180         gtk_widget_show_all(paned);
181
182         n_entries = sizeof(mimeview_popup_entries) /
183                 sizeof(mimeview_popup_entries[0]);
184         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
185                                       "<MimeView>", &popupfactory, mimeview);
186
187         mimeview->paned       = paned;
188         mimeview->scrolledwin = scrolledwin;
189         mimeview->ctree       = ctree;
190         mimeview->vbox        = vbox;
191         mimeview->popupmenu   = popupmenu;
192         mimeview->popupfactory = popupfactory;
193
194         return mimeview;
195 }
196
197 void mimeview_init(MimeView *mimeview)
198 {
199 }
200
201 /* 
202  * Check whether the message is OpenPGP signed
203  */
204 #if USE_GPGME
205 static gboolean mimeview_is_signed(MimeView *mimeview)
206 {
207         MimeInfo *partinfo;
208
209         debug_print("mimeview_is signed of %p\n", mimeview);
210
211         if (!mimeview) return FALSE;
212         if (!mimeview->opened) return FALSE;
213
214         debug_print("mimeview_is_signed: open\n" );
215
216         if (!mimeview->file) return FALSE;
217
218         debug_print("mimeview_is_signed: file\n" );
219
220         partinfo = gtk_ctree_node_get_row_data
221                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
222         g_return_val_if_fail(partinfo != NULL, FALSE);
223
224         /* walk the tree and see whether there is a signature somewhere */
225         do {
226                 if (rfc2015_has_signature(partinfo))
227                         return TRUE;
228         } while ((partinfo = partinfo->parent) != NULL);
229
230         debug_print("mimeview_is_signed: FALSE\n" );
231
232         return FALSE;
233 }
234
235 static void set_unchecked_signature(MimeInfo *mimeinfo)
236 {
237         MimeInfo *sig_partinfo;
238
239         sig_partinfo = rfc2015_find_signature(mimeinfo);
240         if (sig_partinfo == NULL) return;
241
242         g_free(sig_partinfo->sigstatus);
243         sig_partinfo->sigstatus =
244                 g_strdup(_("Select \"Check signature\" to check"));
245
246         g_free(sig_partinfo->sigstatus_full);
247         sig_partinfo->sigstatus_full = NULL;
248 }
249 #endif /* USE_GPGME */
250
251 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
252                            const gchar *file)
253 {
254         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
255         GtkCTreeNode *node;
256         gchar buf[BUFFSIZE];
257         FILE *fp;
258
259         mimeview_clear(mimeview);
260
261         g_return_if_fail(file != NULL);
262         g_return_if_fail(mimeinfo != NULL);
263
264         mimeview->mimeinfo = mimeinfo;
265
266         if ((fp = fopen(file, "r")) == NULL) {
267                 FILE_OP_ERROR(file, "fopen");
268                 return;
269         }
270         mimeview->file = g_strdup(file);
271
272         /* skip headers */
273         if (mimeinfo->mime_type == MIME_MULTIPART) {
274                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
275                 perror("fseek");
276                 while (fgets(buf, sizeof(buf), fp) != NULL)
277                         if (buf[0] == '\r' || buf[0] == '\n') break;
278         }
279
280         procmime_scan_multipart_message(mimeinfo, fp);
281 #if USE_GPGME
282         if (prefs_common.auto_check_signatures)
283                 rfc2015_check_signature(mimeinfo, fp);
284         else
285                 set_unchecked_signature(mimeinfo);
286 #endif
287         fclose(fp);
288
289         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
290                                          mimeview);
291
292         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
293
294         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
295                                            mimeview_selected, mimeview);
296
297         if ((node = gtk_ctree_node_nth(ctree, 0))) {
298                 gtk_ctree_select(ctree, node);
299                 gtk_widget_grab_focus(mimeview->ctree);
300         }
301 }
302
303 void mimeview_destroy(MimeView *mimeview)
304 {
305         procmime_mimeinfo_free_all(mimeview->mimeinfo);
306         g_free(mimeview->file);
307         g_free(mimeview);
308 }
309
310 static void mimeview_set_multipart_tree(MimeView *mimeview,
311                                         MimeInfo *mimeinfo,
312                                         GtkCTreeNode *parent)
313 {
314         g_return_if_fail(mimeinfo != NULL);
315
316         if (!mimeinfo->main && mimeinfo->parent)
317                 parent = mimeview_append_part(mimeview, mimeinfo, parent);
318
319         if (mimeinfo->sub && mimeinfo->sub->children)
320                 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, parent);
321
322         if (mimeinfo->children) {
323                 MimeInfo *child;
324
325                 child = mimeinfo->children;
326                 while (child) {
327                         mimeview_set_multipart_tree(mimeview, child, parent);
328                         child = child->next;
329                 }
330         }
331 }
332
333 static gchar *get_part_name(MimeInfo *partinfo)
334 {
335 #if USE_GPGME
336         if (partinfo->sigstatus)
337                 return partinfo->sigstatus;
338         else
339 #endif
340         if (partinfo->name)
341                 return partinfo->name;
342         else if (partinfo->filename)
343                 return partinfo->filename;
344         else
345                 return "";
346 }
347
348 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
349                                           MimeInfo *partinfo,
350                                           GtkCTreeNode *parent)
351 {
352         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
353         GtkCTreeNode *node;
354         gchar *str[N_MIMEVIEW_COLS];
355
356         str[COL_MIMETYPE] =
357                 partinfo->content_type ? partinfo->content_type : "";
358         str[COL_SIZE] = to_human_readable(partinfo->size);
359         str[COL_NAME] = get_part_name(partinfo);
360
361         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
362                                      NULL, NULL, NULL, NULL,
363                                      FALSE, TRUE);
364         gtk_ctree_node_set_row_data(ctree, node, partinfo);
365
366         return node;
367 }
368
369 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
370 {
371         FILE *fp;
372         const gchar *fname;
373 #if USE_GPGME
374         MimeInfo *pi;
375 #endif
376
377         if (!partinfo) return;
378
379 #if USE_GPGME
380         for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
381                 ;
382         fname = pi ? pi->plaintextfile : mimeview->file;
383 #else
384         fname = mimeview->file;
385 #endif /* USE_GPGME */
386         if (!fname) return;
387
388         if ((fp = fopen(fname, "r")) == NULL) {
389                 FILE_OP_ERROR(fname, "fopen");
390                 return;
391         }
392
393         if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
394                 FILE_OP_ERROR(mimeview->file, "fseek");
395                 fclose(fp);
396                 return;
397         }
398
399         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
400         textview_show_part(mimeview->textview, partinfo, fp);
401
402         fclose(fp);
403 }
404
405 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
406 {
407         gchar *filename;
408
409         if (!partinfo) return;
410
411         filename = procmime_get_tmp_file_name(partinfo);
412
413         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
414                 alertpanel_error
415                         (_("Can't get the part of multipart message."));
416         else {
417                 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
418                 imageview_show_image(mimeview->imageview, partinfo, filename);
419                 unlink(filename);
420         }
421
422         g_free(filename);
423 }
424
425 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
426 {
427         TextView  *textview  = mimeview->textview;
428         ImageView *imageview = mimeview->imageview;
429
430         if (mimeview->type == type) return;
431
432         switch (type) {
433         case MIMEVIEW_IMAGE:
434                 gtk_container_remove(GTK_CONTAINER(mimeview->vbox),
435                                      GTK_WIDGET_PTR(textview));
436                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
437                                   GTK_WIDGET_PTR(imageview));
438                 break;
439         case MIMEVIEW_TEXT:
440                 gtk_container_remove(GTK_CONTAINER(mimeview->vbox),
441                                      GTK_WIDGET_PTR(imageview));
442                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
443                                   GTK_WIDGET_PTR(textview));
444                 break;
445         default:
446                 return;
447         }
448
449         mimeview->type = type;
450 }
451
452 static void mimeview_clear(MimeView *mimeview)
453 {
454         GtkCList *clist = GTK_CLIST(mimeview->ctree);
455
456         procmime_mimeinfo_free_all(mimeview->mimeinfo);
457         mimeview->mimeinfo = NULL;
458
459         gtk_clist_clear(clist);
460         textview_clear(mimeview->textview);
461
462         mimeview->opened = NULL;
463
464         g_free(mimeview->file);
465         mimeview->file = NULL;
466 }
467
468 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
469                               MimeView *mimeview)
470 {
471         MimeInfo *partinfo;
472
473         if (mimeview->opened == node) return;
474         mimeview->opened = node;
475         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
476
477         partinfo = gtk_ctree_node_get_row_data(ctree, node);
478         if (!partinfo) return;
479
480         /* ungrab the mouse event */
481         if (GTK_WIDGET_HAS_GRAB(ctree)) {
482                 gtk_grab_remove(GTK_WIDGET(ctree));
483                 if (gdk_pointer_is_grabbed())
484                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
485         }
486
487         switch (partinfo->mime_type) {
488         case MIME_TEXT:
489         case MIME_TEXT_HTML:
490         case MIME_MESSAGE_RFC822:
491         case MIME_MULTIPART:
492                 mimeview_show_message_part(mimeview, partinfo);
493                 break;
494 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
495         case MIME_IMAGE:
496                 mimeview_show_image_part(mimeview, partinfo);
497                 break;
498 #endif
499         default:
500                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
501 #if USE_GPGME
502                 if (g_strcasecmp(partinfo->content_type,
503                                  "application/pgp-signature") == 0)
504                         textview_show_signature_part(mimeview->textview,
505                                                      partinfo);
506                 else
507 #endif
508                         textview_show_mime_part(mimeview->textview, partinfo);
509                 break;
510         }
511 }
512
513 static void mimeview_start_drag (GtkWidget *widget, gint button,
514                                  GdkEvent *event, MimeView *mimeview)
515 {
516         GtkTargetList *list;
517         GdkDragContext *context;
518         MimeInfo *partinfo;
519         GtkCTree *ctree;
520
521         g_return_if_fail(mimeview != NULL);
522         ctree = GTK_CTREE(mimeview->ctree);
523         partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
524         if (partinfo->filename == NULL && partinfo->name == NULL) return;
525
526         list = gtk_target_list_new(mimeview_mime_types, 1);
527         context = gtk_drag_begin(widget, list,
528                                  GDK_ACTION_COPY, button, event);
529         gtk_drag_set_icon_default(context);
530 }
531
532 static void mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
533                                     MimeView *mimeview)
534 {
535         GtkCList *clist = GTK_CLIST(widget);
536         MimeInfo *partinfo;
537         gint row, column;
538
539         if (!event) return;
540
541         if (event->button == 2 || event->button == 3) {
542                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
543                                                   &row, &column))
544                         return;
545                 gtk_clist_unselect_all(clist);
546                 gtk_clist_select_row(clist, row, column);
547                 gtkut_clist_set_focus_row(clist, row);
548         }
549
550         if (event->button == 2 ||
551             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
552                 /* call external program for image, audio or html */
553                 mimeview_launch(mimeview);
554         } else if (event->button == 3) {
555                 partinfo = gtk_ctree_node_get_row_data
556                         (GTK_CTREE(mimeview->ctree), mimeview->opened);
557                 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
558                                  partinfo->mime_type == MIME_TEXT_HTML ||
559                                  partinfo->mime_type == MIME_MESSAGE_RFC822 ||
560                                  partinfo->mime_type == MIME_IMAGE ||
561                                  partinfo->mime_type == MIME_MULTIPART))
562                         menu_set_sensitive(mimeview->popupfactory,
563                                            "/Display as text", FALSE);
564                 else
565                         menu_set_sensitive(mimeview->popupfactory,
566                                            "/Display as text", TRUE);
567                 if (partinfo &&
568                     partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
569                         menu_set_sensitive(mimeview->popupfactory,
570                                            "/Open", FALSE);
571                 else
572                         menu_set_sensitive(mimeview->popupfactory,
573                                            "/Open", TRUE);
574 #if USE_GPGME
575                 menu_set_sensitive(mimeview->popupfactory,
576                                    "/Check signature",
577                                    mimeview_is_signed(mimeview));
578 #endif
579
580                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
581                                NULL, NULL, NULL, NULL,
582                                event->button, event->time);
583         }
584 }
585
586 #define BREAK_ON_MODIFIER_KEY() \
587         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
588
589 static void mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
590                                  MimeView *mimeview)
591 {
592         SummaryView *summaryview;
593         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
594         GtkCTreeNode *node;
595
596         if (!event) return;
597         if (!mimeview->opened) return;
598
599         switch (event->keyval) {
600         case GDK_space:
601                 if (textview_scroll_page(mimeview->textview, FALSE)) return;
602
603                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
604                 if (node) {
605                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
606                         gtk_sctree_select(GTK_SCTREE(ctree), node);
607                         return;
608                 }
609                 break;
610         case GDK_BackSpace:
611         case GDK_Delete:
612                 textview_scroll_page(mimeview->textview, TRUE);
613                 return;
614         case GDK_Return:
615                 textview_scroll_one_line(mimeview->textview,
616                                          (event->state & GDK_MOD1_MASK) != 0);
617                 return;
618         case GDK_n:
619         case GDK_N:
620                 BREAK_ON_MODIFIER_KEY();
621                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
622
623                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
624                                         GTK_SCROLL_STEP_FORWARD, 0.0);
625                 return;
626         case GDK_p:
627         case GDK_P:
628                 BREAK_ON_MODIFIER_KEY();
629                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
630
631                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
632                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
633                 return;
634         case GDK_y:
635                 BREAK_ON_MODIFIER_KEY();
636                 mimeview_save_as(mimeview);
637                 return;
638         case GDK_t:
639                 BREAK_ON_MODIFIER_KEY();
640                 mimeview_display_as_text(mimeview);
641                 return;
642         case GDK_l:
643                 BREAK_ON_MODIFIER_KEY();
644                 mimeview_launch(mimeview);
645                 return;
646         default:
647                 break;
648         }
649
650         if (!mimeview->messageview->mainwin) return;
651         summaryview = mimeview->messageview->mainwin->summaryview;
652         summary_pass_key_press_event(summaryview, event);
653 }
654
655 static void mimeview_drag_data_get(GtkWidget        *widget,
656                                    GdkDragContext   *drag_context,
657                                    GtkSelectionData *selection_data,
658                                    guint             info,
659                                    guint             time,
660                                    MimeView         *mimeview)
661 {
662         gchar *filename, *uriname;
663         MimeInfo *partinfo;
664
665         if (!mimeview->opened) return;
666         if (!mimeview->file) return;
667
668         partinfo = gtk_ctree_node_get_row_data
669                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
670         if (!partinfo) return;
671         if (!partinfo->filename && !partinfo->name) return;
672
673         filename = partinfo->filename ? partinfo->filename : partinfo->name;
674         filename = g_basename(filename);
675         if (*filename == '\0') return;
676
677         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
678                                filename, NULL);
679
680         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
681                 alertpanel_error
682                         (_("Can't save the part of multipart message."));
683
684         uriname = g_strconcat("file:/", filename, NULL);
685         gtk_selection_data_set(selection_data, selection_data->target, 8,
686                                uriname, strlen(uriname));
687
688         g_free(uriname);
689         g_free(filename);
690 }
691
692 static void mimeview_display_as_text(MimeView *mimeview)
693 {
694         MimeInfo *partinfo;
695
696         if (!mimeview->opened) return;
697
698         partinfo = gtk_ctree_node_get_row_data
699                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
700         mimeview_show_message_part(mimeview, partinfo);
701 }
702
703 static void mimeview_save_as(MimeView *mimeview)
704 {
705         gchar *filename;
706         MimeInfo *partinfo;
707
708         if (!mimeview->opened) return;
709         if (!mimeview->file) return;
710
711         partinfo = gtk_ctree_node_get_row_data
712                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
713         g_return_if_fail(partinfo != NULL);
714
715         filename = filesel_select_file
716                 (_("Save as"),
717                  partinfo->filename ? partinfo->filename : partinfo->name);
718         if (!filename) return;
719         if (is_file_exist(filename)) {
720                 AlertValue aval;
721
722                 aval = alertpanel(_("Overwrite"),
723                                   _("Overwrite existing file?"),
724                                   _("OK"), _("Cancel"), NULL);
725                 if (G_ALERTDEFAULT != aval) return;
726         }
727
728         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
729                 alertpanel_error
730                         (_("Can't save the part of multipart message."));
731 }
732
733 static void mimeview_launch(MimeView *mimeview)
734 {
735         MimeInfo *partinfo;
736         gchar *filename;
737
738         if (!mimeview->opened) return;
739         if (!mimeview->file) return;
740
741         partinfo = gtk_ctree_node_get_row_data
742                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
743         g_return_if_fail(partinfo != NULL);
744
745         filename = procmime_get_tmp_file_name(partinfo);
746
747         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
748                 alertpanel_error
749                         (_("Can't save the part of multipart message."));
750         else
751                 mimeview_view_file(filename, partinfo, NULL);
752
753         g_free(filename);
754 }
755
756 static void mimeview_open_with(MimeView *mimeview)
757 {
758         MimeInfo *partinfo;
759         gchar *filename;
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         g_return_if_fail(partinfo != NULL);
767
768         filename = procmime_get_tmp_file_name(partinfo);
769
770         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
771                 alertpanel_error
772                         (_("Can't save the part of multipart message."));
773         else {
774                 gchar *cmd;
775
776                 cmd = input_dialog
777                         (_("Open with"),
778                          _("Enter the command line to open file:\n"
779                            "(`%s' will be replaced with file name)"),
780                          "gedit \"%s\"");
781                 if (cmd) {
782                         mimeview_view_file(filename, partinfo, cmd);
783                         g_free(cmd);
784                 }
785         }
786
787         g_free(filename);
788 }
789
790 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
791                                const gchar *cmdline)
792 {
793         static gchar *default_image_cmdline = "display '%s'";
794         static gchar *default_audio_cmdline = "play '%s'";
795         static gchar *default_html_cmdline =
796                 "netscape -remote 'openURL(%s,raise)'";
797         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
798         gchar buf[1024];
799         gchar m_buf[1024];
800         const gchar *cmd;
801         const gchar *def_cmd;
802         const gchar *p;
803
804         if (cmdline) {
805                 cmd = cmdline;
806                 def_cmd = NULL;
807         } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
808                 return;
809         } else if (MIME_IMAGE == partinfo->mime_type) {
810                 cmd = prefs_common.mime_image_viewer;
811                 def_cmd = default_image_cmdline;
812         } else if (MIME_AUDIO == partinfo->mime_type) {
813                 cmd = prefs_common.mime_audio_player;
814                 def_cmd = default_audio_cmdline;
815         } else if (MIME_TEXT_HTML == partinfo->mime_type) {
816                 cmd = prefs_common.uri_cmd;
817                 def_cmd = default_html_cmdline;
818         } else {
819                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
820                            partinfo->content_type, "%s");
821                 cmd = m_buf;
822                 def_cmd = NULL;
823         }
824
825         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
826             !strchr(p + 2, '%'))
827                 g_snprintf(buf, sizeof(buf), cmd, filename);
828         else {
829                 if (cmd)
830                         g_warning(_("MIME viewer command line is invalid: `%s'"), cmd);
831                 if (def_cmd)
832                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
833                 else
834                         return;
835         }
836
837         execute_command_line(buf);
838 }
839
840 #if USE_GPGME
841 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
842                              gpointer data)
843 {
844         MimeInfo *partinfo;
845         gchar *part_name;
846
847         partinfo = gtk_ctree_node_get_row_data(ctree, node);
848         g_return_if_fail(partinfo != NULL);
849
850         part_name = get_part_name(partinfo);
851         gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
852 }
853
854 static void mimeview_update_names(MimeView *mimeview)
855 {
856         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
857
858         gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
859 }
860
861 static void mimeview_update_signature_info(MimeView *mimeview)
862 {
863         MimeInfo *partinfo;
864
865         if (!mimeview) return;
866         if (!mimeview->opened) return;
867
868         partinfo = gtk_ctree_node_get_row_data
869                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
870         if (!partinfo) return;
871
872         if (g_strcasecmp(partinfo->content_type,
873                          "application/pgp-signature") == 0) {
874                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
875                 textview_show_signature_part(mimeview->textview, partinfo);
876         }
877 }
878
879 static void mimeview_check_signature(MimeView *mimeview)
880 {
881         MimeInfo *mimeinfo;
882         gchar buf[BUFFSIZE];
883         FILE *fp;
884
885         g_return_if_fail (mimeview_is_signed(mimeview));
886
887         mimeinfo = gtk_ctree_node_get_row_data
888                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
889         g_return_if_fail(mimeinfo != NULL);
890         g_return_if_fail(mimeview->file != NULL);
891
892         while (mimeinfo->parent)
893                 mimeinfo = mimeinfo->parent;
894
895         if ((fp = fopen(mimeview->file, "r")) == NULL) {
896                 FILE_OP_ERROR(mimeview->file, "fopen");
897                 return;
898         }
899
900         /* skip headers */
901         if (mimeinfo->mime_type == MIME_MULTIPART) {
902                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
903                 FILE_OP_ERROR(mimeview->file, "fseek");
904                 while (fgets(buf, sizeof(buf), fp) != NULL)
905                         if (buf[0] == '\r' || buf[0] == '\n') break;
906         }
907
908         procmime_scan_multipart_message(mimeinfo, fp);
909         rfc2015_check_signature(mimeinfo, fp);
910         fclose(fp);
911
912         mimeview_update_names(mimeview);
913         mimeview_update_signature_info(mimeview);
914 }
915 #endif /* USE_GPGME */