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