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