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