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