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