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