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