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