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