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