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