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