641a7cb64d6259c80ae900d6e95afe7a9662280f
[claws.git] / src / mimeview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtknotebook.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtkctree.h>
32 #include <gtk/gtkvbox.h>
33 #include <gtk/gtkhbox.h>
34 #include <gtk/gtkvpaned.h>
35 #include <gtk/gtktext.h>
36 #include <gtk/gtksignal.h>
37 #include <gtk/gtkmenu.h>
38 #include <gtk/gtkdnd.h>
39 #include <gtk/gtkselection.h>
40 #include <gtk/gtktooltips.h>
41 #include <gtk/gtkcontainer.h>
42 #include <gtk/gtkbutton.h>
43 #include <stdio.h>
44
45 #ifndef HAVE_APACHE_FNMATCH
46 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
47  * fnmatch.h */
48 #include <fnmatch.h>
49 #endif
50
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 "procheader.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 gchar *mimeview_get_filename_for_part           (MimeInfo       *partinfo,
88                                                  const gchar    *basedir,
89                                                  gint            number);
90 static gboolean mimeview_write_part             (const gchar    *filename,
91                                                  MimeInfo       *partinfo);
92
93 static void mimeview_selected           (GtkCTree       *ctree,
94                                          GtkCTreeNode   *node,
95                                          gint            column,
96                                          MimeView       *mimeview);
97 static void mimeview_start_drag         (GtkWidget      *widget,
98                                          gint            button,
99                                          GdkEvent       *event,
100                                          MimeView       *mimeview);
101 static gint mimeview_button_pressed     (GtkWidget      *widget,
102                                          GdkEventButton *event,
103                                          MimeView       *mimeview);
104 static gint mimeview_key_pressed        (GtkWidget      *widget,
105                                          GdkEventKey    *event,
106                                          MimeView       *mimeview);
107
108 static void mimeview_drag_data_get      (GtkWidget        *widget,
109                                          GdkDragContext   *drag_context,
110                                          GtkSelectionData *selection_data,
111                                          guint             info,
112                                          guint             time,
113                                          MimeView         *mimeview);
114
115 static void mimeview_display_as_text    (MimeView       *mimeview);
116 static void mimeview_save_as            (MimeView       *mimeview);
117 static void mimeview_save_all           (MimeView       *mimeview);
118 static void mimeview_launch             (MimeView       *mimeview);
119 static void mimeview_open_with          (MimeView       *mimeview);
120 static void mimeview_view_file          (const gchar    *filename,
121                                          MimeInfo       *partinfo,
122                                          const gchar    *cmdline,
123                                          MimeView       *mimeview);
124 static gboolean icon_clicked_cb         (GtkWidget      *button, 
125                                          GdkEventButton *event, 
126                                          MimeView       *mimeview);
127 static void icon_selected               (MimeView       *mimeview, 
128                                          gint            num, 
129                                          MimeInfo       *partinfo);
130 static gint icon_key_pressed            (GtkWidget      *button, 
131                                          GdkEventKey    *event,
132                                          MimeView       *mimeview);
133 static void toggle_icon                 (GtkToggleButton *button,
134                                          MimeView       *mimeview);
135 static void icon_list_append_icon       (MimeView       *mimeview, 
136                                          MimeInfo       *mimeinfo);
137 static void icon_list_create            (MimeView       *mimeview, 
138                                          MimeInfo       *mimeinfo);
139 static void icon_list_clear             (MimeView       *mimeview);
140 static void icon_list_toggle_by_mime_info (MimeView     *mimeview,
141                                            MimeInfo     *mimeinfo);
142 static gboolean icon_list_select_by_number(MimeView     *mimeview,
143                                            gint          number);
144 static void mime_toggle_button_cb       (GtkWidget      *button,
145                                          MimeView       *mimeview);
146 static gboolean part_button_pressed     (MimeView       *mimeview, 
147                                          GdkEventButton *event, 
148                                          MimeInfo       *partinfo);
149 static void icon_scroll_size_allocate_cb(GtkWidget      *widget, 
150                                          GtkAllocation  *layout_size, 
151                                          MimeView       *mimeview);
152
153 static GtkItemFactoryEntry mimeview_popup_entries[] =
154 {
155         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
156         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
157         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
158         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL},
159         {N_("/Save _all..."),     NULL, mimeview_save_all,        0, NULL},
160 };
161
162 static GtkTargetEntry mimeview_mime_types[] =
163 {
164         {"text/uri-list", 0, 0}
165 };
166
167 GSList *mimeviewer_factories;
168 GSList *mimeviews;
169
170 MimeView *mimeview_create(MainWindow *mainwin)
171 {
172         MimeView *mimeview;
173
174         GtkWidget *paned;
175         GtkWidget *scrolledwin;
176         GtkWidget *ctree;
177         GtkWidget *mime_notebook;
178         GtkWidget *popupmenu;
179         GtkWidget *ctree_mainbox;
180         GtkWidget *vbox;
181         GtkWidget *mime_toggle;
182         GtkWidget *icon_mainbox;
183         GtkWidget *icon_scroll;
184         GtkWidget *icon_vbox;
185         GtkWidget *arrow;
186         GtkWidget *scrollbutton;
187         GtkWidget *hbox;
188         GtkTooltips *tooltips;
189         GtkItemFactory *popupfactory;
190         NoticeView *siginfoview;
191         gchar *titles[N_MIMEVIEW_COLS];
192         gint n_entries;
193         gint i;
194
195         debug_print("Creating MIME view...\n");
196         mimeview = g_new0(MimeView, 1);
197
198         titles[COL_MIMETYPE] = _("MIME Type");
199         titles[COL_SIZE]     = _("Size");
200         titles[COL_NAME]     = _("Name");
201
202         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
203         gtk_widget_show(scrolledwin);
204         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
205                                        GTK_POLICY_AUTOMATIC,
206                                        GTK_POLICY_AUTOMATIC);
207
208         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
209         gtk_widget_show(ctree);
210         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
211         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
212         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
213                                            GTK_JUSTIFY_RIGHT);
214         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
215         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 90);
216         for (i = 0; i < N_MIMEVIEW_COLS; i++)
217                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
218                                        GTK_CAN_FOCUS);
219         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
220
221         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
222                          G_CALLBACK(mimeview_selected), mimeview);
223         g_signal_connect(G_OBJECT(ctree), "button_release_event",
224                          G_CALLBACK(mimeview_button_pressed), mimeview);
225         g_signal_connect(G_OBJECT(ctree), "key_press_event",
226                          G_CALLBACK(mimeview_key_pressed), mimeview);
227         g_signal_connect(G_OBJECT (ctree),"start_drag",
228                          G_CALLBACK (mimeview_start_drag), mimeview);
229         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
230                          G_CALLBACK(mimeview_drag_data_get), mimeview);
231
232         mime_notebook = gtk_notebook_new();
233         gtk_widget_show(mime_notebook);
234         GTK_WIDGET_UNSET_FLAGS(mime_notebook, GTK_CAN_FOCUS);
235         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
236         gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
237         
238         icon_vbox = gtk_vbox_new(FALSE, 2);
239         gtk_widget_show(icon_vbox);
240         icon_scroll = gtk_layout_new(NULL, NULL);
241         gtk_widget_show(icon_scroll);
242         gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
243         scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
244         gtk_widget_show(scrollbutton);
245
246         mime_toggle = gtk_toggle_button_new();
247         gtk_widget_show(mime_toggle);
248         arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
249         gtk_widget_show(arrow);
250         gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
251         g_signal_connect(G_OBJECT(mime_toggle), "toggled", 
252                          G_CALLBACK(mime_toggle_button_cb), mimeview);
253
254         icon_mainbox = gtk_vbox_new(FALSE, 0);
255         gtk_widget_show(icon_mainbox);
256         gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
257         gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
258         gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
259         g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate", 
260                          G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
261         
262         ctree_mainbox = gtk_hbox_new(FALSE, 0); 
263         gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
264
265         n_entries = sizeof(mimeview_popup_entries) /
266                 sizeof(mimeview_popup_entries[0]);
267         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
268                                       "<MimeView>", &popupfactory, mimeview);
269         tooltips = gtk_tooltips_new();
270         gtk_tooltips_set_delay(tooltips, 0); 
271
272         vbox = gtk_vbox_new(FALSE, 0);
273         gtk_widget_show(vbox);
274         siginfoview = noticeview_create(mainwin);
275         noticeview_hide(siginfoview);
276         noticeview_set_icon_clickable(siginfoview, TRUE);
277         gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
278         gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
279
280         paned = gtk_vpaned_new();
281         gtk_widget_show(paned);
282         gtk_paned_set_gutter_size(GTK_PANED(paned), 0);
283         gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
284         gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
285         
286         hbox = gtk_hbox_new(FALSE, 0);
287         gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
288         gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
289
290         gtk_widget_show(hbox);
291         gtk_widget_hide(ctree_mainbox);
292
293         mimeview->hbox          = hbox;
294         mimeview->paned         = paned;
295         mimeview->scrolledwin   = scrolledwin;
296         mimeview->ctree         = ctree;
297         mimeview->mime_notebook = mime_notebook;
298         mimeview->popupmenu     = popupmenu;
299         mimeview->popupfactory  = popupfactory;
300         mimeview->type          = -1;
301         mimeview->ctree_mainbox = ctree_mainbox;
302         mimeview->icon_scroll   = icon_scroll;
303         mimeview->icon_vbox     = icon_vbox;
304         mimeview->icon_mainbox  = icon_mainbox;
305         mimeview->icon_count    = 0;
306         mimeview->mainwin       = mainwin;
307         mimeview->tooltips      = tooltips;
308         mimeview->oldsize       = 60;
309         mimeview->mime_toggle   = mime_toggle;
310         mimeview->siginfoview   = siginfoview;
311
312         mimeview->target_list   = gtk_target_list_new(mimeview_mime_types, 1); 
313         
314         mimeviews = g_slist_prepend(mimeviews, mimeview);
315
316         return mimeview;
317 }
318
319 void mimeview_init(MimeView *mimeview)
320 {
321         textview_init(mimeview->textview);
322
323         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
324                 GTK_WIDGET_PTR(mimeview->textview));
325 }
326
327 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
328                            const gchar *file)
329 {
330         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
331         GtkCTreeNode *node;
332
333         mimeview_clear(mimeview);
334
335         g_return_if_fail(file != NULL);
336         g_return_if_fail(mimeinfo != NULL);
337
338         mimeview->mimeinfo = mimeinfo;
339
340         mimeview->file = g_strdup(file);
341
342         g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
343                                         mimeview);
344
345         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
346         icon_list_create(mimeview, mimeinfo);
347
348         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
349                                           mimeview_selected, mimeview);
350
351         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
352         if (node) {
353                 gtk_ctree_select(ctree, node);
354                 icon_list_toggle_by_mime_info
355                         (mimeview, gtk_ctree_node_get_row_data(ctree, node));
356                 gtkut_ctree_set_focus_row(ctree, node);
357         }
358 }
359
360 #ifdef USE_PTHREAD
361 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
362 #endif
363
364 static void mimeview_free_mimeinfo(MimeView *mimeview)
365 {
366         gboolean defer = FALSE;
367 #ifdef USE_PTHREAD
368         defer = (mimeview->check_data != NULL);
369         if (defer)
370                 mimeview->check_data->free_after_use = TRUE;
371 #endif
372         if (mimeview->mimeinfo != NULL && !defer) {
373                 procmime_mimeinfo_free_all(mimeview->mimeinfo);
374                 mimeview->mimeinfo = NULL;
375         } else if (defer) {
376 #ifdef USE_PTHREAD
377                 debug_print("deferring free(mimeinfo) and cancelling check\n");
378                 mimeview_check_sig_cancel_now(mimeview);
379 #endif
380         }
381 }
382
383 void mimeview_destroy(MimeView *mimeview)
384 {
385         GSList *cur;
386         
387         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
388                 MimeViewer *viewer = (MimeViewer *) cur->data;
389                 viewer->destroy_viewer(viewer);
390         }
391         g_slist_free(mimeview->viewers);
392         gtk_target_list_unref(mimeview->target_list);
393
394         mimeview_free_mimeinfo(mimeview);
395 #ifdef USE_PTHREAD
396         if (mimeview->check_data) {
397                 mimeview->check_data->destroy_mimeview = TRUE;
398                 debug_print("deferring destroy\n");
399         } else 
400 #endif
401         {
402                 g_free(mimeview->file);
403                 g_free(mimeview);
404                 mimeviews = g_slist_remove(mimeviews, mimeview);
405         }
406         
407 }
408
409 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
410 {
411         return gtk_ctree_node_get_row_data
412                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
413 }
414
415 static void mimeview_set_multipart_tree(MimeView *mimeview,
416                                         MimeInfo *mimeinfo,
417                                         GtkCTreeNode *parent)
418 {
419         GtkCTreeNode *node;
420
421         g_return_if_fail(mimeinfo != NULL);
422
423         while (mimeinfo != NULL) {
424                 node = mimeview_append_part(mimeview, mimeinfo, parent);
425
426                 if (mimeinfo->node->children)
427                         mimeview_set_multipart_tree(mimeview, (MimeInfo *) mimeinfo->node->children->data, node);
428                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
429         }
430 }
431
432 static const gchar *get_part_name(MimeInfo *partinfo)
433 {
434         const gchar *name;
435
436         name = procmime_mimeinfo_get_parameter(partinfo, "filename");
437         if (name == NULL)
438                 name = procmime_mimeinfo_get_parameter(partinfo, "name");
439         if (name == NULL)
440                 name = "";
441
442         return name;
443 }
444
445 static const gchar *get_part_description(MimeInfo *partinfo)
446 {
447         if (partinfo->description)
448                 return partinfo->description;
449         else
450                 return get_part_name(partinfo);
451 }
452
453 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
454                                           MimeInfo *partinfo,
455                                           GtkCTreeNode *parent)
456 {
457         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
458         GtkCTreeNode *node;
459         static gchar content_type[64];
460         gchar *str[N_MIMEVIEW_COLS];
461
462         if (partinfo->type != MIMETYPE_UNKNOWN && partinfo->subtype) {
463                 g_snprintf(content_type, 64, "%s/%s", procmime_get_media_type_str(partinfo->type), partinfo->subtype);
464         } else {
465                 g_snprintf(content_type, 64, "UNKNOWN");
466         }
467
468         str[COL_MIMETYPE] = content_type;
469         str[COL_SIZE] = to_human_readable(partinfo->length);
470         if (prefs_common.attach_desc)
471                 str[COL_NAME] = (gchar *) get_part_description(partinfo);
472         else
473                 str[COL_NAME] = (gchar *) get_part_name(partinfo);
474
475         node = gtk_sctree_insert_node(ctree, parent, NULL, str, 0,
476                                      NULL, NULL, NULL, NULL,
477                                      FALSE, TRUE);
478         gtk_ctree_node_set_row_data(ctree, node, partinfo);
479
480         return node;
481 }
482
483 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
484 {
485         FILE *fp;
486         const gchar *fname;
487
488         if (!partinfo) return;
489
490         fname = mimeview->file;
491         if (!fname) return;
492
493         if ((fp = g_fopen(fname, "rb")) == NULL) {
494                 FILE_OP_ERROR(fname, "fopen");
495                 return;
496         }
497
498         if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
499                 FILE_OP_ERROR(mimeview->file, "fseek");
500                 fclose(fp);
501                 return;
502         }
503
504         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
505         textview_show_part(mimeview->textview, partinfo, fp);
506
507         fclose(fp);
508 }
509
510 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
511 {
512         GSList *cur;
513         MimeViewerFactory *factory = NULL;
514         MimeViewer *viewer = NULL;
515
516 /*
517  * FNM_CASEFOLD is a GNU extension
518  * if its not defined copy the string to the stack and
519  * convert the copy to lower case
520  */
521 #ifndef FNM_CASEFOLD
522 #define FNM_CASEFOLD 0
523         Xstrdup_a(content_type, content_type, return NULL);
524         g_strdown((gchar *)content_type);
525 #endif
526         
527         for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
528                 MimeViewerFactory *curfactory = cur->data;
529                 gint i = 0;
530
531                 while (curfactory->content_types[i] != NULL) {
532                         if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
533                                 debug_print("%s\n", curfactory->content_types[i]);
534                                 factory = curfactory;
535                                 break;
536                         }
537                         i++;
538                 }
539                 if (factory != NULL)
540                         break;
541         }
542         if (factory == NULL)
543                 return NULL;
544
545         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
546                 MimeViewer *curviewer = cur->data;
547                 
548                 if (curviewer->factory == factory)
549                         return curviewer;
550         }
551         viewer = factory->create_viewer();
552         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
553                 GTK_WIDGET(viewer->get_widget(viewer)));
554                 
555         mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
556
557         return viewer;
558 }
559
560 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
561 {
562         gchar *content_type = NULL;
563         MimeViewer *viewer = NULL;
564
565         if ((partinfo->type == MIMETYPE_APPLICATION) &&
566             (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
567                 const gchar *filename;
568
569                 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
570                 if (filename == NULL)
571                         filename = procmime_mimeinfo_get_parameter(partinfo, "name");
572                 if (filename != NULL)
573                         content_type = procmime_get_mime_type(filename);
574         } else {
575                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
576         }
577
578         if (content_type != NULL) {
579                 viewer = get_viewer_for_content_type(mimeview, content_type);
580                 g_free(content_type);
581         }
582
583         return viewer;
584 }
585
586 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
587 {
588         MimeViewer *viewer;
589         
590         viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
591         if (viewer == NULL) {
592                 if (mimeview->mimeviewer != NULL)
593                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
594                 mimeview->mimeviewer = NULL;
595                 return FALSE;
596         }
597
598         if (mimeview->mimeviewer != NULL)
599                 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
600
601         if (mimeview->mimeviewer != viewer) {
602                 mimeview->mimeviewer = viewer;
603                 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
604         }
605         viewer->show_mimepart(viewer, mimeview->file, partinfo);
606
607         return TRUE;
608 }
609
610 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
611 {
612         TextView  *textview  = mimeview->textview;
613
614         if ((mimeview->type != MIMEVIEW_VIEWER) && 
615             (mimeview->type == type)) return;
616
617         switch (type) {
618         case MIMEVIEW_TEXT:
619                 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
620                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
621                         GTK_WIDGET_PTR(textview)));
622                 break;
623         case MIMEVIEW_VIEWER:
624                 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
625                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
626                         GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
627                 break;
628         default:
629                 return;
630         }
631
632         mimeview->type = type;
633 }
634
635 void mimeview_clear(MimeView *mimeview)
636 {
637         GtkCList *clist = NULL;
638         
639         if (!mimeview)
640                 return;
641
642         if (g_slist_find(mimeviews, mimeview) == NULL)
643                 return;
644         
645         clist = GTK_CLIST(mimeview->ctree);
646         
647         noticeview_hide(mimeview->siginfoview);
648
649         gtk_clist_clear(clist);
650         textview_clear(mimeview->textview);
651         if (mimeview->mimeviewer != NULL)
652                 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
653
654         mimeview_free_mimeinfo(mimeview);
655
656         mimeview->mimeinfo = NULL;
657
658         mimeview->opened = NULL;
659
660         g_free(mimeview->file);
661         mimeview->file = NULL;
662
663         icon_list_clear(mimeview);
664         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
665 }
666
667 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
668 void mimeview_check_signature(MimeView *mimeview);
669 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
670
671 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo, 
672                                         gboolean special, SignatureStatus code)
673 {
674         gchar *text = NULL, *button_text = NULL;
675         void  *func = NULL;
676         StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
677         SignatureStatus mycode = SIGNATURE_UNCHECKED;
678         
679         g_return_if_fail(mimeview != NULL);
680         g_return_if_fail(mimeinfo != NULL);
681         
682         if (special)
683                 mycode = code;
684         else 
685                 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
686
687         switch (mycode) {
688         case SIGNATURE_UNCHECKED:
689                 button_text = _("Check signature");
690                 func = check_signature_cb;
691                 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
692                 break;
693         case SIGNATURE_OK:
694                 button_text = _("View full information");
695                 func = display_full_info_cb;
696                 icon = STOCK_PIXMAP_PRIVACY_PASSED;
697                 break;
698         case SIGNATURE_WARN:
699                 button_text = _("View full information");
700                 func = display_full_info_cb;
701                 icon = STOCK_PIXMAP_PRIVACY_WARN;
702                 break;
703         case SIGNATURE_INVALID:
704                 button_text = _("View full information");
705                 func = display_full_info_cb;
706                 icon = STOCK_PIXMAP_PRIVACY_FAILED;
707                 break;
708         case SIGNATURE_CHECK_FAILED:
709                 button_text = _("Check again");
710                 func = check_signature_cb;
711                 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
712         case SIGNATURE_CHECK_TIMEOUT:
713                 button_text = _("Check again");
714                 func = check_signature_cb;
715                 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
716         default:
717                 break;
718         }
719         if (mycode == SIGNATURE_UNCHECKED) {
720                 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
721                 text = g_strdup_printf("%s %s",
722                         tmp, _("Click the icon or hit 'C' to check it."));
723                 g_free(tmp);
724         } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
725                 text = privacy_mimeinfo_sig_info_short(mimeinfo);
726         } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
727                 text = g_strdup(_("Timeout checking the signature. Click the icon or hit 'C' to try again."));
728         }
729
730         noticeview_set_text(mimeview->siginfoview, text);
731         gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
732
733         g_free(text);
734         noticeview_set_button_text(mimeview->siginfoview, NULL);
735         noticeview_set_button_press_callback(
736                 mimeview->siginfoview,
737                 G_CALLBACK(func),
738                 (gpointer) mimeview);
739         noticeview_set_icon(mimeview->siginfoview, icon);
740         noticeview_set_tooltip(mimeview->siginfoview, button_text);
741 }
742
743 #ifdef USE_PTHREAD
744
745 /* reset all thread stuff, and do the cleanups we've been left to do */
746 static void mimeview_check_data_reset(MimeView *mimeview)
747 {
748         if (!mimeview->check_data)
749                 return;
750
751         if (mimeview->check_data->free_after_use) {
752                 debug_print("freeing deferred mimeinfo\n");
753                 procmime_mimeinfo_free_all(mimeview->check_data->siginfo);
754         }
755         if (mimeview->check_data->destroy_mimeview) {
756                 debug_print("freeing deferred mimeview\n");
757                 g_free(mimeview->file);
758                 g_free(mimeview);
759                 mimeviews = g_slist_remove(mimeviews, mimeview);
760         }
761
762         g_free(mimeview->check_data);
763         mimeview->check_data = NULL;
764 }
765
766 /* GUI update once the checker thread is done or killed */
767 static gboolean mimeview_check_sig_thread_cb(void *data)
768 {
769         MimeView *mimeview = (MimeView *) data;
770         MimeInfo *mimeinfo = mimeview->siginfo;
771
772         debug_print("mimeview_check_sig_thread_cb\n");
773         
774         if (mimeinfo == NULL) {
775                 /* message changed !? */
776                 g_warning("no more siginfo!\n");
777                 goto end;
778         }
779         
780         if (!mimeview->check_data) {
781                 g_warning("nothing to check\n");
782                 return FALSE;
783         }
784
785         if (mimeview->check_data->siginfo != mimeinfo) {
786                 /* message changed !? */
787                 g_warning("different siginfo!\n");
788                 goto end;
789         }
790
791         if (mimeview->check_data->destroy_mimeview ||
792             mimeview->check_data->free_after_use) {
793                 debug_print("not bothering, we're changing message\n"); 
794                 goto end;
795         }
796         
797         /* update status */
798         if (mimeview->check_data->timeout) 
799                 update_signature_noticeview(mimeview, mimeview->siginfo, 
800                         TRUE, SIGNATURE_CHECK_TIMEOUT);
801         else
802                 update_signature_noticeview(mimeview, mimeview->siginfo, 
803                         FALSE, 0);
804         icon_list_clear(mimeview);
805         icon_list_create(mimeview, mimeview->mimeinfo);
806         
807 end:
808         mimeview_check_data_reset(mimeview);
809         return FALSE;
810 }
811
812 /* sig checker thread */
813 static void *mimeview_check_sig_worker_thread(void *data)
814 {
815         MimeView *mimeview = (MimeView *)data;
816         MimeInfo *mimeinfo = mimeview->siginfo;
817         
818         debug_print("checking...\n");
819
820         if (!mimeview->check_data)
821                 return NULL;
822
823         if (mimeinfo && mimeinfo == mimeview->check_data->siginfo)
824                 privacy_mimeinfo_check_signature(mimeinfo);
825         else {
826                 /* that's strange! we changed message without 
827                  * getting killed. */
828                 g_warning("different siginfo!\n");
829                 mimeview_check_data_reset(mimeview);
830                 return NULL;
831         }
832
833         /* use g_timeout so that GUI updates is done from the
834          * correct thread */
835         g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
836         
837         return NULL;
838 }
839
840 /* killer thread - acts when the checker didn't work fast
841  * enough. */
842 static void *mimeview_check_sig_cancel_thread(void *data)
843 {
844         MimeView *mimeview = (MimeView *)data;
845         
846         if (!mimeview->check_data)
847                 return NULL; /* nothing to kill ! */
848
849         /* wait for a few seconds... */
850         debug_print("waiting a while\n");
851
852         sleep(5);
853         
854         if (!mimeview->check_data)
855                 return NULL; /* nothing to kill, it's done in time :) */
856         
857         /* too late, go away checker thread */
858         debug_print("killing checker thread\n");
859         pthread_cancel(mimeview->check_data->th);
860         
861         /* tell upstream it was a timeout */
862         mimeview->check_data->timeout = TRUE;
863         /* use g_timeout so that GUI updates is done from the
864          * correct thread */
865         g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
866
867         return NULL;
868 }
869
870 /* get rid of the checker thread right now - used when changing the
871  * displayed message for example. */
872 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
873 {
874         if (!mimeview->check_data)
875                 return;
876         debug_print("killing checker thread NOW\n");
877         pthread_cancel(mimeview->check_data->th);
878
879         /* tell upstream it was a timeout */
880         mimeview->check_data->timeout = TRUE;
881         mimeview_check_sig_thread_cb(mimeview);
882         return;
883 }
884
885 /* creates a thread to check the signature, and a second one
886  * to kill the first one after a timeout */
887 static void mimeview_check_sig_in_thread(MimeView *mimeview)
888 {
889         pthread_t th, th2;
890         pthread_attr_t detach, detach2;
891         
892         if (mimeview->check_data) {
893                 g_warning("already checking it");
894                 return;
895         }
896         
897         mimeview->check_data = g_new0(SigCheckData, 1);
898         mimeview->check_data->siginfo = mimeview->siginfo;
899         debug_print("creating thread\n");
900
901         pthread_attr_init(&detach);
902         pthread_attr_setdetachstate(&detach, TRUE);
903
904         pthread_attr_init(&detach2);
905         pthread_attr_setdetachstate(&detach2, TRUE);
906
907         /* create the checker thread */
908         if (pthread_create(&th, &detach, 
909                         mimeview_check_sig_worker_thread, 
910                         mimeview) != 0) {
911                 /* arh. We'll do it synchronously. */
912                 g_warning("can't create thread");
913                 g_free(mimeview->check_data);
914                 mimeview->check_data = NULL;
915                 return;
916         } else 
917                 mimeview->check_data->th = th;
918
919         /* create the killer thread */
920         pthread_create(&th2, &detach2, 
921                         mimeview_check_sig_cancel_thread, 
922                         mimeview);
923 }
924 #endif
925
926 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
927 {
928         MimeView *mimeview = (MimeView *) user_data;
929         MimeInfo *mimeinfo = mimeview->siginfo;
930         
931         if (mimeinfo == NULL)
932                 return;
933 #ifdef USE_PTHREAD
934         if (mimeview->check_data)
935                 return;
936 #endif
937         noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
938         GTK_EVENTS_FLUSH();
939 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
940         /* let's do it non-blocking */
941         mimeview_check_sig_in_thread(mimeview);
942         if (!mimeview->check_data) /* let's check syncronously */
943 #endif
944         {
945                 debug_print("checking without thread\n");
946                 privacy_mimeinfo_check_signature(mimeinfo);
947                 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
948                 icon_list_clear(mimeview);
949                 icon_list_create(mimeview, mimeview->mimeinfo);
950         }
951 }
952
953 void mimeview_check_signature(MimeView *mimeview)
954 {
955         check_signature_cb(NULL, mimeview);     
956 }
957
958 static void redisplay_email(GtkWidget *widget, gpointer user_data)
959 {
960         MimeView *mimeview = (MimeView *) user_data;
961         GtkCTreeNode *node = mimeview->opened;
962         mimeview->opened = NULL;
963         mimeview_selected(GTK_CTREE(mimeview->ctree), node, 0, mimeview);
964 }
965
966 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
967 {
968         MimeView *mimeview = (MimeView *) user_data;
969         gchar *siginfo;
970
971         siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
972         textview_set_text(mimeview->textview, siginfo);
973         g_free(siginfo);
974         noticeview_set_button_text(mimeview->siginfoview, NULL);
975         noticeview_set_button_press_callback(
976                 mimeview->siginfoview,
977                 G_CALLBACK(redisplay_email),
978                 (gpointer) mimeview);
979         noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
980 }
981
982 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
983 {
984         MimeInfo *siginfo;
985         MimeInfo *first_text;
986         
987         g_return_if_fail(mimeview != NULL);
988         g_return_if_fail(selected != NULL);
989         
990         if (selected->type == MIMETYPE_MESSAGE 
991         &&  !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
992                 /* if the first text part is signed, check that */
993                 first_text = selected;
994                 while (first_text && first_text->type != MIMETYPE_TEXT) {
995                         first_text = procmime_mimeinfo_next(first_text);
996                 }
997                 if (first_text) {
998                         update_signature_info(mimeview, first_text);
999                         return;
1000                 }       
1001         }
1002
1003         siginfo = selected;
1004         while (siginfo != NULL) {
1005                 if (privacy_mimeinfo_is_signed(siginfo))
1006                         break;
1007                 siginfo = procmime_mimeinfo_parent(siginfo);
1008         }
1009         mimeview->siginfo = siginfo;
1010         
1011         if (siginfo == NULL) {
1012                 noticeview_hide(mimeview->siginfoview);
1013                 return;
1014         }
1015         
1016         update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1017         noticeview_show(mimeview->siginfoview);
1018 }
1019
1020 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
1021                               MimeView *mimeview)
1022 {
1023         MimeInfo *partinfo;
1024         if (mimeview->opened == node) return;
1025         mimeview->opened = node;
1026         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1027
1028         partinfo = gtk_ctree_node_get_row_data(ctree, node);
1029         if (!partinfo) return;
1030
1031         /* ungrab the mouse event */
1032         if (GTK_WIDGET_HAS_GRAB(ctree)) {
1033                 gtk_grab_remove(GTK_WIDGET(ctree));
1034                 if (gdk_pointer_is_grabbed())
1035                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
1036         }
1037         
1038         mimeview->textview->default_text = FALSE;
1039
1040         update_signature_info(mimeview, partinfo);
1041
1042         if (!mimeview_show_part(mimeview, partinfo)) {
1043                 switch (partinfo->type) {
1044                 case MIMETYPE_TEXT:
1045                 case MIMETYPE_MESSAGE:
1046                 case MIMETYPE_MULTIPART:
1047                         mimeview_show_message_part(mimeview, partinfo);
1048                 
1049                         break;
1050                 default:
1051                         mimeview->textview->default_text = TRUE;
1052                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1053                         textview_clear(mimeview->textview);
1054                         textview_show_mime_part(mimeview->textview, partinfo);
1055                         
1056 #if 0 /* this sucks. I'll do better. */
1057                         val = alertpanel_full(_("Unknown part type"), 
1058                                               _("The type of this part is unknown. What would you like "
1059                                                 "to do with it?"),
1060                                               GTK_STOCK_SAVE, GTK_STOCK_OPEN, _("Display as text"),
1061                                               FALSE, NULL, ALERT_WARNING, G_ALERTALTERNATE);
1062                         switch (val) {
1063                                 case G_ALERTDEFAULT:
1064                                         mimeview_save_as(mimeview);
1065                                         break;
1066                                 case G_ALERTALTERNATE:
1067                                         mimeview_open_with(mimeview);
1068                                         break;
1069                                 case G_ALERTOTHER:
1070                                         mimeview_display_as_text(mimeview);
1071                                         break;
1072                                 default:
1073                         }
1074 #endif
1075                         break;
1076                 }
1077         }
1078 }
1079
1080 static void mimeview_start_drag(GtkWidget *widget, gint button,
1081                                 GdkEvent *event, MimeView *mimeview)
1082 {
1083         GdkDragContext *context;
1084         MimeInfo *partinfo;
1085
1086         g_return_if_fail(mimeview != NULL);
1087
1088         partinfo = mimeview_get_selected_part(mimeview);
1089         if (partinfo->disposition == DISPOSITIONTYPE_INLINE) return;
1090
1091         context = gtk_drag_begin(widget, mimeview->target_list,
1092                                  GDK_ACTION_COPY, button, event);
1093         gtk_drag_set_icon_default(context);
1094 }
1095
1096 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1097                                     MimeView *mimeview)
1098 {
1099         GtkCList *clist = GTK_CLIST(widget);
1100         gint row, column;
1101
1102         if (!event) return FALSE;
1103
1104         if (event->button == 2 || event->button == 3) {
1105                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
1106                                                   &row, &column))
1107                         return FALSE;
1108                 gtk_clist_unselect_all(clist);
1109                 gtk_clist_select_row(clist, row, column);
1110                 gtkut_clist_set_focus_row(clist, row);
1111         }
1112         part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1113
1114         return FALSE;
1115 }
1116
1117 /* from gdkevents.c */
1118 #define DOUBLE_CLICK_TIME 250
1119
1120 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event, 
1121                                     MimeInfo *partinfo)
1122 {
1123         static MimeInfo *lastinfo;
1124         static guint32 lasttime;
1125
1126         if (event->button == 2 ||
1127             (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1128                 /* call external program for image, audio or html */
1129                 mimeview_launch(mimeview);
1130                 return TRUE;
1131         } else if (event->button == 3) {
1132                 if (partinfo && (partinfo->type == MIMETYPE_TEXT ||
1133                                  partinfo->type == MIMETYPE_MESSAGE ||
1134                                  partinfo->type == MIMETYPE_IMAGE ||
1135                                  partinfo->type == MIMETYPE_MULTIPART))
1136                         menu_set_sensitive(mimeview->popupfactory,
1137                                            "/Display as text", FALSE);
1138                 else
1139                         menu_set_sensitive(mimeview->popupfactory,
1140                                            "/Display as text", TRUE);
1141                 if (partinfo &&
1142                     partinfo->type == MIMETYPE_APPLICATION &&
1143                     !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1144                         menu_set_sensitive(mimeview->popupfactory,
1145                                            "/Open", FALSE);
1146                 else
1147                         menu_set_sensitive(mimeview->popupfactory,
1148                                            "/Open", TRUE);
1149
1150                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1151                                   "pop_partinfo", partinfo);
1152                                     
1153                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1154                                NULL, NULL, NULL, NULL,
1155                                event->button, event->time);
1156                 return TRUE;
1157         }
1158
1159         lastinfo = partinfo;
1160         lasttime = event->time;
1161         return FALSE;
1162 }
1163
1164
1165 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1166 {
1167         mimeview_key_pressed(mimeview->ctree, event, mimeview);
1168 }
1169
1170 #define BREAK_ON_MODIFIER_KEY() \
1171         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1172
1173 #define KEY_PRESS_EVENT_STOP() \
1174         g_signal_stop_emission_by_name(G_OBJECT(ctree), \
1175                                        "key_press_event");
1176
1177 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1178                                  MimeView *mimeview)
1179 {
1180         SummaryView *summaryview;
1181         GtkCTree *ctree = GTK_CTREE(widget);
1182         GtkCTreeNode *node;
1183
1184         if (!event) return FALSE;
1185         if (!mimeview->opened) return FALSE;
1186
1187         summaryview = mimeview->messageview->mainwin->summaryview;
1188         
1189         if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1190                 return FALSE;
1191                 
1192         switch (event->keyval) {
1193         case GDK_space:
1194                 if (textview_scroll_page(mimeview->textview, FALSE))
1195                         return TRUE;
1196
1197                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
1198                 if (node) {
1199                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1200                         gtk_sctree_select(GTK_SCTREE(ctree), node);
1201                         return TRUE;
1202                 }
1203                 break;
1204         case GDK_BackSpace:
1205                 textview_scroll_page(mimeview->textview, TRUE);
1206                 return TRUE;
1207         case GDK_Return:
1208                 textview_scroll_one_line(mimeview->textview,
1209                                          (event->state & GDK_MOD1_MASK) != 0);
1210                 return TRUE;
1211         case GDK_n:
1212         case GDK_N:
1213                 BREAK_ON_MODIFIER_KEY();
1214                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
1215                 KEY_PRESS_EVENT_STOP();
1216                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
1217                                         GTK_SCROLL_STEP_FORWARD, 0.0);
1218                 return TRUE;
1219         case GDK_p:
1220         case GDK_P:
1221                 BREAK_ON_MODIFIER_KEY();
1222                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
1223                 KEY_PRESS_EVENT_STOP();
1224                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
1225                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
1226                 return TRUE;
1227         case GDK_y:
1228                 BREAK_ON_MODIFIER_KEY();
1229                 KEY_PRESS_EVENT_STOP();
1230                 mimeview_save_as(mimeview);
1231                 return TRUE;
1232         case GDK_t:
1233                 BREAK_ON_MODIFIER_KEY();
1234                 KEY_PRESS_EVENT_STOP();
1235                 mimeview_display_as_text(mimeview);
1236                 return TRUE;    
1237         case GDK_l:
1238                 BREAK_ON_MODIFIER_KEY();
1239                 KEY_PRESS_EVENT_STOP();
1240                 mimeview_launch(mimeview);
1241                 return TRUE;
1242         case GDK_o:
1243                 BREAK_ON_MODIFIER_KEY();
1244                 KEY_PRESS_EVENT_STOP();
1245                 mimeview_open_with(mimeview);
1246                 return TRUE;
1247         case GDK_c:
1248                 BREAK_ON_MODIFIER_KEY();
1249                 KEY_PRESS_EVENT_STOP();
1250                 mimeview_check_signature(mimeview);
1251                 return TRUE;
1252         default:
1253                 break;
1254         }
1255
1256         if (!mimeview->messageview->mainwin) return FALSE;
1257
1258         summary_pass_key_press_event(summaryview, event);
1259         return TRUE;
1260 }
1261
1262 static void mimeview_drag_data_get(GtkWidget        *widget,
1263                                    GdkDragContext   *drag_context,
1264                                    GtkSelectionData *selection_data,
1265                                    guint             info,
1266                                    guint             time,
1267                                    MimeView         *mimeview)
1268 {
1269         gchar *filename = NULL, *uriname, *tmp;
1270         MimeInfo *partinfo;
1271
1272         if (!mimeview->opened) return;
1273         if (!mimeview->file) return;
1274
1275         partinfo = mimeview_get_selected_part(mimeview);
1276         if (!partinfo) return;
1277
1278         if (strlen(get_part_name(partinfo)) > 0) {
1279                 filename = g_path_get_basename(get_part_name(partinfo));
1280                 if (*filename == '\0') return;
1281         } else if (partinfo->type == MIMETYPE_MESSAGE 
1282                    && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1283                 gchar *name = NULL;
1284                 GPtrArray *headers = NULL;
1285                 FILE *fp;
1286
1287                 fp = g_fopen(partinfo->data.filename, "rb");
1288                 fseek(fp, partinfo->offset, SEEK_SET);
1289                 headers = procheader_get_header_array_asis(fp);
1290                 if (headers) {
1291                         gint i;
1292                         for (i = 0; i < headers->len; i++) {
1293                                 Header *header = g_ptr_array_index(headers, i);
1294                                 if (procheader_headername_equal(header->name, "Subject")) {
1295                                         unfold_line(header->body);
1296                                         name = g_strconcat(header->body, ".txt", NULL);
1297                                         subst_for_filename(name);
1298                                 }
1299                         }
1300                         procheader_header_array_destroy(headers);
1301                 }
1302                 fclose(fp);
1303                 if (name)
1304                         filename = g_path_get_basename(name);
1305                 g_free(name);
1306         }
1307         if (filename == NULL)
1308                 filename = g_path_get_basename("Unnamed part");
1309                 
1310
1311
1312         tmp = filename;
1313         
1314         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1315                                filename, NULL);
1316
1317         g_free(tmp);
1318
1319         if (procmime_get_part(filename, partinfo) < 0)
1320                 alertpanel_error
1321                         (_("Can't save the part of multipart message."));
1322         uriname = g_strconcat("file://", filename, NULL);
1323         gtk_selection_data_set(selection_data, selection_data->target, 8,
1324                                uriname, strlen(uriname));
1325
1326         g_free(uriname);
1327         g_free(filename);
1328 }
1329
1330 /**
1331  * Returns a filename (with path) for an attachment
1332  * \param partinfo The attachment to save
1333  * \param basedir The target directory
1334  * \param number Used for dummy filename if attachment is unnamed
1335  */
1336 gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1337                                       const gchar *basedir,
1338                                       gint number)
1339 {
1340         gchar *fullname;
1341         gchar *filename;
1342
1343         filename = g_strdup(get_part_name(partinfo));
1344         if (!filename || !*filename)
1345                 filename = g_strdup_printf("noname.%d", number);
1346
1347         if (!g_utf8_validate(filename, -1, NULL)) {
1348                 gchar *tmp = conv_filename_to_utf8(filename);
1349                 g_free(filename);
1350                 filename = tmp;
1351         }
1352         
1353         subst_for_filename(filename);
1354
1355         fullname = g_strconcat
1356                 (basedir, G_DIR_SEPARATOR_S, (filename[0] == G_DIR_SEPARATOR)
1357                  ? &filename[1] : filename, NULL);
1358
1359         g_free(filename);
1360         filename = conv_filename_from_utf8(fullname);
1361         g_free(fullname);
1362         return filename;
1363 }
1364
1365 /**
1366  * Write a single attachment to file
1367  * \param filename Filename with path
1368  * \param partinfo Attachment to save
1369  */
1370 static gboolean mimeview_write_part(const gchar *filename,
1371                                     MimeInfo *partinfo)
1372 {
1373         gchar *dir;
1374         
1375         dir= g_path_get_dirname(filename);
1376         if (!is_dir_exist(dir))
1377                 make_dir_hier(dir);
1378         g_free(dir);
1379
1380         if (is_file_exist(filename)) {
1381                 AlertValue aval;
1382                 gchar *res;
1383                 gchar *tmp;
1384                 
1385                 if (!g_utf8_validate(filename, -1, NULL))
1386                         tmp = conv_filename_to_utf8(filename);
1387                 else 
1388                         tmp = g_strdup(filename);
1389                 
1390                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1391                                       tmp);
1392                 g_free(tmp);
1393                 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL, 
1394                                   GTK_STOCK_OK, NULL);
1395                 g_free(res);                                      
1396                 if (G_ALERTALTERNATE != aval) return FALSE;
1397         }
1398
1399         if (procmime_get_part(filename, partinfo) < 0) {
1400                 alertpanel_error
1401                         (_("Can't save the part of multipart message."));
1402                 return FALSE;
1403         }
1404
1405         return TRUE;
1406 }
1407
1408 /**
1409  * Menu callback: Save all attached files
1410  * \param mimeview Current display
1411  */
1412 static void mimeview_save_all(MimeView *mimeview)
1413 {
1414         MimeInfo *partinfo;
1415         gchar *dirname;
1416         gchar *startdir = NULL;
1417         gint number = 1;
1418
1419         if (!mimeview->opened) return;
1420         if (!mimeview->file) return;
1421         if (!mimeview->mimeinfo) return;
1422
1423         partinfo = mimeview->mimeinfo;
1424         if (prefs_common.attach_save_dir)
1425                 startdir = g_strconcat(prefs_common.attach_save_dir,
1426                                        G_DIR_SEPARATOR_S, NULL);
1427
1428         dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1429         if (!dirname) {
1430                 if (startdir) g_free(startdir);
1431                 return;
1432         }
1433
1434         if (!is_dir_exist (dirname)) {
1435                 alertpanel_error(_("'%s' is not a directory."),
1436                                  dirname);
1437                 if (startdir) g_free(startdir);
1438                 return;
1439         }
1440
1441         if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1442                 dirname[strlen(dirname)-1] = '\0';
1443
1444         /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1445         if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1446                 partinfo = procmime_mimeinfo_next(partinfo);
1447         if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1448                 partinfo = procmime_mimeinfo_next(partinfo);
1449                 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1450                         partinfo = procmime_mimeinfo_next(partinfo);
1451         }
1452                 
1453         while (partinfo != NULL) {
1454                 if (partinfo->type != MIMETYPE_MESSAGE &&
1455                     partinfo->type != MIMETYPE_MULTIPART &&
1456                     partinfo->disposition != DISPOSITIONTYPE_INLINE) {
1457                         gchar *filename = mimeview_get_filename_for_part
1458                                 (partinfo, dirname, number++);
1459
1460                         mimeview_write_part(filename, partinfo);
1461                         g_free(filename);
1462                 }
1463                 partinfo = procmime_mimeinfo_next(partinfo);
1464         }
1465
1466         if (prefs_common.attach_save_dir)
1467                 g_free(prefs_common.attach_save_dir);
1468
1469         prefs_common.attach_save_dir = g_strdup(dirname);
1470
1471         if (startdir) g_free(startdir);
1472 }
1473
1474 /**
1475  * Menu callback: Save the selected attachment
1476  * \param mimeview Current display
1477  */
1478 static void mimeview_save_as(MimeView *mimeview)
1479 {
1480         gchar *filename;
1481         gchar *filepath = NULL;
1482         gchar *filedir = NULL;
1483         MimeInfo *partinfo;
1484         gchar *partname = NULL;
1485
1486         if (!mimeview->opened) return;
1487         if (!mimeview->file) return;
1488
1489         partinfo = mimeview_get_selected_part(mimeview);
1490         if (!partinfo) { 
1491                 partinfo = (MimeInfo *) g_object_get_data
1492                          (G_OBJECT(mimeview->popupmenu),
1493                          "pop_partinfo");
1494                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1495                                   "pop_partinfo", NULL);
1496         }                        
1497         g_return_if_fail(partinfo != NULL);
1498         
1499         if (get_part_name(partinfo) == NULL) {
1500                 return;
1501         }
1502         partname = g_strdup(get_part_name(partinfo));
1503         
1504         if (!g_utf8_validate(partname, -1, NULL)) {
1505                 gchar *tmp = conv_filename_to_utf8(partname);
1506                 g_free(partname);
1507                 partname = tmp;
1508         }
1509
1510         subst_for_filename(partname);
1511         
1512         if (prefs_common.attach_save_dir)
1513                 filepath = g_strconcat(prefs_common.attach_save_dir,
1514                                        G_DIR_SEPARATOR_S, partname, NULL);
1515         else
1516                 filepath = g_strdup(partname);
1517
1518         g_free(partname);
1519
1520         filename = filesel_select_file_save(_("Save as"), filepath);
1521         if (!filename) {
1522                 g_free(filepath);
1523                 return;
1524         }
1525
1526         mimeview_write_part(filename, partinfo);
1527
1528         filedir = g_path_get_dirname(filename);
1529         if (filedir && strcmp(filedir, ".")) {
1530                 if (prefs_common.attach_save_dir)
1531                         g_free(prefs_common.attach_save_dir);
1532                 prefs_common.attach_save_dir = g_strdup(filedir);
1533         }
1534
1535         g_free(filedir);
1536         g_free(filepath);
1537 }
1538
1539 static void mimeview_display_as_text(MimeView *mimeview)
1540 {
1541         MimeInfo *partinfo;
1542
1543         if (!mimeview->opened) return;
1544
1545         partinfo = mimeview_get_selected_part(mimeview);
1546         if (!partinfo)  {
1547                 partinfo = (MimeInfo *) g_object_get_data
1548                         (G_OBJECT(mimeview->popupmenu),
1549                          "pop_partinfo");
1550                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1551                                   "pop_partinfo", NULL);
1552         
1553         }                        
1554         g_return_if_fail(partinfo != NULL);
1555         mimeview_show_message_part(mimeview, partinfo);
1556 }
1557
1558 static void mimeview_launch(MimeView *mimeview)
1559 {
1560         MimeInfo *partinfo;
1561         gchar *filename;
1562
1563         if (!mimeview->opened) return;
1564         if (!mimeview->file) return;
1565
1566         partinfo = mimeview_get_selected_part(mimeview);
1567         if (!partinfo) { 
1568                 partinfo = (MimeInfo *) g_object_get_data
1569                         (G_OBJECT(mimeview->popupmenu),
1570                          "pop_partinfo");
1571                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1572                                   "pop_partinfo", NULL);
1573         }                        
1574         g_return_if_fail(partinfo != NULL);
1575
1576         filename = procmime_get_tmp_file_name(partinfo);
1577
1578         if (procmime_get_part(filename, partinfo) < 0)
1579                 alertpanel_error
1580                         (_("Can't save the part of multipart message."));
1581         else
1582                 mimeview_view_file(filename, partinfo, NULL, mimeview);
1583
1584         g_free(filename);
1585 }
1586
1587 static void mimeview_open_with(MimeView *mimeview)
1588 {
1589         MimeInfo *partinfo;
1590         gchar *filename;
1591         gchar *cmd;
1592         gchar *mime_command = NULL;
1593         gchar *content_type = NULL;
1594
1595         if (!mimeview) return;
1596         if (!mimeview->opened) return;
1597         if (!mimeview->file) return;
1598
1599         partinfo = mimeview_get_selected_part(mimeview);
1600         if (!partinfo) { 
1601                 partinfo = (MimeInfo *) g_object_get_data
1602                         (G_OBJECT(mimeview->popupmenu),
1603                          "pop_partinfo");
1604                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1605                                   "pop_partinfo", NULL);
1606         }                        
1607         g_return_if_fail(partinfo != NULL);
1608
1609         filename = procmime_get_tmp_file_name(partinfo);
1610
1611         if (procmime_get_part(filename, partinfo) < 0) {
1612                 alertpanel_error
1613                         (_("Can't save the part of multipart message."));
1614                 g_free(filename);
1615                 return;
1616         }
1617
1618         if (!prefs_common.mime_open_cmd_history)
1619                 prefs_common.mime_open_cmd_history =
1620                         add_history(NULL, prefs_common.mime_open_cmd);
1621
1622         if ((partinfo->type == MIMETYPE_APPLICATION) &&
1623             (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
1624                 /* guess content-type from filename */
1625                 content_type = procmime_get_mime_type(filename);
1626         } 
1627         if (content_type == NULL) {
1628                 content_type = procmime_get_content_type_str(partinfo->type,
1629                         partinfo->subtype);
1630         }
1631         mime_command = mailcap_get_command_for_type(content_type);
1632         g_free(content_type);
1633         cmd = input_dialog_combo
1634                 (_("Open with"),
1635                  _("Enter the command line to open file:\n"
1636                    "('%s' will be replaced with file name)"),
1637                  mime_command ? mime_command : prefs_common.mime_open_cmd,
1638                  prefs_common.mime_open_cmd_history,
1639                  TRUE);
1640         g_free(mime_command);
1641         if (cmd) {
1642                 mimeview_view_file(filename, partinfo, cmd, mimeview);
1643                 g_free(prefs_common.mime_open_cmd);
1644                 prefs_common.mime_open_cmd = cmd;
1645                 prefs_common.mime_open_cmd_history =
1646                         add_history(prefs_common.mime_open_cmd_history, cmd);
1647         }
1648
1649         g_free(filename);
1650 }
1651
1652 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1653                                const gchar *cmdline, MimeView *mimeview)
1654 {
1655         static gchar *default_image_cmdline = DEFAULT_IMAGE_VIEWER_CMD;
1656         static gchar *default_audio_cmdline = DEFAULT_AUDIO_PLAYER_CMD;
1657         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1658         static gchar *mime_cmdline = DEFAULT_MIME_CMD;
1659         gchar buf[1024];
1660         gchar m_buf[1024];
1661         const gchar *cmd;
1662         const gchar *def_cmd;
1663         const gchar *p;
1664
1665         if (cmdline) {
1666                 cmd = cmdline;
1667                 def_cmd = NULL;
1668         } else if (MIMETYPE_APPLICATION == partinfo->type &&
1669                    !g_ascii_strcasecmp(partinfo->subtype, "octet-stream")) {
1670                 mimeview_open_with(mimeview);
1671                 return;
1672         } else if (MIMETYPE_IMAGE == partinfo->type) {
1673                 cmd = prefs_common.mime_image_viewer;
1674                 def_cmd = default_image_cmdline;
1675         } else if (MIMETYPE_AUDIO == partinfo->type) {
1676                 cmd = prefs_common.mime_audio_player;
1677                 def_cmd = default_audio_cmdline;
1678         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1679                 cmd = prefs_common.uri_cmd;
1680                 def_cmd = default_html_cmdline;
1681         } else {
1682                 gchar *content_type;
1683                 
1684                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
1685                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1686                            content_type, "%s");
1687                 g_free(content_type);
1688                 cmd = m_buf;
1689                 def_cmd = NULL;
1690         }
1691
1692         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1693             !strchr(p + 2, '%'))
1694                 g_snprintf(buf, sizeof(buf), cmd, filename);
1695         else {
1696                 if (cmd)
1697                         g_warning("MIME viewer command line is invalid: '%s'", cmd);
1698                 if (def_cmd)
1699                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1700                 else {
1701                         mimeview_open_with(mimeview);
1702                         return;
1703                 }
1704         }
1705
1706         if (execute_command_line(buf, TRUE) != 0)
1707                 mimeview_open_with(mimeview);
1708 }
1709
1710 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1711 {
1712         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1713 }
1714
1715 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1716 {
1717         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1718 }
1719
1720 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1721 {
1722         GSList *mimeview_list, *viewer_list;
1723
1724         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1725                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1726                 
1727                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1728                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1729                         mimeview->mimeviewer = NULL;
1730                 }
1731
1732                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1733                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1734
1735                         mimeviewer->destroy_viewer(mimeviewer);
1736                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1737                 }
1738         }
1739
1740         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1741 }
1742
1743 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1744 {
1745         gint      num;
1746         MimeInfo *partinfo;
1747
1748         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1749         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1750
1751         icon_selected(mimeview, num, partinfo);
1752         gtk_widget_grab_focus(button);
1753         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1754                 toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1755                 if (event->button == 2 || event->button == 3)
1756                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
1757                                                      TRUE);
1758         }
1759
1760         part_button_pressed(mimeview, event, partinfo);
1761
1762         return FALSE;
1763 }
1764
1765 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1766 {
1767         GtkCTreeNode *node;
1768         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1769         if (node)
1770                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1771 }               
1772
1773 #undef  KEY_PRESS_EVENT_STOP
1774 #define KEY_PRESS_EVENT_STOP() \
1775         g_signal_stop_emission_by_name(G_OBJECT(button), \
1776                                        "key_press_event");
1777
1778 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1779                              MimeView *mimeview)
1780 {
1781         gint          num;
1782         MimeInfo     *partinfo;
1783         SummaryView  *summaryview;
1784         TextView     *textview;
1785
1786         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1787         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1788         
1789         if (!event) return FALSE;
1790
1791         textview = mimeview->textview;
1792
1793         switch (event->keyval) {
1794         case GDK_space:
1795                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1796                         /* stop the button being untoggled */
1797                         KEY_PRESS_EVENT_STOP();
1798                         if (textview_scroll_page(textview, FALSE))
1799                                 return TRUE;
1800
1801                         if (icon_list_select_by_number(mimeview, num + 1))
1802                                 return TRUE;
1803                 } else {
1804                         icon_selected(mimeview, num, partinfo);
1805                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1806                         return TRUE;
1807                 }
1808
1809                 break;
1810         case GDK_BackSpace:
1811                 textview_scroll_page(textview, TRUE);
1812                 return TRUE;
1813         case GDK_Return:
1814                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1815                         KEY_PRESS_EVENT_STOP();
1816                         textview_scroll_one_line(textview,
1817                                                  (event->state & GDK_MOD1_MASK) != 0);
1818                         return TRUE;
1819                 } else {
1820                         icon_selected(mimeview, num, partinfo);
1821                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1822                         return TRUE;
1823                 }
1824
1825         case GDK_n:
1826         case GDK_N:
1827                 BREAK_ON_MODIFIER_KEY();
1828                 if (icon_list_select_by_number(mimeview, num + 1)) {
1829                         KEY_PRESS_EVENT_STOP();
1830                         return TRUE;
1831                 }
1832                 break;
1833                 
1834         case GDK_p:
1835         case GDK_P:
1836                 BREAK_ON_MODIFIER_KEY();
1837                 if (icon_list_select_by_number(mimeview, num - 1)) {
1838                         KEY_PRESS_EVENT_STOP();
1839                         return TRUE;
1840                 }
1841                 break;
1842
1843         case GDK_y:
1844                 BREAK_ON_MODIFIER_KEY();
1845                 KEY_PRESS_EVENT_STOP();
1846                 mimeview_save_as(mimeview);
1847                 return TRUE;
1848         case GDK_t:
1849                 BREAK_ON_MODIFIER_KEY();
1850                 KEY_PRESS_EVENT_STOP();
1851                 mimeview_display_as_text(mimeview);
1852                 return TRUE;    
1853         case GDK_l:
1854                 BREAK_ON_MODIFIER_KEY();
1855                 KEY_PRESS_EVENT_STOP();
1856                 mimeview_launch(mimeview);
1857                 return TRUE;
1858         case GDK_o:
1859                 BREAK_ON_MODIFIER_KEY();
1860                 KEY_PRESS_EVENT_STOP();
1861                 mimeview_open_with(mimeview);
1862                 return TRUE;
1863         case GDK_c:
1864                 BREAK_ON_MODIFIER_KEY();
1865                 KEY_PRESS_EVENT_STOP();
1866                 mimeview_check_signature(mimeview);
1867                 return TRUE;
1868         default:
1869                 break;
1870         }
1871
1872         if (!mimeview->messageview->mainwin) return FALSE;
1873         summaryview = mimeview->messageview->mainwin->summaryview;
1874         summary_pass_key_press_event(summaryview, event);
1875         return TRUE;
1876 }
1877
1878 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1879 {
1880         GList *child;
1881         
1882         child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
1883         for (; child != NULL; child = g_list_next(child)) {
1884                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1885                     GTK_TOGGLE_BUTTON(child->data) != button &&
1886                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1887                         gtk_toggle_button_set_active
1888                                 (GTK_TOGGLE_BUTTON(child->data),
1889                                  FALSE);
1890         }
1891 }
1892
1893 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1894 {
1895         GtkWidget *pixmap = NULL;
1896         GtkWidget *vbox;
1897         GtkWidget *button;
1898         gchar *tip;
1899         gchar *tiptmp;
1900         const gchar *desc = NULL; 
1901         gchar *sigshort = NULL;
1902         gchar *content_type;
1903         StockPixmap stockp;
1904         MimeInfo *partinfo;
1905         MimeInfo *siginfo = NULL;
1906         MimeInfo *encrypted = NULL;
1907         
1908         vbox = mimeview->icon_vbox;
1909         mimeview->icon_count++;
1910         button = gtk_toggle_button_new();
1911         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1912         g_object_set_data(G_OBJECT(button), "icon_number", 
1913                           GINT_TO_POINTER(mimeview->icon_count));
1914         g_object_set_data(G_OBJECT(button), "partinfo", 
1915                           mimeinfo);
1916         
1917         switch (mimeinfo->type) {
1918                 
1919         case MIMETYPE_TEXT:
1920                 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1921                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1922                 else if  (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
1923                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1924                 else
1925                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1926                 break;
1927         case MIMETYPE_MESSAGE:
1928                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1929                 break;
1930         case MIMETYPE_APPLICATION:
1931                 stockp = STOCK_PIXMAP_MIME_APPLICATION;
1932                 break;
1933         case MIMETYPE_IMAGE:
1934                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1935                 break;
1936         case MIMETYPE_AUDIO:
1937                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1938                 break;
1939         default:
1940                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1941                 break;
1942         }
1943         
1944         partinfo = mimeinfo;
1945         while (partinfo != NULL) {
1946                 if (privacy_mimeinfo_is_signed(partinfo)) {
1947                         siginfo = partinfo;
1948                         break;
1949                 }
1950                 if (privacy_mimeinfo_is_encrypted(partinfo)) {
1951                         encrypted = partinfo;
1952                         break;
1953                 }
1954                 partinfo = procmime_mimeinfo_parent(partinfo);
1955         }       
1956
1957         if (siginfo != NULL) {
1958                 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
1959                 case SIGNATURE_UNCHECKED:
1960                 case SIGNATURE_CHECK_FAILED:
1961                 case SIGNATURE_CHECK_TIMEOUT:
1962                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1963                             STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1964                         break;
1965                 case SIGNATURE_OK:
1966                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1967                             STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1968                         break;
1969                 case SIGNATURE_WARN:
1970                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1971                             STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
1972                         break;
1973                 case SIGNATURE_INVALID:
1974                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1975                             STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1976                         break;
1977                 }
1978                 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
1979         } else if (encrypted != NULL) {
1980                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1981                             STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);         
1982         } else {
1983                 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
1984                                                           OVERLAY_NONE, 6, 3);
1985         }
1986         gtk_container_add(GTK_CONTAINER(button), pixmap);
1987         
1988         if (!desc) {
1989                 if (prefs_common.attach_desc)
1990                         desc = get_part_description(mimeinfo);
1991                 else
1992                         desc = get_part_name(mimeinfo);
1993         }
1994
1995         content_type = procmime_get_content_type_str(mimeinfo->type,
1996                                                      mimeinfo->subtype);
1997
1998         tip = g_strjoin("\n", content_type,
1999                         to_human_readable(mimeinfo->length), NULL);
2000         g_free(content_type);
2001         if (desc && *desc) {
2002                 gchar *tmp = NULL;
2003                 if (!g_utf8_validate(desc, -1, NULL)) {
2004                         tmp = conv_filename_to_utf8(desc);
2005                 } else {
2006                         tmp = g_strdup(desc);
2007                 }
2008                 tiptmp = g_strjoin("\n", tmp, tip, NULL);
2009                 g_free(tip);
2010                 tip = tiptmp;
2011                 g_free(tmp);
2012         }
2013         if (sigshort && *sigshort) {
2014                 tiptmp = g_strjoin("\n", tip, sigshort, NULL);
2015                 g_free(tip);
2016                 tip = tiptmp;
2017         }
2018         g_free(sigshort);
2019
2020         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
2021         g_free(tip);
2022         gtk_widget_show_all(button);
2023         gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK, 
2024                             mimeview_mime_types, 1, GDK_ACTION_COPY);
2025         g_signal_connect(G_OBJECT(button), "button_release_event", 
2026                          G_CALLBACK(icon_clicked_cb), mimeview);
2027         g_signal_connect(G_OBJECT(button), "key_press_event", 
2028                          G_CALLBACK(icon_key_pressed), mimeview);
2029         g_signal_connect(G_OBJECT(button), "drag_data_get",
2030                          G_CALLBACK(mimeview_drag_data_get), mimeview);
2031         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2032
2033 }
2034
2035 static void icon_list_clear (MimeView *mimeview)
2036 {
2037         GList     *child;
2038         GtkAdjustment *adj;
2039         
2040         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
2041         for (; child != NULL; child = g_list_next(child)) {
2042                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
2043                                        GTK_WIDGET(child->data));
2044         }
2045         mimeview->icon_count = 0;
2046         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2047         gtk_adjustment_set_value(adj, adj->lower);
2048 }
2049
2050 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
2051                                           MimeInfo      *mimeinfo)
2052 {
2053         GList *child;
2054         
2055         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
2056         for (; child != NULL; child = g_list_next(child)) {
2057                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
2058                     g_object_get_data(G_OBJECT(child->data),
2059                                       "partinfo") == (gpointer)mimeinfo) {
2060                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
2061                         gtk_toggle_button_set_active
2062                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
2063                 }                                
2064         }
2065 }
2066
2067 /*!
2068  *\brief        Used to 'click' the next or previous icon.
2069  *
2070  *\return       true if the icon 'number' exists and was selected.
2071  */
2072 static gboolean icon_list_select_by_number(MimeView     *mimeview,
2073                                            gint          number)
2074 {
2075         GList *child;
2076
2077         if (number == 0) return FALSE;
2078         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
2079         for (; child != NULL; child = g_list_next(child)) {
2080                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
2081                     GPOINTER_TO_INT(g_object_get_data(G_OBJECT(child->data),
2082                                         "icon_number")) == number) {
2083                         icon_selected(mimeview, number,
2084                                       (MimeInfo*)g_object_get_data(G_OBJECT(child->data),
2085                                                                    "partinfo"));
2086                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
2087                         gtk_toggle_button_set_active
2088                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
2089                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
2090                 
2091                         return TRUE;
2092                 }                                
2093         }
2094         return FALSE;
2095 }
2096
2097 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
2098                                          GtkAllocation *size, MimeView *mimeview)
2099 {
2100         GtkAllocation *mainbox_size;
2101         GtkAllocation *vbox_size;
2102         GtkAllocation *layout_size;
2103         GtkAdjustment *adj;
2104         
2105         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2106
2107         mainbox_size = &mimeview->icon_mainbox->allocation;
2108         vbox_size = &mimeview->icon_vbox->allocation;
2109         layout_size = &mimeview->icon_scroll->allocation;
2110                 
2111         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
2112                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
2113                             MAX(vbox_size->height, layout_size->height));
2114         adj->step_increment = 5;
2115 }
2116
2117 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2118 {
2119         GtkRequisition size;
2120
2121         g_return_if_fail(mimeinfo != NULL);
2122
2123         while (mimeinfo != NULL) {
2124                 if (mimeinfo->type != MIMETYPE_MULTIPART)
2125                         icon_list_append_icon(mimeview, mimeinfo);
2126                 if (mimeinfo->node->children != NULL)
2127                         icon_list_create(mimeview, 
2128                                 (MimeInfo *) mimeinfo->node->children->data);
2129                 mimeinfo = mimeinfo->node->next != NULL 
2130                          ? (MimeInfo *) mimeinfo->node->next->data 
2131                          : NULL;
2132         }
2133         gtk_widget_size_request(mimeview->icon_vbox, &size);
2134         if (size.width > mimeview->icon_mainbox->requisition.width) {
2135                 gtk_widget_set_size_request(mimeview->icon_mainbox, 
2136                                             size.width, -1);
2137         }
2138
2139 }
2140
2141 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
2142 {
2143         gtk_widget_ref(button); 
2144
2145         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
2146                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
2147                                         GTK_SHADOW_NONE);
2148                 gtk_widget_hide(mimeview->icon_mainbox);
2149                 gtk_widget_show(mimeview->ctree_mainbox);
2150                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
2151
2152                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
2153                                         button);
2154                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
2155                                    button, FALSE, FALSE, 0);
2156                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
2157         } else {
2158                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
2159                               GTK_SHADOW_NONE);
2160                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
2161                 gtk_widget_hide(mimeview->ctree_mainbox);
2162                 gtk_widget_show(mimeview->icon_mainbox);
2163                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2164
2165                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
2166                                         button);
2167                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
2168                                    button, FALSE, FALSE, 0);
2169                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
2170                 if (mimeview->opened)
2171                         icon_list_toggle_by_mime_info
2172                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
2173                                                                        mimeview->opened));
2174
2175                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
2176         }
2177         gtk_widget_grab_focus(button);
2178         gtk_widget_unref(button);
2179
2180 }
2181
2182 void mimeview_update (MimeView *mimeview) {
2183         if (mimeview && mimeview->mimeinfo) {
2184                 icon_list_clear(mimeview);
2185                 icon_list_create(mimeview, mimeview->mimeinfo);
2186         }
2187 }