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