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