2705ed8a520320dc0d250c7572e6ebf596f8903e
[claws.git] / src / mimeview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <gdk/gdkkeysyms.h>
28 #include <gtk/gtknotebook.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkctree.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkhbox.h>
33 #include <gtk/gtkvpaned.h>
34 #include <gtk/gtktext.h>
35 #include <gtk/gtksignal.h>
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkdnd.h>
38 #include <gtk/gtkselection.h>
39 #include <gtk/gtktooltips.h>
40 #include <gtk/gtkcontainer.h>
41 #include <gtk/gtkbutton.h>
42 #include <stdio.h>
43
44 #ifndef HAVE_APACHE_FNMATCH
45 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
46  * fnmatch.h */
47 #include <fnmatch.h>
48 #endif
49
50 #include "intl.h"
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 "rfc2015.h"
64 #include "stock_pixmap.h"
65 #include "gtk/gtkvscrollbutton.h"
66
67
68 typedef enum
69 {
70         COL_MIMETYPE = 0,
71         COL_SIZE     = 1,
72         COL_NAME     = 2
73 } MimeViewColumnPos;
74
75 #define N_MIMEVIEW_COLS 3
76
77 static void mimeview_set_multipart_tree         (MimeView       *mimeview,
78                                                  MimeInfo       *mimeinfo,
79                                                  GtkCTreeNode   *parent);
80 static GtkCTreeNode *mimeview_append_part       (MimeView       *mimeview,
81                                                  MimeInfo       *partinfo,
82                                                  GtkCTreeNode   *parent);
83 static void mimeview_show_message_part          (MimeView       *mimeview,
84                                                  MimeInfo       *partinfo);
85 static void mimeview_change_view_type           (MimeView       *mimeview,
86                                                  MimeViewType    type);
87 gchar *mimeview_get_filename_for_part           (MimeInfo       *partinfo,
88                                                  const gchar    *basedir,
89                                                  gint            number);
90 static gboolean mimeview_write_part             (const gchar    *filename,
91                                                  MimeInfo       *partinfo);
92
93 static void mimeview_selected           (GtkCTree       *ctree,
94                                          GtkCTreeNode   *node,
95                                          gint            column,
96                                          MimeView       *mimeview);
97 static void mimeview_start_drag         (GtkWidget      *widget,
98                                          gint            button,
99                                          GdkEvent       *event,
100                                          MimeView       *mimeview);
101 static gint mimeview_button_pressed     (GtkWidget      *widget,
102                                          GdkEventButton *event,
103                                          MimeView       *mimeview);
104 static gint mimeview_key_pressed        (GtkWidget      *widget,
105                                          GdkEventKey    *event,
106                                          MimeView       *mimeview);
107
108 static void mimeview_drag_data_get      (GtkWidget        *widget,
109                                          GdkDragContext   *drag_context,
110                                          GtkSelectionData *selection_data,
111                                          guint             info,
112                                          guint             time,
113                                          MimeView         *mimeview);
114
115 static void mimeview_display_as_text    (MimeView       *mimeview);
116 static void mimeview_save_as            (MimeView       *mimeview);
117 static void mimeview_save_all           (MimeView       *mimeview);
118 static void mimeview_launch             (MimeView       *mimeview);
119 static void mimeview_open_with          (MimeView       *mimeview);
120 static void mimeview_view_file          (const gchar    *filename,
121                                          MimeInfo       *partinfo,
122                                          const gchar    *cmdline);
123 static gboolean icon_clicked_cb         (GtkWidget      *button, 
124                                          GdkEventButton *event, 
125                                          MimeView       *mimeview);
126 static void icon_selected               (MimeView       *mimeview, 
127                                          gint            num, 
128                                          MimeInfo       *partinfo);
129 static gint icon_key_pressed            (GtkWidget      *button, 
130                                          GdkEventKey    *event,
131                                          MimeView       *mimeview);
132 static void toggle_icon                 (GtkToggleButton *button,
133                                          MimeView       *mimeview);
134 static void icon_list_append_icon       (MimeView       *mimeview, 
135                                          MimeInfo       *mimeinfo);
136 static void icon_list_create            (MimeView       *mimeview, 
137                                          MimeInfo       *mimeinfo);
138 static void icon_list_clear             (MimeView       *mimeview);
139 static void icon_list_toggle_by_mime_info (MimeView     *mimeview,
140                                            MimeInfo     *mimeinfo);
141 static gboolean icon_list_select_by_number(MimeView     *mimeview,
142                                            gint          number);
143 static void mime_toggle_button_cb       (GtkWidget      *button,
144                                          MimeView       *mimeview);
145 static gboolean part_button_pressed     (MimeView       *mimeview, 
146                                          GdkEventButton *event, 
147                                          MimeInfo       *partinfo);
148 static void icon_scroll_size_allocate_cb(GtkWidget      *widget, 
149                                          GtkAllocation  *layout_size, 
150                                          MimeView       *mimeview);
151
152 static GtkItemFactoryEntry mimeview_popup_entries[] =
153 {
154         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
155         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
156         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
157         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL},
158         {N_("/Save _all..."),     NULL, mimeview_save_all,        0, NULL},
159 };
160
161 static GtkTargetEntry mimeview_mime_types[] =
162 {
163         {"text/uri-list", 0, 0}
164 };
165
166 GSList *mimeviewer_factories;
167 GSList *mimeviews;
168
169 MimeView *mimeview_create(MainWindow *mainwin)
170 {
171         MimeView *mimeview;
172
173         GtkWidget *paned;
174         GtkWidget *scrolledwin;
175         GtkWidget *ctree;
176         GtkWidget *mime_notebook;
177         GtkWidget *popupmenu;
178         GtkWidget *ctree_mainbox;
179         GtkWidget *vbox;
180         GtkWidget *mime_toggle;
181         GtkWidget *icon_mainbox;
182         GtkWidget *icon_scroll;
183         GtkWidget *icon_vbox;
184         GtkWidget *arrow;
185         GtkWidget *scrollbutton;
186         GtkWidget *hbox;
187         GtkTooltips *tooltips;
188         GtkItemFactory *popupfactory;
189         NoticeView *siginfoview;
190         gchar *titles[N_MIMEVIEW_COLS];
191         gint n_entries;
192         gint i;
193
194         debug_print("Creating MIME view...\n");
195         mimeview = g_new0(MimeView, 1);
196
197         titles[COL_MIMETYPE] = _("MIME Type");
198         titles[COL_SIZE]     = _("Size");
199         titles[COL_NAME]     = _("Name");
200
201         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
202         gtk_widget_show(scrolledwin);
203         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
204                                        GTK_POLICY_AUTOMATIC,
205                                        GTK_POLICY_ALWAYS);
206
207         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
208         gtk_widget_show(ctree);
209         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
210         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
211         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
212                                            GTK_JUSTIFY_RIGHT);
213         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
214         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 90);
215         for (i = 0; i < N_MIMEVIEW_COLS; i++)
216                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
217                                        GTK_CAN_FOCUS);
218         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
219
220         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
221                          G_CALLBACK(mimeview_selected), mimeview);
222         g_signal_connect(G_OBJECT(ctree), "button_release_event",
223                          G_CALLBACK(mimeview_button_pressed), mimeview);
224         g_signal_connect(G_OBJECT(ctree), "key_press_event",
225                          G_CALLBACK(mimeview_key_pressed), mimeview);
226         g_signal_connect(G_OBJECT (ctree),"start_drag",
227                          G_CALLBACK (mimeview_start_drag), mimeview);
228         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
229                          G_CALLBACK(mimeview_drag_data_get), mimeview);
230
231         mime_notebook = gtk_notebook_new();
232         gtk_widget_show(mime_notebook);
233         GTK_WIDGET_UNSET_FLAGS(mime_notebook, GTK_CAN_FOCUS);
234         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
235         gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
236         
237         icon_vbox = gtk_vbox_new(FALSE, 2);
238         gtk_widget_show(icon_vbox);
239         icon_scroll = gtk_layout_new(NULL, NULL);
240         gtk_widget_show(icon_scroll);
241         gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
242         scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
243         gtk_widget_show(scrollbutton);
244
245         mime_toggle = gtk_toggle_button_new();
246         gtk_widget_show(mime_toggle);
247         arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
248         gtk_widget_show(arrow);
249         gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
250         g_signal_connect(G_OBJECT(mime_toggle), "toggled", 
251                          G_CALLBACK(mime_toggle_button_cb), mimeview);
252
253         icon_mainbox = gtk_vbox_new(FALSE, 0);
254         gtk_widget_show(icon_mainbox);
255         gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
256         gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
257         gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
258         g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate", 
259                          G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
260         
261         ctree_mainbox = gtk_hbox_new(FALSE, 0); 
262         gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
263
264         n_entries = sizeof(mimeview_popup_entries) /
265                 sizeof(mimeview_popup_entries[0]);
266         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
267                                       "<MimeView>", &popupfactory, mimeview);
268         tooltips = gtk_tooltips_new();
269         gtk_tooltips_set_delay(tooltips, 0); 
270
271         vbox = gtk_vbox_new(FALSE, 0);
272         gtk_widget_show(vbox);
273         siginfoview = noticeview_create(mainwin);
274         noticeview_hide(siginfoview);
275         gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
276         gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
277
278         paned = gtk_vpaned_new();
279         gtk_widget_show(paned);
280         gtk_paned_set_gutter_size(GTK_PANED(paned), 0);
281         gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
282         gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
283         
284         hbox = gtk_hbox_new(FALSE, 0);
285         gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
286         gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
287
288         gtk_widget_show(hbox);
289         gtk_widget_hide(ctree_mainbox);
290
291         mimeview->hbox          = hbox;
292         mimeview->paned         = paned;
293         mimeview->scrolledwin   = scrolledwin;
294         mimeview->ctree         = ctree;
295         mimeview->mime_notebook = mime_notebook;
296         mimeview->popupmenu     = popupmenu;
297         mimeview->popupfactory  = popupfactory;
298         mimeview->type          = -1;
299         mimeview->ctree_mainbox = ctree_mainbox;
300         mimeview->icon_scroll   = icon_scroll;
301         mimeview->icon_vbox     = icon_vbox;
302         mimeview->icon_mainbox  = icon_mainbox;
303         mimeview->icon_count    = 0;
304         mimeview->mainwin       = mainwin;
305         mimeview->tooltips      = tooltips;
306         mimeview->oldsize       = 60;
307         mimeview->mime_toggle   = mime_toggle;
308         mimeview->siginfoview   = siginfoview;
309
310         mimeview->target_list   = gtk_target_list_new(mimeview_mime_types, 1); 
311         
312         mimeviews = g_slist_prepend(mimeviews, mimeview);
313
314         return mimeview;
315 }
316
317 void mimeview_init(MimeView *mimeview)
318 {
319         textview_init(mimeview->textview);
320
321         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
322                 GTK_WIDGET_PTR(mimeview->textview));
323 }
324
325 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
326                            const gchar *file)
327 {
328         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
329         GtkCTreeNode *node;
330
331         mimeview_clear(mimeview);
332
333         g_return_if_fail(file != NULL);
334         g_return_if_fail(mimeinfo != NULL);
335
336         mimeview->mimeinfo = mimeinfo;
337
338         mimeview->file = g_strdup(file);
339
340         g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
341                                         mimeview);
342
343         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
344         icon_list_create(mimeview, mimeinfo);
345
346         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
347                                           mimeview_selected, mimeview);
348
349         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
350         if (node) {
351                 gtk_ctree_select(ctree, node);
352                 icon_list_toggle_by_mime_info
353                         (mimeview, gtk_ctree_node_get_row_data(ctree, node));
354                 gtkut_ctree_set_focus_row(ctree, node);
355         }
356 }
357
358 void mimeview_destroy(MimeView *mimeview)
359 {
360         GSList *cur;
361         
362         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
363                 MimeViewer *viewer = (MimeViewer *) cur->data;
364                 gtk_container_remove(GTK_CONTAINER(mimeview->mime_notebook),
365                         GTK_WIDGET(viewer->get_widget(viewer)));
366                 viewer->destroy_viewer(viewer);
367         }
368         g_slist_free(mimeview->viewers);
369         gtk_target_list_unref(mimeview->target_list);
370
371         procmime_mimeinfo_free_all(mimeview->mimeinfo);
372         g_free(mimeview->file);
373         g_free(mimeview);
374
375         mimeviews = g_slist_remove(mimeviews, mimeview);
376         
377 }
378
379 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
380 {
381         return gtk_ctree_node_get_row_data
382                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
383 }
384
385 static void mimeview_set_multipart_tree(MimeView *mimeview,
386                                         MimeInfo *mimeinfo,
387                                         GtkCTreeNode *parent)
388 {
389         GtkCTreeNode *node;
390
391         g_return_if_fail(mimeinfo != NULL);
392
393         while (mimeinfo != NULL) {
394                 node = mimeview_append_part(mimeview, mimeinfo, parent);
395
396                 if (mimeinfo->node->children)
397                         mimeview_set_multipart_tree(mimeview, (MimeInfo *) mimeinfo->node->children->data, node);
398                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
399         }
400 }
401
402 static const gchar *get_part_name(MimeInfo *partinfo)
403 {
404         const gchar *name;
405
406         name = procmime_mimeinfo_get_parameter(partinfo, "filename");
407         if (name == NULL)
408                 name = procmime_mimeinfo_get_parameter(partinfo, "name");
409         if (name == NULL)
410                 name = "";
411
412         return name;
413 }
414
415 static const gchar *get_part_description(MimeInfo *partinfo)
416 {
417         if (partinfo->description)
418                 return partinfo->description;
419         else
420                 return get_part_name(partinfo);
421 }
422
423 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
424                                           MimeInfo *partinfo,
425                                           GtkCTreeNode *parent)
426 {
427         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
428         GtkCTreeNode *node;
429         static gchar content_type[64];
430         gchar *str[N_MIMEVIEW_COLS];
431
432         if (partinfo->type != MIMETYPE_UNKNOWN && partinfo->subtype) {
433                 g_snprintf(content_type, 64, "%s/%s", procmime_get_type_str(partinfo->type), partinfo->subtype);
434         } else {
435                 g_snprintf(content_type, 64, "UNKNOWN");
436         }
437
438         str[COL_MIMETYPE] = content_type;
439         str[COL_SIZE] = to_human_readable(partinfo->length);
440         if (prefs_common.attach_desc)
441                 str[COL_NAME] = (gchar *) get_part_description(partinfo);
442         else
443                 str[COL_NAME] = (gchar *) get_part_name(partinfo);
444
445         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
446                                      NULL, NULL, NULL, NULL,
447                                      FALSE, TRUE);
448         gtk_ctree_node_set_row_data(ctree, node, partinfo);
449
450         return node;
451 }
452
453 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
454 {
455         FILE *fp;
456         const gchar *fname;
457
458         if (!partinfo) return;
459
460         fname = mimeview->file;
461         if (!fname) return;
462
463         if ((fp = fopen(fname, "rb")) == NULL) {
464                 FILE_OP_ERROR(fname, "fopen");
465                 return;
466         }
467
468         if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
469                 FILE_OP_ERROR(mimeview->file, "fseek");
470                 fclose(fp);
471                 return;
472         }
473
474         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
475         textview_show_part(mimeview->textview, partinfo, fp);
476
477         fclose(fp);
478 }
479
480 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
481 {
482         GSList *cur;
483         MimeViewerFactory *factory = NULL;
484         MimeViewer *viewer = NULL;
485
486 /*
487  * FNM_CASEFOLD is a GNU extension
488  * if its not defined copy the string to the stack and
489  * convert the copy to lower case
490  */
491 #ifndef FNM_CASEFOLD
492 #define FNM_CASEFOLD 0
493         Xstrdup_a(content_type, content_type, return NULL);
494         g_strdown((gchar *)content_type);
495 #endif
496         
497         for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
498                 MimeViewerFactory *curfactory = cur->data;
499                 gint i = 0;
500
501                 while (curfactory->content_types[i] != NULL) {
502                         if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
503                                 debug_print("%s\n", curfactory->content_types[i]);
504                                 factory = curfactory;
505                                 break;
506                         }
507                         i++;
508                 }
509                 if (factory != NULL)
510                         break;
511         }
512         if (factory == NULL)
513                 return NULL;
514
515         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
516                 MimeViewer *curviewer = cur->data;
517                 
518                 if (curviewer->factory == factory)
519                         return curviewer;
520         }
521         viewer = factory->create_viewer();
522         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
523                 GTK_WIDGET(viewer->get_widget(viewer)));
524                 
525         mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
526
527         return viewer;
528 }
529
530 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
531 {
532         gchar *content_type = NULL;
533         MimeViewer *viewer = NULL;
534
535         if ((partinfo->type == MIMETYPE_APPLICATION) &&
536             (!g_strcasecmp(partinfo->subtype, "octet-stream"))) {
537                 const gchar *filename;
538
539                 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
540                 if (filename == NULL)
541                         filename = procmime_mimeinfo_get_parameter(partinfo, "name");
542                 if (filename != NULL)
543                         content_type = procmime_get_mime_type(filename);
544         } else {
545                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
546         }
547
548         if (content_type != NULL) {
549                 viewer = get_viewer_for_content_type(mimeview, content_type);
550                 g_free(content_type);
551         }
552
553         return viewer;
554 }
555
556 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
557 {
558         MimeViewer *viewer;
559         
560         viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
561         if (viewer == NULL) {
562                 if (mimeview->mimeviewer != NULL)
563                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
564                 mimeview->mimeviewer = NULL;
565                 return FALSE;
566         }
567
568         if (mimeview->mimeviewer != viewer) {
569                 if (mimeview->mimeviewer != NULL)
570                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
571                 mimeview->mimeviewer = viewer;
572                 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
573         }
574         viewer->show_mimepart(viewer, mimeview->file, partinfo);
575
576         return TRUE;
577 }
578
579 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
580 {
581         TextView  *textview  = mimeview->textview;
582
583         if ((mimeview->type != MIMEVIEW_VIEWER) && 
584             (mimeview->type == type)) return;
585
586         switch (type) {
587         case MIMEVIEW_TEXT:
588                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
589                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
590                         GTK_WIDGET_PTR(textview)));
591                 break;
592         case MIMEVIEW_VIEWER:
593                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
594                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
595                         GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
596                 break;
597         default:
598                 return;
599         }
600
601         mimeview->type = type;
602 }
603
604 void mimeview_clear(MimeView *mimeview)
605 {
606         GtkCList *clist = GTK_CLIST(mimeview->ctree);
607
608         noticeview_hide(mimeview->siginfoview);
609
610         gtk_clist_clear(clist);
611         textview_clear(mimeview->textview);
612         if (mimeview->mimeviewer != NULL)
613                 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
614
615         if (mimeview->mimeinfo != NULL)
616                 procmime_mimeinfo_free_all(mimeview->mimeinfo);
617         mimeview->mimeinfo = NULL;
618
619         mimeview->opened = NULL;
620
621         g_free(mimeview->file);
622         mimeview->file = NULL;
623
624         icon_list_clear(mimeview);
625 }
626
627 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
628 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
629
630 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo)
631 {
632         gchar *text = NULL, *button_text = NULL;
633         void  *func = NULL;
634         StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
635
636         g_return_if_fail(mimeview != NULL);
637         g_return_if_fail(mimeinfo != NULL);
638         
639         switch (privacy_mimeinfo_get_sig_status(mimeinfo)) {
640         case SIGNATURE_UNCHECKED:
641                 button_text = _("Check");
642                 func = check_signature_cb;
643                 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
644                 break;
645         case SIGNATURE_OK:
646                 button_text = _("Full info");
647                 func = display_full_info_cb;
648                 icon = STOCK_PIXMAP_PRIVACY_PASSED;
649                 break;
650         case SIGNATURE_WARN:
651                 button_text = _("Full info");
652                 func = display_full_info_cb;
653                 icon = STOCK_PIXMAP_PRIVACY_WARN;
654                 break;
655         case SIGNATURE_INVALID:
656                 button_text = _("Full info");
657                 func = display_full_info_cb;
658                 icon = STOCK_PIXMAP_PRIVACY_FAILED;
659                 break;
660         case SIGNATURE_CHECK_FAILED:
661                 button_text = _("Check again");
662                 func = check_signature_cb;
663                 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
664         default:
665                 break;
666         }
667         text = privacy_mimeinfo_sig_info_short(mimeinfo);
668         noticeview_set_text(mimeview->siginfoview, text);
669         g_free(text);
670         noticeview_set_button_text(mimeview->siginfoview, button_text);
671         noticeview_set_button_press_callback(
672                 mimeview->siginfoview,
673                 GTK_SIGNAL_FUNC(func),
674                 (gpointer) mimeview);
675         noticeview_set_icon(mimeview->siginfoview, icon);
676 }
677
678 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
679 {
680         MimeView *mimeview = (MimeView *) user_data;
681         MimeInfo *mimeinfo = mimeview->siginfo;
682         
683         privacy_mimeinfo_check_signature(mimeinfo);
684         update_signature_noticeview(mimeview, mimeview->siginfo);
685         icon_list_clear(mimeview);
686         icon_list_create(mimeview, mimeview->mimeinfo);
687 }
688
689 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
690 {
691         MimeView *mimeview = (MimeView *) user_data;
692         gchar *siginfo;
693
694         siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
695         textview_set_text(mimeview->textview, siginfo);
696         g_free(siginfo);
697         noticeview_set_button_text(mimeview->siginfoview, NULL);
698 }
699
700 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
701 {
702         MimeInfo *siginfo;
703
704         g_return_if_fail(mimeview != NULL);
705         g_return_if_fail(selected != NULL);
706         
707         siginfo = selected;
708         while (siginfo != NULL) {
709                 if (privacy_mimeinfo_is_signed(siginfo))
710                         break;
711                 siginfo = procmime_mimeinfo_parent(siginfo);
712         }
713         mimeview->siginfo = siginfo;
714         
715         if (siginfo == NULL) {
716                 noticeview_hide(mimeview->siginfoview);
717                 return;
718         }
719         
720         update_signature_noticeview(mimeview, siginfo);
721         noticeview_show(mimeview->siginfoview);
722 }
723
724 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
725                               MimeView *mimeview)
726 {
727         MimeInfo *partinfo;
728
729         if (mimeview->opened == node) return;
730         mimeview->opened = node;
731         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
732
733         partinfo = gtk_ctree_node_get_row_data(ctree, node);
734         if (!partinfo) return;
735
736         /* ungrab the mouse event */
737         if (GTK_WIDGET_HAS_GRAB(ctree)) {
738                 gtk_grab_remove(GTK_WIDGET(ctree));
739                 if (gdk_pointer_is_grabbed())
740                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
741         }
742         
743         mimeview->textview->default_text = FALSE;
744
745         update_signature_info(mimeview, partinfo);
746
747         if (!mimeview_show_part(mimeview, partinfo)) {
748                 switch (partinfo->type) {
749                 case MIMETYPE_TEXT:
750                 case MIMETYPE_MESSAGE:
751                 case MIMETYPE_MULTIPART:
752                         mimeview_show_message_part(mimeview, partinfo);
753                 
754                         break;
755                 default:
756                         mimeview->textview->default_text = TRUE;        
757                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
758                         textview_show_mime_part(mimeview->textview, partinfo);
759                         break;
760                 }
761         }
762 }
763
764 static void mimeview_start_drag(GtkWidget *widget, gint button,
765                                 GdkEvent *event, MimeView *mimeview)
766 {
767         GdkDragContext *context;
768         MimeInfo *partinfo;
769
770         g_return_if_fail(mimeview != NULL);
771
772         partinfo = mimeview_get_selected_part(mimeview);
773         if (partinfo->disposition == DISPOSITIONTYPE_INLINE) return;
774
775         context = gtk_drag_begin(widget, mimeview->target_list,
776                                  GDK_ACTION_COPY, button, event);
777         gtk_drag_set_icon_default(context);
778 }
779
780 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
781                                     MimeView *mimeview)
782 {
783         GtkCList *clist = GTK_CLIST(widget);
784         gint row, column;
785
786         if (!event) return FALSE;
787
788         if (event->button == 2 || event->button == 3) {
789                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
790                                                   &row, &column))
791                         return FALSE;
792                 gtk_clist_unselect_all(clist);
793                 gtk_clist_select_row(clist, row, column);
794                 gtkut_clist_set_focus_row(clist, row);
795         }
796         part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
797
798         return FALSE;
799 }
800
801 /* from gdkevents.c */
802 #define DOUBLE_CLICK_TIME 250
803
804 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event, 
805                                     MimeInfo *partinfo)
806 {
807         static MimeInfo *lastinfo;
808         static guint32 lasttime;
809
810         if (event->button == 2 ||
811             (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
812                 /* call external program for image, audio or html */
813                 mimeview_launch(mimeview);
814                 return TRUE;
815         } else if (event->button == 3) {
816                 if (partinfo && (partinfo->type == MIMETYPE_TEXT ||
817                                  partinfo->type == MIMETYPE_MESSAGE ||
818                                  partinfo->type == MIMETYPE_IMAGE ||
819                                  partinfo->type == MIMETYPE_MULTIPART))
820                         menu_set_sensitive(mimeview->popupfactory,
821                                            "/Display as text", FALSE);
822                 else
823                         menu_set_sensitive(mimeview->popupfactory,
824                                            "/Display as text", TRUE);
825                 if (partinfo &&
826                     partinfo->type == MIMETYPE_APPLICATION &&
827                     !g_strcasecmp(partinfo->subtype, "octet-stream"))
828                         menu_set_sensitive(mimeview->popupfactory,
829                                            "/Open", FALSE);
830                 else
831                         menu_set_sensitive(mimeview->popupfactory,
832                                            "/Open", TRUE);
833
834                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
835                                   "pop_partinfo", partinfo);
836                                     
837                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
838                                NULL, NULL, NULL, NULL,
839                                event->button, event->time);
840                 return TRUE;
841         }
842
843         lastinfo = partinfo;
844         lasttime = event->time;
845         return FALSE;
846 }
847
848
849 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
850 {
851         mimeview_key_pressed(mimeview->ctree, event, mimeview);
852 }
853
854 #define BREAK_ON_MODIFIER_KEY() \
855         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
856
857 #define KEY_PRESS_EVENT_STOP() \
858         g_signal_stop_emission_by_name(G_OBJECT(ctree), \
859                                        "key_press_event");
860
861 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
862                                  MimeView *mimeview)
863 {
864         SummaryView *summaryview;
865         GtkCTree *ctree = GTK_CTREE(widget);
866         GtkCTreeNode *node;
867
868         if (!event) return FALSE;
869         if (!mimeview->opened) return FALSE;
870
871         summaryview = mimeview->messageview->mainwin->summaryview;
872         
873         if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
874                 return FALSE;
875                 
876         switch (event->keyval) {
877         case GDK_space:
878                 if (textview_scroll_page(mimeview->textview, FALSE))
879                         return TRUE;
880
881                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
882                 if (node) {
883                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
884                         gtk_sctree_select(GTK_SCTREE(ctree), node);
885                         return TRUE;
886                 }
887                 break;
888         case GDK_BackSpace:
889                 textview_scroll_page(mimeview->textview, TRUE);
890                 return TRUE;
891         case GDK_Return:
892                 textview_scroll_one_line(mimeview->textview,
893                                          (event->state & GDK_MOD1_MASK) != 0);
894                 return TRUE;
895         case GDK_n:
896         case GDK_N:
897                 BREAK_ON_MODIFIER_KEY();
898                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
899                 KEY_PRESS_EVENT_STOP();
900                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
901                                         GTK_SCROLL_STEP_FORWARD, 0.0);
902                 return TRUE;
903         case GDK_p:
904         case GDK_P:
905                 BREAK_ON_MODIFIER_KEY();
906                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
907                 KEY_PRESS_EVENT_STOP();
908                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
909                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
910                 return TRUE;
911         case GDK_y:
912                 BREAK_ON_MODIFIER_KEY();
913                 KEY_PRESS_EVENT_STOP();
914                 mimeview_save_as(mimeview);
915                 return TRUE;
916         case GDK_t:
917                 BREAK_ON_MODIFIER_KEY();
918                 KEY_PRESS_EVENT_STOP();
919                 mimeview_display_as_text(mimeview);
920                 return TRUE;    
921         case GDK_l:
922                 BREAK_ON_MODIFIER_KEY();
923                 KEY_PRESS_EVENT_STOP();
924                 mimeview_launch(mimeview);
925                 return TRUE;
926         case GDK_o:
927                 BREAK_ON_MODIFIER_KEY();
928                 KEY_PRESS_EVENT_STOP();
929                 mimeview_open_with(mimeview);
930                 return TRUE;
931         default:
932                 break;
933         }
934
935         if (!mimeview->messageview->mainwin) return FALSE;
936
937         summary_pass_key_press_event(summaryview, event);
938         return TRUE;
939 }
940
941 static void mimeview_drag_data_get(GtkWidget        *widget,
942                                    GdkDragContext   *drag_context,
943                                    GtkSelectionData *selection_data,
944                                    guint             info,
945                                    guint             time,
946                                    MimeView         *mimeview)
947 {
948         gchar *filename, *uriname, *tmp;
949         MimeInfo *partinfo;
950
951         if (!mimeview->opened) return;
952         if (!mimeview->file) return;
953
954         partinfo = mimeview_get_selected_part(mimeview);
955         if (!partinfo) return;
956
957         filename = g_path_get_basename(get_part_name(partinfo));
958         if (*filename == '\0') return;
959
960         tmp = filename;
961         
962         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
963                                filename, NULL);
964
965         g_free(tmp);
966         
967         if (procmime_get_part(filename, partinfo) < 0)
968                 alertpanel_error
969                         (_("Can't save the part of multipart message."));
970
971         uriname = g_strconcat("file:/", filename, NULL);
972         gtk_selection_data_set(selection_data, selection_data->target, 8,
973                                uriname, strlen(uriname));
974
975         g_free(uriname);
976         g_free(filename);
977 }
978
979 /**
980  * Returns a filename (with path) for an attachment
981  * \param partinfo The attachment to save
982  * \param basedir The target directory
983  * \param number Used for dummy filename if attachment is unnamed
984  */
985 gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
986                                       const gchar *basedir,
987                                       gint number)
988 {
989         gchar *fullname;
990         gchar *filename;
991
992         filename = g_strdup(get_part_name(partinfo));
993         if (!filename || !*filename)
994                 filename = g_strdup_printf("noname.%d", number);
995         subst_for_shellsafe_filename(filename);
996
997         fullname = g_strconcat
998                 (basedir, G_DIR_SEPARATOR_S, (filename[0] == G_DIR_SEPARATOR)
999                  ? &filename[1] : filename, NULL);
1000         subst_chars(fullname, "/\\", G_DIR_SEPARATOR);
1001
1002         g_free(filename);
1003         return fullname;
1004 }
1005
1006 /**
1007  * Write a single attachment to file
1008  * \param filename Filename with path
1009  * \param partinfo Attachment to save
1010  */
1011 static gboolean mimeview_write_part(const gchar *filename,
1012                                     MimeInfo *partinfo)
1013 {
1014         gchar *dir;
1015         
1016         dir= g_dirname(filename);
1017         if (!is_dir_exist(dir))
1018                 make_dir_hier(dir);
1019         g_free(dir);
1020
1021         if (is_file_exist(filename)) {
1022                 AlertValue aval;
1023                 gchar *res;
1024                 
1025                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1026                                       filename);
1027                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
1028                                   _("Cancel"), NULL);
1029                 g_free(res);                                      
1030                 if (G_ALERTDEFAULT != aval) return FALSE;
1031         }
1032
1033         if (procmime_get_part(filename, partinfo) < 0) {
1034                 alertpanel_error
1035                         (_("Can't save the part of multipart message."));
1036                 return FALSE;
1037         }
1038
1039         return TRUE;
1040 }
1041
1042 /**
1043  * Menu callback: Save all attached files
1044  * \param mimeview Current display
1045  */
1046 static void mimeview_save_all(MimeView *mimeview)
1047 {
1048         MimeInfo *partinfo;
1049         gchar *dirname;
1050         gchar *startdir = NULL;
1051         gint number = 1;
1052
1053         if (!mimeview->opened) return;
1054         if (!mimeview->file) return;
1055         if (!mimeview->mimeinfo) return;
1056
1057         partinfo = mimeview->mimeinfo;
1058         if (prefs_common.attach_save_dir)
1059                 startdir = g_strconcat(prefs_common.attach_save_dir,
1060                                        G_DIR_SEPARATOR_S, NULL);
1061
1062         dirname = filesel_select_file_open(_("Select destination folder"), startdir);
1063         if (!dirname) {
1064                 if (startdir) g_free(startdir);
1065                 return;
1066         }
1067
1068         if (!is_dir_exist (dirname)) {
1069                 alertpanel_error(_("`%s' is not a directory."),
1070                                  dirname);
1071                 if (startdir) g_free(startdir);
1072                 return;
1073         }
1074
1075         if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1076                 dirname[strlen(dirname)-1] = '\0';
1077
1078         while (partinfo != NULL) {
1079                 if (partinfo->type != MIMETYPE_MESSAGE &&
1080                     partinfo->type != MIMETYPE_MULTIPART &&
1081                     partinfo->disposition != DISPOSITIONTYPE_INLINE) {
1082                         gchar *filename = mimeview_get_filename_for_part
1083                                 (partinfo, dirname, number++);
1084
1085                         mimeview_write_part(filename, partinfo);
1086                         g_free(filename);
1087                 }
1088                 partinfo = procmime_mimeinfo_next(partinfo);
1089         }
1090
1091         if (prefs_common.attach_save_dir)
1092                 g_free(prefs_common.attach_save_dir);
1093
1094         prefs_common.attach_save_dir = g_strdup(dirname);
1095
1096         if (startdir) g_free(startdir);
1097 }
1098
1099 /**
1100  * Menu callback: Save the selected attachment
1101  * \param mimeview Current display
1102  */
1103 static void mimeview_save_as(MimeView *mimeview)
1104 {
1105         gchar *filename;
1106         gchar *defname = NULL;
1107         gchar *filepath = NULL;
1108         gchar *filedir = NULL;
1109         MimeInfo *partinfo;
1110         const gchar *partname = NULL;
1111
1112         if (!mimeview->opened) return;
1113         if (!mimeview->file) return;
1114
1115         partinfo = mimeview_get_selected_part(mimeview);
1116         if (!partinfo) { 
1117                 partinfo = (MimeInfo *) g_object_get_data
1118                          (G_OBJECT(mimeview->popupmenu),
1119                          "pop_partinfo");
1120                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1121                                   "pop_partinfo", NULL);
1122         }                        
1123         g_return_if_fail(partinfo != NULL);
1124         
1125         if ((partname = get_part_name(partinfo)) != NULL) {
1126                 Xstrdup_a(defname, partname, return);
1127                 subst_for_shellsafe_filename(defname);
1128         }
1129
1130         if (prefs_common.attach_save_dir)
1131                 filepath = g_strconcat(prefs_common.attach_save_dir,
1132                                        G_DIR_SEPARATOR_S, defname, NULL);
1133         else
1134                 filepath = g_strdup(defname);
1135
1136         filename = filesel_select_file_save(_("Save as"), filepath);
1137         if (!filename) {
1138                 g_free(filepath);
1139                 return;
1140         }
1141
1142         mimeview_write_part(filename, partinfo);
1143
1144         filedir = g_dirname(filename);
1145         if (filedir && strcmp(filedir, ".")) {
1146                 if (prefs_common.attach_save_dir)
1147                         g_free(prefs_common.attach_save_dir);
1148                 prefs_common.attach_save_dir = g_strdup(filedir);
1149         }
1150
1151         g_free(filedir);
1152         g_free(filepath);
1153 }
1154
1155 static void mimeview_display_as_text(MimeView *mimeview)
1156 {
1157         MimeInfo *partinfo;
1158
1159         if (!mimeview->opened) return;
1160
1161         partinfo = mimeview_get_selected_part(mimeview);
1162         if (!partinfo)  {
1163                 partinfo = (MimeInfo *) g_object_get_data
1164                         (G_OBJECT(mimeview->popupmenu),
1165                          "pop_partinfo");
1166                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1167                                   "pop_partinfo", NULL);
1168         
1169         }                        
1170         g_return_if_fail(partinfo != NULL);
1171         mimeview_show_message_part(mimeview, partinfo);
1172 }
1173
1174 static void mimeview_launch(MimeView *mimeview)
1175 {
1176         MimeInfo *partinfo;
1177         gchar *filename;
1178
1179         if (!mimeview->opened) return;
1180         if (!mimeview->file) return;
1181
1182         partinfo = mimeview_get_selected_part(mimeview);
1183         if (!partinfo) { 
1184                 partinfo = (MimeInfo *) g_object_get_data
1185                         (G_OBJECT(mimeview->popupmenu),
1186                          "pop_partinfo");
1187                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1188                                   "pop_partinfo", NULL);
1189         }                        
1190         g_return_if_fail(partinfo != NULL);
1191
1192         filename = procmime_get_tmp_file_name(partinfo);
1193
1194         if (procmime_get_part(filename, partinfo) < 0)
1195                 alertpanel_error
1196                         (_("Can't save the part of multipart message."));
1197         else
1198                 mimeview_view_file(filename, partinfo, NULL);
1199
1200         g_free(filename);
1201 }
1202
1203 static void mimeview_open_with(MimeView *mimeview)
1204 {
1205         MimeInfo *partinfo;
1206         gchar *filename;
1207         gchar *cmd;
1208
1209         if (!mimeview->opened) return;
1210         if (!mimeview->file) return;
1211
1212         partinfo = mimeview_get_selected_part(mimeview);
1213         if (!partinfo) { 
1214                 partinfo = (MimeInfo *) g_object_get_data
1215                         (G_OBJECT(mimeview->popupmenu),
1216                          "pop_partinfo");
1217                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1218                                   "pop_partinfo", NULL);
1219         }                        
1220         g_return_if_fail(partinfo != NULL);
1221
1222         filename = procmime_get_tmp_file_name(partinfo);
1223
1224         if (procmime_get_part(filename, partinfo) < 0) {
1225                 alertpanel_error
1226                         (_("Can't save the part of multipart message."));
1227                 g_free(filename);
1228                 return;
1229         }
1230
1231         if (!prefs_common.mime_open_cmd_history)
1232                 prefs_common.mime_open_cmd_history =
1233                         add_history(NULL, prefs_common.mime_open_cmd);
1234
1235         cmd = input_dialog_combo
1236                 (_("Open with"),
1237                  _("Enter the command line to open file:\n"
1238                    "(`%s' will be replaced with file name)"),
1239                  prefs_common.mime_open_cmd,
1240                  prefs_common.mime_open_cmd_history,
1241                  TRUE);
1242         if (cmd) {
1243                 mimeview_view_file(filename, partinfo, cmd);
1244                 g_free(prefs_common.mime_open_cmd);
1245                 prefs_common.mime_open_cmd = cmd;
1246                 prefs_common.mime_open_cmd_history =
1247                         add_history(prefs_common.mime_open_cmd_history, cmd);
1248         }
1249
1250         g_free(filename);
1251 }
1252
1253 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1254                                const gchar *cmdline)
1255 {
1256         static gchar *default_image_cmdline = "display '%s'";
1257         static gchar *default_audio_cmdline = "play '%s'";
1258         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1259         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1260         gchar buf[1024];
1261         gchar m_buf[1024];
1262         const gchar *cmd;
1263         const gchar *def_cmd;
1264         const gchar *p;
1265
1266         if (cmdline) {
1267                 cmd = cmdline;
1268                 def_cmd = NULL;
1269         } else if (MIMETYPE_APPLICATION == partinfo->type &&
1270                    !g_strcasecmp(partinfo->subtype, "octet-stream")) {
1271                 return;
1272         } else if (MIMETYPE_IMAGE == partinfo->type) {
1273                 cmd = prefs_common.mime_image_viewer;
1274                 def_cmd = default_image_cmdline;
1275         } else if (MIMETYPE_AUDIO == partinfo->type) {
1276                 cmd = prefs_common.mime_audio_player;
1277                 def_cmd = default_audio_cmdline;
1278         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1279                 cmd = prefs_common.uri_cmd;
1280                 def_cmd = default_html_cmdline;
1281         } else {
1282                 gchar *content_type;
1283                 
1284                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
1285                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1286                            content_type, "%s");
1287                 g_free(content_type);
1288                 cmd = m_buf;
1289                 def_cmd = NULL;
1290         }
1291
1292         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1293             !strchr(p + 2, '%'))
1294                 g_snprintf(buf, sizeof(buf), cmd, filename);
1295         else {
1296                 if (cmd)
1297                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1298                 if (def_cmd)
1299                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1300                 else
1301                         return;
1302         }
1303
1304         execute_command_line(buf, TRUE);
1305 }
1306
1307 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1308 {
1309         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1310 }
1311
1312 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1313 {
1314         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1315 }
1316
1317 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1318 {
1319         GSList *mimeview_list, *viewer_list;
1320
1321         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1322                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1323                 
1324                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1325                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1326                         mimeview->mimeviewer = NULL;
1327                 }
1328
1329                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1330                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1331
1332                         mimeviewer->destroy_viewer(mimeviewer);
1333                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1334                 }
1335         }
1336
1337         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1338 }
1339
1340 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1341 {
1342         gint      num;
1343         MimeInfo *partinfo;
1344
1345         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1346         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1347
1348         icon_selected(mimeview, num, partinfo);
1349         gtk_widget_grab_focus(button);
1350         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1351                 toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1352                 if (event->button == 2 || event->button == 3)
1353                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
1354                                                      TRUE);
1355         }
1356
1357         part_button_pressed(mimeview, event, partinfo);
1358
1359         return FALSE;
1360 }
1361
1362 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1363 {
1364         GtkCTreeNode *node;
1365         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1366         if (node)
1367                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1368 }               
1369
1370 #undef  KEY_PRESS_EVENT_STOP
1371 #define KEY_PRESS_EVENT_STOP() \
1372         g_signal_stop_emission_by_name(G_OBJECT(button), \
1373                                        "key_press_event");
1374
1375 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1376                              MimeView *mimeview)
1377 {
1378         gint          num;
1379         MimeInfo     *partinfo;
1380         SummaryView  *summaryview;
1381         TextView     *textview;
1382
1383         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1384         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1385         
1386         if (!event) return FALSE;
1387
1388         textview = mimeview->textview;
1389
1390         switch (event->keyval) {
1391         case GDK_space:
1392                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1393                         /* stop the button being untoggled */
1394                         KEY_PRESS_EVENT_STOP();
1395                         if (textview_scroll_page(textview, FALSE))
1396                                 return TRUE;
1397
1398                         if (icon_list_select_by_number(mimeview, num + 1))
1399                                 return TRUE;
1400                 } else {
1401                         icon_selected(mimeview, num, partinfo);
1402                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1403                         return TRUE;
1404                 }
1405
1406                 break;
1407         case GDK_BackSpace:
1408                 textview_scroll_page(textview, TRUE);
1409                 return TRUE;
1410         case GDK_Return:
1411                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1412                         KEY_PRESS_EVENT_STOP();
1413                         textview_scroll_one_line(textview,
1414                                                  (event->state & GDK_MOD1_MASK) != 0);
1415                         return TRUE;
1416                 } else {
1417                         icon_selected(mimeview, num, partinfo);
1418                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1419                         return TRUE;
1420                 }
1421
1422         case GDK_n:
1423         case GDK_N:
1424                 BREAK_ON_MODIFIER_KEY();
1425                 if (icon_list_select_by_number(mimeview, num + 1)) {
1426                         KEY_PRESS_EVENT_STOP();
1427                         return TRUE;
1428                 }
1429                 break;
1430                 
1431         case GDK_p:
1432         case GDK_P:
1433                 BREAK_ON_MODIFIER_KEY();
1434                 if (icon_list_select_by_number(mimeview, num - 1)) {
1435                         KEY_PRESS_EVENT_STOP();
1436                         return TRUE;
1437                 }
1438                 break;
1439
1440         case GDK_y:
1441                 BREAK_ON_MODIFIER_KEY();
1442                 KEY_PRESS_EVENT_STOP();
1443                 mimeview_save_as(mimeview);
1444                 return TRUE;
1445         case GDK_t:
1446                 BREAK_ON_MODIFIER_KEY();
1447                 KEY_PRESS_EVENT_STOP();
1448                 mimeview_display_as_text(mimeview);
1449                 return TRUE;    
1450         case GDK_l:
1451                 BREAK_ON_MODIFIER_KEY();
1452                 KEY_PRESS_EVENT_STOP();
1453                 mimeview_launch(mimeview);
1454                 return TRUE;
1455         case GDK_o:
1456                 BREAK_ON_MODIFIER_KEY();
1457                 KEY_PRESS_EVENT_STOP();
1458                 mimeview_open_with(mimeview);
1459                 return TRUE;
1460         default:
1461                 break;
1462         }
1463
1464         if (!mimeview->messageview->mainwin) return FALSE;
1465         summaryview = mimeview->messageview->mainwin->summaryview;
1466         summary_pass_key_press_event(summaryview, event);
1467         return TRUE;
1468 }
1469
1470 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1471 {
1472         GList *child;
1473         
1474         child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
1475         for (; child != NULL; child = g_list_next(child)) {
1476                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1477                     GTK_TOGGLE_BUTTON(child->data) != button &&
1478                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1479                         gtk_toggle_button_set_active
1480                                 (GTK_TOGGLE_BUTTON(child->data),
1481                                  FALSE);
1482         }
1483 }
1484
1485 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1486 {
1487         GtkWidget *pixmap;
1488         GtkWidget *vbox;
1489         GtkWidget *button;
1490         gchar *tip;
1491         gchar *tiptmp;
1492         const gchar *desc = NULL; 
1493         gchar *sigshort = NULL;
1494         gchar *content_type;
1495         StockPixmap stockp;
1496         MimeInfo *partinfo;
1497         MimeInfo *siginfo = NULL;
1498         MimeInfo *encrypted = NULL;
1499         
1500         vbox = mimeview->icon_vbox;
1501         mimeview->icon_count++;
1502         button = gtk_toggle_button_new();
1503         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1504         g_object_set_data(G_OBJECT(button), "icon_number", 
1505                           GINT_TO_POINTER(mimeview->icon_count));
1506         g_object_set_data(G_OBJECT(button), "partinfo", 
1507                           mimeinfo);
1508         
1509         switch (mimeinfo->type) {
1510                 
1511         case MIMETYPE_TEXT:
1512                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "html"))
1513                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1514                 else if  (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "enriched"))
1515                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1516                 else
1517                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1518                 break;
1519         case MIMETYPE_MESSAGE:
1520                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1521                 break;
1522         case MIMETYPE_APPLICATION:
1523                 stockp = STOCK_PIXMAP_MIME_APPLICATION;
1524                 break;
1525         case MIMETYPE_IMAGE:
1526                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1527                 break;
1528         case MIMETYPE_AUDIO:
1529                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1530                 break;
1531         default:
1532                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1533                 break;
1534         }
1535         
1536         partinfo = mimeinfo;
1537         while (partinfo != NULL) {
1538                 if (privacy_mimeinfo_is_signed(partinfo)) {
1539                         siginfo = partinfo;
1540                         break;
1541                 }
1542                 if (privacy_mimeinfo_is_encrypted(partinfo)) {
1543                         encrypted = partinfo;
1544                         break;
1545                 }
1546                 partinfo = procmime_mimeinfo_parent(partinfo);
1547         }       
1548
1549         if (siginfo != NULL) {
1550                 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
1551                 case SIGNATURE_UNCHECKED:
1552                 case SIGNATURE_CHECK_FAILED:
1553                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1554                             STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1555                         break;
1556                 case SIGNATURE_OK:
1557                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1558                             STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1559                         break;
1560                 case SIGNATURE_WARN:
1561                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1562                             STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
1563                         break;
1564                 case SIGNATURE_INVALID:
1565                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1566                             STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1567                         break;
1568                 }
1569                 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
1570         } else if (encrypted != NULL) {
1571                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1572                             STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);         
1573         } else {
1574                 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
1575                                                           OVERLAY_NONE, 6, 3);
1576         }
1577         gtk_container_add(GTK_CONTAINER(button), pixmap);
1578         
1579         if (!desc) {
1580                 if (prefs_common.attach_desc)
1581                         desc = get_part_description(mimeinfo);
1582                 else
1583                         desc = get_part_name(mimeinfo);
1584         }
1585
1586         content_type = procmime_get_content_type_str(mimeinfo->type,
1587                                                      mimeinfo->subtype);
1588
1589         tip = g_strjoin("\n", content_type,
1590                         to_human_readable(mimeinfo->length), NULL);
1591         g_free(content_type);
1592         if (desc && *desc) {
1593                 tiptmp = g_strjoin("\n", desc, tip, NULL);
1594                 g_free(tip);
1595                 tip = tiptmp;
1596         }
1597         if (sigshort && *sigshort) {
1598                 tiptmp = g_strjoin("\n", tip, sigshort, NULL);
1599                 g_free(tip);
1600                 tip = tiptmp;
1601         }
1602         g_free(sigshort);
1603
1604         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1605         g_free(tip);
1606         gtk_widget_show_all(button);
1607         g_signal_connect(G_OBJECT(button), "button_release_event", 
1608                          G_CALLBACK(icon_clicked_cb), mimeview);
1609         g_signal_connect(G_OBJECT(button), "key_press_event", 
1610                          G_CALLBACK(icon_key_pressed), mimeview);
1611         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1612
1613 }
1614
1615 static void icon_list_clear (MimeView *mimeview)
1616 {
1617         GList     *child;
1618         GtkAdjustment *adj;
1619         
1620         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1621         for (; child != NULL; child = g_list_next(child)) {
1622                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1623                                        GTK_WIDGET(child->data));
1624         }
1625         mimeview->icon_count = 0;
1626         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1627         adj->value = adj->lower;
1628 }
1629
1630 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
1631                                           MimeInfo      *mimeinfo)
1632 {
1633         GList *child;
1634         
1635         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1636         for (; child != NULL; child = g_list_next(child)) {
1637                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1638                     g_object_get_data(G_OBJECT(child->data),
1639                                       "partinfo") == (gpointer)mimeinfo) {
1640                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1641                         gtk_toggle_button_set_active
1642                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1643                 }                                
1644         }
1645 }
1646
1647 /*!
1648  *\brief        Used to 'click' the next or previous icon.
1649  *
1650  *\return       true if the icon 'number' exists and was selected.
1651  */
1652 static gboolean icon_list_select_by_number(MimeView     *mimeview,
1653                                            gint          number)
1654 {
1655         GList *child;
1656
1657         if (number == 0) return FALSE;
1658         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1659         for (; child != NULL; child = g_list_next(child)) {
1660                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1661                     GPOINTER_TO_INT(g_object_get_data(G_OBJECT(child->data),
1662                                         "icon_number")) == number) {
1663                         icon_selected(mimeview, number,
1664                                       (MimeInfo*)g_object_get_data(G_OBJECT(child->data),
1665                                                                    "partinfo"));
1666                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1667                         gtk_toggle_button_set_active
1668                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1669                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
1670                 
1671                         return TRUE;
1672                 }                                
1673         }
1674         return FALSE;
1675 }
1676
1677 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1678                                          GtkAllocation *size, MimeView *mimeview)
1679 {
1680         GtkAllocation *mainbox_size;
1681         GtkAllocation *vbox_size;
1682         GtkAllocation *layout_size;
1683         GtkAdjustment *adj;
1684         
1685         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1686
1687         mainbox_size = &mimeview->icon_mainbox->allocation;
1688         vbox_size = &mimeview->icon_vbox->allocation;
1689         layout_size = &mimeview->icon_scroll->allocation;
1690                 
1691         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1692                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1693                             MAX(vbox_size->height, layout_size->height));
1694         adj->step_increment = 5;
1695 }
1696
1697 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1698 {
1699         GtkRequisition size;
1700         gint           width;
1701         g_return_if_fail(mimeinfo != NULL);
1702
1703         while (mimeinfo != NULL) {
1704                 if (mimeinfo->type != MIMETYPE_MULTIPART)
1705                         icon_list_append_icon(mimeview, mimeinfo);
1706                 if (mimeinfo->node->children != NULL)
1707                         icon_list_create(mimeview, (MimeInfo *) mimeinfo->node->children->data);
1708                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
1709         }
1710         gtk_widget_size_request(mimeview->icon_vbox, &size);
1711         width = size.width + 4;
1712         if (width > mimeview->icon_mainbox->requisition.width) {
1713                 gtk_widget_set_size_request(mimeview->icon_mainbox, 
1714                                             width, -1);
1715         }
1716
1717 }
1718
1719 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1720 {
1721         gtk_widget_ref(button); 
1722
1723         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1724                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1725                                         GTK_SHADOW_NONE);
1726                 gtk_widget_hide(mimeview->icon_mainbox);
1727                 gtk_widget_show(mimeview->ctree_mainbox);
1728                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1729
1730                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1731                                         button);
1732                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1733                                    button, FALSE, FALSE, 0);
1734                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
1735         } else {
1736                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1737                               GTK_SHADOW_NONE);
1738                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1739                 gtk_widget_hide(mimeview->ctree_mainbox);
1740                 gtk_widget_show(mimeview->icon_mainbox);
1741                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1742
1743                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1744                                         button);
1745                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1746                                    button, FALSE, FALSE, 0);
1747                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1748                 if (mimeview->opened)
1749                         icon_list_toggle_by_mime_info
1750                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
1751                                                                        mimeview->opened));
1752
1753                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
1754         }
1755         gtk_widget_grab_focus(button);
1756         gtk_widget_unref(button);
1757
1758 }
1759
1760 void mimeview_update (MimeView *mimeview) {
1761         if (mimeview && mimeview->mimeinfo) {
1762                 icon_list_clear(mimeview);
1763                 icon_list_create(mimeview, mimeview->mimeinfo);
1764         }
1765 }