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