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