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