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