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