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