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