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