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