0.8.11claws48
[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 #include <fnmatch.h>
41  
42 #include "intl.h"
43 #include "main.h"
44 #include "mimeview.h"
45 #include "textview.h"
46 #include "procmime.h"
47 #include "summaryview.h"
48 #include "menu.h"
49 #include "filesel.h"
50 #include "alertpanel.h"
51 #include "inputdialog.h"
52 #include "utils.h"
53 #include "gtkutils.h"
54 #include "prefs_common.h"
55 #include "rfc2015.h"
56 #include "pgptext.h"
57
58 typedef enum
59 {
60         COL_MIMETYPE = 0,
61         COL_SIZE     = 1,
62         COL_NAME     = 2
63 } MimeViewColumnPos;
64
65 #define N_MIMEVIEW_COLS 3
66
67 static void mimeview_set_multipart_tree         (MimeView       *mimeview,
68                                                  MimeInfo       *mimeinfo,
69                                                  GtkCTreeNode   *parent);
70 static GtkCTreeNode *mimeview_append_part       (MimeView       *mimeview,
71                                                  MimeInfo       *partinfo,
72                                                  GtkCTreeNode   *parent);
73 static void mimeview_show_message_part          (MimeView       *mimeview,
74                                                  MimeInfo       *partinfo);
75 static void mimeview_change_view_type           (MimeView       *mimeview,
76                                                  MimeViewType    type);
77 static void mimeview_clear                      (MimeView       *mimeview);
78
79 static void mimeview_selected           (GtkCTree       *ctree,
80                                          GtkCTreeNode   *node,
81                                          gint            column,
82                                          MimeView       *mimeview);
83 static void mimeview_start_drag         (GtkWidget      *widget,
84                                          gint            button,
85                                          GdkEvent       *event,
86                                          MimeView       *mimeview);
87 static gint mimeview_button_pressed     (GtkWidget      *widget,
88                                          GdkEventButton *event,
89                                          MimeView       *mimeview);
90 static gint mimeview_key_pressed        (GtkWidget      *widget,
91                                          GdkEventKey    *event,
92                                          MimeView       *mimeview);
93
94 static void mimeview_drag_data_get      (GtkWidget        *widget,
95                                          GdkDragContext   *drag_context,
96                                          GtkSelectionData *selection_data,
97                                          guint             info,
98                                          guint             time,
99                                          MimeView         *mimeview);
100
101 static void mimeview_display_as_text    (MimeView       *mimeview);
102 static void mimeview_save_as            (MimeView       *mimeview);
103 static void mimeview_save_all           (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
110 static GtkItemFactoryEntry mimeview_popup_entries[] =
111 {
112         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
113         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
114         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
115         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL},
116         {N_("/Save _all..."),     NULL, mimeview_save_all,        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 GSList *mimeviewer_factories;
129 GSList *mimeviews;
130
131 MimeView *mimeview_create(void)
132 {
133         MimeView *mimeview;
134
135         GtkWidget *notebook;
136         GtkWidget *vbox;
137         GtkWidget *paned;
138         GtkWidget *scrolledwin;
139         GtkWidget *ctree;
140         GtkWidget *mime_vbox;
141         GtkWidget *popupmenu;
142         GtkItemFactory *popupfactory;
143         gchar *titles[N_MIMEVIEW_COLS];
144         gint n_entries;
145         gint i;
146
147         debug_print("Creating MIME view...\n");
148         mimeview = g_new0(MimeView, 1);
149
150         titles[COL_MIMETYPE] = _("MIME Type");
151         titles[COL_SIZE]     = _("Size");
152         titles[COL_NAME]     = _("Name");
153
154         notebook = gtk_notebook_new();
155         gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
156
157         vbox = gtk_vbox_new(FALSE, 0);
158         gtk_container_add(GTK_CONTAINER(notebook), vbox);
159         gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
160                                         _("Text"));
161
162         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
163         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
164                                        GTK_POLICY_AUTOMATIC,
165                                        GTK_POLICY_ALWAYS);
166         gtk_widget_set_usize(scrolledwin, -1, 80);
167
168         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
169         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
170         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
171         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
172                                            GTK_JUSTIFY_RIGHT);
173         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
174         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
175         for (i = 0; i < N_MIMEVIEW_COLS; i++)
176                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
177                                        GTK_CAN_FOCUS);
178         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
179
180         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
181                            GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
182         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
183                            GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
184         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
185                            GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
186         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
187                            GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
188         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
189                            GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
190     
191         mime_vbox = gtk_vbox_new(FALSE, 0);
192
193         paned = gtk_vpaned_new();
194         gtk_paned_add1(GTK_PANED(paned), scrolledwin);
195         gtk_paned_add2(GTK_PANED(paned), mime_vbox);
196         gtk_container_add(GTK_CONTAINER(notebook), paned);
197         gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
198                                         _("Attachments"));
199
200         gtk_widget_show_all(notebook);
201
202         gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
203
204         n_entries = sizeof(mimeview_popup_entries) /
205                 sizeof(mimeview_popup_entries[0]);
206         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
207                                       "<MimeView>", &popupfactory, mimeview);
208
209         mimeview->notebook     = notebook;
210         mimeview->vbox         = vbox;
211         mimeview->paned        = paned;
212         mimeview->scrolledwin  = scrolledwin;
213         mimeview->ctree        = ctree;
214         mimeview->mime_vbox    = mime_vbox;
215         mimeview->popupmenu    = popupmenu;
216         mimeview->popupfactory = popupfactory;
217         mimeview->type         = -1;
218
219         mimeviews = g_slist_prepend(mimeviews, mimeview);
220
221         return mimeview;
222 }
223
224 void mimeview_init(MimeView *mimeview)
225 {
226 }
227
228 /* 
229  * Check whether the message is OpenPGP signed
230  */
231 #if USE_GPGME
232 static gboolean mimeview_is_signed(MimeView *mimeview)
233 {
234         MimeInfo *partinfo = NULL;
235
236         debug_print("mimeview_is signed of %p\n", mimeview);
237
238         if (!mimeview) return FALSE;
239         if (!mimeview->opened) return FALSE;
240
241         debug_print("mimeview_is_signed: open\n" );
242
243         if (!mimeview->file) return FALSE;
244
245         debug_print("mimeview_is_signed: file\n" );
246
247         partinfo = gtk_ctree_node_get_row_data
248                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
249         g_return_val_if_fail(partinfo != NULL, FALSE);
250
251         /* walk the tree and see whether there is a signature somewhere */
252         do {
253                 if (rfc2015_has_signature(partinfo))
254                         return TRUE;
255         } while ((partinfo = partinfo->parent) != NULL);
256
257         debug_print("mimeview_is_signed: FALSE\n" );
258
259         return FALSE;
260 }
261
262 static void set_unchecked_signature(MimeInfo *mimeinfo)
263 {
264         MimeInfo *sig_partinfo;
265
266         sig_partinfo = rfc2015_find_signature(mimeinfo);
267         if (sig_partinfo == NULL) return;
268
269         g_free(sig_partinfo->sigstatus);
270         sig_partinfo->sigstatus =
271                 g_strdup(_("Select \"Check signature\" to check"));
272
273         g_free(sig_partinfo->sigstatus_full);
274         sig_partinfo->sigstatus_full = NULL;
275 }
276 #endif /* USE_GPGME */
277
278 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
279                            const gchar *file)
280 {
281         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
282         GtkCTreeNode *node;
283         gchar buf[BUFFSIZE];
284         FILE *fp;
285
286         mimeview_clear(mimeview);
287         textview_clear(mimeview->messageview->textview);
288
289         g_return_if_fail(file != NULL);
290         g_return_if_fail(mimeinfo != NULL);
291
292         mimeview->mimeinfo = mimeinfo;
293
294         if ((fp = fopen(file, "rb")) == NULL) {
295                 FILE_OP_ERROR(file, "fopen");
296                 return;
297         }
298         mimeview->file = g_strdup(file);
299
300         /* skip MIME part headers */
301         if (mimeinfo->mime_type == MIME_MULTIPART) {
302                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
303                         perror("fseek");
304                 while (fgets(buf, sizeof(buf), fp) != NULL)
305                         if (buf[0] == '\r' || buf[0] == '\n') break;
306         }
307
308         procmime_scan_multipart_message(mimeinfo, fp);
309 #if USE_GPGME
310         if ((prefs_common.auto_check_signatures)
311             && (gpg_started))
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         GSList *cur;
352         
353         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
354                 MimeViewer *viewer = (MimeViewer *) cur->data;
355                 viewer->destroy_viewer(viewer);
356         }
357         g_slist_free(mimeview->viewers);
358
359         procmime_mimeinfo_free_all(mimeview->mimeinfo);
360         g_free(mimeview->file);
361         g_free(mimeview);
362
363         mimeviews = g_slist_remove(mimeviews, mimeview);
364 }
365
366 static void mimeview_set_multipart_tree(MimeView *mimeview,
367                                         MimeInfo *mimeinfo,
368                                         GtkCTreeNode *parent)
369 {
370         GtkCTreeNode *current = parent;
371
372         g_return_if_fail(mimeinfo != NULL);
373
374         if (!mimeinfo->sub && mimeinfo->parent)
375                 current = mimeview_append_part(mimeview, mimeinfo, parent);
376         if (mimeinfo->sub && !mimeinfo->sub->children &&
377             mimeinfo->sub->mime_type != MIME_TEXT &&
378             mimeinfo->sub->mime_type != MIME_TEXT_HTML) {
379                 mimeview_append_part(mimeview, mimeinfo->sub, parent);
380                 return;
381         }
382
383         if (mimeinfo->sub)
384                 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
385
386         if (mimeinfo->children)
387                 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
388
389         if (mimeinfo->next)
390                 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
391 }
392
393 static gchar *get_part_name(MimeInfo *partinfo)
394 {
395 #if USE_GPGME
396         if (partinfo->sigstatus)
397                 return partinfo->sigstatus;
398         else
399 #endif
400         if (partinfo->name)
401                 return partinfo->name;
402         else if (partinfo->filename)
403                 return partinfo->filename;
404         else if (partinfo->description)
405                 return partinfo->description;
406         else
407                 return "";
408 }
409
410 static gchar *get_part_description(MimeInfo *partinfo)
411 {
412         if (partinfo->description)
413                 return partinfo->description;
414         else if (partinfo->name)
415                 return partinfo->name;
416         else if (partinfo->filename)
417                 return partinfo->filename;
418         else
419                 return "";
420 }
421
422 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
423                                           MimeInfo *partinfo,
424                                           GtkCTreeNode *parent)
425 {
426         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
427         GtkCTreeNode *node;
428         gchar *str[N_MIMEVIEW_COLS];
429
430         str[COL_MIMETYPE] =
431                 partinfo->content_type ? partinfo->content_type : "";
432         str[COL_SIZE] = to_human_readable(partinfo->size);
433         if (prefs_common.attach_desc)
434                 str[COL_NAME] = get_part_description(partinfo);
435         else
436                 str[COL_NAME] = get_part_name(partinfo);
437
438         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
439                                      NULL, NULL, NULL, NULL,
440                                      FALSE, TRUE);
441         gtk_ctree_node_set_row_data(ctree, node, partinfo);
442
443         return node;
444 }
445
446 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
447 {
448         FILE *fp;
449         const gchar *fname;
450 #if USE_GPGME
451         MimeInfo *pi;
452 #endif
453
454         if (!partinfo) return;
455
456 #if USE_GPGME
457         for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
458                 ;
459         fname = pi ? pi->plaintextfile : mimeview->file;
460 #else
461         fname = mimeview->file;
462 #endif /* USE_GPGME */
463         if (!fname) return;
464
465         if ((fp = fopen(fname, "rb")) == NULL) {
466                 FILE_OP_ERROR(fname, "fopen");
467                 return;
468         }
469
470         if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
471                 FILE_OP_ERROR(mimeview->file, "fseek");
472                 fclose(fp);
473                 return;
474         }
475
476         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
477         textview_show_part(mimeview->textview, partinfo, fp);
478
479         fclose(fp);
480 }
481
482 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
483 {
484         GSList *cur;
485         MimeViewerFactory *factory = NULL;
486         MimeViewer *viewer = NULL;
487         
488         for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
489                 MimeViewerFactory *curfactory = cur->data;
490
491                 if(!fnmatch(curfactory->content_type, content_type, 0)) {
492                         factory = curfactory;
493                         break;
494                 }
495         }
496         if (factory == NULL)
497                 return NULL;
498
499         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
500                 MimeViewer *curviewer = cur->data;
501                 
502                 if (curviewer->factory == factory)
503                         return curviewer;
504         }
505         viewer = factory->create_viewer();
506         mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
507
508         return viewer;
509 }
510
511 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
512 {
513         gchar *content_type = NULL;
514         MimeViewer *viewer;
515
516         if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
517             (partinfo->name != NULL)) {
518                 content_type = procmime_get_mime_type(partinfo->name);
519         } else {
520                 content_type = g_strdup(partinfo->content_type);
521         }
522
523         viewer = get_viewer_for_content_type(mimeview, content_type);
524         g_free(content_type);
525
526         return viewer;
527 }
528
529 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
530 {
531         MimeViewer *viewer;
532         
533         viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
534         if (viewer == NULL) {
535                 if (mimeview->mimeviewer != NULL)
536                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
537                 mimeview->mimeviewer = NULL;
538                 return FALSE;
539         }
540
541         if (mimeview->mimeviewer != viewer) {
542                 if (mimeview->mimeviewer != NULL)
543                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
544                 mimeview->mimeviewer = viewer;
545                 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
546         }
547         viewer->show_mimepart(viewer, mimeview->file, partinfo);
548
549         return TRUE;
550 }
551
552 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
553 {
554         TextView  *textview  = mimeview->textview;
555         GList *children;
556
557         if ((mimeview->type != MIMEVIEW_VIEWER) && 
558             (mimeview->type == type)) return;
559
560         children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
561         if (children) {
562                 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
563                                        GTK_WIDGET(children->data));
564                 g_list_free(children);
565         }
566
567         switch (type) {
568         case MIMEVIEW_TEXT:
569                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
570                                   GTK_WIDGET_PTR(textview));
571                 break;
572         case MIMEVIEW_VIEWER:
573                 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
574                                   GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
575                 break;
576         default:
577                 return;
578         }
579
580         mimeview->type = type;
581 }
582
583 static void mimeview_clear(MimeView *mimeview)
584 {
585         GtkCList *clist = GTK_CLIST(mimeview->ctree);
586
587         procmime_mimeinfo_free_all(mimeview->mimeinfo);
588         mimeview->mimeinfo = NULL;
589
590         gtk_clist_clear(clist);
591         textview_clear(mimeview->textview);
592         if (mimeview->mimeviewer != NULL)
593                 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
594
595         mimeview->opened = NULL;
596
597         g_free(mimeview->file);
598         mimeview->file = NULL;
599
600         /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
601 }
602
603 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
604                               MimeView *mimeview)
605 {
606         MimeInfo *partinfo;
607
608         if (mimeview->opened == node) return;
609         mimeview->opened = node;
610         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
611
612         partinfo = gtk_ctree_node_get_row_data(ctree, node);
613         if (!partinfo) return;
614
615         /* ungrab the mouse event */
616         if (GTK_WIDGET_HAS_GRAB(ctree)) {
617                 gtk_grab_remove(GTK_WIDGET(ctree));
618                 if (gdk_pointer_is_grabbed())
619                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
620         }
621         
622         mimeview->textview->default_text = FALSE;
623         
624         if (!mimeview_show_part(mimeview, partinfo)) {
625                 switch (partinfo->mime_type) {
626                 case MIME_TEXT:
627                 case MIME_TEXT_HTML:
628                 case MIME_TEXT_ENRICHED:
629                 case MIME_MESSAGE_RFC822:
630                 case MIME_MULTIPART:
631                         mimeview_show_message_part(mimeview, partinfo);
632                 
633                         break;
634                 default:
635                         mimeview->textview->default_text = TRUE;        
636                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
637 #if USE_GPGME
638                         if (g_strcasecmp(partinfo->content_type,
639                                          "application/pgp-signature") == 0)
640                                 textview_show_signature_part(mimeview->textview,
641                                                              partinfo);
642                         else
643 #endif
644                                 textview_show_mime_part(mimeview->textview, partinfo);
645                         break;
646                 }
647         }
648 }
649
650 static void mimeview_start_drag(GtkWidget *widget, gint button,
651                                 GdkEvent *event, MimeView *mimeview)
652 {
653         GtkTargetList *list;
654         GdkDragContext *context;
655         MimeInfo *partinfo;
656         GtkCTree *ctree;
657
658         g_return_if_fail(mimeview != NULL);
659         ctree = GTK_CTREE(mimeview->ctree);
660         partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
661         if (partinfo->filename == NULL && partinfo->name == NULL) return;
662
663         list = gtk_target_list_new(mimeview_mime_types, 1);
664         context = gtk_drag_begin(widget, list,
665                                  GDK_ACTION_COPY, button, event);
666         gtk_drag_set_icon_default(context);
667 }
668
669 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
670                                     MimeView *mimeview)
671 {
672         GtkCList *clist = GTK_CLIST(widget);
673         MimeInfo *partinfo;
674         gint row, column;
675
676         if (!event) return FALSE;
677
678         if (event->button == 2 || event->button == 3) {
679                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
680                                                   &row, &column))
681                         return FALSE;
682                 gtk_clist_unselect_all(clist);
683                 gtk_clist_select_row(clist, row, column);
684                 gtkut_clist_set_focus_row(clist, row);
685         }
686
687         if (event->button == 2 ||
688             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
689                 /* call external program for image, audio or html */
690                 mimeview_launch(mimeview);
691         } else if (event->button == 3) {
692                 partinfo = gtk_ctree_node_get_row_data
693                         (GTK_CTREE(mimeview->ctree), mimeview->opened);
694                 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
695                                  partinfo->mime_type == MIME_TEXT_HTML ||
696                                  partinfo->mime_type == MIME_TEXT_ENRICHED ||
697                                  partinfo->mime_type == MIME_MESSAGE_RFC822 ||
698                                  partinfo->mime_type == MIME_IMAGE ||
699                                  partinfo->mime_type == MIME_MULTIPART))
700                         menu_set_sensitive(mimeview->popupfactory,
701                                            "/Display as text", FALSE);
702                 else
703                         menu_set_sensitive(mimeview->popupfactory,
704                                            "/Display as text", TRUE);
705                 if (partinfo &&
706                     partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
707                         menu_set_sensitive(mimeview->popupfactory,
708                                            "/Open", FALSE);
709                 else
710                         menu_set_sensitive(mimeview->popupfactory,
711                                            "/Open", TRUE);
712
713 #if USE_GPGME
714                 menu_set_sensitive(mimeview->popupfactory,
715                                    "/Check signature",
716                                    mimeview_is_signed(mimeview));
717 #endif
718
719                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
720                                NULL, NULL, NULL, NULL,
721                                event->button, event->time);
722         }
723
724         return TRUE;
725 }
726
727 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
728 {
729         mimeview_key_pressed(mimeview->ctree, event, mimeview);
730 }
731
732 #define BREAK_ON_MODIFIER_KEY() \
733         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
734
735 #define KEY_PRESS_EVENT_STOP() \
736         if (gtk_signal_n_emissions_by_name \
737                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
738                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
739                                              "key_press_event"); \
740         }
741
742 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
743                                  MimeView *mimeview)
744 {
745         SummaryView *summaryview;
746         GtkCTree *ctree = GTK_CTREE(widget);
747         GtkCTreeNode *node;
748
749         if (!event) return FALSE;
750         if (!mimeview->opened) return FALSE;
751
752         switch (event->keyval) {
753         case GDK_space:
754                 if (textview_scroll_page(mimeview->textview, FALSE))
755                         return TRUE;
756
757                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
758                 if (node) {
759                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
760                         gtk_sctree_select(GTK_SCTREE(ctree), node);
761                         return TRUE;
762                 }
763                 break;
764         case GDK_BackSpace:
765                 textview_scroll_page(mimeview->textview, TRUE);
766                 return TRUE;
767         case GDK_Return:
768                 textview_scroll_one_line(mimeview->textview,
769                                          (event->state & GDK_MOD1_MASK) != 0);
770                 return TRUE;
771         case GDK_n:
772         case GDK_N:
773                 BREAK_ON_MODIFIER_KEY();
774                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
775                 KEY_PRESS_EVENT_STOP();
776
777                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
778                                         GTK_SCROLL_STEP_FORWARD, 0.0);
779                 return TRUE;
780         case GDK_p:
781         case GDK_P:
782                 BREAK_ON_MODIFIER_KEY();
783                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
784                 KEY_PRESS_EVENT_STOP();
785
786                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
787                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
788                 return TRUE;
789         case GDK_y:
790                 BREAK_ON_MODIFIER_KEY();
791                 KEY_PRESS_EVENT_STOP();
792                 mimeview_save_as(mimeview);
793                 return TRUE;
794         case GDK_t:
795                 BREAK_ON_MODIFIER_KEY();
796                 KEY_PRESS_EVENT_STOP();
797                 mimeview_display_as_text(mimeview);
798                 return TRUE;    
799         case GDK_l:
800                 BREAK_ON_MODIFIER_KEY();
801                 KEY_PRESS_EVENT_STOP();
802                 mimeview_launch(mimeview);
803                 return TRUE;
804         default:
805                 break;
806         }
807
808         if (!mimeview->messageview->mainwin) return FALSE;
809         summaryview = mimeview->messageview->mainwin->summaryview;
810         summary_pass_key_press_event(summaryview, event);
811         return TRUE;
812 }
813
814 static void mimeview_drag_data_get(GtkWidget        *widget,
815                                    GdkDragContext   *drag_context,
816                                    GtkSelectionData *selection_data,
817                                    guint             info,
818                                    guint             time,
819                                    MimeView         *mimeview)
820 {
821         gchar *filename, *uriname;
822         MimeInfo *partinfo;
823
824         if (!mimeview->opened) return;
825         if (!mimeview->file) return;
826
827         partinfo = gtk_ctree_node_get_row_data
828                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
829         if (!partinfo) return;
830         if (!partinfo->filename && !partinfo->name) return;
831
832         filename = partinfo->filename ? partinfo->filename : partinfo->name;
833         filename = g_basename(filename);
834         if (*filename == '\0') return;
835
836         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
837                                filename, NULL);
838
839         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
840                 alertpanel_error
841                         (_("Can't save the part of multipart message."));
842
843         uriname = g_strconcat("file:/", filename, NULL);
844         gtk_selection_data_set(selection_data, selection_data->target, 8,
845                                uriname, strlen(uriname));
846
847         g_free(uriname);
848         g_free(filename);
849 }
850
851 static void mimeview_save_all(MimeView *mimeview)
852 {
853         gchar *dirname;
854         gchar *defname = NULL;
855         MimeInfo *partinfo;
856         MimeInfo *attachment;
857         gchar buf[1024];
858
859         if (!mimeview->opened) return;
860         if (!mimeview->file) return;
861
862         partinfo = gtk_ctree_node_get_row_data
863                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
864         g_return_if_fail(partinfo != NULL);
865
866         dirname = filesel_select_file(_("Save as"), defname);
867         if (!dirname) return;
868
869         /* return to first children */
870         if (!partinfo->parent->children) return;  /* multipart container? */
871         attachment = partinfo->parent->children->next;
872         /* for each attachment, extract it in the selected dir. */
873         while (attachment != NULL) {
874                 static guint subst_cnt = 1;
875                 gchar *attachdir;
876                 gchar *attachname = g_strdup(get_part_name(attachment));
877                 AlertValue aval = G_ALERTDEFAULT;
878                 gchar *res;
879
880                 if (!attachname || !strlen(attachname))
881                         attachname = g_strdup_printf("noname.%d",subst_cnt++);
882                 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
883                 g_snprintf(buf, sizeof(buf), "%s%s",
884                            dirname,
885                            (attachname[0] == G_DIR_SEPARATOR)
886                            ? &attachname[1]
887                            : attachname);
888                 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
889                 attachdir = g_dirname(buf);
890                 make_dir_hier(attachdir);
891                 g_free(attachdir);
892
893                 if (is_file_exist(buf)) {
894                         res = g_strdup_printf(_("Overwrite existing file '%s'?"),
895                                               attachname);
896                         aval = alertpanel(_("Overwrite"), res, _("OK"), 
897                                           _("Cancel"), NULL);
898                         g_free(res);                                      
899                 }
900                 g_free(attachname);
901
902                 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
903                         alertpanel_error(_("Can't save the part of multipart message."));
904                 attachment = attachment->next;
905         }
906 }
907
908 static void mimeview_display_as_text(MimeView *mimeview)
909 {
910         MimeInfo *partinfo;
911
912         if (!mimeview->opened) return;
913
914         partinfo = gtk_ctree_node_get_row_data
915                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
916         mimeview_show_message_part(mimeview, partinfo);
917 }
918
919 static void mimeview_save_as(MimeView *mimeview)
920 {
921         gchar *filename;
922         gchar *defname = NULL;
923         MimeInfo *partinfo;
924         gchar *res;
925
926         if (!mimeview->opened) return;
927         if (!mimeview->file) return;
928
929         partinfo = gtk_ctree_node_get_row_data
930                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
931         g_return_if_fail(partinfo != NULL);
932
933         if (partinfo->filename)
934                 defname = partinfo->filename;
935         else if (partinfo->name) {
936                 Xstrdup_a(defname, partinfo->name, return);
937                 subst_for_filename(defname);
938         }
939
940         filename = filesel_select_file(_("Save as"), defname);
941         if (!filename) return;
942         if (is_file_exist(filename)) {
943                 AlertValue aval;
944                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
945                                       filename);
946                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
947                                   _("Cancel"), NULL);
948                 g_free(res);                                      
949                 if (G_ALERTDEFAULT != aval) return;
950         }
951
952         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
953                 alertpanel_error
954                         (_("Can't save the part of multipart message."));
955 }
956
957 static void mimeview_launch(MimeView *mimeview)
958 {
959         MimeInfo *partinfo;
960         gchar *filename;
961
962         if (!mimeview->opened) return;
963         if (!mimeview->file) return;
964
965         partinfo = gtk_ctree_node_get_row_data
966                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
967         g_return_if_fail(partinfo != NULL);
968
969         filename = procmime_get_tmp_file_name(partinfo);
970
971         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
972                 alertpanel_error
973                         (_("Can't save the part of multipart message."));
974         else
975                 mimeview_view_file(filename, partinfo, NULL);
976
977         g_free(filename);
978 }
979
980 static void mimeview_open_with(MimeView *mimeview)
981 {
982         MimeInfo *partinfo;
983         gchar *filename;
984         gchar *cmd;
985
986         if (!mimeview->opened) return;
987         if (!mimeview->file) return;
988
989         partinfo = gtk_ctree_node_get_row_data
990                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
991         g_return_if_fail(partinfo != NULL);
992
993         filename = procmime_get_tmp_file_name(partinfo);
994
995         if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
996                 alertpanel_error
997                         (_("Can't save the part of multipart message."));
998                 g_free(filename);
999                 return;
1000         }
1001
1002         if (!prefs_common.mime_open_cmd_history)
1003                 prefs_common.mime_open_cmd_history =
1004                         add_history(NULL, prefs_common.mime_open_cmd);
1005
1006         cmd = input_dialog_combo
1007                 (_("Open with"),
1008                  _("Enter the command line to open file:\n"
1009                    "(`%s' will be replaced with file name)"),
1010                  prefs_common.mime_open_cmd,
1011                  prefs_common.mime_open_cmd_history,
1012                  TRUE);
1013         if (cmd) {
1014                 mimeview_view_file(filename, partinfo, cmd);
1015                 g_free(prefs_common.mime_open_cmd);
1016                 prefs_common.mime_open_cmd = cmd;
1017                 prefs_common.mime_open_cmd_history =
1018                         add_history(prefs_common.mime_open_cmd_history, cmd);
1019         }
1020
1021         g_free(filename);
1022 }
1023
1024 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1025                                const gchar *cmdline)
1026 {
1027         static gchar *default_image_cmdline = "display '%s'";
1028         static gchar *default_audio_cmdline = "play '%s'";
1029         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1030         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1031         gchar buf[1024];
1032         gchar m_buf[1024];
1033         const gchar *cmd;
1034         const gchar *def_cmd;
1035         const gchar *p;
1036
1037         if (cmdline) {
1038                 cmd = cmdline;
1039                 def_cmd = NULL;
1040         } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1041                 return;
1042         } else if (MIME_IMAGE == partinfo->mime_type) {
1043                 cmd = prefs_common.mime_image_viewer;
1044                 def_cmd = default_image_cmdline;
1045         } else if (MIME_AUDIO == partinfo->mime_type) {
1046                 cmd = prefs_common.mime_audio_player;
1047                 def_cmd = default_audio_cmdline;
1048         } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1049                 cmd = prefs_common.uri_cmd;
1050                 def_cmd = default_html_cmdline;
1051         } else {
1052                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1053                            partinfo->content_type, "%s");
1054                 cmd = m_buf;
1055                 def_cmd = NULL;
1056         }
1057
1058         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1059             !strchr(p + 2, '%'))
1060                 g_snprintf(buf, sizeof(buf), cmd, filename);
1061         else {
1062                 if (cmd)
1063                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1064                 if (def_cmd)
1065                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1066                 else
1067                         return;
1068         }
1069
1070         execute_command_line(buf, TRUE);
1071 }
1072
1073 #if USE_GPGME
1074 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1075                              gpointer data)
1076 {
1077         MimeInfo *partinfo;
1078         gchar *part_name;
1079
1080         partinfo = gtk_ctree_node_get_row_data(ctree, node);
1081         g_return_if_fail(partinfo != NULL);
1082
1083         part_name = get_part_name(partinfo);
1084         gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1085 }
1086
1087 static void mimeview_update_names(MimeView *mimeview)
1088 {
1089         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1090
1091         gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1092 }
1093
1094 static void mimeview_update_signature_info(MimeView *mimeview)
1095 {
1096         MimeInfo *partinfo;
1097
1098         if (!mimeview) return;
1099         if (!mimeview->opened) return;
1100
1101         partinfo = gtk_ctree_node_get_row_data
1102                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1103         if (!partinfo) return;
1104
1105         if (g_strcasecmp(partinfo->content_type,
1106                          "application/pgp-signature") == 0) {
1107                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1108                 textview_show_signature_part(mimeview->textview, partinfo);
1109         }
1110 }
1111
1112 void mimeview_check_signature(MimeView *mimeview)
1113 {
1114         MimeInfo *mimeinfo;
1115         FILE *fp;
1116
1117         g_return_if_fail (mimeview_is_signed(mimeview));
1118         g_return_if_fail (gpg_started);
1119
1120         mimeinfo = gtk_ctree_node_get_row_data
1121                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1122         g_return_if_fail(mimeinfo != NULL);
1123         g_return_if_fail(mimeview->file != NULL);
1124
1125         while (mimeinfo->parent)
1126                 mimeinfo = mimeinfo->parent;
1127
1128         if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1129                 FILE_OP_ERROR(mimeview->file, "fopen");
1130                 return;
1131         }
1132
1133         rfc2015_check_signature(mimeinfo, fp);
1134         fclose(fp);
1135
1136         mimeview_update_names(mimeview);
1137         mimeview_update_signature_info(mimeview);
1138
1139         textview_show_message(mimeview->messageview->textview, mimeinfo,
1140                               mimeview->file);
1141 }
1142 #endif /* USE_GPGME */
1143
1144 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1145 {
1146         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1147 }
1148
1149 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1150 {
1151         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1152 }
1153
1154 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1155 {
1156         GSList *mimeview_list, *viewer_list;
1157
1158         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1159                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1160                 
1161                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1162                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1163                         mimeview->mimeviewer = NULL;
1164                 }
1165
1166                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1167                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1168
1169                         mimeviewer->destroy_viewer(mimeviewer);
1170                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1171                 }
1172         }
1173
1174         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1175 }