Fix SSL certificate list not shown; Fix clicks in mimetree
[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 gboolean 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         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
216                          G_CALLBACK(mimeview_selected), mimeview);
217         g_signal_connect(G_OBJECT(ctree), "button_press_event",
218                          G_CALLBACK(mimeview_button_pressed), mimeview);
219         g_signal_connect(G_OBJECT(ctree), "key_press_event",
220                          G_CALLBACK(mimeview_key_pressed), mimeview);
221         g_signal_connect(G_OBJECT (ctree),"start_drag",
222                          G_CALLBACK (mimeview_start_drag), mimeview);
223         g_signal_connect(G_OBJECT(ctree), "drag_data_get",
224                          G_CALLBACK(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         g_signal_connect(G_OBJECT(mime_toggle), "toggled", 
246                          G_CALLBACK(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         g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate", 
254                          G_CALLBACK(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         g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
336                                         mimeview);
337
338         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
339         icon_list_create(mimeview, mimeinfo);
340
341         g_signal_handlers_unblock_by_func(G_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 FALSE;
792 }
793
794 static gboolean 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                 return TRUE;
802         } else if (event->button == 3) {
803                 if (partinfo && (partinfo->type == MIMETYPE_TEXT ||
804                                  partinfo->type == MIMETYPE_MESSAGE ||
805                                  partinfo->type == MIMETYPE_IMAGE ||
806                                  partinfo->type == MIMETYPE_MULTIPART))
807                         menu_set_sensitive(mimeview->popupfactory,
808                                            "/Display as text", FALSE);
809                 else
810                         menu_set_sensitive(mimeview->popupfactory,
811                                            "/Display as text", TRUE);
812                 if (partinfo &&
813                     partinfo->type == MIMETYPE_APPLICATION &&
814                     !g_strcasecmp(partinfo->subtype, "octet-stream"))
815                         menu_set_sensitive(mimeview->popupfactory,
816                                            "/Open", FALSE);
817                 else
818                         menu_set_sensitive(mimeview->popupfactory,
819                                            "/Open", TRUE);
820
821                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
822                                     "pop_partinfo", partinfo);
823                                     
824                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
825                                NULL, NULL, NULL, NULL,
826                                event->button, event->time);
827                 return TRUE;
828         }
829
830         return FALSE;
831 }
832
833
834 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
835 {
836         mimeview_key_pressed(mimeview->ctree, event, mimeview);
837 }
838
839 #define BREAK_ON_MODIFIER_KEY() \
840         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
841
842 #warning FIXME_GTK2
843 #if 0
844 #define KEY_PRESS_EVENT_STOP() \
845         if (gtk_signal_n_emissions_by_name \
846                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
847                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
848                                              "key_press_event"); \
849         }
850 #else
851 #define KEY_PRESS_EVENT_STOP()
852 #endif
853
854 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
855                                  MimeView *mimeview)
856 {
857         SummaryView *summaryview;
858         GtkCTree *ctree = GTK_CTREE(widget);
859         GtkCTreeNode *node;
860
861         if (!event) return FALSE;
862         if (!mimeview->opened) return FALSE;
863
864         switch (event->keyval) {
865         case GDK_space:
866                 if (textview_scroll_page(mimeview->textview, FALSE))
867                         return TRUE;
868
869                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
870                 if (node) {
871                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
872                         gtk_sctree_select(GTK_SCTREE(ctree), node);
873                         return TRUE;
874                 }
875                 break;
876         case GDK_BackSpace:
877                 textview_scroll_page(mimeview->textview, TRUE);
878                 return TRUE;
879         case GDK_Return:
880                 textview_scroll_one_line(mimeview->textview,
881                                          (event->state & GDK_MOD1_MASK) != 0);
882                 return TRUE;
883         case GDK_n:
884         case GDK_N:
885                 BREAK_ON_MODIFIER_KEY();
886                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
887                 KEY_PRESS_EVENT_STOP();
888                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
889                                         GTK_SCROLL_STEP_FORWARD, 0.0);
890                 return TRUE;
891         case GDK_p:
892         case GDK_P:
893                 BREAK_ON_MODIFIER_KEY();
894                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
895                 KEY_PRESS_EVENT_STOP();
896                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
897                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
898                 return TRUE;
899         case GDK_y:
900                 BREAK_ON_MODIFIER_KEY();
901                 KEY_PRESS_EVENT_STOP();
902                 mimeview_save_as(mimeview);
903                 return TRUE;
904         case GDK_t:
905                 BREAK_ON_MODIFIER_KEY();
906                 KEY_PRESS_EVENT_STOP();
907                 mimeview_display_as_text(mimeview);
908                 return TRUE;    
909         case GDK_l:
910                 BREAK_ON_MODIFIER_KEY();
911                 KEY_PRESS_EVENT_STOP();
912                 mimeview_launch(mimeview);
913                 return TRUE;
914         case GDK_o:
915                 BREAK_ON_MODIFIER_KEY();
916                 KEY_PRESS_EVENT_STOP();
917                 mimeview_open_with(mimeview);
918                 return TRUE;
919         default:
920                 break;
921         }
922
923         if (!mimeview->messageview->mainwin) return FALSE;
924         summaryview = mimeview->messageview->mainwin->summaryview;
925         summary_pass_key_press_event(summaryview, event);
926         return TRUE;
927 }
928
929 static void mimeview_drag_data_get(GtkWidget        *widget,
930                                    GdkDragContext   *drag_context,
931                                    GtkSelectionData *selection_data,
932                                    guint             info,
933                                    guint             time,
934                                    MimeView         *mimeview)
935 {
936         gchar *filename, *uriname;
937         MimeInfo *partinfo;
938
939         if (!mimeview->opened) return;
940         if (!mimeview->file) return;
941
942         partinfo = mimeview_get_selected_part(mimeview);
943         if (!partinfo) return;
944
945         filename = g_basename(get_part_name(partinfo));
946         if (*filename == '\0') return;
947
948         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
949                                filename, NULL);
950
951         if (procmime_get_part(filename, partinfo) < 0)
952                 alertpanel_error
953                         (_("Can't save the part of multipart message."));
954
955         uriname = g_strconcat("file:/", filename, NULL);
956         gtk_selection_data_set(selection_data, selection_data->target, 8,
957                                uriname, strlen(uriname));
958
959         g_free(uriname);
960         g_free(filename);
961 }
962
963 static void mimeview_save_all(MimeView *mimeview)
964 {
965         gchar *dirname;
966         gchar *defname = NULL;
967         MimeInfo *attachment;
968         gchar buf[1024];
969
970         if (!mimeview->opened) return;
971         if (!mimeview->file) return;
972
973         attachment = mimeview->mimeinfo;
974         g_return_if_fail(attachment != NULL);
975
976         dirname = filesel_select_file(_("Save as"), defname);
977         if (!dirname) return;
978
979         if (!is_dir_exist (dirname)) {
980                 alertpanel_error(_("`%s' is not a directory."),
981                                  dirname);
982                 g_free (dirname);
983                 dirname = NULL;
984                 return;
985         }
986         
987         { /* add a / after the dirname, in case the user didn't */
988                 gchar *dirname_tmp = NULL;
989                 int dirname_last_char = strlen (dirname) - 1;
990
991                 if (dirname[dirname_last_char] != G_DIR_SEPARATOR) {
992                         dirname_tmp = g_strconcat (dirname, G_DIR_SEPARATOR_S, NULL);
993                         g_free (dirname);
994                         dirname = dirname_tmp;
995                 }
996         }
997
998         /* for each attachment, extract it in the selected dir. */
999         while (attachment != NULL) {
1000                 if (attachment->type != MIMETYPE_MESSAGE &&
1001                     attachment->type != MIMETYPE_MULTIPART &&
1002                     attachment->disposition != DISPOSITIONTYPE_INLINE) {
1003                         static guint subst_cnt = 1;
1004                         gchar *attachdir;
1005                         gchar *attachname = g_strdup(get_part_name(attachment));
1006                         AlertValue aval = G_ALERTDEFAULT;
1007                         gchar *res;
1008
1009                         if (!attachname || !strlen(attachname))
1010                                 attachname = g_strdup_printf("noname.%d",subst_cnt++);
1011                         subst_chars(attachname, ":?*&|<>\t\r\n", '_');
1012                         g_snprintf(buf, sizeof(buf), "%s%s",
1013                                    dirname,
1014                                    (attachname[0] == G_DIR_SEPARATOR)
1015                                    ? &attachname[1]
1016                                    : attachname);
1017                         subst_chars(buf, "/\\", G_DIR_SEPARATOR);
1018                         attachdir = g_dirname(buf);
1019                         make_dir_hier(attachdir);
1020                         g_free(attachdir);
1021                         
1022                         if (is_file_exist(buf)) {
1023                                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1024                                                       attachname);
1025                                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
1026                                                   _("Cancel"), NULL);
1027                                 g_free(res);                                      
1028                         }
1029                         g_free(attachname);
1030                         
1031                         if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, attachment) < 0))
1032                                 alertpanel_error(_("Can't save the part of multipart message."));
1033                 }
1034                 attachment = procmime_mimeinfo_next(attachment);
1035         }
1036 }
1037
1038 static void mimeview_display_as_text(MimeView *mimeview)
1039 {
1040         MimeInfo *partinfo;
1041
1042         if (!mimeview->opened) return;
1043
1044         partinfo = mimeview_get_selected_part(mimeview);
1045         if (!partinfo)  {
1046                 partinfo = (MimeInfo *) gtk_object_get_data
1047                         (GTK_OBJECT(mimeview->popupmenu),
1048                          "pop_partinfo");
1049                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1050                                     "pop_partinfo", NULL);
1051         
1052         }                        
1053         g_return_if_fail(partinfo != NULL);
1054         mimeview_show_message_part(mimeview, partinfo);
1055 }
1056
1057 static void mimeview_save_as(MimeView *mimeview)
1058 {
1059         gchar *filename;
1060         gchar *defname = NULL;
1061         MimeInfo *partinfo;
1062         gchar *res;
1063         const gchar *partname = NULL;
1064
1065         if (!mimeview->opened) return;
1066         if (!mimeview->file) return;
1067
1068         partinfo = mimeview_get_selected_part(mimeview);
1069         if (!partinfo) { 
1070                 partinfo = (MimeInfo *) gtk_object_get_data
1071                         (GTK_OBJECT(mimeview->popupmenu),
1072                          "pop_partinfo");
1073                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1074                                     "pop_partinfo", NULL);
1075         }                        
1076         g_return_if_fail(partinfo != NULL);
1077         
1078         if ((partname = get_part_name(partinfo)) != NULL) {
1079                 Xstrdup_a(defname, partname, return);
1080                 subst_for_filename(defname);
1081         }
1082
1083         filename = filesel_select_file(_("Save as"), defname);
1084         if (!filename) return;
1085         if (is_file_exist(filename)) {
1086                 AlertValue aval;
1087                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1088                                       filename);
1089                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
1090                                   _("Cancel"), NULL);
1091                 g_free(res);                                      
1092                 if (G_ALERTDEFAULT != aval) return;
1093         }
1094
1095         if (procmime_get_part(filename, partinfo) < 0)
1096                 alertpanel_error
1097                         (_("Can't save the part of multipart message."));
1098 }
1099
1100 static void mimeview_launch(MimeView *mimeview)
1101 {
1102         MimeInfo *partinfo;
1103         gchar *filename;
1104
1105         if (!mimeview->opened) return;
1106         if (!mimeview->file) return;
1107
1108         partinfo = mimeview_get_selected_part(mimeview);
1109         if (!partinfo) { 
1110                 partinfo = (MimeInfo *) gtk_object_get_data
1111                         (GTK_OBJECT(mimeview->popupmenu),
1112                          "pop_partinfo");
1113                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1114                                     "pop_partinfo", NULL);
1115         }                        
1116         g_return_if_fail(partinfo != NULL);
1117
1118         filename = procmime_get_tmp_file_name(partinfo);
1119
1120         if (procmime_get_part(filename, partinfo) < 0)
1121                 alertpanel_error
1122                         (_("Can't save the part of multipart message."));
1123         else
1124                 mimeview_view_file(filename, partinfo, NULL);
1125
1126         g_free(filename);
1127 }
1128
1129 static void mimeview_open_with(MimeView *mimeview)
1130 {
1131         MimeInfo *partinfo;
1132         gchar *filename;
1133         gchar *cmd;
1134
1135         if (!mimeview->opened) return;
1136         if (!mimeview->file) return;
1137
1138         partinfo = mimeview_get_selected_part(mimeview);
1139         if (!partinfo) { 
1140                 partinfo = (MimeInfo *) gtk_object_get_data
1141                         (GTK_OBJECT(mimeview->popupmenu),
1142                          "pop_partinfo");
1143                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1144                                     "pop_partinfo", NULL);
1145         }                        
1146         g_return_if_fail(partinfo != NULL);
1147
1148         filename = procmime_get_tmp_file_name(partinfo);
1149
1150         if (procmime_get_part(filename, partinfo) < 0) {
1151                 alertpanel_error
1152                         (_("Can't save the part of multipart message."));
1153                 g_free(filename);
1154                 return;
1155         }
1156
1157         if (!prefs_common.mime_open_cmd_history)
1158                 prefs_common.mime_open_cmd_history =
1159                         add_history(NULL, prefs_common.mime_open_cmd);
1160
1161         cmd = input_dialog_combo
1162                 (_("Open with"),
1163                  _("Enter the command line to open file:\n"
1164                    "(`%s' will be replaced with file name)"),
1165                  prefs_common.mime_open_cmd,
1166                  prefs_common.mime_open_cmd_history,
1167                  TRUE);
1168         if (cmd) {
1169                 mimeview_view_file(filename, partinfo, cmd);
1170                 g_free(prefs_common.mime_open_cmd);
1171                 prefs_common.mime_open_cmd = cmd;
1172                 prefs_common.mime_open_cmd_history =
1173                         add_history(prefs_common.mime_open_cmd_history, cmd);
1174         }
1175
1176         g_free(filename);
1177 }
1178
1179 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1180                                const gchar *cmdline)
1181 {
1182         static gchar *default_image_cmdline = "display '%s'";
1183         static gchar *default_audio_cmdline = "play '%s'";
1184         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1185         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1186         gchar buf[1024];
1187         gchar m_buf[1024];
1188         const gchar *cmd;
1189         const gchar *def_cmd;
1190         const gchar *p;
1191
1192         if (cmdline) {
1193                 cmd = cmdline;
1194                 def_cmd = NULL;
1195         } else if (MIMETYPE_APPLICATION == partinfo->type &&
1196                    !g_strcasecmp(partinfo->subtype, "octet-stream")) {
1197                 return;
1198         } else if (MIMETYPE_IMAGE == partinfo->type) {
1199                 cmd = prefs_common.mime_image_viewer;
1200                 def_cmd = default_image_cmdline;
1201         } else if (MIMETYPE_AUDIO == partinfo->type) {
1202                 cmd = prefs_common.mime_audio_player;
1203                 def_cmd = default_audio_cmdline;
1204         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1205                 cmd = prefs_common.uri_cmd;
1206                 def_cmd = default_html_cmdline;
1207         } else {
1208                 gchar *content_type;
1209                 
1210                 content_type = g_strdup_printf("%s/%s", procmime_get_type_str(partinfo->type), partinfo->subtype);
1211                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1212                            content_type, "%s");
1213                 g_free(content_type);
1214                 cmd = m_buf;
1215                 def_cmd = NULL;
1216         }
1217
1218         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1219             !strchr(p + 2, '%'))
1220                 g_snprintf(buf, sizeof(buf), cmd, filename);
1221         else {
1222                 if (cmd)
1223                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1224                 if (def_cmd)
1225                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1226                 else
1227                         return;
1228         }
1229
1230         execute_command_line(buf, TRUE);
1231 }
1232
1233 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1234 {
1235         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1236 }
1237
1238 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1239 {
1240         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1241 }
1242
1243 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1244 {
1245         GSList *mimeview_list, *viewer_list;
1246
1247         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1248                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1249                 
1250                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1251                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1252                         mimeview->mimeviewer = NULL;
1253                 }
1254
1255                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1256                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1257
1258                         mimeviewer->destroy_viewer(mimeviewer);
1259                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1260                 }
1261         }
1262
1263         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1264 }
1265
1266 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1267 {
1268         gint      num;
1269         MimeInfo *partinfo;
1270
1271         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1272         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1273
1274         icon_selected(mimeview, num, partinfo);
1275         gtk_widget_grab_focus(button);
1276         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1277                 toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1278                 if (event->button == 2 || event->button == 3)
1279                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
1280                                                      TRUE);
1281         } else {
1282                 gtk_signal_emit_stop_by_name(GTK_OBJECT(button), "button_press_event");
1283         }
1284
1285         part_button_pressed(mimeview, event, partinfo);
1286
1287         return FALSE;
1288 }
1289
1290 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1291 {
1292         GtkCTreeNode *node;
1293         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1294         if (node)
1295                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1296 }               
1297
1298 #warning FIXME_GTK2
1299 #if 0
1300 #undef  KEY_PRESS_EVENT_STOP
1301 #define KEY_PRESS_EVENT_STOP() \
1302         if (gtk_signal_n_emissions_by_name \
1303                 (GTK_OBJECT(button), "key_press_event") > 0) { \
1304                 gtk_signal_emit_stop_by_name(GTK_OBJECT(button), \
1305                                              "key_press_event"); \
1306         }
1307 #endif
1308 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1309                              MimeView *mimeview)
1310 {
1311         gint          num;
1312         MimeInfo     *partinfo;
1313         SummaryView  *summaryview;
1314         TextView     *textview;
1315
1316         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1317         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1318         
1319         if (!event) return FALSE;
1320
1321         textview = mimeview->textview;
1322
1323         switch (event->keyval) {
1324         case GDK_space:
1325                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1326                         /* stop the button being untoggled */
1327                         KEY_PRESS_EVENT_STOP();
1328                         if (textview_scroll_page(textview, FALSE))
1329                                 return TRUE;
1330
1331                         if (icon_list_select_by_number(mimeview, num + 1))
1332                                 return TRUE;
1333                 } else {
1334                         icon_selected(mimeview, num, partinfo);
1335                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1336                         return TRUE;
1337                 }
1338
1339                 break;
1340         case GDK_BackSpace:
1341                 textview_scroll_page(textview, TRUE);
1342                 return TRUE;
1343         case GDK_Return:
1344                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1345                         KEY_PRESS_EVENT_STOP();
1346                         textview_scroll_one_line(textview,
1347                                                  (event->state & GDK_MOD1_MASK) != 0);
1348                         return TRUE;
1349                 } else {
1350                         icon_selected(mimeview, num, partinfo);
1351                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1352                         return TRUE;
1353                 }
1354
1355         case GDK_n:
1356         case GDK_N:
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_p:
1365         case GDK_P:
1366                 BREAK_ON_MODIFIER_KEY();
1367                 if (icon_list_select_by_number(mimeview, num - 1)) {
1368                         KEY_PRESS_EVENT_STOP();
1369                         return TRUE;
1370                 }
1371                 break;
1372
1373         case GDK_y:
1374                 BREAK_ON_MODIFIER_KEY();
1375                 KEY_PRESS_EVENT_STOP();
1376                 mimeview_save_as(mimeview);
1377                 return TRUE;
1378         case GDK_t:
1379                 BREAK_ON_MODIFIER_KEY();
1380                 KEY_PRESS_EVENT_STOP();
1381                 mimeview_display_as_text(mimeview);
1382                 return TRUE;    
1383         case GDK_l:
1384                 BREAK_ON_MODIFIER_KEY();
1385                 KEY_PRESS_EVENT_STOP();
1386                 mimeview_launch(mimeview);
1387                 return TRUE;
1388         case GDK_o:
1389                 BREAK_ON_MODIFIER_KEY();
1390                 KEY_PRESS_EVENT_STOP();
1391                 mimeview_open_with(mimeview);
1392                 return TRUE;
1393         default:
1394                 break;
1395         }
1396
1397         if (!mimeview->messageview->mainwin) return FALSE;
1398         summaryview = mimeview->messageview->mainwin->summaryview;
1399         summary_pass_key_press_event(summaryview, event);
1400         return TRUE;
1401 }
1402
1403 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1404 {
1405         GList *child;
1406         
1407         child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
1408         for (; child != NULL; child = g_list_next(child)) {
1409                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1410                     GTK_TOGGLE_BUTTON(child->data) != button &&
1411                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1412                         gtk_toggle_button_set_active
1413                                 (GTK_TOGGLE_BUTTON(child->data),
1414                                  FALSE);
1415         }
1416 }
1417
1418 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1419 {
1420         GtkWidget *pixmap;
1421         GtkWidget *vbox;
1422         GtkWidget *button;
1423         gchar *tip;
1424         const gchar *desc = NULL;
1425         StockPixmap stockp;
1426         
1427         vbox = mimeview->icon_vbox;
1428         mimeview->icon_count++;
1429         button = gtk_toggle_button_new();
1430         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1431         gtk_object_set_data(GTK_OBJECT(button), "icon_number", 
1432                 GINT_TO_POINTER(mimeview->icon_count));
1433         gtk_object_set_data(GTK_OBJECT(button), "partinfo", 
1434                 mimeinfo);
1435         
1436         switch (mimeinfo->type) {
1437                 
1438         case MIMETYPE_TEXT:
1439                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "html"))
1440                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1441                 else if  (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "enriched"))
1442                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1443                 else
1444                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1445                 break;
1446         case MIMETYPE_MESSAGE:
1447                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1448                 break;
1449         case MIMETYPE_APPLICATION:
1450                 stockp = STOCK_PIXMAP_MIME_APPLICATION;
1451                 break;
1452         case MIMETYPE_IMAGE:
1453                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1454                 break;
1455         case MIMETYPE_AUDIO:
1456                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1457                 break;
1458         default:
1459                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1460                 break;
1461         }
1462         
1463         pixmap = stock_pixmap_widget(mimeview->mainwin->window, stockp);
1464         gtk_container_add(GTK_CONTAINER(button), pixmap);
1465         
1466         if (!desc) {
1467                 if (prefs_common.attach_desc)
1468                         desc = get_part_description(mimeinfo);
1469                 else
1470                         desc = get_part_name(mimeinfo);
1471         }
1472
1473         if (desc && *desc)
1474                 tip = g_strdup_printf("%s\n%s/%s\n%s", desc,
1475                                 procmime_get_type_str(mimeinfo->type),
1476                                 mimeinfo->subtype, 
1477                                 to_human_readable(mimeinfo->length));
1478         else            
1479                 tip = g_strdup_printf("%s/%s\n%s",
1480                                 procmime_get_type_str(mimeinfo->type),
1481                                 mimeinfo->subtype, 
1482                                 to_human_readable(mimeinfo->length));
1483
1484         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1485         g_free(tip);
1486         gtk_widget_show_all(button);
1487         g_signal_connect(G_OBJECT(button), "button_press_event", 
1488                          G_CALLBACK(icon_clicked_cb), mimeview);
1489         g_signal_connect(G_OBJECT(button), "key_press_event", 
1490                          G_CALLBACK(icon_key_pressed), mimeview);
1491         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1492
1493 }
1494
1495 static void icon_list_clear (MimeView *mimeview)
1496 {
1497         GList     *child;
1498         GtkAdjustment *adj;
1499         
1500         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1501         for (; child != NULL; child = g_list_next(child)) {
1502                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1503                                        GTK_WIDGET(child->data));
1504         }
1505         mimeview->icon_count = 0;
1506         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1507         adj->value = adj->lower;
1508         gtk_signal_emit_by_name(GTK_OBJECT (adj), "value_changed");
1509 }
1510
1511 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
1512                                           MimeInfo      *mimeinfo)
1513 {
1514         GList *child;
1515         
1516         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1517         for (; child != NULL; child = g_list_next(child)) {
1518                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1519                     gtk_object_get_data(GTK_OBJECT(child->data),
1520                                         "partinfo") == (gpointer)mimeinfo) {
1521                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1522                         gtk_toggle_button_set_active
1523                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1524                 }                                
1525         }
1526 }
1527
1528 /*!
1529  *\brief        Used to 'click' the next or previous icon.
1530  *
1531  *\return       true if the icon 'number' exists and was selected.
1532  */
1533 static gboolean icon_list_select_by_number(MimeView     *mimeview,
1534                                            gint          number)
1535 {
1536         GList *child;
1537
1538         if (number == 0) return FALSE;
1539         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1540         for (; child != NULL; child = g_list_next(child)) {
1541                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1542                     GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(child->data),
1543                                         "icon_number")) == number) {
1544                         icon_selected(mimeview, number,
1545                                       (MimeInfo*)gtk_object_get_data(GTK_OBJECT(child->data),
1546                                                                      "partinfo"));
1547                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1548                         gtk_toggle_button_set_active
1549                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1550                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
1551                 
1552                         return TRUE;
1553                 }                                
1554         }
1555         return FALSE;
1556 }
1557
1558 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1559                                          GtkAllocation *size, MimeView *mimeview)
1560 {
1561         GtkAllocation *mainbox_size;
1562         GtkAllocation *vbox_size;
1563         GtkAllocation *layout_size;
1564         GtkAdjustment *adj;
1565         
1566         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1567
1568         mainbox_size = &mimeview->icon_mainbox->allocation;
1569         vbox_size = &mimeview->icon_vbox->allocation;
1570         layout_size = &mimeview->icon_scroll->allocation;
1571         
1572 #warning FIXME_GTK2 /* this code cause hang up. */
1573 #if 0
1574         /* centralise the vbox */
1575         gtk_layout_move(GTK_LAYOUT(mimeview->icon_scroll), mimeview->icon_vbox, 
1576                         (mainbox_size->width - vbox_size->width)/2, 0);
1577 #endif
1578         
1579         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1580                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1581                             MAX(vbox_size->height, layout_size->height));
1582         adj->step_increment = 5;
1583 }
1584
1585 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1586 {
1587         GtkRequisition size;
1588         gint           width;
1589         g_return_if_fail(mimeinfo != NULL);
1590
1591         while (mimeinfo != NULL) {
1592                 if (mimeinfo->type != MIMETYPE_MULTIPART)
1593                         icon_list_append_icon(mimeview, mimeinfo);
1594                 if (mimeinfo->node->children != NULL)
1595                         icon_list_create(mimeview, (MimeInfo *) mimeinfo->node->children->data);
1596                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
1597         }
1598         gtk_widget_size_request(mimeview->icon_vbox, &size);
1599         width = size.width + 4;
1600         if (width > mimeview->icon_mainbox->requisition.width) {
1601                 gtk_widget_set_size_request(mimeview->icon_mainbox, 
1602                                             width, -1);
1603         }
1604
1605 }
1606
1607 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1608 {
1609         gtk_widget_ref(button); 
1610
1611         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1612                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1613                                         GTK_SHADOW_NONE);
1614                 gtk_widget_hide(mimeview->icon_mainbox);
1615                 gtk_widget_show(mimeview->ctree_mainbox);
1616                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1617
1618                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1619                                         button);
1620                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1621                                    button, FALSE, FALSE, 0);
1622                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
1623         } else {
1624                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1625                               GTK_SHADOW_NONE);
1626                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1627                 gtk_widget_hide(mimeview->ctree_mainbox);
1628                 gtk_widget_show(mimeview->icon_mainbox);
1629                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1630
1631                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1632                                         button);
1633                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1634                                    button, FALSE, FALSE, 0);
1635                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1636                 if (mimeview->opened)
1637                         icon_list_toggle_by_mime_info
1638                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
1639                                                                        mimeview->opened));
1640
1641                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
1642         }
1643         gtk_widget_grab_focus(button);
1644         gtk_widget_unref(button);
1645
1646 }
1647
1648 void mimeview_update (MimeView *mimeview) {
1649         if (mimeview && mimeview->mimeinfo) {
1650                 icon_list_clear(mimeview);
1651                 icon_list_create(mimeview, mimeview->mimeinfo);
1652         }
1653 }