remove redundant check introduced in 0.9.9claws1
[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         icon_list_clear(mimeview);
681         icon_list_create(mimeview, mimeview->mimeinfo);
682 }
683
684 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
685 {
686         MimeView *mimeview = (MimeView *) user_data;
687         gchar *siginfo;
688
689         siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
690         textview_set_text(mimeview->textview, siginfo);
691         g_free(siginfo);
692         noticeview_set_button_text(mimeview->siginfoview, NULL);
693 }
694
695 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
696 {
697         MimeInfo *siginfo;
698
699         g_return_if_fail(mimeview != NULL);
700         g_return_if_fail(selected != NULL);
701         
702         siginfo = selected;
703         while (siginfo != NULL) {
704                 if (privacy_mimeinfo_is_signed(siginfo))
705                         break;
706                 siginfo = procmime_mimeinfo_parent(siginfo);
707         }
708         mimeview->siginfo = siginfo;
709         
710         if (siginfo == NULL) {
711                 noticeview_hide(mimeview->siginfoview);
712                 return;
713         }
714         
715         update_signature_noticeview(mimeview, siginfo);
716         noticeview_show(mimeview->siginfoview);
717 }
718
719 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
720                               MimeView *mimeview)
721 {
722         MimeInfo *partinfo;
723
724         if (mimeview->opened == node) return;
725         mimeview->opened = node;
726         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
727
728         partinfo = gtk_ctree_node_get_row_data(ctree, node);
729         if (!partinfo) return;
730
731         /* ungrab the mouse event */
732         if (GTK_WIDGET_HAS_GRAB(ctree)) {
733                 gtk_grab_remove(GTK_WIDGET(ctree));
734                 if (gdk_pointer_is_grabbed())
735                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
736         }
737         
738         mimeview->textview->default_text = FALSE;
739
740         update_signature_info(mimeview, partinfo);
741
742         if (!mimeview_show_part(mimeview, partinfo)) {
743                 switch (partinfo->type) {
744                 case MIMETYPE_TEXT:
745                 case MIMETYPE_MESSAGE:
746                 case MIMETYPE_MULTIPART:
747                         mimeview_show_message_part(mimeview, partinfo);
748                 
749                         break;
750                 default:
751                         mimeview->textview->default_text = TRUE;        
752                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
753                         textview_show_mime_part(mimeview->textview, partinfo);
754                         break;
755                 }
756         }
757 }
758
759 static void mimeview_start_drag(GtkWidget *widget, gint button,
760                                 GdkEvent *event, MimeView *mimeview)
761 {
762         GdkDragContext *context;
763         MimeInfo *partinfo;
764
765         g_return_if_fail(mimeview != NULL);
766
767         partinfo = mimeview_get_selected_part(mimeview);
768         if (partinfo->disposition == DISPOSITIONTYPE_INLINE) return;
769
770         context = gtk_drag_begin(widget, mimeview->target_list,
771                                  GDK_ACTION_COPY, button, event);
772         gtk_drag_set_icon_default(context);
773 }
774
775 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
776                                     MimeView *mimeview)
777 {
778         GtkCList *clist = GTK_CLIST(widget);
779         gint row, column;
780
781         if (!event) return FALSE;
782
783         if (event->button == 2 || event->button == 3) {
784                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
785                                                   &row, &column))
786                         return FALSE;
787                 gtk_clist_unselect_all(clist);
788                 gtk_clist_select_row(clist, row, column);
789                 gtkut_clist_set_focus_row(clist, row);
790         }
791         part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
792
793         return TRUE;
794 }
795
796 static void part_button_pressed(MimeView *mimeview, GdkEventButton *event, 
797                                 MimeInfo *partinfo)
798 {
799         if (event->button == 2 ||
800             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
801                 /* call external program for image, audio or html */
802                 mimeview_launch(mimeview);
803         } else if (event->button == 3) {
804                 if (partinfo && (partinfo->type == MIMETYPE_TEXT ||
805                                  partinfo->type == MIMETYPE_MESSAGE ||
806                                  partinfo->type == MIMETYPE_IMAGE ||
807                                  partinfo->type == MIMETYPE_MULTIPART))
808                         menu_set_sensitive(mimeview->popupfactory,
809                                            "/Display as text", FALSE);
810                 else
811                         menu_set_sensitive(mimeview->popupfactory,
812                                            "/Display as text", TRUE);
813                 if (partinfo &&
814                     partinfo->type == MIMETYPE_APPLICATION &&
815                     !g_strcasecmp(partinfo->subtype, "octet-stream"))
816                         menu_set_sensitive(mimeview->popupfactory,
817                                            "/Open", FALSE);
818                 else
819                         menu_set_sensitive(mimeview->popupfactory,
820                                            "/Open", TRUE);
821
822                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
823                                     "pop_partinfo", partinfo);
824                                     
825                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
826                                NULL, NULL, NULL, NULL,
827                                event->button, event->time);
828         }
829 }
830
831
832 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
833 {
834         mimeview_key_pressed(mimeview->ctree, event, mimeview);
835 }
836
837 #define BREAK_ON_MODIFIER_KEY() \
838         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
839
840 #define KEY_PRESS_EVENT_STOP() \
841         if (gtk_signal_n_emissions_by_name \
842                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
843                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
844                                              "key_press_event"); \
845         }
846
847 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
848                                  MimeView *mimeview)
849 {
850         SummaryView *summaryview;
851         GtkCTree *ctree = GTK_CTREE(widget);
852         GtkCTreeNode *node;
853
854         if (!event) return FALSE;
855         if (!mimeview->opened) return FALSE;
856
857         switch (event->keyval) {
858         case GDK_space:
859                 if (textview_scroll_page(mimeview->textview, FALSE))
860                         return TRUE;
861
862                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
863                 if (node) {
864                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
865                         gtk_sctree_select(GTK_SCTREE(ctree), node);
866                         return TRUE;
867                 }
868                 break;
869         case GDK_BackSpace:
870                 textview_scroll_page(mimeview->textview, TRUE);
871                 return TRUE;
872         case GDK_Return:
873                 textview_scroll_one_line(mimeview->textview,
874                                          (event->state & GDK_MOD1_MASK) != 0);
875                 return TRUE;
876         case GDK_n:
877         case GDK_N:
878                 BREAK_ON_MODIFIER_KEY();
879                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
880                 KEY_PRESS_EVENT_STOP();
881
882                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
883                                         GTK_SCROLL_STEP_FORWARD, 0.0);
884                 return TRUE;
885         case GDK_p:
886         case GDK_P:
887                 BREAK_ON_MODIFIER_KEY();
888                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
889                 KEY_PRESS_EVENT_STOP();
890
891                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
892                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
893                 return TRUE;
894         case GDK_y:
895                 BREAK_ON_MODIFIER_KEY();
896                 KEY_PRESS_EVENT_STOP();
897                 mimeview_save_as(mimeview);
898                 return TRUE;
899         case GDK_t:
900                 BREAK_ON_MODIFIER_KEY();
901                 KEY_PRESS_EVENT_STOP();
902                 mimeview_display_as_text(mimeview);
903                 return TRUE;    
904         case GDK_l:
905                 BREAK_ON_MODIFIER_KEY();
906                 KEY_PRESS_EVENT_STOP();
907                 mimeview_launch(mimeview);
908                 return TRUE;
909         case GDK_o:
910                 BREAK_ON_MODIFIER_KEY();
911                 KEY_PRESS_EVENT_STOP();
912                 mimeview_open_with(mimeview);
913                 return TRUE;
914         default:
915                 break;
916         }
917
918         if (!mimeview->messageview->mainwin) return FALSE;
919         summaryview = mimeview->messageview->mainwin->summaryview;
920         summary_pass_key_press_event(summaryview, event);
921         return TRUE;
922 }
923
924 static void mimeview_drag_data_get(GtkWidget        *widget,
925                                    GdkDragContext   *drag_context,
926                                    GtkSelectionData *selection_data,
927                                    guint             info,
928                                    guint             time,
929                                    MimeView         *mimeview)
930 {
931         gchar *filename, *uriname;
932         MimeInfo *partinfo;
933
934         if (!mimeview->opened) return;
935         if (!mimeview->file) return;
936
937         partinfo = mimeview_get_selected_part(mimeview);
938         if (!partinfo) return;
939
940         filename = g_basename(get_part_name(partinfo));
941         if (*filename == '\0') return;
942
943         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
944                                filename, NULL);
945
946         if (procmime_get_part(filename, partinfo) < 0)
947                 alertpanel_error
948                         (_("Can't save the part of multipart message."));
949
950         uriname = g_strconcat("file:/", filename, NULL);
951         gtk_selection_data_set(selection_data, selection_data->target, 8,
952                                uriname, strlen(uriname));
953
954         g_free(uriname);
955         g_free(filename);
956 }
957
958 static void mimeview_save_all(MimeView *mimeview)
959 {
960         gchar *dirname;
961         gchar *defname = NULL;
962         MimeInfo *attachment;
963         gchar buf[1024];
964
965         if (!mimeview->opened) return;
966         if (!mimeview->file) return;
967
968         attachment = mimeview->mimeinfo;
969         g_return_if_fail(attachment != NULL);
970
971         dirname = filesel_select_file(_("Save as"), defname);
972         if (!dirname) return;
973
974         if (!is_dir_exist (dirname)) {
975                 alertpanel_error(_("`%s' is not a directory."),
976                                  dirname);
977                 g_free (dirname);
978                 dirname = NULL;
979                 return;
980         }
981         
982         { /* add a / after the dirname, in case the user didn't */
983                 gchar *dirname_tmp = NULL;
984                 int dirname_last_char = strlen (dirname) - 1;
985
986                 if (dirname[dirname_last_char] != G_DIR_SEPARATOR) {
987                         dirname_tmp = g_strconcat (dirname, G_DIR_SEPARATOR_S, NULL);
988                         g_free (dirname);
989                         dirname = dirname_tmp;
990                 }
991         }
992
993         /* for each attachment, extract it in the selected dir. */
994         while (attachment != NULL) {
995                 if (attachment->type != MIMETYPE_MESSAGE &&
996                     attachment->type != MIMETYPE_MULTIPART &&
997                     attachment->disposition != DISPOSITIONTYPE_INLINE) {
998                         static guint subst_cnt = 1;
999                         gchar *attachdir;
1000                         gchar *attachname = g_strdup(get_part_name(attachment));
1001                         AlertValue aval = G_ALERTDEFAULT;
1002                         gchar *res;
1003
1004                         if (!attachname || !strlen(attachname))
1005                                 attachname = g_strdup_printf("noname.%d",subst_cnt++);
1006                         subst_chars(attachname, ":?*&|<>\t\r\n", '_');
1007                         g_snprintf(buf, sizeof(buf), "%s%s",
1008                                    dirname,
1009                                    (attachname[0] == G_DIR_SEPARATOR)
1010                                    ? &attachname[1]
1011                                    : attachname);
1012                         subst_chars(buf, "/\\", G_DIR_SEPARATOR);
1013                         attachdir = g_dirname(buf);
1014                         make_dir_hier(attachdir);
1015                         g_free(attachdir);
1016                         
1017                         if (is_file_exist(buf)) {
1018                                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1019                                                       attachname);
1020                                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
1021                                                   _("Cancel"), NULL);
1022                                 g_free(res);                                      
1023                         }
1024                         g_free(attachname);
1025                         
1026                         if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, attachment) < 0))
1027                                 alertpanel_error(_("Can't save the part of multipart message."));
1028                 }
1029                 attachment = procmime_mimeinfo_next(attachment);
1030         }
1031 }
1032
1033 static void mimeview_display_as_text(MimeView *mimeview)
1034 {
1035         MimeInfo *partinfo;
1036
1037         if (!mimeview->opened) return;
1038
1039         partinfo = mimeview_get_selected_part(mimeview);
1040         if (!partinfo)  {
1041                 partinfo = (MimeInfo *) gtk_object_get_data
1042                         (GTK_OBJECT(mimeview->popupmenu),
1043                          "pop_partinfo");
1044                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1045                                     "pop_partinfo", NULL);
1046         
1047         }                        
1048         g_return_if_fail(partinfo != NULL);
1049         mimeview_show_message_part(mimeview, partinfo);
1050 }
1051
1052 static void mimeview_save_as(MimeView *mimeview)
1053 {
1054         gchar *filename;
1055         gchar *defname = NULL;
1056         MimeInfo *partinfo;
1057         gchar *res;
1058         const gchar *partname = NULL;
1059
1060         if (!mimeview->opened) return;
1061         if (!mimeview->file) return;
1062
1063         partinfo = mimeview_get_selected_part(mimeview);
1064         if (!partinfo) { 
1065                 partinfo = (MimeInfo *) gtk_object_get_data
1066                         (GTK_OBJECT(mimeview->popupmenu),
1067                          "pop_partinfo");
1068                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1069                                     "pop_partinfo", NULL);
1070         }                        
1071         g_return_if_fail(partinfo != NULL);
1072         
1073         if ((partname = get_part_name(partinfo)) != NULL) {
1074                 Xstrdup_a(defname, partname, return);
1075                 subst_for_filename(defname);
1076         }
1077
1078         filename = filesel_select_file(_("Save as"), defname);
1079         if (!filename) return;
1080         if (is_file_exist(filename)) {
1081                 AlertValue aval;
1082                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1083                                       filename);
1084                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
1085                                   _("Cancel"), NULL);
1086                 g_free(res);                                      
1087                 if (G_ALERTDEFAULT != aval) return;
1088         }
1089
1090         if (procmime_get_part(filename, partinfo) < 0)
1091                 alertpanel_error
1092                         (_("Can't save the part of multipart message."));
1093 }
1094
1095 static void mimeview_launch(MimeView *mimeview)
1096 {
1097         MimeInfo *partinfo;
1098         gchar *filename;
1099
1100         if (!mimeview->opened) return;
1101         if (!mimeview->file) return;
1102
1103         partinfo = mimeview_get_selected_part(mimeview);
1104         if (!partinfo) { 
1105                 partinfo = (MimeInfo *) gtk_object_get_data
1106                         (GTK_OBJECT(mimeview->popupmenu),
1107                          "pop_partinfo");
1108                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1109                                     "pop_partinfo", NULL);
1110         }                        
1111         g_return_if_fail(partinfo != NULL);
1112
1113         filename = procmime_get_tmp_file_name(partinfo);
1114
1115         if (procmime_get_part(filename, partinfo) < 0)
1116                 alertpanel_error
1117                         (_("Can't save the part of multipart message."));
1118         else
1119                 mimeview_view_file(filename, partinfo, NULL);
1120
1121         g_free(filename);
1122 }
1123
1124 static void mimeview_open_with(MimeView *mimeview)
1125 {
1126         MimeInfo *partinfo;
1127         gchar *filename;
1128         gchar *cmd;
1129
1130         if (!mimeview->opened) return;
1131         if (!mimeview->file) return;
1132
1133         partinfo = mimeview_get_selected_part(mimeview);
1134         if (!partinfo) { 
1135                 partinfo = (MimeInfo *) gtk_object_get_data
1136                         (GTK_OBJECT(mimeview->popupmenu),
1137                          "pop_partinfo");
1138                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1139                                     "pop_partinfo", NULL);
1140         }                        
1141         g_return_if_fail(partinfo != NULL);
1142
1143         filename = procmime_get_tmp_file_name(partinfo);
1144
1145         if (procmime_get_part(filename, partinfo) < 0) {
1146                 alertpanel_error
1147                         (_("Can't save the part of multipart message."));
1148                 g_free(filename);
1149                 return;
1150         }
1151
1152         if (!prefs_common.mime_open_cmd_history)
1153                 prefs_common.mime_open_cmd_history =
1154                         add_history(NULL, prefs_common.mime_open_cmd);
1155
1156         cmd = input_dialog_combo
1157                 (_("Open with"),
1158                  _("Enter the command line to open file:\n"
1159                    "(`%s' will be replaced with file name)"),
1160                  prefs_common.mime_open_cmd,
1161                  prefs_common.mime_open_cmd_history,
1162                  TRUE);
1163         if (cmd) {
1164                 mimeview_view_file(filename, partinfo, cmd);
1165                 g_free(prefs_common.mime_open_cmd);
1166                 prefs_common.mime_open_cmd = cmd;
1167                 prefs_common.mime_open_cmd_history =
1168                         add_history(prefs_common.mime_open_cmd_history, cmd);
1169         }
1170
1171         g_free(filename);
1172 }
1173
1174 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1175                                const gchar *cmdline)
1176 {
1177         static gchar *default_image_cmdline = "display '%s'";
1178         static gchar *default_audio_cmdline = "play '%s'";
1179         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1180         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1181         gchar buf[1024];
1182         gchar m_buf[1024];
1183         const gchar *cmd;
1184         const gchar *def_cmd;
1185         const gchar *p;
1186
1187         if (cmdline) {
1188                 cmd = cmdline;
1189                 def_cmd = NULL;
1190         } else if (MIMETYPE_APPLICATION == partinfo->type &&
1191                    !g_strcasecmp(partinfo->subtype, "octet-stream")) {
1192                 return;
1193         } else if (MIMETYPE_IMAGE == partinfo->type) {
1194                 cmd = prefs_common.mime_image_viewer;
1195                 def_cmd = default_image_cmdline;
1196         } else if (MIMETYPE_AUDIO == partinfo->type) {
1197                 cmd = prefs_common.mime_audio_player;
1198                 def_cmd = default_audio_cmdline;
1199         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1200                 cmd = prefs_common.uri_cmd;
1201                 def_cmd = default_html_cmdline;
1202         } else {
1203                 gchar *content_type;
1204                 
1205                 content_type = g_strdup_printf("%s/%s", procmime_get_type_str(partinfo->type), partinfo->subtype);
1206                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1207                            content_type, "%s");
1208                 g_free(content_type);
1209                 cmd = m_buf;
1210                 def_cmd = NULL;
1211         }
1212
1213         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1214             !strchr(p + 2, '%'))
1215                 g_snprintf(buf, sizeof(buf), cmd, filename);
1216         else {
1217                 if (cmd)
1218                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1219                 if (def_cmd)
1220                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1221                 else
1222                         return;
1223         }
1224
1225         execute_command_line(buf, TRUE);
1226 }
1227
1228 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1229 {
1230         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1231 }
1232
1233 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1234 {
1235         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1236 }
1237
1238 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1239 {
1240         GSList *mimeview_list, *viewer_list;
1241
1242         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1243                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1244                 
1245                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1246                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1247                         mimeview->mimeviewer = NULL;
1248                 }
1249
1250                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1251                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1252
1253                         mimeviewer->destroy_viewer(mimeviewer);
1254                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1255                 }
1256         }
1257
1258         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1259 }
1260
1261 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1262 {
1263         gint      num;
1264         MimeInfo *partinfo;
1265
1266         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1267         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1268
1269         icon_selected(mimeview, num, partinfo);
1270         gtk_widget_grab_focus(button);
1271         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1272                 toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1273                 if (event->button == 2 || event->button == 3)
1274                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
1275                                                      TRUE);
1276         } else {
1277                 gtk_signal_emit_stop_by_name(GTK_OBJECT(button), "button_press_event");
1278         }
1279
1280         part_button_pressed(mimeview, event, partinfo);
1281
1282         return TRUE;
1283 }
1284
1285 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1286 {
1287         GtkCTreeNode *node;
1288         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1289         if (node)
1290                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1291 }               
1292
1293 #undef  KEY_PRESS_EVENT_STOP
1294 #define KEY_PRESS_EVENT_STOP() \
1295         if (gtk_signal_n_emissions_by_name \
1296                 (GTK_OBJECT(button), "key_press_event") > 0) { \
1297                 gtk_signal_emit_stop_by_name(GTK_OBJECT(button), \
1298                                              "key_press_event"); \
1299         }
1300 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1301                              MimeView *mimeview)
1302 {
1303         gint          num;
1304         MimeInfo     *partinfo;
1305         SummaryView  *summaryview;
1306         TextView     *textview;
1307
1308         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1309         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1310         
1311         if (!event) return FALSE;
1312
1313         textview = mimeview->textview;
1314
1315         switch (event->keyval) {
1316         case GDK_space:
1317                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1318                         /* stop the button being untoggled */
1319                         KEY_PRESS_EVENT_STOP();
1320                         if (textview_scroll_page(textview, FALSE))
1321                                 return TRUE;
1322
1323                         if (icon_list_select_by_number(mimeview, num + 1))
1324                                 return TRUE;
1325                 } else {
1326                         icon_selected(mimeview, num, partinfo);
1327                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1328                         return TRUE;
1329                 }
1330
1331                 break;
1332         case GDK_BackSpace:
1333                 textview_scroll_page(textview, TRUE);
1334                 return TRUE;
1335         case GDK_Return:
1336                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1337                         KEY_PRESS_EVENT_STOP();
1338                         textview_scroll_one_line(textview,
1339                                                  (event->state & GDK_MOD1_MASK) != 0);
1340                         return TRUE;
1341                 } else {
1342                         icon_selected(mimeview, num, partinfo);
1343                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1344                         return TRUE;
1345                 }
1346
1347         case GDK_n:
1348         case GDK_N:
1349                 BREAK_ON_MODIFIER_KEY();
1350                 if (icon_list_select_by_number(mimeview, num + 1)) {
1351                         KEY_PRESS_EVENT_STOP();
1352                         return TRUE;
1353                 }
1354                 break;
1355                 
1356         case GDK_p:
1357         case GDK_P:
1358                 BREAK_ON_MODIFIER_KEY();
1359                 if (icon_list_select_by_number(mimeview, num - 1)) {
1360                         KEY_PRESS_EVENT_STOP();
1361                         return TRUE;
1362                 }
1363                 break;
1364
1365         case GDK_y:
1366                 BREAK_ON_MODIFIER_KEY();
1367                 KEY_PRESS_EVENT_STOP();
1368                 mimeview_save_as(mimeview);
1369                 return TRUE;
1370         case GDK_t:
1371                 BREAK_ON_MODIFIER_KEY();
1372                 KEY_PRESS_EVENT_STOP();
1373                 mimeview_display_as_text(mimeview);
1374                 return TRUE;    
1375         case GDK_l:
1376                 BREAK_ON_MODIFIER_KEY();
1377                 KEY_PRESS_EVENT_STOP();
1378                 mimeview_launch(mimeview);
1379                 return TRUE;
1380         case GDK_o:
1381                 BREAK_ON_MODIFIER_KEY();
1382                 KEY_PRESS_EVENT_STOP();
1383                 mimeview_open_with(mimeview);
1384                 return TRUE;
1385         default:
1386                 break;
1387         }
1388
1389         if (!mimeview->messageview->mainwin) return FALSE;
1390         summaryview = mimeview->messageview->mainwin->summaryview;
1391         summary_pass_key_press_event(summaryview, event);
1392         return TRUE;
1393 }
1394
1395 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1396 {
1397         GList *child;
1398         
1399         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1400         for (; child != NULL; child = g_list_next(child)) {
1401                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1402                     GTK_TOGGLE_BUTTON(child->data) != button &&
1403                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1404                         gtk_toggle_button_set_active
1405                                 (GTK_TOGGLE_BUTTON(child->data),
1406                                  FALSE);
1407         }
1408 }
1409
1410 static GdkColor nocheck_sig_color = {
1411         (gulong)0,
1412         (gushort)0,
1413         (gushort)0,
1414         (gushort)0xcfff
1415 };
1416
1417 static GdkColor good_sig_color = {
1418         (gulong)0,
1419         (gushort)0,
1420         (gushort)0xbfff,
1421         (gushort)0
1422 };
1423
1424 static GdkColor bad_sig_color = {
1425         (gulong)0,
1426         (gushort)0xffff,
1427         (gushort)0,
1428         (gushort)0
1429 };
1430
1431 #define COLOR_BORDER 3
1432
1433 static gboolean icon_drawing_area_expose_event_cb (GtkWidget *widget, GdkEventExpose *expose,
1434                                                    gpointer data) 
1435 {
1436         GdkColor *color = (GdkColor *)data;
1437         GdkDrawable *drawable = widget->window; 
1438         GdkGC *gc_pix;
1439         GdkPixmap *stock_pixmap;
1440         GdkBitmap *stock_mask;
1441
1442         stock_pixmap = (GdkPixmap *)gtk_object_get_data(GTK_OBJECT(widget), "pixmap");
1443         stock_mask   = (GdkBitmap *)gtk_object_get_data(GTK_OBJECT(widget), "mask");
1444         
1445         g_return_val_if_fail(stock_pixmap != NULL, FALSE);
1446         g_return_val_if_fail(stock_mask != NULL, FALSE);
1447
1448         gc_pix = gdk_gc_new((GdkWindow *)drawable);
1449                                                  
1450         gdk_window_clear_area (drawable, expose->area.x, expose->area.y,
1451                                expose->area.width, expose->area.height);
1452
1453         if (color != NULL) {
1454                 gdk_gc_set_foreground(gc_pix, color);
1455                 if (color == &good_sig_color) {
1456                         gdk_draw_rectangle(drawable, gc_pix, TRUE, 0 , 0, 
1457                                            widget->allocation.width,
1458                                            widget->allocation.height);
1459                 } else {
1460                         gdk_gc_set_line_attributes(gc_pix, COLOR_BORDER - 1, GDK_LINE_ON_OFF_DASH, 
1461                                                    GDK_CAP_BUTT, GDK_JOIN_MITER);
1462                         gdk_draw_rectangle(drawable, gc_pix, FALSE, 1 , 1, 
1463                                            widget->allocation.width - 2,
1464                                            widget->allocation.height - 2);
1465                 }
1466         }
1467
1468 #define LEFT COLOR_BORDER + widget->allocation.width - widget->requisition.width
1469 #define TOP  COLOR_BORDER + widget->allocation.height - widget->requisition.height   
1470
1471         gdk_gc_set_tile(gc_pix, stock_pixmap);
1472         gdk_gc_set_ts_origin(gc_pix, LEFT, TOP);
1473         gdk_gc_set_clip_mask(gc_pix, stock_mask);
1474         gdk_gc_set_clip_origin(gc_pix, LEFT, TOP);
1475         gdk_gc_set_fill(gc_pix, GDK_TILED);
1476         gdk_draw_rectangle(drawable, gc_pix, TRUE, LEFT, TOP, 
1477                            widget->requisition.width - COLOR_BORDER * 2,
1478                            widget->requisition.height - COLOR_BORDER * 2);
1479
1480         gdk_gc_destroy(gc_pix);
1481         
1482         return TRUE;
1483
1484 }
1485
1486 static GtkWidget* icon_stock_pixmap_with_privacy (GtkWidget *window, StockPixmap icon,
1487                                                   MimeInfo *mimeinfo)
1488 {
1489         GdkPixmap *stock_pixmap;
1490         GdkBitmap *stock_mask;
1491         GtkWidget *widget;
1492         GtkWidget *stock_wid;
1493         GdkColor *color = NULL;
1494         MimeInfo *siginfo;
1495         gboolean is_signed = FALSE;
1496         gint height;
1497         gint width;
1498
1499         siginfo = mimeinfo;
1500         while (siginfo != NULL) {
1501                 if (privacy_mimeinfo_is_signed(siginfo)) {
1502                         is_signed = TRUE;
1503                         break;
1504                 }
1505                 siginfo = procmime_mimeinfo_parent(siginfo);
1506         }
1507         
1508         stock_wid = stock_pixmap_widget(window, icon);
1509         gtk_pixmap_get(GTK_PIXMAP(stock_wid), &stock_pixmap, &stock_mask);
1510         height = stock_wid->requisition.height;
1511         width  = stock_wid->requisition.width;
1512         gdk_pixmap_ref(stock_pixmap);
1513         gdk_pixmap_ref(stock_mask);
1514         gtk_widget_destroy(stock_wid);
1515
1516         widget = gtk_drawing_area_new();
1517         gtk_drawing_area_size(GTK_DRAWING_AREA(widget), 
1518                               width + COLOR_BORDER * 2, 
1519                               height +  COLOR_BORDER * 2);
1520         gtk_object_set_data_full(GTK_OBJECT(widget), "pixmap", stock_pixmap,
1521                                  (GtkDestroyNotify)gdk_pixmap_unref);
1522         gtk_object_set_data_full(GTK_OBJECT(widget), "mask", stock_mask,
1523                                  (GtkDestroyNotify)gdk_pixmap_unref);
1524
1525         if (is_signed) {
1526                 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
1527                 case SIGNATURE_UNCHECKED:
1528                         color = &nocheck_sig_color;
1529                         break;
1530                 case SIGNATURE_OK:
1531                         color = &good_sig_color;
1532                         break;
1533                 default:
1534                         color = &bad_sig_color;
1535                         break;
1536                 }
1537                 if (!color->pixel) 
1538                         gdk_colormap_alloc_color(gtk_widget_get_colormap(widget),
1539                                                  color, FALSE, TRUE);
1540         } else {
1541                 color = NULL;
1542         }
1543         gtk_signal_connect(GTK_OBJECT(widget), "expose_event", 
1544                            GTK_SIGNAL_FUNC
1545                                 (icon_drawing_area_expose_event_cb),
1546                            color);
1547         return widget;
1548 }
1549
1550 #undef COLOR_BORDER
1551
1552 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1553 {
1554         GtkWidget *pixmap;
1555         GtkWidget *vbox;
1556         GtkWidget *button;
1557         gchar *tip;
1558         const gchar *desc = NULL;
1559         StockPixmap stockp;
1560         
1561         vbox = mimeview->icon_vbox;
1562         mimeview->icon_count++;
1563         button = gtk_toggle_button_new();
1564         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1565         gtk_object_set_data(GTK_OBJECT(button), "icon_number", 
1566                 GINT_TO_POINTER(mimeview->icon_count));
1567         gtk_object_set_data(GTK_OBJECT(button), "partinfo", 
1568                 mimeinfo);
1569         
1570         switch (mimeinfo->type) {
1571                 
1572         case MIMETYPE_TEXT:
1573                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "html"))
1574                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1575                 else if  (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "enriched"))
1576                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1577                 else
1578                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1579                 break;
1580         case MIMETYPE_MESSAGE:
1581                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1582                 break;
1583         case MIMETYPE_APPLICATION:
1584                 stockp = STOCK_PIXMAP_MIME_APPLICATION;
1585                 break;
1586         case MIMETYPE_IMAGE:
1587                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1588                 break;
1589         case MIMETYPE_AUDIO:
1590                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1591                 break;
1592         default:
1593                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1594                 break;
1595         }
1596         
1597         pixmap = icon_stock_pixmap_with_privacy(mimeview->mainwin->window, stockp,
1598                                                         mimeinfo);
1599         gtk_container_add(GTK_CONTAINER(button), pixmap);
1600         
1601         if (!desc) {
1602                 if (prefs_common.attach_desc)
1603                         desc = get_part_description(mimeinfo);
1604                 else
1605                         desc = get_part_name(mimeinfo);
1606         }
1607
1608         if (desc && *desc)
1609                 tip = g_strdup_printf("%s\n%s/%s\n%s", desc,
1610                                 procmime_get_type_str(mimeinfo->type),
1611                                 mimeinfo->subtype, 
1612                                 to_human_readable(mimeinfo->length));
1613         else            
1614                 tip = g_strdup_printf("%s/%s\n%s",
1615                                 procmime_get_type_str(mimeinfo->type),
1616                                 mimeinfo->subtype, 
1617                                 to_human_readable(mimeinfo->length));
1618
1619         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1620         g_free(tip);
1621         gtk_widget_show_all(button);
1622         gtk_signal_connect(GTK_OBJECT(button), "button_press_event", 
1623                            GTK_SIGNAL_FUNC(icon_clicked_cb), mimeview);
1624         gtk_signal_connect(GTK_OBJECT(button), "key_press_event", 
1625                            GTK_SIGNAL_FUNC(icon_key_pressed), mimeview);
1626         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1627
1628 }
1629
1630 static void icon_list_clear (MimeView *mimeview)
1631 {
1632         GList     *child;
1633         GtkAdjustment *adj;
1634         
1635         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1636         for (; child != NULL; child = g_list_next(child)) {
1637                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1638                                        GTK_WIDGET(child->data));
1639         }
1640         mimeview->icon_count = 0;
1641         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1642         adj->value = adj->lower;
1643         gtk_signal_emit_by_name(GTK_OBJECT (adj), "value_changed");
1644 }
1645
1646 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
1647                                           MimeInfo      *mimeinfo)
1648 {
1649         GList *child;
1650         
1651         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1652         for (; child != NULL; child = g_list_next(child)) {
1653                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1654                     gtk_object_get_data(GTK_OBJECT(child->data),
1655                                         "partinfo") == (gpointer)mimeinfo) {
1656                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1657                         gtk_toggle_button_set_active
1658                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1659                 }                                
1660         }
1661 }
1662
1663 /*!
1664  *\brief        Used to 'click' the next or previous icon.
1665  *
1666  *\return       true if the icon 'number' exists and was selected.
1667  */
1668 static gboolean icon_list_select_by_number(MimeView     *mimeview,
1669                                            gint          number)
1670 {
1671         GList *child;
1672
1673         if (number == 0) return FALSE;
1674         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1675         for (; child != NULL; child = g_list_next(child)) {
1676                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1677                     GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(child->data),
1678                                         "icon_number")) == number) {
1679                         icon_selected(mimeview, number,
1680                                       (MimeInfo*)gtk_object_get_data(GTK_OBJECT(child->data),
1681                                                                      "partinfo"));
1682                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1683                         gtk_toggle_button_set_active
1684                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1685                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
1686                 
1687                         return TRUE;
1688                 }                                
1689         }
1690         return FALSE;
1691 }
1692
1693 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1694                                         GtkAllocation *size, MimeView *mimeview)
1695 {
1696         GtkAllocation *mainbox_size;
1697         GtkAllocation *vbox_size;
1698         GtkAllocation *layout_size;
1699         GtkAdjustment *adj;
1700         
1701         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1702
1703         mainbox_size = &mimeview->icon_mainbox->allocation;
1704         vbox_size = &mimeview->icon_vbox->allocation;
1705         layout_size = &mimeview->icon_scroll->allocation;
1706         
1707         /* centralise the vbox */
1708         gtk_layout_move(GTK_LAYOUT(mimeview->icon_scroll), mimeview->icon_vbox, 
1709                         (mainbox_size->width - vbox_size->width)/2, 0);
1710         
1711         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1712                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1713                             MAX(vbox_size->height, layout_size->height));
1714         adj->step_increment = 5;
1715 }
1716
1717 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1718 {
1719         GtkRequisition size;
1720         gint           width;
1721         g_return_if_fail(mimeinfo != NULL);
1722
1723         while (mimeinfo != NULL) {
1724                 if (mimeinfo->type != MIMETYPE_MULTIPART)
1725                         icon_list_append_icon(mimeview, mimeinfo);
1726                 if (mimeinfo->node->children != NULL)
1727                         icon_list_create(mimeview, (MimeInfo *) mimeinfo->node->children->data);
1728                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
1729         }
1730         gtk_widget_size_request(mimeview->icon_vbox, &size);
1731         width = size.width + 4;
1732         if (width > mimeview->icon_mainbox->requisition.width) {
1733                 gtk_widget_set_usize(mimeview->icon_mainbox, 
1734                                      width, -1);
1735         }
1736
1737 }
1738
1739 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1740 {
1741         gtk_widget_ref(button); 
1742
1743         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1744                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1745                                         GTK_SHADOW_NONE);
1746                 gtk_widget_hide(mimeview->icon_mainbox);
1747                 gtk_widget_show(mimeview->ctree_mainbox);
1748                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1749
1750                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1751                                         button);
1752                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1753                                    button, FALSE, FALSE, 0);
1754                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
1755         } else {
1756                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1757                               GTK_SHADOW_NONE);
1758                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1759                 gtk_widget_hide(mimeview->ctree_mainbox);
1760                 gtk_widget_show(mimeview->icon_mainbox);
1761                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1762
1763                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1764                                         button);
1765                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1766                                    button, FALSE, FALSE, 0);
1767                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1768                 if (mimeview->opened)
1769                         icon_list_toggle_by_mime_info
1770                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
1771                                                                        mimeview->opened));
1772
1773                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
1774         }
1775         gtk_widget_grab_focus(button);
1776         gtk_widget_unref(button);
1777
1778 }
1779
1780 void mimeview_update (MimeView *mimeview) {
1781         if (mimeview && mimeview->mimeinfo) {
1782                 icon_list_clear(mimeview);
1783                 icon_list_create(mimeview, mimeview->mimeinfo);
1784         }
1785 }