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