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