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