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