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