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