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