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