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