063d2ef76eb3fcfe13033aaeb3571dc4439b1383
[claws.git] / src / mimeview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtknotebook.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkctree.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkhbox.h>
33 #include <gtk/gtkvpaned.h>
34 #include <gtk/gtktext.h>
35 #include <gtk/gtksignal.h>
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkdnd.h>
38 #include <gtk/gtkselection.h>
39 #include <gtk/gtktooltips.h>
40 #include <gtk/gtkcontainer.h>
41 #include <gtk/gtkbutton.h>
42 #include <stdio.h>
43
44 #ifndef HAVE_APACHE_FNMATCH
45 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
46  * fnmatch.h */
47 #include <fnmatch.h>
48 #endif
49
50 #include "intl.h"
51 #include "main.h"
52 #include "mimeview.h"
53 #include "textview.h"
54 #include "procmime.h"
55 #include "summaryview.h"
56 #include "menu.h"
57 #include "filesel.h"
58 #include "alertpanel.h"
59 #include "inputdialog.h"
60 #include "utils.h"
61 #include "gtkutils.h"
62 #include "prefs_common.h"
63 #include "rfc2015.h"
64 #include "stock_pixmap.h"
65 #include "gtk/gtkvscrollbutton.h"
66
67
68 typedef enum
69 {
70         COL_MIMETYPE = 0,
71         COL_SIZE     = 1,
72         COL_NAME     = 2
73 } MimeViewColumnPos;
74
75 #define N_MIMEVIEW_COLS 3
76
77 static void mimeview_set_multipart_tree         (MimeView       *mimeview,
78                                                  MimeInfo       *mimeinfo,
79                                                  GtkCTreeNode   *parent);
80 static GtkCTreeNode *mimeview_append_part       (MimeView       *mimeview,
81                                                  MimeInfo       *partinfo,
82                                                  GtkCTreeNode   *parent);
83 static void mimeview_show_message_part          (MimeView       *mimeview,
84                                                  MimeInfo       *partinfo);
85 static void mimeview_change_view_type           (MimeView       *mimeview,
86                                                  MimeViewType    type);
87
88 static void mimeview_selected           (GtkCTree       *ctree,
89                                          GtkCTreeNode   *node,
90                                          gint            column,
91                                          MimeView       *mimeview);
92 static void mimeview_start_drag         (GtkWidget      *widget,
93                                          gint            button,
94                                          GdkEvent       *event,
95                                          MimeView       *mimeview);
96 static gint mimeview_button_pressed     (GtkWidget      *widget,
97                                          GdkEventButton *event,
98                                          MimeView       *mimeview);
99 static gint mimeview_key_pressed        (GtkWidget      *widget,
100                                          GdkEventKey    *event,
101                                          MimeView       *mimeview);
102
103 static void mimeview_drag_data_get      (GtkWidget        *widget,
104                                          GdkDragContext   *drag_context,
105                                          GtkSelectionData *selection_data,
106                                          guint             info,
107                                          guint             time,
108                                          MimeView         *mimeview);
109
110 static void mimeview_display_as_text    (MimeView       *mimeview);
111 static void mimeview_save_as            (MimeView       *mimeview);
112 static void mimeview_save_all           (MimeView       *mimeview);
113 static void mimeview_launch             (MimeView       *mimeview);
114 static void mimeview_open_with          (MimeView       *mimeview);
115 static void mimeview_view_file          (const gchar    *filename,
116                                          MimeInfo       *partinfo,
117                                          const gchar    *cmdline);
118 static gboolean icon_clicked_cb         (GtkWidget      *button, 
119                                          GdkEventButton *event, 
120                                          MimeView       *mimeview);
121 static void icon_selected               (MimeView       *mimeview, 
122                                          gint            num, 
123                                          MimeInfo       *partinfo);
124 static gint icon_key_pressed            (GtkWidget      *button, 
125                                          GdkEventKey    *event,
126                                          MimeView       *mimeview);
127 static void toggle_icon                 (GtkToggleButton *button,
128                                          MimeView       *mimeview);
129 static void icon_list_append_icon       (MimeView       *mimeview, 
130                                          MimeInfo       *mimeinfo);
131 static void icon_list_create            (MimeView       *mimeview, 
132                                          MimeInfo       *mimeinfo);
133 static void icon_list_clear             (MimeView       *mimeview);
134 static void icon_list_toggle_by_mime_info (MimeView     *mimeview,
135                                            MimeInfo     *mimeinfo);
136 static gboolean icon_list_select_by_number(MimeView     *mimeview,
137                                            gint          number);
138 static void mime_toggle_button_cb       (GtkWidget      *button,
139                                          MimeView       *mimeview);
140 static void part_button_pressed         (MimeView       *mimeview, 
141                                          GdkEventButton *event, 
142                                          MimeInfo       *partinfo);
143 static void icon_scroll_size_allocate_cb(GtkWidget      *widget, 
144                                          GtkAllocation  *layout_size, 
145                                          MimeView       *mimeview);
146
147 static GtkItemFactoryEntry mimeview_popup_entries[] =
148 {
149         {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
150         {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
151         {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
152         {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL},
153         {N_("/Save _all..."),     NULL, mimeview_save_all,        0, NULL},
154 };
155
156 static GtkTargetEntry mimeview_mime_types[] =
157 {
158         {"text/uri-list", 0, 0}
159 };
160
161 GSList *mimeviewer_factories;
162 GSList *mimeviews;
163
164 MimeView *mimeview_create(MainWindow *mainwin)
165 {
166         MimeView *mimeview;
167
168         GtkWidget *paned;
169         GtkWidget *scrolledwin;
170         GtkWidget *ctree;
171         GtkWidget *mime_notebook;
172         GtkWidget *popupmenu;
173         GtkWidget *ctree_mainbox;
174         GtkWidget *vbox;
175         GtkWidget *mime_toggle;
176         GtkWidget *icon_mainbox;
177         GtkWidget *icon_scroll;
178         GtkWidget *icon_vbox;
179         GtkWidget *arrow;
180         GtkWidget *scrollbutton;
181         GtkWidget *hbox;
182         GtkTooltips *tooltips;
183         GtkItemFactory *popupfactory;
184         NoticeView *siginfoview;
185         gchar *titles[N_MIMEVIEW_COLS];
186         gint n_entries;
187         gint i;
188
189         debug_print("Creating MIME view...\n");
190         mimeview = g_new0(MimeView, 1);
191
192         titles[COL_MIMETYPE] = _("MIME Type");
193         titles[COL_SIZE]     = _("Size");
194         titles[COL_NAME]     = _("Name");
195
196         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
197         gtk_widget_show(scrolledwin);
198         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
199                                        GTK_POLICY_AUTOMATIC,
200                                        GTK_POLICY_ALWAYS);
201
202         ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
203         gtk_widget_show(ctree);
204         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
205         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
206         gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
207                                            GTK_JUSTIFY_RIGHT);
208         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
209         gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 90);
210         for (i = 0; i < N_MIMEVIEW_COLS; i++)
211                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
212                                        GTK_CAN_FOCUS);
213         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
214
215         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
216                            GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
217         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
218                            GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
219         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
220                            GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
221         gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
222                            GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
223         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
224                            GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
225
226         mime_notebook = gtk_notebook_new();
227         gtk_widget_show(mime_notebook);
228         GTK_WIDGET_UNSET_FLAGS(mime_notebook, GTK_CAN_FOCUS);
229         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
230         gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
231         
232         icon_vbox = gtk_vbox_new(FALSE, 2);
233         gtk_widget_show(icon_vbox);
234         icon_scroll = gtk_layout_new(NULL, NULL);
235         gtk_widget_show(icon_scroll);
236         gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
237         scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
238         gtk_widget_show(scrollbutton);
239
240         mime_toggle = gtk_toggle_button_new();
241         gtk_widget_show(mime_toggle);
242         arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
243         gtk_widget_show(arrow);
244         gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
245         gtk_signal_connect(GTK_OBJECT(mime_toggle), "toggled", 
246                            GTK_SIGNAL_FUNC(mime_toggle_button_cb), mimeview);
247
248         icon_mainbox = gtk_vbox_new(FALSE, 0);
249         gtk_widget_show(icon_mainbox);
250         gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
251         gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
252         gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
253         gtk_signal_connect(GTK_OBJECT(icon_mainbox), "size_allocate", 
254                         GTK_SIGNAL_FUNC(icon_scroll_size_allocate_cb), mimeview);
255         
256         ctree_mainbox = gtk_hbox_new(FALSE, 0); 
257         gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
258
259         n_entries = sizeof(mimeview_popup_entries) /
260                 sizeof(mimeview_popup_entries[0]);
261         popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
262                                       "<MimeView>", &popupfactory, mimeview);
263         tooltips = gtk_tooltips_new();
264         gtk_tooltips_set_delay(tooltips, 0); 
265
266         vbox = gtk_vbox_new(FALSE, 0);
267         gtk_widget_show(vbox);
268         siginfoview = noticeview_create(mainwin);
269         noticeview_hide(siginfoview);
270         gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
271         gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
272
273         paned = gtk_vpaned_new();
274         gtk_widget_show(paned);
275         gtk_paned_set_gutter_size(GTK_PANED(paned), 0);
276         gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
277         gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
278         
279         hbox = gtk_hbox_new(FALSE, 0);
280         gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
281         gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
282
283         gtk_widget_show(hbox);
284         gtk_widget_hide(ctree_mainbox);
285
286         mimeview->hbox          = hbox;
287         mimeview->paned         = paned;
288         mimeview->scrolledwin   = scrolledwin;
289         mimeview->ctree         = ctree;
290         mimeview->mime_notebook = mime_notebook;
291         mimeview->popupmenu     = popupmenu;
292         mimeview->popupfactory  = popupfactory;
293         mimeview->type          = -1;
294         mimeview->ctree_mainbox = ctree_mainbox;
295         mimeview->icon_scroll   = icon_scroll;
296         mimeview->icon_vbox     = icon_vbox;
297         mimeview->icon_mainbox  = icon_mainbox;
298         mimeview->icon_count    = 0;
299         mimeview->mainwin       = mainwin;
300         mimeview->tooltips      = tooltips;
301         mimeview->oldsize       = 60;
302         mimeview->mime_toggle   = mime_toggle;
303         mimeview->siginfoview   = siginfoview;
304
305         mimeview->target_list   = gtk_target_list_new(mimeview_mime_types, 1); 
306         
307         mimeviews = g_slist_prepend(mimeviews, mimeview);
308
309         return mimeview;
310 }
311
312 void mimeview_init(MimeView *mimeview)
313 {
314         textview_init(mimeview->textview);
315
316         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
317                 GTK_WIDGET_PTR(mimeview->textview));
318 }
319
320 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
321                            const gchar *file)
322 {
323         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
324         GtkCTreeNode *node;
325
326         mimeview_clear(mimeview);
327
328         g_return_if_fail(file != NULL);
329         g_return_if_fail(mimeinfo != NULL);
330
331         mimeview->mimeinfo = mimeinfo;
332
333         mimeview->file = g_strdup(file);
334
335         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
336                                          mimeview);
337
338         mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
339         icon_list_create(mimeview, mimeinfo);
340
341         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
342                                            mimeview_selected, mimeview);
343
344         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
345         if (node) {
346                 gtk_ctree_select(ctree, node);
347                 icon_list_toggle_by_mime_info
348                         (mimeview, gtk_ctree_node_get_row_data(ctree, node));
349                 gtkut_ctree_set_focus_row(ctree, node);
350                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mimeview->mime_toggle)))
351                         gtk_widget_grab_focus(mimeview->ctree);
352         }
353 }
354
355 void mimeview_destroy(MimeView *mimeview)
356 {
357         GSList *cur;
358         
359         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
360                 MimeViewer *viewer = (MimeViewer *) cur->data;
361                 gtk_container_remove(GTK_CONTAINER(mimeview->mime_notebook),
362                         GTK_WIDGET(viewer->get_widget(viewer)));
363                 viewer->destroy_viewer(viewer);
364         }
365         g_slist_free(mimeview->viewers);
366         gtk_target_list_unref(mimeview->target_list);
367
368         procmime_mimeinfo_free_all(mimeview->mimeinfo);
369         g_free(mimeview->file);
370         g_free(mimeview);
371
372         mimeviews = g_slist_remove(mimeviews, mimeview);
373         
374 }
375
376 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
377 {
378         return gtk_ctree_node_get_row_data
379                 (GTK_CTREE(mimeview->ctree), mimeview->opened);
380 }
381
382 static void mimeview_set_multipart_tree(MimeView *mimeview,
383                                         MimeInfo *mimeinfo,
384                                         GtkCTreeNode *parent)
385 {
386         GtkCTreeNode *node;
387
388         g_return_if_fail(mimeinfo != NULL);
389
390         while (mimeinfo != NULL) {
391                 node = mimeview_append_part(mimeview, mimeinfo, parent);
392
393                 if (mimeinfo->children)
394                         mimeview_set_multipart_tree(mimeview, mimeinfo->children, node);
395                 mimeinfo = mimeinfo->next;
396         }
397 }
398
399 static gchar *get_part_name(MimeInfo *partinfo)
400 {
401         gchar *name;
402
403         name = g_hash_table_lookup(partinfo->parameters, "name");
404         if(name == NULL)
405                 name = "";
406
407         return name;
408 }
409
410 static gchar *get_part_description(MimeInfo *partinfo)
411 {
412         if (partinfo->description)
413                 return partinfo->description;
414         else if (g_hash_table_lookup(partinfo->parameters, "name") != NULL)
415                 return g_hash_table_lookup(partinfo->parameters, "name");
416         else
417                 return "";
418 }
419
420 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
421                                           MimeInfo *partinfo,
422                                           GtkCTreeNode *parent)
423 {
424         GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
425         GtkCTreeNode *node;
426         static gchar content_type[64];
427         gchar *str[N_MIMEVIEW_COLS];
428
429         if (partinfo->type != MIMETYPE_UNKNOWN && partinfo->subtype) {
430                 g_snprintf(content_type, 64, "%s/%s", procmime_get_type_str(partinfo->type), partinfo->subtype);
431         } else {
432                 g_snprintf(content_type, 64, "UNKNOWN");
433         }
434
435         str[COL_MIMETYPE] = content_type;
436         str[COL_SIZE] = to_human_readable(partinfo->length);
437         if (prefs_common.attach_desc)
438                 str[COL_NAME] = get_part_description(partinfo);
439         else
440                 str[COL_NAME] = get_part_name(partinfo);
441
442         node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
443                                      NULL, NULL, NULL, NULL,
444                                      FALSE, TRUE);
445         gtk_ctree_node_set_row_data(ctree, node, partinfo);
446
447         return node;
448 }
449
450 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
451 {
452         FILE *fp;
453         const gchar *fname;
454
455         if (!partinfo) return;
456
457         fname = mimeview->file;
458         if (!fname) return;
459
460         if ((fp = fopen(fname, "rb")) == NULL) {
461                 FILE_OP_ERROR(fname, "fopen");
462                 return;
463         }
464
465         if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
466                 FILE_OP_ERROR(mimeview->file, "fseek");
467                 fclose(fp);
468                 return;
469         }
470
471         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
472         textview_show_part(mimeview->textview, partinfo, fp);
473
474         fclose(fp);
475 }
476
477 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
478 {
479         GSList *cur;
480         MimeViewerFactory *factory = NULL;
481         MimeViewer *viewer = NULL;
482
483 /*
484  * FNM_CASEFOLD is a GNU extension
485  * if its not defined copy the string to the stack and
486  * convert the copy to lower case
487  */
488 #ifndef FNM_CASEFOLD
489 #define FNM_CASEFOLD 0
490         Xstrdup_a(content_type, content_type, return NULL);
491         g_strdown((gchar *)content_type);
492 #endif
493         
494         for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
495                 MimeViewerFactory *curfactory = cur->data;
496                 gint i = 0;
497
498                 while (curfactory->content_types[i] != NULL) {
499                         debug_print("%s\n", curfactory->content_types[i]);
500                         if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
501                                 factory = curfactory;
502                                 break;
503                         }
504                         i++;
505                 }
506                 if (factory != NULL)
507                         break;
508         }
509         if (factory == NULL)
510                 return NULL;
511
512         for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
513                 MimeViewer *curviewer = cur->data;
514                 
515                 if (curviewer->factory == factory)
516                         return curviewer;
517         }
518         viewer = factory->create_viewer();
519         gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
520                 GTK_WIDGET(viewer->get_widget(viewer)));
521                 
522         mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
523
524         return viewer;
525 }
526
527 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
528 {
529         gchar *content_type = NULL;
530         MimeViewer *viewer = NULL;
531
532         if ((partinfo->type == MIMETYPE_APPLICATION) &&
533             (g_strcasecmp(partinfo->subtype, "octet-stream")) &&
534             (partinfo->name != NULL)) {
535                 content_type = procmime_get_mime_type(partinfo->name);
536         } else {
537                 content_type = g_strdup_printf("%s/%s", procmime_get_type_str(partinfo->type), partinfo->subtype);
538         }
539
540         if (content_type != NULL) {
541                 viewer = get_viewer_for_content_type(mimeview, content_type);
542                 g_free(content_type);
543         }
544
545         return viewer;
546 }
547
548 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
549 {
550         MimeViewer *viewer;
551         
552         viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
553         if (viewer == NULL) {
554                 if (mimeview->mimeviewer != NULL)
555                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
556                 mimeview->mimeviewer = NULL;
557                 return FALSE;
558         }
559
560         if (mimeview->mimeviewer != viewer) {
561                 if (mimeview->mimeviewer != NULL)
562                         mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
563                 mimeview->mimeviewer = viewer;
564                 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
565         }
566         viewer->show_mimepart(viewer, mimeview->file, partinfo);
567
568         return TRUE;
569 }
570
571 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
572 {
573         TextView  *textview  = mimeview->textview;
574
575         if ((mimeview->type != MIMEVIEW_VIEWER) && 
576             (mimeview->type == type)) return;
577
578         switch (type) {
579         case MIMEVIEW_TEXT:
580                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
581                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
582                         GTK_WIDGET_PTR(textview)));
583                 break;
584         case MIMEVIEW_VIEWER:
585                 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
586                         gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook), 
587                         GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
588                 break;
589         default:
590                 return;
591         }
592
593         mimeview->type = type;
594 }
595
596 void mimeview_clear(MimeView *mimeview)
597 {
598         GtkCList *clist = GTK_CLIST(mimeview->ctree);
599
600         noticeview_hide(mimeview->siginfoview);
601
602         gtk_clist_clear(clist);
603         textview_clear(mimeview->textview);
604         if (mimeview->mimeviewer != NULL)
605                 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
606
607         if (mimeview->mimeinfo != NULL)
608                 procmime_mimeinfo_free_all(mimeview->mimeinfo);
609         mimeview->mimeinfo = NULL;
610
611         mimeview->opened = NULL;
612
613         g_free(mimeview->file);
614         mimeview->file = NULL;
615
616         icon_list_clear(mimeview);
617 }
618
619 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
620                               MimeView *mimeview)
621 {
622         MimeInfo *partinfo;
623
624         if (mimeview->opened == node) return;
625         mimeview->opened = node;
626         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
627
628         partinfo = gtk_ctree_node_get_row_data(ctree, node);
629         if (!partinfo) return;
630
631         /* ungrab the mouse event */
632         if (GTK_WIDGET_HAS_GRAB(ctree)) {
633                 gtk_grab_remove(GTK_WIDGET(ctree));
634                 if (gdk_pointer_is_grabbed())
635                         gdk_pointer_ungrab(GDK_CURRENT_TIME);
636         }
637         
638         mimeview->textview->default_text = FALSE;
639
640         if (privacy_mimeinfo_is_signed(partinfo)) {
641                 noticeview_set_text(mimeview->siginfoview, "Signed Part");
642                 noticeview_show(mimeview->siginfoview);
643         } else {
644                 noticeview_hide(mimeview->siginfoview);
645         }
646
647         if (!mimeview_show_part(mimeview, partinfo)) {
648                 switch (partinfo->type) {
649                 case MIMETYPE_TEXT:
650                 case MIMETYPE_MESSAGE:
651                 case MIMETYPE_MULTIPART:
652                         mimeview_show_message_part(mimeview, partinfo);
653                 
654                         break;
655                 default:
656                         mimeview->textview->default_text = TRUE;        
657                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
658                         textview_show_mime_part(mimeview->textview, partinfo);
659                         break;
660                 }
661         }
662 }
663
664 static void mimeview_start_drag(GtkWidget *widget, gint button,
665                                 GdkEvent *event, MimeView *mimeview)
666 {
667         GdkDragContext *context;
668         MimeInfo *partinfo;
669
670         g_return_if_fail(mimeview != NULL);
671
672         partinfo = mimeview_get_selected_part(mimeview);
673         if (partinfo->filename == NULL && partinfo->name == NULL) return;
674
675         context = gtk_drag_begin(widget, mimeview->target_list,
676                                  GDK_ACTION_COPY, button, event);
677         gtk_drag_set_icon_default(context);
678 }
679
680 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
681                                     MimeView *mimeview)
682 {
683         GtkCList *clist = GTK_CLIST(widget);
684         gint row, column;
685
686         if (!event) return FALSE;
687
688         if (event->button == 2 || event->button == 3) {
689                 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
690                                                   &row, &column))
691                         return FALSE;
692                 gtk_clist_unselect_all(clist);
693                 gtk_clist_select_row(clist, row, column);
694                 gtkut_clist_set_focus_row(clist, row);
695         }
696         part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
697
698         return TRUE;
699 }
700
701 static void part_button_pressed(MimeView *mimeview, GdkEventButton *event, 
702                                 MimeInfo *partinfo)
703 {
704         if (event->button == 2 ||
705             (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
706                 /* call external program for image, audio or html */
707                 mimeview_launch(mimeview);
708         } else if (event->button == 3) {
709                 if (partinfo && (partinfo->type == MIMETYPE_TEXT ||
710                                  partinfo->type == MIMETYPE_MESSAGE ||
711                                  partinfo->type == MIMETYPE_IMAGE ||
712                                  partinfo->type == MIMETYPE_MULTIPART))
713                         menu_set_sensitive(mimeview->popupfactory,
714                                            "/Display as text", FALSE);
715                 else
716                         menu_set_sensitive(mimeview->popupfactory,
717                                            "/Display as text", TRUE);
718                 if (partinfo &&
719                     partinfo->type == MIMETYPE_APPLICATION)
720                         menu_set_sensitive(mimeview->popupfactory,
721                                            "/Open", FALSE);
722                 else
723                         menu_set_sensitive(mimeview->popupfactory,
724                                            "/Open", TRUE);
725
726                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
727                                     "pop_partinfo", partinfo);
728                                     
729                 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
730                                NULL, NULL, NULL, NULL,
731                                event->button, event->time);
732         }
733 }
734
735
736 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
737 {
738         mimeview_key_pressed(mimeview->ctree, event, mimeview);
739 }
740
741 #define BREAK_ON_MODIFIER_KEY() \
742         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
743
744 #define KEY_PRESS_EVENT_STOP() \
745         if (gtk_signal_n_emissions_by_name \
746                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
747                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
748                                              "key_press_event"); \
749         }
750
751 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
752                                  MimeView *mimeview)
753 {
754         SummaryView *summaryview;
755         GtkCTree *ctree = GTK_CTREE(widget);
756         GtkCTreeNode *node;
757
758         if (!event) return FALSE;
759         if (!mimeview->opened) return FALSE;
760
761         switch (event->keyval) {
762         case GDK_space:
763                 if (textview_scroll_page(mimeview->textview, FALSE))
764                         return TRUE;
765
766                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
767                 if (node) {
768                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
769                         gtk_sctree_select(GTK_SCTREE(ctree), node);
770                         return TRUE;
771                 }
772                 break;
773         case GDK_BackSpace:
774                 textview_scroll_page(mimeview->textview, TRUE);
775                 return TRUE;
776         case GDK_Return:
777                 textview_scroll_one_line(mimeview->textview,
778                                          (event->state & GDK_MOD1_MASK) != 0);
779                 return TRUE;
780         case GDK_n:
781         case GDK_N:
782                 BREAK_ON_MODIFIER_KEY();
783                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
784                 KEY_PRESS_EVENT_STOP();
785
786                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
787                                         GTK_SCROLL_STEP_FORWARD, 0.0);
788                 return TRUE;
789         case GDK_p:
790         case GDK_P:
791                 BREAK_ON_MODIFIER_KEY();
792                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
793                 KEY_PRESS_EVENT_STOP();
794
795                 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
796                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
797                 return TRUE;
798         case GDK_y:
799                 BREAK_ON_MODIFIER_KEY();
800                 KEY_PRESS_EVENT_STOP();
801                 mimeview_save_as(mimeview);
802                 return TRUE;
803         case GDK_t:
804                 BREAK_ON_MODIFIER_KEY();
805                 KEY_PRESS_EVENT_STOP();
806                 mimeview_display_as_text(mimeview);
807                 return TRUE;    
808         case GDK_l:
809                 BREAK_ON_MODIFIER_KEY();
810                 KEY_PRESS_EVENT_STOP();
811                 mimeview_launch(mimeview);
812                 return TRUE;
813         default:
814                 break;
815         }
816
817         if (!mimeview->messageview->mainwin) return FALSE;
818         summaryview = mimeview->messageview->mainwin->summaryview;
819         summary_pass_key_press_event(summaryview, event);
820         return TRUE;
821 }
822
823 static void mimeview_drag_data_get(GtkWidget        *widget,
824                                    GdkDragContext   *drag_context,
825                                    GtkSelectionData *selection_data,
826                                    guint             info,
827                                    guint             time,
828                                    MimeView         *mimeview)
829 {
830         gchar *filename, *uriname;
831         MimeInfo *partinfo;
832
833         if (!mimeview->opened) return;
834         if (!mimeview->file) return;
835
836         partinfo = mimeview_get_selected_part(mimeview);
837         if (!partinfo) return;
838         if (!partinfo->filename && !partinfo->name) return;
839
840         filename = partinfo->filename ? partinfo->filename : partinfo->name;
841         filename = g_basename(filename);
842         if (*filename == '\0') return;
843
844         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
845                                filename, NULL);
846
847         if (procmime_get_part(filename, partinfo) < 0)
848                 alertpanel_error
849                         (_("Can't save the part of multipart message."));
850
851         uriname = g_strconcat("file:/", filename, NULL);
852         gtk_selection_data_set(selection_data, selection_data->target, 8,
853                                uriname, strlen(uriname));
854
855         g_free(uriname);
856         g_free(filename);
857 }
858
859 static void mimeview_save_all(MimeView *mimeview)
860 {
861         gchar *dirname;
862         gchar *defname = NULL;
863         MimeInfo *partinfo;
864         MimeInfo *attachment;
865         gchar buf[1024];
866
867         if (!mimeview->opened) return;
868         if (!mimeview->file) return;
869
870         partinfo = mimeview_get_selected_part(mimeview);
871         g_return_if_fail(partinfo != NULL);
872
873         dirname = filesel_select_file(_("Save as"), defname);
874         if (!dirname) return;
875
876         /* return to first children */
877         if (!partinfo->parent->children) return;  /* multipart container? */
878         attachment = partinfo->parent->children->next;
879         /* for each attachment, extract it in the selected dir. */
880         while (attachment != NULL) {
881                 static guint subst_cnt = 1;
882                 gchar *attachdir;
883                 gchar *attachname = g_strdup(get_part_name(attachment));
884                 AlertValue aval = G_ALERTDEFAULT;
885                 gchar *res;
886
887                 if (!attachname || !strlen(attachname))
888                         attachname = g_strdup_printf("noname.%d",subst_cnt++);
889                 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
890                 g_snprintf(buf, sizeof(buf), "%s%s",
891                            dirname,
892                            (attachname[0] == G_DIR_SEPARATOR)
893                            ? &attachname[1]
894                            : attachname);
895                 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
896                 attachdir = g_dirname(buf);
897                 make_dir_hier(attachdir);
898                 g_free(attachdir);
899
900                 if (is_file_exist(buf)) {
901                         res = g_strdup_printf(_("Overwrite existing file '%s'?"),
902                                               attachname);
903                         aval = alertpanel(_("Overwrite"), res, _("OK"), 
904                                           _("Cancel"), NULL);
905                         g_free(res);                                      
906                 }
907                 g_free(attachname);
908
909                 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, attachment) < 0))
910                         alertpanel_error(_("Can't save the part of multipart message."));
911                 attachment = attachment->next;
912         }
913 }
914
915 static void mimeview_display_as_text(MimeView *mimeview)
916 {
917         MimeInfo *partinfo;
918
919         if (!mimeview->opened) return;
920
921         partinfo = mimeview_get_selected_part(mimeview);
922         if (!partinfo)  {
923                 partinfo = (MimeInfo *) gtk_object_get_data
924                         (GTK_OBJECT(mimeview->popupmenu),
925                          "pop_partinfo");
926                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
927                                     "pop_partinfo", NULL);
928         
929         }                        
930         g_return_if_fail(partinfo != NULL);
931         mimeview_show_message_part(mimeview, partinfo);
932 }
933
934 static void mimeview_save_as(MimeView *mimeview)
935 {
936         gchar *filename;
937         gchar *defname = NULL;
938         MimeInfo *partinfo;
939         gchar *res;
940
941         if (!mimeview->opened) return;
942         if (!mimeview->file) return;
943
944         partinfo = mimeview_get_selected_part(mimeview);
945         if (!partinfo) { 
946                 partinfo = (MimeInfo *) gtk_object_get_data
947                         (GTK_OBJECT(mimeview->popupmenu),
948                          "pop_partinfo");
949                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
950                                     "pop_partinfo", NULL);
951         }                        
952         g_return_if_fail(partinfo != NULL);
953
954         if (partinfo->filename)
955                 defname = partinfo->filename;
956         else if (partinfo->name) {
957                 Xstrdup_a(defname, partinfo->name, return);
958                 subst_for_filename(defname);
959         }
960
961         filename = filesel_select_file(_("Save as"), defname);
962         if (!filename) return;
963         if (is_file_exist(filename)) {
964                 AlertValue aval;
965                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
966                                       filename);
967                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
968                                   _("Cancel"), NULL);
969                 g_free(res);                                      
970                 if (G_ALERTDEFAULT != aval) return;
971         }
972
973         if (procmime_get_part(filename, partinfo) < 0)
974                 alertpanel_error
975                         (_("Can't save the part of multipart message."));
976 }
977
978 static void mimeview_launch(MimeView *mimeview)
979 {
980         MimeInfo *partinfo;
981         gchar *filename;
982
983         if (!mimeview->opened) return;
984         if (!mimeview->file) return;
985
986         partinfo = mimeview_get_selected_part(mimeview);
987         if (!partinfo) { 
988                 partinfo = (MimeInfo *) gtk_object_get_data
989                         (GTK_OBJECT(mimeview->popupmenu),
990                          "pop_partinfo");
991                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
992                                     "pop_partinfo", NULL);
993         }                        
994         g_return_if_fail(partinfo != NULL);
995
996         filename = procmime_get_tmp_file_name(partinfo);
997
998         if (procmime_get_part(filename, partinfo) < 0)
999                 alertpanel_error
1000                         (_("Can't save the part of multipart message."));
1001         else
1002                 mimeview_view_file(filename, partinfo, NULL);
1003
1004         g_free(filename);
1005 }
1006
1007 static void mimeview_open_with(MimeView *mimeview)
1008 {
1009         MimeInfo *partinfo;
1010         gchar *filename;
1011         gchar *cmd;
1012
1013         if (!mimeview->opened) return;
1014         if (!mimeview->file) return;
1015
1016         partinfo = mimeview_get_selected_part(mimeview);
1017         if (!partinfo) { 
1018                 partinfo = (MimeInfo *) gtk_object_get_data
1019                         (GTK_OBJECT(mimeview->popupmenu),
1020                          "pop_partinfo");
1021                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1022                                     "pop_partinfo", NULL);
1023         }                        
1024         g_return_if_fail(partinfo != NULL);
1025
1026         filename = procmime_get_tmp_file_name(partinfo);
1027
1028         if (procmime_get_part(filename, partinfo) < 0) {
1029                 alertpanel_error
1030                         (_("Can't save the part of multipart message."));
1031                 g_free(filename);
1032                 return;
1033         }
1034
1035         if (!prefs_common.mime_open_cmd_history)
1036                 prefs_common.mime_open_cmd_history =
1037                         add_history(NULL, prefs_common.mime_open_cmd);
1038
1039         cmd = input_dialog_combo
1040                 (_("Open with"),
1041                  _("Enter the command line to open file:\n"
1042                    "(`%s' will be replaced with file name)"),
1043                  prefs_common.mime_open_cmd,
1044                  prefs_common.mime_open_cmd_history,
1045                  TRUE);
1046         if (cmd) {
1047                 mimeview_view_file(filename, partinfo, cmd);
1048                 g_free(prefs_common.mime_open_cmd);
1049                 prefs_common.mime_open_cmd = cmd;
1050                 prefs_common.mime_open_cmd_history =
1051                         add_history(prefs_common.mime_open_cmd_history, cmd);
1052         }
1053
1054         g_free(filename);
1055 }
1056
1057 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1058                                const gchar *cmdline)
1059 {
1060         static gchar *default_image_cmdline = "display '%s'";
1061         static gchar *default_audio_cmdline = "play '%s'";
1062         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1063         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1064         gchar buf[1024];
1065         gchar m_buf[1024];
1066         const gchar *cmd;
1067         const gchar *def_cmd;
1068         const gchar *p;
1069
1070         if (cmdline) {
1071                 cmd = cmdline;
1072                 def_cmd = NULL;
1073         } else if (MIMETYPE_APPLICATION == partinfo->type) {
1074                 return;
1075         } else if (MIMETYPE_IMAGE == partinfo->type) {
1076                 cmd = prefs_common.mime_image_viewer;
1077                 def_cmd = default_image_cmdline;
1078         } else if (MIMETYPE_AUDIO == partinfo->type) {
1079                 cmd = prefs_common.mime_audio_player;
1080                 def_cmd = default_audio_cmdline;
1081         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1082                 cmd = prefs_common.uri_cmd;
1083                 def_cmd = default_html_cmdline;
1084         } else {
1085                 gchar *content_type;
1086                 
1087                 content_type = g_strdup_printf("%s/%s", procmime_get_type_str(partinfo->type), partinfo->subtype);
1088                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1089                            content_type, "%s");
1090                 g_free(content_type);
1091                 cmd = m_buf;
1092                 def_cmd = NULL;
1093         }
1094
1095         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1096             !strchr(p + 2, '%'))
1097                 g_snprintf(buf, sizeof(buf), cmd, filename);
1098         else {
1099                 if (cmd)
1100                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1101                 if (def_cmd)
1102                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1103                 else
1104                         return;
1105         }
1106
1107         execute_command_line(buf, TRUE);
1108 }
1109
1110 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1111 {
1112         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1113 }
1114
1115 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1116 {
1117         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1118 }
1119
1120 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1121 {
1122         GSList *mimeview_list, *viewer_list;
1123
1124         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1125                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1126                 
1127                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1128                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1129                         mimeview->mimeviewer = NULL;
1130                 }
1131
1132                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1133                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1134
1135                         mimeviewer->destroy_viewer(mimeviewer);
1136                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1137                 }
1138         }
1139
1140         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1141 }
1142
1143 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1144 {
1145         gint      num;
1146         MimeInfo *partinfo;
1147
1148         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1149         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1150         if (event->button == 1) { 
1151                 icon_selected(mimeview, num, partinfo);
1152                 gtk_widget_grab_focus(button);
1153                 
1154                 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
1155                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1156                 else
1157                         gtk_signal_emit_stop_by_name(GTK_OBJECT(button), "button_press_event");
1158         }               
1159
1160         part_button_pressed(mimeview, event, partinfo);
1161
1162         return TRUE;
1163 }
1164
1165 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1166 {
1167         GtkCTreeNode *node;
1168         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1169         if (node)
1170                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1171 }               
1172
1173 #undef  KEY_PRESS_EVENT_STOP
1174 #define KEY_PRESS_EVENT_STOP() \
1175         if (gtk_signal_n_emissions_by_name \
1176                 (GTK_OBJECT(button), "key_press_event") > 0) { \
1177                 gtk_signal_emit_stop_by_name(GTK_OBJECT(button), \
1178                                              "key_press_event"); \
1179         }
1180 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1181                              MimeView *mimeview)
1182 {
1183         gint          num;
1184         MimeInfo     *partinfo;
1185         SummaryView  *summaryview;
1186         TextView     *textview;
1187
1188         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1189         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1190         
1191         if (!event) return FALSE;
1192
1193         textview = mimeview->textview;
1194
1195         switch (event->keyval) {
1196         case GDK_space:
1197                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1198                         /* stop the button being untoggled */
1199                         KEY_PRESS_EVENT_STOP();
1200                         if (textview_scroll_page(textview, FALSE))
1201                                 return TRUE;
1202
1203                         if (icon_list_select_by_number(mimeview, num + 1))
1204                                 return TRUE;
1205                 } else {
1206                         icon_selected(mimeview, num, partinfo);
1207                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1208                         return TRUE;
1209                 }
1210
1211                 break;
1212         case GDK_BackSpace:
1213                 textview_scroll_page(textview, TRUE);
1214                 return TRUE;
1215         case GDK_Return:
1216                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1217                         KEY_PRESS_EVENT_STOP();
1218                         textview_scroll_one_line(textview,
1219                                                  (event->state & GDK_MOD1_MASK) != 0);
1220                         return TRUE;
1221                 } else {
1222                         icon_selected(mimeview, num, partinfo);
1223                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1224                         return TRUE;
1225                 }
1226
1227         case GDK_n:
1228         case GDK_N:
1229                 BREAK_ON_MODIFIER_KEY();
1230                 if (icon_list_select_by_number(mimeview, num + 1))
1231                         KEY_PRESS_EVENT_STOP();
1232                         return TRUE;
1233                 break;
1234                 
1235         case GDK_p:
1236         case GDK_P:
1237                 BREAK_ON_MODIFIER_KEY();
1238                 if (icon_list_select_by_number(mimeview, num - 1))
1239                         KEY_PRESS_EVENT_STOP();
1240                         return TRUE;
1241                 break;
1242
1243         case GDK_y:
1244                 BREAK_ON_MODIFIER_KEY();
1245                 KEY_PRESS_EVENT_STOP();
1246                 mimeview_save_as(mimeview);
1247                 return TRUE;
1248         case GDK_t:
1249                 BREAK_ON_MODIFIER_KEY();
1250                 KEY_PRESS_EVENT_STOP();
1251                 mimeview_display_as_text(mimeview);
1252                 return TRUE;    
1253         case GDK_l:
1254                 BREAK_ON_MODIFIER_KEY();
1255                 KEY_PRESS_EVENT_STOP();
1256                 mimeview_launch(mimeview);
1257                 return TRUE;
1258         default:
1259                 break;
1260         }
1261
1262         if (!mimeview->messageview->mainwin) return FALSE;
1263         summaryview = mimeview->messageview->mainwin->summaryview;
1264         summary_pass_key_press_event(summaryview, event);
1265         return TRUE;
1266 }
1267
1268 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1269 {
1270         GList *child;
1271         
1272         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1273         for (; child != NULL; child = g_list_next(child)) {
1274                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1275                     GTK_TOGGLE_BUTTON(child->data) != button &&
1276                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1277                         gtk_toggle_button_set_active
1278                                 (GTK_TOGGLE_BUTTON(child->data),
1279                                  FALSE);
1280         }
1281 }
1282
1283 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1284 {
1285         GtkWidget *pixmap;
1286         GtkWidget *vbox;
1287         GtkWidget *button;
1288         gchar *tip;
1289         gchar *desc = NULL;
1290         StockPixmap stockp;
1291         
1292         vbox = mimeview->icon_vbox;
1293         mimeview->icon_count++;
1294         button = gtk_toggle_button_new();
1295         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1296         gtk_object_set_data(GTK_OBJECT(button), "icon_number", 
1297                 GINT_TO_POINTER(mimeview->icon_count));
1298         gtk_object_set_data(GTK_OBJECT(button), "partinfo", 
1299                 mimeinfo);
1300         
1301         switch (mimeinfo->type) {
1302                 
1303         case MIMETYPE_TEXT:
1304                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "html"))
1305                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1306                 else if  (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "enriched"))
1307                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1308                 else
1309                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1310                 break;
1311         case MIMETYPE_MESSAGE:
1312                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1313                 break;
1314         case MIMETYPE_APPLICATION:
1315                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "octet-stream"))
1316                         stockp = STOCK_PIXMAP_MIME_APPLICATION_OCTET_STREAM;
1317                 else
1318                         stockp = STOCK_PIXMAP_MIME_APPLICATION;
1319                 break;
1320         case MIMETYPE_IMAGE:
1321                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1322                 break;
1323         case MIMETYPE_AUDIO:
1324                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1325                 break;
1326         default:
1327                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1328                 break;
1329         }
1330         
1331         pixmap = stock_pixmap_widget(mimeview->mainwin->window, stockp);
1332         gtk_container_add(GTK_CONTAINER(button), pixmap);
1333         
1334         if (!desc) {
1335                 if (prefs_common.attach_desc)
1336                         desc = get_part_description(mimeinfo);
1337                 else
1338                         desc = get_part_name(mimeinfo);
1339         }
1340
1341         if (desc && *desc)
1342                 tip = g_strdup_printf("%s\n%s/%s\n%s", desc,
1343                                 procmime_get_type_str(mimeinfo->type),
1344                                 mimeinfo->subtype, 
1345                                 to_human_readable(mimeinfo->length));
1346         else            
1347                 tip = g_strdup_printf("%s/%s\n%s",
1348                                 procmime_get_type_str(mimeinfo->type),
1349                                 mimeinfo->subtype, 
1350                                 to_human_readable(mimeinfo->length));
1351
1352         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1353         g_free(tip);
1354         gtk_widget_show_all(button);
1355         gtk_signal_connect(GTK_OBJECT(button), "button_press_event", 
1356                            GTK_SIGNAL_FUNC(icon_clicked_cb), mimeview);
1357         gtk_signal_connect(GTK_OBJECT(button), "key_press_event", 
1358                            GTK_SIGNAL_FUNC(icon_key_pressed), mimeview);
1359         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1360
1361 }
1362
1363 static void icon_list_clear (MimeView *mimeview)
1364 {
1365         GList     *child;
1366         GtkAdjustment *adj;
1367         
1368         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1369         for (; child != NULL; child = g_list_next(child)) {
1370                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1371                                        GTK_WIDGET(child->data));
1372         }
1373         mimeview->icon_count = 0;
1374         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1375         adj->value = adj->lower;
1376         gtk_signal_emit_by_name(GTK_OBJECT (adj), "value_changed");
1377 }
1378
1379 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
1380                                           MimeInfo      *mimeinfo)
1381 {
1382         GList *child;
1383         
1384         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1385         for (; child != NULL; child = g_list_next(child)) {
1386                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1387                     gtk_object_get_data(GTK_OBJECT(child->data),
1388                                         "partinfo") == (gpointer)mimeinfo) {
1389                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1390                         gtk_toggle_button_set_active
1391                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1392                 }                                
1393         }
1394 }
1395
1396 /*!
1397  *\brief        Used to 'click' the next or previous icon.
1398  *
1399  *\return       true if the icon 'number' exists and was selected.
1400  */
1401 static gboolean icon_list_select_by_number(MimeView     *mimeview,
1402                                            gint          number)
1403 {
1404         GList *child;
1405
1406         if (number == 0) return FALSE;
1407         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1408         for (; child != NULL; child = g_list_next(child)) {
1409                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1410                     GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(child->data),
1411                                         "icon_number")) == number) {
1412                         icon_selected(mimeview, number,
1413                                       (MimeInfo*)gtk_object_get_data(GTK_OBJECT(child->data),
1414                                                                      "partinfo"));
1415                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1416                         gtk_toggle_button_set_active
1417                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1418                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
1419                 
1420                         return TRUE;
1421                 }                                
1422         }
1423         return FALSE;
1424 }
1425
1426 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1427                                         GtkAllocation *size, MimeView *mimeview)
1428 {
1429         GtkAllocation *mainbox_size;
1430         GtkAllocation *vbox_size;
1431         GtkAllocation *layout_size;
1432         GtkAdjustment *adj;
1433         
1434         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1435
1436         mainbox_size = &mimeview->icon_mainbox->allocation;
1437         vbox_size = &mimeview->icon_vbox->allocation;
1438         layout_size = &mimeview->icon_scroll->allocation;
1439         
1440         /* centralise the vbox */
1441         gtk_layout_move(GTK_LAYOUT(mimeview->icon_scroll), mimeview->icon_vbox, 
1442                         (mainbox_size->width - vbox_size->width)/2, 0);
1443         
1444         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1445                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1446                             MAX(vbox_size->height, layout_size->height));
1447         adj->step_increment = 5;
1448 }
1449
1450 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1451 {
1452         GtkRequisition size;
1453         gint           width;
1454         g_return_if_fail(mimeinfo != NULL);
1455
1456         while (mimeinfo != NULL) {
1457                 if (mimeinfo->type != MIMETYPE_MULTIPART)
1458                         icon_list_append_icon(mimeview, mimeinfo);
1459                 if (mimeinfo->children != NULL)
1460                         icon_list_create(mimeview, mimeinfo->children);
1461                 mimeinfo = mimeinfo->next;
1462         }
1463         gtk_widget_size_request(mimeview->icon_vbox, &size);
1464         width = size.width + 4;
1465         if (width > mimeview->icon_mainbox->requisition.width) {
1466                 gtk_widget_set_usize(mimeview->icon_mainbox, 
1467                                      width, -1);
1468         }
1469
1470 }
1471
1472 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1473 {
1474         gtk_widget_ref(button); 
1475
1476         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1477                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1478                                         GTK_SHADOW_NONE);
1479                 gtk_widget_hide(mimeview->icon_mainbox);
1480                 gtk_widget_show(mimeview->ctree_mainbox);
1481                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1482
1483                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1484                                         button);
1485                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1486                                    button, FALSE, FALSE, 0);
1487                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
1488         } else {
1489                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1490                               GTK_SHADOW_NONE);
1491                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1492                 gtk_widget_hide(mimeview->ctree_mainbox);
1493                 gtk_widget_show(mimeview->icon_mainbox);
1494                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1495
1496                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1497                                         button);
1498                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1499                                    button, FALSE, FALSE, 0);
1500                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1501                 if (mimeview->opened)
1502                         icon_list_toggle_by_mime_info
1503                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
1504                                                                        mimeview->opened));
1505
1506                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
1507         }
1508         gtk_widget_grab_focus(button);
1509         gtk_widget_unref(button);
1510
1511 }
1512
1513 void mimeview_update (MimeView *mimeview) {
1514         if (mimeview && mimeview->mimeinfo) {
1515                 icon_list_clear(mimeview);
1516                 icon_list_create(mimeview, mimeview->mimeinfo);
1517         }
1518 }