3b6bfd1f8e3af8a3bc640cd1990efef2428bf28b
[claws.git] / src / mimeview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtknotebook.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkctree.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkhbox.h>
33 #include <gtk/gtkvpaned.h>
34 #include <gtk/gtktext.h>
35 #include <gtk/gtksignal.h>
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkdnd.h>
38 #include <gtk/gtkselection.h>
39 #include <gtk/gtktooltips.h>
40 #include <gtk/gtkcontainer.h>
41 #include <gtk/gtkbutton.h>
42 #include <stdio.h>
43
44 #ifndef HAVE_APACHE_FNMATCH
45 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
46  * fnmatch.h */
47 #include <fnmatch.h>
48 #endif
49
50 #include "intl.h"
51 #include "main.h"
52 #include "mimeview.h"
53 #include "textview.h"
54 #include "procmime.h"
55 #include "summaryview.h"
56 #include "menu.h"
57 #include "filesel.h"
58 #include "alertpanel.h"
59 #include "inputdialog.h"
60 #include "utils.h"
61 #include "gtkutils.h"
62 #include "prefs_common.h"
63 #include "rfc2015.h"
64 #include "stock_pixmap.h"
65 #include "gtk/gtkvscrollbutton.h"
66
67
68 typedef enum
69 {
70         COL_MIMETYPE = 0,
71         COL_SIZE     = 1,
72         COL_NAME     = 2
73 } MimeViewColumnPos;
74
75 #define N_MIMEVIEW_COLS 3
76
77 static void mimeview_set_multipart_tree         (MimeView       *mimeview,
78                                                  MimeInfo       *mimeinfo,
79                                                  GtkCTreeNode   *parent);
80 static GtkCTreeNode *mimeview_append_part       (MimeView       *mimeview,
81                                                  MimeInfo       *partinfo,
82                                                  GtkCTreeNode   *parent);
83 static void mimeview_show_message_part          (MimeView       *mimeview,
84                                                  MimeInfo       *partinfo);
85 static void mimeview_change_view_type           (MimeView       *mimeview,
86                                                  MimeViewType    type);
87 static void mimeview_clear                      (MimeView       *mimeview);
88
89 static void mimeview_selected           (GtkCTree       *ctree,
90                                          GtkCTreeNode   *node,
91                                          gint            column,
92                                          MimeView       *mimeview);
93 static void mimeview_start_drag         (GtkWidget      *widget,
94                                          gint            button,
95                                          GdkEvent       *event,
96                                          MimeView       *mimeview);
97 static gint mimeview_button_pressed     (GtkWidget      *widget,
98                                          GdkEventButton *event,
99                                          MimeView       *mimeview);
100 static gint mimeview_key_pressed        (GtkWidget      *widget,
101                                          GdkEventKey    *event,
102                                          MimeView       *mimeview);
103
104 static void mimeview_drag_data_get      (GtkWidget        *widget,
105                                          GdkDragContext   *drag_context,
106                                          GtkSelectionData *selection_data,
107                                          guint             info,
108                                          guint             time,
109                                          MimeView         *mimeview);
110
111 static void mimeview_display_as_text    (MimeView       *mimeview);
112 static void mimeview_save_as            (MimeView       *mimeview);
113 static void mimeview_save_all           (MimeView       *mimeview);
114 static void mimeview_launch             (MimeView       *mimeview);
115 static void mimeview_open_with          (MimeView       *mimeview);
116 static void mimeview_view_file          (const gchar    *filename,
117                                          MimeInfo       *partinfo,
118                                          const gchar    *cmdline);
119 static gboolean icon_clicked_cb         (GtkWidget      *button, 
120                                          GdkEventButton *event, 
121                                          MimeView       *mimeview);
122 static void icon_selected               (MimeView       *mimeview, 
123                                          gint            num, 
124                                          MimeInfo       *partinfo);
125 static gint icon_key_pressed            (GtkWidget      *button, 
126                                          GdkEventKey    *event,
127                                          MimeView       *mimeview);
128 static void icon_list_append_icon       (MimeView       *mimeview, 
129                                          MimeInfo       *mimeinfo);
130 static void icon_list_create            (MimeView       *mimeview, 
131                                          MimeInfo       *mimeinfo);
132 static void icon_list_clear             (MimeView       *mimeview);
133 static void mime_toggle_button_cb       (GtkWidget      *button,
134                                          MimeView       *mimeview);
135 static void part_button_pressed         (MimeView       *mimeview, 
136                                          GdkEventButton *event, 
137                                          MimeInfo       *partinfo);
138 static void icon_scroll_size_allocate_cb(GtkWidget      *widget, 
139                                          GtkAllocation  *layout_size, 
140                                          MimeView       *mimeview);
141
142 static GtkItemFactoryEntry mimeview_popup_entries[] =
143 {
144         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
145         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
146         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
147         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL},
148         {N_("/Save _all..."),     NULL, mimeview_save_all,        0, NULL}
149 #if USE_GPGME
150         ,
151         {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
152 #endif
153 };
154
155 static GtkTargetEntry mimeview_mime_types[] =
156 {
157         {"text/uri-list", 0, 0}
158 };
159
160 GSList *mimeviewer_factories;
161 GSList *mimeviews;
162
163 MimeView *mimeview_create(MainWindow *mainwin)
164 {
165         MimeView *mimeview;
166
167         GtkWidget *notebook;
168         GtkWidget *vbox;
169         GtkWidget *paned;
170         GtkWidget *scrolledwin;
171         GtkWidget *ctree;
172         GtkWidget *mime_notebook;
173         GtkWidget *popupmenu;
174         GtkWidget *ctree_mainbox;
175         GtkWidget *mime_toggle;
176         GtkWidget *icon_mainbox;
177         GtkWidget *icon_scroll;
178         GtkWidget *icon_vbox;
179         GtkWidget *arrow;
180         GtkWidget *scrollbutton;
181         GtkWidget *hbox;
182         GtkTooltips *tooltips;
183         GtkItemFactory *popupfactory;
184         gchar *titles[N_MIMEVIEW_COLS];
185         gint n_entries;
186         gint i;
187
188         debug_print("Creating MIME view...\n");
189         mimeview = g_new0(MimeView, 1);
190
191         titles[COL_MIMETYPE] = _("MIME Type");
192         titles[COL_SIZE]     = _("Size");
193         titles[COL_NAME]     = _("Name");
194
195         notebook = gtk_notebook_new();
196         gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
197         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
198         gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
199
200         vbox = gtk_vbox_new(FALSE, 0);
201         gtk_container_add(GTK_CONTAINER(notebook), vbox);
202         
203         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
204         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
205                                        GTK_POLICY_AUTOMATIC,
206                                        GTK_POLICY_ALWAYS);
207
208         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
209         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
210         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
211         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
212                                            GTK_JUSTIFY_RIGHT);
213         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
214         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 90);
215         for (i = 0; i < N_MIMEVIEW_COLS; i++)
216                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
217                                        GTK_CAN_FOCUS);
218         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
219
220         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
221                            GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
222         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
223                            GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
224         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
225                            GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
226         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
227                            GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
228         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
229                            GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
230
231         mime_notebook = gtk_notebook_new();
232         gtk_widget_show(mime_notebook);
233         GTK_WIDGET_UNSET_FLAGS(mime_notebook, GTK_CAN_FOCUS);
234         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
235         gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
236         
237         gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
238                                 
239         icon_vbox = gtk_vbox_new(FALSE,0);
240         icon_scroll = gtk_layout_new(NULL, NULL);
241         gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
242         scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
243
244         mime_toggle = gtk_toggle_button_new();
245         arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
246         gtk_widget_show(arrow);
247         gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
248         gtk_signal_connect(GTK_OBJECT(mime_toggle), "toggled", 
249                            GTK_SIGNAL_FUNC(mime_toggle_button_cb), mimeview);
250
251         icon_mainbox = gtk_vbox_new(FALSE, 0);
252         gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
253         gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
254         gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
255         gtk_signal_connect(GTK_OBJECT(icon_mainbox), "size_allocate", 
256                         GTK_SIGNAL_FUNC(icon_scroll_size_allocate_cb), mimeview);
257         
258         ctree_mainbox = gtk_hbox_new(FALSE, 0); 
259         gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
260
261         n_entries = sizeof(mimeview_popup_entries) /
262                 sizeof(mimeview_popup_entries[0]);
263         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
264                                       "<MimeView>", &popupfactory, mimeview);
265         tooltips = gtk_tooltips_new();
266         gtk_tooltips_set_delay(tooltips, 0); 
267
268         paned = gtk_vpaned_new();
269         gtk_paned_set_gutter_size(GTK_PANED(paned), 0);
270         gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
271         gtk_paned_pack2(GTK_PANED(paned), mime_notebook, TRUE, TRUE);
272         gtk_container_add(GTK_CONTAINER(notebook), paned);
273         
274         hbox = gtk_hbox_new(FALSE, 0);
275         gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 0);
276         gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
277
278         gtk_widget_show_all(hbox);
279         gtk_widget_hide(ctree_mainbox);
280
281         mimeview->notebook      = notebook;
282         mimeview->vbox          = vbox;
283         mimeview->hbox          = hbox;
284         mimeview->paned         = paned;
285         mimeview->scrolledwin   = scrolledwin;
286         mimeview->ctree         = ctree;
287         mimeview->mime_notebook = mime_notebook;
288         mimeview->popupmenu     = popupmenu;
289         mimeview->popupfactory  = popupfactory;
290         mimeview->type          = -1;
291         mimeview->ctree_mainbox = ctree_mainbox;
292         mimeview->icon_scroll   = icon_scroll;
293         mimeview->icon_vbox     = icon_vbox;
294         mimeview->icon_mainbox  = icon_mainbox;
295         mimeview->icon_count    = 0;
296         mimeview->mainwin       = mainwin;
297         mimeview->tooltips      = tooltips;
298         mimeview->oldsize       = 60;
299         mimeview->mime_toggle   = mime_toggle;
300
301         mimeview->target_list   = gtk_target_list_new(mimeview_mime_types, 1); 
302         
303         mimeviews = g_slist_prepend(mimeviews, mimeview);
304
305         return mimeview;
306 }
307
308 void mimeview_init(MimeView *mimeview)
309 {
310         textview_init(mimeview->textview);
311
312         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
313                 GTK_WIDGET_PTR(mimeview->textview));
314 }
315
316 /* 
317  * Check whether the message is OpenPGP signed
318  */
319 #if USE_GPGME
320 static gboolean mimeview_is_signed(MimeView *mimeview)
321 {
322         MimeInfo *partinfo = NULL;
323
324         debug_print("mimeview_is signed of %p\n", mimeview);
325
326         if (!mimeview) return FALSE;
327         if (!mimeview->opened) return FALSE;
328
329         debug_print("mimeview_is_signed: open\n" );
330
331         if (!mimeview->file) return FALSE;
332
333         debug_print("mimeview_is_signed: file\n" );
334
335         partinfo = gtk_ctree_node_get_row_data
336                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
337         g_return_val_if_fail(partinfo != NULL, FALSE);
338
339         /* walk the tree and see whether there is a signature somewhere */
340         do {
341                 if (rfc2015_has_signature(partinfo))
342                         return TRUE;
343         } while ((partinfo = partinfo->parent) != NULL);
344
345         debug_print("mimeview_is_signed: FALSE\n" );
346
347         return FALSE;
348 }
349
350 static void set_unchecked_signature(MimeInfo *mimeinfo)
351 {
352         MimeInfo *sig_partinfo;
353
354         sig_partinfo = rfc2015_find_signature(mimeinfo);
355         if (sig_partinfo == NULL) return;
356
357         g_free(sig_partinfo->sigstatus);
358         sig_partinfo->sigstatus =
359                 g_strdup(_("Right-click here to verify the signature"));
360
361         g_free(sig_partinfo->sigstatus_full);
362         sig_partinfo->sigstatus_full = NULL;
363 }
364 #endif /* USE_GPGME */
365
366 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
367                            const gchar *file)
368 {
369         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
370         GtkCTreeNode *node;
371         FILE *fp;
372
373         mimeview_clear(mimeview);
374         textview_clear(mimeview->messageview->textview);
375
376         g_return_if_fail(file != NULL);
377         g_return_if_fail(mimeinfo != NULL);
378
379         mimeview->mimeinfo = mimeinfo;
380
381         mimeview->file = g_strdup(file);
382
383 #if USE_GPGME
384         if (prefs_common.auto_check_signatures && gpg_started) {
385                 if ((fp = fopen(file, "rb")) == NULL) {
386                         FILE_OP_ERROR(file, "fopen");
387                         return;
388                 }
389                 rfc2015_check_signature(mimeinfo, fp);
390                 fclose(fp);
391         } else
392                 set_unchecked_signature(mimeinfo);
393 #endif
394
395         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
396                                          mimeview);
397
398         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
399         icon_list_create(mimeview, mimeinfo);
400
401         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
402                                            mimeview_selected, mimeview);
403
404         /* search first text part */
405         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
406              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
407                 MimeInfo *partinfo;
408
409                 partinfo = gtk_ctree_node_get_row_data(ctree, node);
410                 if (partinfo &&
411                     (partinfo->mime_type == MIME_TEXT ||
412                      partinfo->mime_type == MIME_TEXT_HTML))
413                         break;
414         }
415         textview_show_message(mimeview->messageview->textview, mimeinfo, file);
416
417         if (!node)
418                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
419
420         if (node) {
421                 gtk_ctree_select(ctree, node);
422                 gtkut_ctree_set_focus_row(ctree, node);
423                 gtk_widget_grab_focus(mimeview->ctree);
424         }
425 }
426
427 void mimeview_destroy(MimeView *mimeview)
428 {
429         GSList *cur;
430         
431         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
432                 MimeViewer *viewer = (MimeViewer *) cur->data;
433                 gtk_container_remove(GTK_CONTAINER(mimeview->mime_notebook),
434                         GTK_WIDGET(viewer->get_widget(viewer)));
435                 viewer->destroy_viewer(viewer);
436         }
437         g_slist_free(mimeview->viewers);
438         gtk_target_list_unref(mimeview->target_list);
439
440         procmime_mimeinfo_free_all(mimeview->mimeinfo);
441         g_free(mimeview->file);
442         g_free(mimeview);
443
444         mimeviews = g_slist_remove(mimeviews, mimeview);
445         
446 }
447
448 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
449 {
450         if (gtk_notebook_get_current_page
451                 (GTK_NOTEBOOK(mimeview->notebook)) == 0)
452                 return NULL;
453
454         return gtk_ctree_node_get_row_data
455                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
456 }
457
458 static void mimeview_set_multipart_tree(MimeView *mimeview,
459                                         MimeInfo *mimeinfo,
460                                         GtkCTreeNode *parent)
461 {
462         GtkCTreeNode *node;
463
464         g_return_if_fail(mimeinfo != NULL);
465
466         if (mimeinfo->children)
467                 mimeinfo = mimeinfo->children;
468
469         while (mimeinfo != NULL) {
470                 node = mimeview_append_part(mimeview, mimeinfo, parent);
471
472                 if (mimeinfo->children)
473                         mimeview_set_multipart_tree(mimeview, mimeinfo, node);
474                 else if (mimeinfo->sub &&
475                          mimeinfo->sub->mime_type != MIME_TEXT &&
476                          mimeinfo->sub->mime_type != MIME_TEXT_HTML)
477                         mimeview_set_multipart_tree(mimeview, mimeinfo->sub,
478                                                     node);
479                 mimeinfo = mimeinfo->next;
480         }
481 }
482
483 static gchar *get_part_name(MimeInfo *partinfo)
484 {
485 #if USE_GPGME
486         if (partinfo->sigstatus)
487                 return partinfo->sigstatus;
488         else
489 #endif
490         if (partinfo->name)
491                 return partinfo->name;
492         else if (partinfo->filename)
493                 return partinfo->filename;
494         else if (partinfo->description)
495                 return partinfo->description;
496         else
497                 return "";
498 }
499
500 static gchar *get_part_description(MimeInfo *partinfo)
501 {
502         if (partinfo->description)
503                 return partinfo->description;
504         else if (partinfo->name)
505                 return partinfo->name;
506         else if (partinfo->filename)
507                 return partinfo->filename;
508         else
509                 return "";
510 }
511
512 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
513                                           MimeInfo *partinfo,
514                                           GtkCTreeNode *parent)
515 {
516         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
517         GtkCTreeNode *node;
518         gchar *str[N_MIMEVIEW_COLS];
519
520         str[COL_MIMETYPE] =
521                 partinfo->content_type ? partinfo->content_type : "";
522         str[COL_SIZE] = to_human_readable(partinfo->size);
523         if (prefs_common.attach_desc)
524                 str[COL_NAME] = get_part_description(partinfo);
525         else
526                 str[COL_NAME] = get_part_name(partinfo);
527
528         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
529                                      NULL, NULL, NULL, NULL,
530                                      FALSE, TRUE);
531         gtk_ctree_node_set_row_data(ctree, node, partinfo);
532
533         return node;
534 }
535
536 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
537 {
538         FILE *fp;
539         const gchar *fname;
540 #if USE_GPGME
541         MimeInfo *pi;
542 #endif
543
544         if (!partinfo) return;
545
546 #if USE_GPGME
547         for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
548                 ;
549         fname = pi ? pi->plaintextfile : mimeview->file;
550 #else
551         fname = mimeview->file;
552 #endif /* USE_GPGME */
553         if (!fname) return;
554
555         if ((fp = fopen(fname, "rb")) == NULL) {
556                 FILE_OP_ERROR(fname, "fopen");
557                 return;
558         }
559
560         if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
561                 FILE_OP_ERROR(mimeview->file, "fseek");
562                 fclose(fp);
563                 return;
564         }
565
566         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
567         textview_show_part(mimeview->textview, partinfo, fp);
568
569         fclose(fp);
570 }
571
572 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
573 {
574         GSList *cur;
575         MimeViewerFactory *factory = NULL;
576         MimeViewer *viewer = NULL;
577
578 /*
579  * FNM_CASEFOLD is a GNU extension
580  * if its not defined copy the string to the stack and
581  * convert the copy to lower case
582  */
583 #ifndef FNM_CASEFOLD
584 #define FNM_CASEFOLD 0
585         Xstrdup_a(content_type, content_type, return NULL);
586         g_strdown((gchar *)content_type);
587 #endif
588         
589         for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
590                 MimeViewerFactory *curfactory = cur->data;
591                 gint i = 0;
592
593                 while (curfactory->content_types[i] != NULL) {
594                         debug_print("%s\n", curfactory->content_types[i]);
595                         if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
596                                 factory = curfactory;
597                                 break;
598                         }
599                         i++;
600                 }
601                 if (factory != NULL)
602                         break;
603         }
604         if (factory == NULL)
605                 return NULL;
606
607         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
608                 MimeViewer *curviewer = cur->data;
609                 
610                 if (curviewer->factory == factory)
611                         return curviewer;
612         }
613         viewer = factory->create_viewer();
614         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
615                 GTK_WIDGET(viewer->get_widget(viewer)));
616                 
617         mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
618
619         return viewer;
620 }
621
622 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
623 {
624         gchar *content_type = NULL;
625         MimeViewer *viewer = NULL;
626
627         if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
628             (partinfo->name != NULL)) {
629                 content_type = procmime_get_mime_type(partinfo->name);
630         } else {
631                 content_type = g_strdup(partinfo->content_type);
632         }
633
634         if (content_type != NULL) {
635                 viewer = get_viewer_for_content_type(mimeview, content_type);
636                 g_free(content_type);
637         }
638
639         return viewer;
640 }
641
642 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
643 {
644         MimeViewer *viewer;
645         
646         viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
647         if (viewer == NULL) {
648                 if (mimeview->mimeviewer != NULL)
649                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
650                 mimeview->mimeviewer = NULL;
651                 return FALSE;
652         }
653
654         if (mimeview->mimeviewer != viewer) {
655                 if (mimeview->mimeviewer != NULL)
656                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
657                 mimeview->mimeviewer = viewer;
658                 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
659         }
660         viewer->show_mimepart(viewer, mimeview->file, partinfo);
661
662         return TRUE;
663 }
664
665 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
666 {
667         TextView  *textview  = mimeview->textview;
668
669         if ((mimeview->type != MIMEVIEW_VIEWER) && 
670             (mimeview->type == type)) return;
671
672         switch (type) {
673         case MIMEVIEW_TEXT:
674                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
675                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
676                         GTK_WIDGET_PTR(textview)));
677                 break;
678         case MIMEVIEW_VIEWER:
679                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
680                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
681                         GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
682                 break;
683         default:
684                 return;
685         }
686
687         mimeview->type = type;
688 }
689
690 static void mimeview_clear(MimeView *mimeview)
691 {
692         GtkCList *clist = GTK_CLIST(mimeview->ctree);
693
694         procmime_mimeinfo_free_all(mimeview->mimeinfo);
695         mimeview->mimeinfo = NULL;
696
697         gtk_clist_clear(clist);
698         textview_clear(mimeview->textview);
699         if (mimeview->mimeviewer != NULL)
700                 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
701
702         mimeview->opened = NULL;
703
704         g_free(mimeview->file);
705         mimeview->file = NULL;
706         icon_list_clear(mimeview);
707
708         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mimeview->mime_toggle)))
709                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0);
710 }
711
712 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
713                               MimeView *mimeview)
714 {
715         MimeInfo *partinfo;
716
717         if (mimeview->opened == node) return;
718         mimeview->opened = node;
719         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
720
721         partinfo = gtk_ctree_node_get_row_data(ctree, node);
722         if (!partinfo) return;
723
724         /* ungrab the mouse event */
725         if (GTK_WIDGET_HAS_GRAB(ctree)) {
726                 gtk_grab_remove(GTK_WIDGET(ctree));
727                 if (gdk_pointer_is_grabbed())
728                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
729         }
730         
731         mimeview->textview->default_text = FALSE;
732         
733         if (!mimeview_show_part(mimeview, partinfo)) {
734                 switch (partinfo->mime_type) {
735                 case MIME_TEXT:
736                 case MIME_TEXT_HTML:
737                 case MIME_TEXT_ENRICHED:
738                 case MIME_MESSAGE_RFC822:
739                 case MIME_MULTIPART:
740                         mimeview_show_message_part(mimeview, partinfo);
741                 
742                         break;
743                 default:
744                         mimeview->textview->default_text = TRUE;        
745                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
746 #if USE_GPGME
747                         if (g_strcasecmp(partinfo->content_type,
748                                          "application/pgp-signature") == 0)
749                                 textview_show_signature_part(mimeview->textview,
750                                                              partinfo);
751                         else
752 #endif
753                                 textview_show_mime_part(mimeview->textview, partinfo);
754                         break;
755                 }
756         }
757 }
758
759 static void mimeview_start_drag(GtkWidget *widget, gint button,
760                                 GdkEvent *event, MimeView *mimeview)
761 {
762         GdkDragContext *context;
763         MimeInfo *partinfo;
764
765         g_return_if_fail(mimeview != NULL);
766
767         partinfo = mimeview_get_selected_part(mimeview);
768         if (partinfo->filename == NULL && partinfo->name == NULL) return;
769
770         context = gtk_drag_begin(widget, mimeview->target_list,
771                                  GDK_ACTION_COPY, button, event);
772         gtk_drag_set_icon_default(context);
773 }
774
775 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
776                                     MimeView *mimeview)
777 {
778         GtkCList *clist = GTK_CLIST(widget);
779         gint row, column;
780
781         if (!event) return FALSE;
782
783         if (event->button == 2 || event->button == 3) {
784                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
785                                                   &row, &column))
786                         return FALSE;
787                 gtk_clist_unselect_all(clist);
788                 gtk_clist_select_row(clist, row, column);
789                 gtkut_clist_set_focus_row(clist, row);
790         }
791         part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
792
793         return TRUE;
794 }
795
796 static void part_button_pressed(MimeView *mimeview, GdkEventButton *event, 
797                                 MimeInfo *partinfo)
798 {
799         if (event->button == 2 ||
800             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
801                 /* call external program for image, audio or html */
802                 mimeview_launch(mimeview);
803         } else if (event->button == 3) {
804                 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
805                                  partinfo->mime_type == MIME_TEXT_HTML ||
806                                  partinfo->mime_type == MIME_TEXT_ENRICHED ||
807                                  partinfo->mime_type == MIME_MESSAGE_RFC822 ||
808                                  partinfo->mime_type == MIME_IMAGE ||
809                                  partinfo->mime_type == MIME_MULTIPART))
810                         menu_set_sensitive(mimeview->popupfactory,
811                                            "/Display as text", FALSE);
812                 else
813                         menu_set_sensitive(mimeview->popupfactory,
814                                            "/Display as text", TRUE);
815                 if (partinfo &&
816                     partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
817                         menu_set_sensitive(mimeview->popupfactory,
818                                            "/Open", FALSE);
819                 else
820                         menu_set_sensitive(mimeview->popupfactory,
821                                            "/Open", TRUE);
822
823 #if USE_GPGME
824                 menu_set_sensitive(mimeview->popupfactory,
825                                    "/Check signature",
826                                    mimeview_is_signed(mimeview));
827 #endif
828
829                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
830                                NULL, NULL, NULL, NULL,
831                                event->button, event->time);
832         }
833
834 }
835
836
837 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
838 {
839         mimeview_key_pressed(mimeview->ctree, event, mimeview);
840 }
841
842 #define BREAK_ON_MODIFIER_KEY() \
843         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
844
845 #define KEY_PRESS_EVENT_STOP() \
846         if (gtk_signal_n_emissions_by_name \
847                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
848                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
849                                              "key_press_event"); \
850         }
851
852 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
853                                  MimeView *mimeview)
854 {
855         SummaryView *summaryview;
856         GtkCTree *ctree = GTK_CTREE(widget);
857         GtkCTreeNode *node;
858
859         if (!event) return FALSE;
860         if (!mimeview->opened) return FALSE;
861
862         switch (event->keyval) {
863         case GDK_space:
864                 if (textview_scroll_page(mimeview->textview, FALSE))
865                         return TRUE;
866
867                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
868                 if (node) {
869                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
870                         gtk_sctree_select(GTK_SCTREE(ctree), node);
871                         return TRUE;
872                 }
873                 break;
874         case GDK_BackSpace:
875                 textview_scroll_page(mimeview->textview, TRUE);
876                 return TRUE;
877         case GDK_Return:
878                 textview_scroll_one_line(mimeview->textview,
879                                          (event->state & GDK_MOD1_MASK) != 0);
880                 return TRUE;
881         case GDK_n:
882         case GDK_N:
883                 BREAK_ON_MODIFIER_KEY();
884                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
885                 KEY_PRESS_EVENT_STOP();
886
887                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
888                                         GTK_SCROLL_STEP_FORWARD, 0.0);
889                 return TRUE;
890         case GDK_p:
891         case GDK_P:
892                 BREAK_ON_MODIFIER_KEY();
893                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
894                 KEY_PRESS_EVENT_STOP();
895
896                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
897                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
898                 return TRUE;
899         case GDK_y:
900                 BREAK_ON_MODIFIER_KEY();
901                 KEY_PRESS_EVENT_STOP();
902                 mimeview_save_as(mimeview);
903                 return TRUE;
904         case GDK_t:
905                 BREAK_ON_MODIFIER_KEY();
906                 KEY_PRESS_EVENT_STOP();
907                 mimeview_display_as_text(mimeview);
908                 return TRUE;    
909         case GDK_l:
910                 BREAK_ON_MODIFIER_KEY();
911                 KEY_PRESS_EVENT_STOP();
912                 mimeview_launch(mimeview);
913                 return TRUE;
914         default:
915                 break;
916         }
917
918         if (!mimeview->messageview->mainwin) return FALSE;
919         summaryview = mimeview->messageview->mainwin->summaryview;
920         summary_pass_key_press_event(summaryview, event);
921         return TRUE;
922 }
923
924 static void mimeview_drag_data_get(GtkWidget        *widget,
925                                    GdkDragContext   *drag_context,
926                                    GtkSelectionData *selection_data,
927                                    guint             info,
928                                    guint             time,
929                                    MimeView         *mimeview)
930 {
931         gchar *filename, *uriname;
932         MimeInfo *partinfo;
933
934         if (!mimeview->opened) return;
935         if (!mimeview->file) return;
936
937         partinfo = mimeview_get_selected_part(mimeview);
938         if (!partinfo) return;
939         if (!partinfo->filename && !partinfo->name) return;
940
941         filename = partinfo->filename ? partinfo->filename : partinfo->name;
942         filename = g_basename(filename);
943         if (*filename == '\0') return;
944
945         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
946                                filename, NULL);
947
948         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
949                 alertpanel_error
950                         (_("Can't save the part of multipart message."));
951
952         uriname = g_strconcat("file:/", filename, NULL);
953         gtk_selection_data_set(selection_data, selection_data->target, 8,
954                                uriname, strlen(uriname));
955
956         g_free(uriname);
957         g_free(filename);
958 }
959
960 static void mimeview_save_all(MimeView *mimeview)
961 {
962         gchar *dirname;
963         gchar *defname = NULL;
964         MimeInfo *partinfo;
965         MimeInfo *attachment;
966         gchar buf[1024];
967
968         if (!mimeview->opened) return;
969         if (!mimeview->file) return;
970
971         partinfo = mimeview_get_selected_part(mimeview);
972         g_return_if_fail(partinfo != NULL);
973
974         dirname = filesel_select_file(_("Save as"), defname);
975         if (!dirname) return;
976
977         /* return to first children */
978         if (!partinfo->parent->children) return;  /* multipart container? */
979         attachment = partinfo->parent->children->next;
980         /* for each attachment, extract it in the selected dir. */
981         while (attachment != NULL) {
982                 static guint subst_cnt = 1;
983                 gchar *attachdir;
984                 gchar *attachname = g_strdup(get_part_name(attachment));
985                 AlertValue aval = G_ALERTDEFAULT;
986                 gchar *res;
987
988                 if (!attachname || !strlen(attachname))
989                         attachname = g_strdup_printf("noname.%d",subst_cnt++);
990                 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
991                 g_snprintf(buf, sizeof(buf), "%s%s",
992                            dirname,
993                            (attachname[0] == G_DIR_SEPARATOR)
994                            ? &attachname[1]
995                            : attachname);
996                 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
997                 attachdir = g_dirname(buf);
998                 make_dir_hier(attachdir);
999                 g_free(attachdir);
1000
1001                 if (is_file_exist(buf)) {
1002                         res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1003                                               attachname);
1004                         aval = alertpanel(_("Overwrite"), res, _("OK"), 
1005                                           _("Cancel"), NULL);
1006                         g_free(res);                                      
1007                 }
1008                 g_free(attachname);
1009
1010                 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
1011                         alertpanel_error(_("Can't save the part of multipart message."));
1012                 attachment = attachment->next;
1013         }
1014 }
1015
1016 static void mimeview_display_as_text(MimeView *mimeview)
1017 {
1018         MimeInfo *partinfo;
1019
1020         if (!mimeview->opened) return;
1021
1022         partinfo = mimeview_get_selected_part(mimeview);
1023         g_return_if_fail(partinfo != NULL);
1024         mimeview_show_message_part(mimeview, partinfo);
1025 }
1026
1027 static void mimeview_save_as(MimeView *mimeview)
1028 {
1029         gchar *filename;
1030         gchar *defname = NULL;
1031         MimeInfo *partinfo;
1032         gchar *res;
1033
1034         if (!mimeview->opened) return;
1035         if (!mimeview->file) return;
1036
1037         partinfo = mimeview_get_selected_part(mimeview);
1038         g_return_if_fail(partinfo != NULL);
1039
1040         if (partinfo->filename)
1041                 defname = partinfo->filename;
1042         else if (partinfo->name) {
1043                 Xstrdup_a(defname, partinfo->name, return);
1044                 subst_for_filename(defname);
1045         }
1046
1047         filename = filesel_select_file(_("Save as"), defname);
1048         if (!filename) return;
1049         if (is_file_exist(filename)) {
1050                 AlertValue aval;
1051                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1052                                       filename);
1053                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
1054                                   _("Cancel"), NULL);
1055                 g_free(res);                                      
1056                 if (G_ALERTDEFAULT != aval) return;
1057         }
1058
1059         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
1060                 alertpanel_error
1061                         (_("Can't save the part of multipart message."));
1062 }
1063
1064 static void mimeview_launch(MimeView *mimeview)
1065 {
1066         MimeInfo *partinfo;
1067         gchar *filename;
1068
1069         if (!mimeview->opened) return;
1070         if (!mimeview->file) return;
1071
1072         partinfo = mimeview_get_selected_part(mimeview);
1073         g_return_if_fail(partinfo != NULL);
1074
1075         filename = procmime_get_tmp_file_name(partinfo);
1076
1077         if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
1078                 alertpanel_error
1079                         (_("Can't save the part of multipart message."));
1080         else
1081                 mimeview_view_file(filename, partinfo, NULL);
1082
1083         g_free(filename);
1084 }
1085
1086 static void mimeview_open_with(MimeView *mimeview)
1087 {
1088         MimeInfo *partinfo;
1089         gchar *filename;
1090         gchar *cmd;
1091
1092         if (!mimeview->opened) return;
1093         if (!mimeview->file) return;
1094
1095         partinfo = mimeview_get_selected_part(mimeview);
1096         g_return_if_fail(partinfo != NULL);
1097
1098         filename = procmime_get_tmp_file_name(partinfo);
1099
1100         if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1101                 alertpanel_error
1102                         (_("Can't save the part of multipart message."));
1103                 g_free(filename);
1104                 return;
1105         }
1106
1107         if (!prefs_common.mime_open_cmd_history)
1108                 prefs_common.mime_open_cmd_history =
1109                         add_history(NULL, prefs_common.mime_open_cmd);
1110
1111         cmd = input_dialog_combo
1112                 (_("Open with"),
1113                  _("Enter the command line to open file:\n"
1114                    "(`%s' will be replaced with file name)"),
1115                  prefs_common.mime_open_cmd,
1116                  prefs_common.mime_open_cmd_history,
1117                  TRUE);
1118         if (cmd) {
1119                 mimeview_view_file(filename, partinfo, cmd);
1120                 g_free(prefs_common.mime_open_cmd);
1121                 prefs_common.mime_open_cmd = cmd;
1122                 prefs_common.mime_open_cmd_history =
1123                         add_history(prefs_common.mime_open_cmd_history, cmd);
1124         }
1125
1126         g_free(filename);
1127 }
1128
1129 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1130                                const gchar *cmdline)
1131 {
1132         static gchar *default_image_cmdline = "display '%s'";
1133         static gchar *default_audio_cmdline = "play '%s'";
1134         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1135         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1136         gchar buf[1024];
1137         gchar m_buf[1024];
1138         const gchar *cmd;
1139         const gchar *def_cmd;
1140         const gchar *p;
1141
1142         if (cmdline) {
1143                 cmd = cmdline;
1144                 def_cmd = NULL;
1145         } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1146                 return;
1147         } else if (MIME_IMAGE == partinfo->mime_type) {
1148                 cmd = prefs_common.mime_image_viewer;
1149                 def_cmd = default_image_cmdline;
1150         } else if (MIME_AUDIO == partinfo->mime_type) {
1151                 cmd = prefs_common.mime_audio_player;
1152                 def_cmd = default_audio_cmdline;
1153         } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1154                 cmd = prefs_common.uri_cmd;
1155                 def_cmd = default_html_cmdline;
1156         } else {
1157                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1158                            partinfo->content_type, "%s");
1159                 cmd = m_buf;
1160                 def_cmd = NULL;
1161         }
1162
1163         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1164             !strchr(p + 2, '%'))
1165                 g_snprintf(buf, sizeof(buf), cmd, filename);
1166         else {
1167                 if (cmd)
1168                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1169                 if (def_cmd)
1170                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1171                 else
1172                         return;
1173         }
1174
1175         execute_command_line(buf, TRUE);
1176 }
1177
1178 #if USE_GPGME
1179 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1180                              gpointer data)
1181 {
1182         MimeInfo *partinfo;
1183         gchar *part_name;
1184
1185         partinfo = gtk_ctree_node_get_row_data(ctree, node);
1186         g_return_if_fail(partinfo != NULL);
1187
1188         part_name = get_part_name(partinfo);
1189         gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1190 }
1191
1192 static void mimeview_update_names(MimeView *mimeview)
1193 {
1194         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1195
1196         gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1197 }
1198
1199 static void mimeview_update_signature_info(MimeView *mimeview)
1200 {
1201         MimeInfo *partinfo;
1202
1203         if (!mimeview) return;
1204         if (!mimeview->opened) return;
1205
1206         partinfo = mimeview_get_selected_part(mimeview);
1207         if (!partinfo) return;
1208
1209         if (g_strcasecmp(partinfo->content_type,
1210                          "application/pgp-signature") == 0) {
1211                 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1212                 textview_show_signature_part(mimeview->textview, partinfo);
1213         }
1214 }
1215
1216 void mimeview_check_signature(MimeView *mimeview)
1217 {
1218         MimeInfo *mimeinfo;
1219         FILE *fp;
1220
1221         g_return_if_fail (mimeview_is_signed(mimeview));
1222         g_return_if_fail (gpg_started);
1223
1224         mimeinfo = gtk_ctree_node_get_row_data
1225                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1226         g_return_if_fail(mimeinfo != NULL);
1227         g_return_if_fail(mimeview->file != NULL);
1228
1229         while (mimeinfo->parent)
1230                 mimeinfo = mimeinfo->parent;
1231
1232         if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1233                 FILE_OP_ERROR(mimeview->file, "fopen");
1234                 return;
1235         }
1236
1237         rfc2015_check_signature(mimeinfo, fp);
1238         fclose(fp);
1239
1240         mimeview_update_names(mimeview);
1241         mimeview_update_signature_info(mimeview);
1242
1243         textview_show_message(mimeview->messageview->textview, mimeinfo,
1244                               mimeview->file);
1245                               
1246         mimeview_update(mimeview);
1247 }
1248 #endif /* USE_GPGME */
1249
1250 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1251 {
1252         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1253 }
1254
1255 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1256 {
1257         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1258 }
1259
1260 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1261 {
1262         GSList *mimeview_list, *viewer_list;
1263
1264         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1265                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1266                 
1267                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1268                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1269                         mimeview->mimeviewer = NULL;
1270                 }
1271
1272                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1273                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1274
1275                         mimeviewer->destroy_viewer(mimeviewer);
1276                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1277                 }
1278         }
1279
1280         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1281 }
1282
1283 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1284 {
1285         gint      num;
1286         MimeInfo *partinfo;
1287
1288         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1289         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1290         if (event->button == 1) 
1291                 icon_selected(mimeview, num, partinfo);
1292         part_button_pressed(mimeview, event, partinfo);
1293         return TRUE;
1294 }
1295
1296 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1297 {
1298         if (num == 1 && (partinfo->mime_type == MIME_TEXT ||  
1299                          partinfo->mime_type == MIME_TEXT_HTML))  {
1300                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0);
1301                 /* don't set the ctree, as it will unload the plugin, and
1302                  * we want to be able to switch quickly between the text
1303                  * part and the attachment */
1304         } else {
1305                 GtkCTreeNode *node;
1306                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 1);
1307                 node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1308                 if (node)
1309                         gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1310         }
1311 }               
1312
1313 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1314                              MimeView *mimeview)
1315 {
1316         gint      num;
1317         MimeInfo *partinfo;
1318
1319         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1320         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1321         
1322         if (!event) return FALSE;
1323
1324         switch (event->keyval) {
1325         case GDK_Return:
1326                 icon_selected(mimeview, num, partinfo);
1327                 return TRUE;
1328                 break;
1329         default:
1330                 break;
1331         }
1332         return FALSE;
1333 }
1334
1335 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1336 {
1337         GtkWidget *pixmap;
1338         GtkWidget *vbox;
1339         GtkWidget *button;
1340         GtkWidget *sep;
1341         gchar *tip;
1342         gchar *desc = NULL;
1343         StockPixmap stockp;
1344         
1345         vbox = mimeview->icon_vbox;
1346         mimeview->icon_count++;
1347         if (mimeview->icon_count > 1) {
1348                 sep = gtk_hseparator_new();
1349                 gtk_widget_show(sep);
1350                 gtk_box_pack_start(GTK_BOX(vbox), sep, TRUE, TRUE, 3);
1351         }
1352         button = gtk_button_new();
1353         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1354         gtk_object_set_data(GTK_OBJECT(button), "icon_number", 
1355                 GINT_TO_POINTER(mimeview->icon_count));
1356         gtk_object_set_data(GTK_OBJECT(button), "partinfo", 
1357                 mimeinfo);
1358         
1359         switch (mimeinfo->mime_type) {
1360                 
1361         case MIME_TEXT:
1362         case MIME_MESSAGE_RFC822:
1363                 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1364                 break;
1365         case MIME_TEXT_HTML:
1366                 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1367                 break;
1368         case MIME_APPLICATION:
1369 #ifdef USE_GPGME        
1370                 if (mimeinfo->content_type
1371                         &&  g_strcasecmp(mimeinfo->content_type, 
1372                                          "application/pgp-signature")  == 0) {
1373                         if (mimeinfo->sigstatus_full) {
1374                                 desc = mimeinfo->sigstatus;
1375                                 if (mimeinfo->sig_ok)
1376                                         stockp = STOCK_PIXMAP_MIME_GPG_PASSED;
1377                                 else if (mimeinfo->sig_unknown) 
1378                                         stockp = STOCK_PIXMAP_MIME_GPG_UNKNOWN;
1379                                 else if (mimeinfo->sig_expired 
1380                                          || mimeinfo->key_expired) 
1381                                         stockp = STOCK_PIXMAP_MIME_GPG_EXPIRED;
1382                                 else
1383                                         stockp = STOCK_PIXMAP_MIME_GPG_FAILED;
1384                         } else
1385                                 stockp = STOCK_PIXMAP_MIME_GPG_SIGNED;
1386                 } else
1387 #endif  
1388                         stockp = STOCK_PIXMAP_MIME_APPLICATION;
1389                 break;
1390         case MIME_APPLICATION_OCTET_STREAM:
1391                 stockp = STOCK_PIXMAP_MIME_APPLICATION_OCTET_STREAM;
1392                 break;
1393         case MIME_IMAGE:
1394                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1395                 break;
1396         case MIME_AUDIO:
1397                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1398                 break;
1399         case MIME_TEXT_ENRICHED:
1400                 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1401                 break;
1402         default:
1403                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1404                 break;
1405         }
1406         
1407         pixmap = stock_pixmap_widget(mimeview->mainwin->window, stockp);
1408         gtk_container_add(GTK_CONTAINER(button), pixmap);
1409         
1410         if (!desc) {
1411                 if (prefs_common.attach_desc)
1412                         desc = get_part_description(mimeinfo);
1413                 else
1414                         desc = get_part_name(mimeinfo);
1415         }
1416         if (desc && *desc)
1417                 tip = g_strdup_printf("%s\n%s\n%s", desc, mimeinfo->content_type, 
1418                                 to_human_readable(mimeinfo->size));
1419         else            
1420                 tip = g_strdup_printf("%s\n%s", mimeinfo->content_type, 
1421                                 to_human_readable(mimeinfo->size));
1422
1423         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1424         g_free(tip);
1425         gtk_widget_show_all(button);
1426         gtk_signal_connect(GTK_OBJECT(button), "button_press_event", 
1427                            GTK_SIGNAL_FUNC(icon_clicked_cb), mimeview);
1428         gtk_signal_connect(GTK_OBJECT(button), "key_press_event", 
1429                            GTK_SIGNAL_FUNC(icon_key_pressed), mimeview);
1430         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1431
1432 }
1433
1434 static void icon_list_clear (MimeView *mimeview)
1435 {
1436         GList     *child;
1437         GtkAdjustment *adj;
1438         
1439         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1440         for (; child != NULL; child = g_list_next(child)) {
1441                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1442                                        GTK_WIDGET(child->data));
1443         }
1444         mimeview->icon_count = 0;
1445         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1446         adj->value = 0;
1447 }
1448
1449 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1450                                         GtkAllocation *size, MimeView *mimeview)
1451 {
1452         GtkAllocation *mainbox_size;
1453         GtkAllocation *vbox_size;
1454         GtkAllocation *layout_size;
1455         GtkAdjustment *adj;
1456         
1457         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1458
1459         mainbox_size = &mimeview->icon_mainbox->allocation;
1460         vbox_size = &mimeview->icon_vbox->allocation;
1461         layout_size = &mimeview->icon_scroll->allocation;
1462         
1463         /* centralise the vbox */
1464         gtk_layout_move(GTK_LAYOUT(mimeview->icon_scroll), mimeview->icon_vbox, 
1465                         (mainbox_size->width - vbox_size->width)/2, 0);
1466         
1467         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1468                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1469                             MAX(vbox_size->height, layout_size->height));
1470         adj->step_increment = 5;
1471 }
1472
1473 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1474 {
1475         GtkRequisition size;
1476         gint           width;
1477         g_return_if_fail(mimeinfo != NULL);
1478
1479         if (mimeinfo->children)
1480                 mimeinfo = mimeinfo->children;
1481
1482         while (mimeinfo != NULL) {
1483                 if (mimeinfo->children)
1484                         icon_list_create(mimeview, mimeinfo);
1485                 else if (mimeinfo->sub &&
1486                          mimeinfo->sub->mime_type != MIME_TEXT &&
1487                          mimeinfo->sub->mime_type != MIME_TEXT_HTML)
1488                         icon_list_create(mimeview, mimeinfo->sub);
1489                 else 
1490                         icon_list_append_icon(mimeview, mimeinfo);
1491                 mimeinfo = mimeinfo->next;
1492         }
1493         gtk_widget_size_request(mimeview->icon_vbox, &size);
1494         width = size.width + 4;
1495         if (width > mimeview->icon_mainbox->requisition.width) {
1496                 gtk_widget_set_usize(mimeview->icon_mainbox, 
1497                                      width, -1);
1498         }
1499
1500 }
1501
1502 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1503 {
1504         gtk_widget_ref(button); 
1505
1506         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1507                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1508                                         GTK_SHADOW_NONE);
1509                 gtk_widget_hide(mimeview->icon_mainbox);
1510                 gtk_widget_show(mimeview->ctree_mainbox);
1511                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1512
1513                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1514                                         button);
1515                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1516                                    button, FALSE, FALSE, 0);
1517                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 1); 
1518         } else {
1519                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1520                               GTK_SHADOW_NONE);
1521                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1522                 gtk_widget_hide(mimeview->ctree_mainbox);
1523                 gtk_widget_show(mimeview->icon_mainbox);
1524                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1525
1526                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1527                                         button);
1528                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1529                                    button, FALSE, FALSE, 0);
1530                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1531
1532         }
1533         gtk_widget_grab_focus(button);
1534         gtk_widget_unref(button);
1535
1536 }
1537
1538 void mimeview_update (MimeView *mimeview) {
1539         if (mimeview && mimeview->mimeinfo) {
1540                 icon_list_clear(mimeview);
1541                 icon_list_create(mimeview, mimeview->mimeinfo);
1542         }
1543 }