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