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