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