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