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