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