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