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