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