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