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