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