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