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