24e9cb1c4d7cb23f540d96c22bde928d5d8a5d9d
[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->node->children)
394                         mimeview_set_multipart_tree(mimeview, (MimeInfo *) mimeinfo->node->children->data, node);
395                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
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         if (!is_dir_exist (dirname)) {
877                 alertpanel_error(_("`%s' is not a directory."),
878                                  dirname);
879                 g_free (dirname);
880                 return;
881         }
882         
883         { /* add a / after the dirname, in case the user didn't */
884                 gchar *dirname_tmp = NULL;
885                 int dirname_last_char = strlen (dirname) - 1;
886
887                 if (dirname[dirname_last_char] != G_DIR_SEPARATOR) {
888                         dirname_tmp = g_strconcat (dirname, G_DIR_SEPARATOR_S, NULL);
889                         g_free (dirname);
890                         dirname = dirname_tmp;
891                 }
892         }
893
894         /* return to first children */
895         if (partinfo->node->parent->children == NULL) return;  /* multipart container? */
896         attachment = partinfo->node->parent->children->next != NULL ?
897             (MimeInfo *) partinfo->node->parent->children->next->data : NULL;
898         /* for each attachment, extract it in the selected dir. */
899         while (attachment != NULL) {
900                 static guint subst_cnt = 1;
901                 gchar *attachdir;
902                 gchar *attachname = g_strdup(get_part_name(attachment));
903                 AlertValue aval = G_ALERTDEFAULT;
904                 gchar *res;
905
906                 if (!attachname || !strlen(attachname))
907                         attachname = g_strdup_printf("noname.%d",subst_cnt++);
908                 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
909                 g_snprintf(buf, sizeof(buf), "%s%s",
910                            dirname,
911                            (attachname[0] == G_DIR_SEPARATOR)
912                            ? &attachname[1]
913                            : attachname);
914                 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
915                 attachdir = g_dirname(buf);
916                 make_dir_hier(attachdir);
917                 g_free(attachdir);
918
919                 if (is_file_exist(buf)) {
920                         res = g_strdup_printf(_("Overwrite existing file '%s'?"),
921                                               attachname);
922                         aval = alertpanel(_("Overwrite"), res, _("OK"), 
923                                           _("Cancel"), NULL);
924                         g_free(res);                                      
925                 }
926                 g_free(attachname);
927
928                 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, attachment) < 0))
929                         alertpanel_error(_("Can't save the part of multipart message."));
930                 attachment = attachment->node->next != NULL ? (MimeInfo *) attachment->node->next->data : NULL;
931         }
932 }
933
934 static void mimeview_display_as_text(MimeView *mimeview)
935 {
936         MimeInfo *partinfo;
937
938         if (!mimeview->opened) return;
939
940         partinfo = mimeview_get_selected_part(mimeview);
941         if (!partinfo)  {
942                 partinfo = (MimeInfo *) gtk_object_get_data
943                         (GTK_OBJECT(mimeview->popupmenu),
944                          "pop_partinfo");
945                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
946                                     "pop_partinfo", NULL);
947         
948         }                        
949         g_return_if_fail(partinfo != NULL);
950         mimeview_show_message_part(mimeview, partinfo);
951 }
952
953 static void mimeview_save_as(MimeView *mimeview)
954 {
955         gchar *filename;
956         gchar *defname = NULL;
957         MimeInfo *partinfo;
958         gchar *res;
959
960         if (!mimeview->opened) return;
961         if (!mimeview->file) return;
962
963         partinfo = mimeview_get_selected_part(mimeview);
964         if (!partinfo) { 
965                 partinfo = (MimeInfo *) gtk_object_get_data
966                         (GTK_OBJECT(mimeview->popupmenu),
967                          "pop_partinfo");
968                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
969                                     "pop_partinfo", NULL);
970         }                        
971         g_return_if_fail(partinfo != NULL);
972
973         if (partinfo->filename)
974                 defname = partinfo->filename;
975         else if (partinfo->name) {
976                 Xstrdup_a(defname, partinfo->name, return);
977                 subst_for_filename(defname);
978         }
979
980         filename = filesel_select_file(_("Save as"), defname);
981         if (!filename) return;
982         if (is_file_exist(filename)) {
983                 AlertValue aval;
984                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
985                                       filename);
986                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
987                                   _("Cancel"), NULL);
988                 g_free(res);                                      
989                 if (G_ALERTDEFAULT != aval) return;
990         }
991
992         if (procmime_get_part(filename, partinfo) < 0)
993                 alertpanel_error
994                         (_("Can't save the part of multipart message."));
995 }
996
997 static void mimeview_launch(MimeView *mimeview)
998 {
999         MimeInfo *partinfo;
1000         gchar *filename;
1001
1002         if (!mimeview->opened) return;
1003         if (!mimeview->file) return;
1004
1005         partinfo = mimeview_get_selected_part(mimeview);
1006         if (!partinfo) { 
1007                 partinfo = (MimeInfo *) gtk_object_get_data
1008                         (GTK_OBJECT(mimeview->popupmenu),
1009                          "pop_partinfo");
1010                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1011                                     "pop_partinfo", NULL);
1012         }                        
1013         g_return_if_fail(partinfo != NULL);
1014
1015         filename = procmime_get_tmp_file_name(partinfo);
1016
1017         if (procmime_get_part(filename, partinfo) < 0)
1018                 alertpanel_error
1019                         (_("Can't save the part of multipart message."));
1020         else
1021                 mimeview_view_file(filename, partinfo, NULL);
1022
1023         g_free(filename);
1024 }
1025
1026 static void mimeview_open_with(MimeView *mimeview)
1027 {
1028         MimeInfo *partinfo;
1029         gchar *filename;
1030         gchar *cmd;
1031
1032         if (!mimeview->opened) return;
1033         if (!mimeview->file) return;
1034
1035         partinfo = mimeview_get_selected_part(mimeview);
1036         if (!partinfo) { 
1037                 partinfo = (MimeInfo *) gtk_object_get_data
1038                         (GTK_OBJECT(mimeview->popupmenu),
1039                          "pop_partinfo");
1040                 gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
1041                                     "pop_partinfo", NULL);
1042         }                        
1043         g_return_if_fail(partinfo != NULL);
1044
1045         filename = procmime_get_tmp_file_name(partinfo);
1046
1047         if (procmime_get_part(filename, partinfo) < 0) {
1048                 alertpanel_error
1049                         (_("Can't save the part of multipart message."));
1050                 g_free(filename);
1051                 return;
1052         }
1053
1054         if (!prefs_common.mime_open_cmd_history)
1055                 prefs_common.mime_open_cmd_history =
1056                         add_history(NULL, prefs_common.mime_open_cmd);
1057
1058         cmd = input_dialog_combo
1059                 (_("Open with"),
1060                  _("Enter the command line to open file:\n"
1061                    "(`%s' will be replaced with file name)"),
1062                  prefs_common.mime_open_cmd,
1063                  prefs_common.mime_open_cmd_history,
1064                  TRUE);
1065         if (cmd) {
1066                 mimeview_view_file(filename, partinfo, cmd);
1067                 g_free(prefs_common.mime_open_cmd);
1068                 prefs_common.mime_open_cmd = cmd;
1069                 prefs_common.mime_open_cmd_history =
1070                         add_history(prefs_common.mime_open_cmd_history, cmd);
1071         }
1072
1073         g_free(filename);
1074 }
1075
1076 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1077                                const gchar *cmdline)
1078 {
1079         static gchar *default_image_cmdline = "display '%s'";
1080         static gchar *default_audio_cmdline = "play '%s'";
1081         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1082         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1083         gchar buf[1024];
1084         gchar m_buf[1024];
1085         const gchar *cmd;
1086         const gchar *def_cmd;
1087         const gchar *p;
1088
1089         if (cmdline) {
1090                 cmd = cmdline;
1091                 def_cmd = NULL;
1092         } else if (MIMETYPE_APPLICATION == partinfo->type) {
1093                 return;
1094         } else if (MIMETYPE_IMAGE == partinfo->type) {
1095                 cmd = prefs_common.mime_image_viewer;
1096                 def_cmd = default_image_cmdline;
1097         } else if (MIMETYPE_AUDIO == partinfo->type) {
1098                 cmd = prefs_common.mime_audio_player;
1099                 def_cmd = default_audio_cmdline;
1100         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1101                 cmd = prefs_common.uri_cmd;
1102                 def_cmd = default_html_cmdline;
1103         } else {
1104                 gchar *content_type;
1105                 
1106                 content_type = g_strdup_printf("%s/%s", procmime_get_type_str(partinfo->type), partinfo->subtype);
1107                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1108                            content_type, "%s");
1109                 g_free(content_type);
1110                 cmd = m_buf;
1111                 def_cmd = NULL;
1112         }
1113
1114         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1115             !strchr(p + 2, '%'))
1116                 g_snprintf(buf, sizeof(buf), cmd, filename);
1117         else {
1118                 if (cmd)
1119                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1120                 if (def_cmd)
1121                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1122                 else
1123                         return;
1124         }
1125
1126         execute_command_line(buf, TRUE);
1127 }
1128
1129 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1130 {
1131         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1132 }
1133
1134 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1135 {
1136         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1137 }
1138
1139 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1140 {
1141         GSList *mimeview_list, *viewer_list;
1142
1143         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1144                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1145                 
1146                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1147                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1148                         mimeview->mimeviewer = NULL;
1149                 }
1150
1151                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1152                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1153
1154                         mimeviewer->destroy_viewer(mimeviewer);
1155                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1156                 }
1157         }
1158
1159         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1160 }
1161
1162 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1163 {
1164         gint      num;
1165         MimeInfo *partinfo;
1166
1167         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1168         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1169         if (event->button == 1) { 
1170                 icon_selected(mimeview, num, partinfo);
1171                 gtk_widget_grab_focus(button);
1172                 
1173                 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
1174                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1175                 else
1176                         gtk_signal_emit_stop_by_name(GTK_OBJECT(button), "button_press_event");
1177         }               
1178
1179         part_button_pressed(mimeview, event, partinfo);
1180
1181         return TRUE;
1182 }
1183
1184 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1185 {
1186         GtkCTreeNode *node;
1187         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1188         if (node)
1189                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1190 }               
1191
1192 #undef  KEY_PRESS_EVENT_STOP
1193 #define KEY_PRESS_EVENT_STOP() \
1194         if (gtk_signal_n_emissions_by_name \
1195                 (GTK_OBJECT(button), "key_press_event") > 0) { \
1196                 gtk_signal_emit_stop_by_name(GTK_OBJECT(button), \
1197                                              "key_press_event"); \
1198         }
1199 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1200                              MimeView *mimeview)
1201 {
1202         gint          num;
1203         MimeInfo     *partinfo;
1204         SummaryView  *summaryview;
1205         TextView     *textview;
1206
1207         num      = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
1208         partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
1209         
1210         if (!event) return FALSE;
1211
1212         textview = mimeview->textview;
1213
1214         switch (event->keyval) {
1215         case GDK_space:
1216                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1217                         /* stop the button being untoggled */
1218                         KEY_PRESS_EVENT_STOP();
1219                         if (textview_scroll_page(textview, FALSE))
1220                                 return TRUE;
1221
1222                         if (icon_list_select_by_number(mimeview, num + 1))
1223                                 return TRUE;
1224                 } else {
1225                         icon_selected(mimeview, num, partinfo);
1226                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1227                         return TRUE;
1228                 }
1229
1230                 break;
1231         case GDK_BackSpace:
1232                 textview_scroll_page(textview, TRUE);
1233                 return TRUE;
1234         case GDK_Return:
1235                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1236                         KEY_PRESS_EVENT_STOP();
1237                         textview_scroll_one_line(textview,
1238                                                  (event->state & GDK_MOD1_MASK) != 0);
1239                         return TRUE;
1240                 } else {
1241                         icon_selected(mimeview, num, partinfo);
1242                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1243                         return TRUE;
1244                 }
1245
1246         case GDK_n:
1247         case GDK_N:
1248                 BREAK_ON_MODIFIER_KEY();
1249                 if (icon_list_select_by_number(mimeview, num + 1)) {
1250                         KEY_PRESS_EVENT_STOP();
1251                         return TRUE;
1252                 }
1253                 break;
1254                 
1255         case GDK_p:
1256         case GDK_P:
1257                 BREAK_ON_MODIFIER_KEY();
1258                 if (icon_list_select_by_number(mimeview, num - 1)) {
1259                         KEY_PRESS_EVENT_STOP();
1260                         return TRUE;
1261                 }
1262                 break;
1263
1264         case GDK_y:
1265                 BREAK_ON_MODIFIER_KEY();
1266                 KEY_PRESS_EVENT_STOP();
1267                 mimeview_save_as(mimeview);
1268                 return TRUE;
1269         case GDK_t:
1270                 BREAK_ON_MODIFIER_KEY();
1271                 KEY_PRESS_EVENT_STOP();
1272                 mimeview_display_as_text(mimeview);
1273                 return TRUE;    
1274         case GDK_l:
1275                 BREAK_ON_MODIFIER_KEY();
1276                 KEY_PRESS_EVENT_STOP();
1277                 mimeview_launch(mimeview);
1278                 return TRUE;
1279         default:
1280                 break;
1281         }
1282
1283         if (!mimeview->messageview->mainwin) return FALSE;
1284         summaryview = mimeview->messageview->mainwin->summaryview;
1285         summary_pass_key_press_event(summaryview, event);
1286         return TRUE;
1287 }
1288
1289 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1290 {
1291         GList *child;
1292         
1293         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1294         for (; child != NULL; child = g_list_next(child)) {
1295                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1296                     GTK_TOGGLE_BUTTON(child->data) != button &&
1297                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1298                         gtk_toggle_button_set_active
1299                                 (GTK_TOGGLE_BUTTON(child->data),
1300                                  FALSE);
1301         }
1302 }
1303
1304 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1305 {
1306         GtkWidget *pixmap;
1307         GtkWidget *vbox;
1308         GtkWidget *button;
1309         gchar *tip;
1310         gchar *desc = NULL;
1311         StockPixmap stockp;
1312         
1313         vbox = mimeview->icon_vbox;
1314         mimeview->icon_count++;
1315         button = gtk_toggle_button_new();
1316         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1317         gtk_object_set_data(GTK_OBJECT(button), "icon_number", 
1318                 GINT_TO_POINTER(mimeview->icon_count));
1319         gtk_object_set_data(GTK_OBJECT(button), "partinfo", 
1320                 mimeinfo);
1321         
1322         switch (mimeinfo->type) {
1323                 
1324         case MIMETYPE_TEXT:
1325                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "html"))
1326                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1327                 else if  (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "enriched"))
1328                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1329                 else
1330                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1331                 break;
1332         case MIMETYPE_MESSAGE:
1333                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1334                 break;
1335         case MIMETYPE_APPLICATION:
1336                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "octet-stream"))
1337                         stockp = STOCK_PIXMAP_MIME_APPLICATION_OCTET_STREAM;
1338                 else
1339                         stockp = STOCK_PIXMAP_MIME_APPLICATION;
1340                 break;
1341         case MIMETYPE_IMAGE:
1342                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1343                 break;
1344         case MIMETYPE_AUDIO:
1345                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1346                 break;
1347         default:
1348                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1349                 break;
1350         }
1351         
1352         pixmap = stock_pixmap_widget(mimeview->mainwin->window, stockp);
1353         gtk_container_add(GTK_CONTAINER(button), pixmap);
1354         
1355         if (!desc) {
1356                 if (prefs_common.attach_desc)
1357                         desc = get_part_description(mimeinfo);
1358                 else
1359                         desc = get_part_name(mimeinfo);
1360         }
1361
1362         if (desc && *desc)
1363                 tip = g_strdup_printf("%s\n%s/%s\n%s", desc,
1364                                 procmime_get_type_str(mimeinfo->type),
1365                                 mimeinfo->subtype, 
1366                                 to_human_readable(mimeinfo->length));
1367         else            
1368                 tip = g_strdup_printf("%s/%s\n%s",
1369                                 procmime_get_type_str(mimeinfo->type),
1370                                 mimeinfo->subtype, 
1371                                 to_human_readable(mimeinfo->length));
1372
1373         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1374         g_free(tip);
1375         gtk_widget_show_all(button);
1376         gtk_signal_connect(GTK_OBJECT(button), "button_press_event", 
1377                            GTK_SIGNAL_FUNC(icon_clicked_cb), mimeview);
1378         gtk_signal_connect(GTK_OBJECT(button), "key_press_event", 
1379                            GTK_SIGNAL_FUNC(icon_key_pressed), mimeview);
1380         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1381
1382 }
1383
1384 static void icon_list_clear (MimeView *mimeview)
1385 {
1386         GList     *child;
1387         GtkAdjustment *adj;
1388         
1389         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1390         for (; child != NULL; child = g_list_next(child)) {
1391                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1392                                        GTK_WIDGET(child->data));
1393         }
1394         mimeview->icon_count = 0;
1395         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1396         adj->value = adj->lower;
1397         gtk_signal_emit_by_name(GTK_OBJECT (adj), "value_changed");
1398 }
1399
1400 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
1401                                           MimeInfo      *mimeinfo)
1402 {
1403         GList *child;
1404         
1405         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1406         for (; child != NULL; child = g_list_next(child)) {
1407                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1408                     gtk_object_get_data(GTK_OBJECT(child->data),
1409                                         "partinfo") == (gpointer)mimeinfo) {
1410                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1411                         gtk_toggle_button_set_active
1412                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1413                 }                                
1414         }
1415 }
1416
1417 /*!
1418  *\brief        Used to 'click' the next or previous icon.
1419  *
1420  *\return       true if the icon 'number' exists and was selected.
1421  */
1422 static gboolean icon_list_select_by_number(MimeView     *mimeview,
1423                                            gint          number)
1424 {
1425         GList *child;
1426
1427         if (number == 0) return FALSE;
1428         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1429         for (; child != NULL; child = g_list_next(child)) {
1430                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1431                     GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(child->data),
1432                                         "icon_number")) == number) {
1433                         icon_selected(mimeview, number,
1434                                       (MimeInfo*)gtk_object_get_data(GTK_OBJECT(child->data),
1435                                                                      "partinfo"));
1436                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1437                         gtk_toggle_button_set_active
1438                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1439                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
1440                 
1441                         return TRUE;
1442                 }                                
1443         }
1444         return FALSE;
1445 }
1446
1447 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1448                                         GtkAllocation *size, MimeView *mimeview)
1449 {
1450         GtkAllocation *mainbox_size;
1451         GtkAllocation *vbox_size;
1452         GtkAllocation *layout_size;
1453         GtkAdjustment *adj;
1454         
1455         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1456
1457         mainbox_size = &mimeview->icon_mainbox->allocation;
1458         vbox_size = &mimeview->icon_vbox->allocation;
1459         layout_size = &mimeview->icon_scroll->allocation;
1460         
1461         /* centralise the vbox */
1462         gtk_layout_move(GTK_LAYOUT(mimeview->icon_scroll), mimeview->icon_vbox, 
1463                         (mainbox_size->width - vbox_size->width)/2, 0);
1464         
1465         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1466                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1467                             MAX(vbox_size->height, layout_size->height));
1468         adj->step_increment = 5;
1469 }
1470
1471 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1472 {
1473         GtkRequisition size;
1474         gint           width;
1475         g_return_if_fail(mimeinfo != NULL);
1476
1477         while (mimeinfo != NULL) {
1478                 if (mimeinfo->type != MIMETYPE_MULTIPART)
1479                         icon_list_append_icon(mimeview, mimeinfo);
1480                 if (mimeinfo->node->children != NULL)
1481                         icon_list_create(mimeview, (MimeInfo *) mimeinfo->node->children->data);
1482                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
1483         }
1484         gtk_widget_size_request(mimeview->icon_vbox, &size);
1485         width = size.width + 4;
1486         if (width > mimeview->icon_mainbox->requisition.width) {
1487                 gtk_widget_set_usize(mimeview->icon_mainbox, 
1488                                      width, -1);
1489         }
1490
1491 }
1492
1493 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1494 {
1495         gtk_widget_ref(button); 
1496
1497         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1498                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1499                                         GTK_SHADOW_NONE);
1500                 gtk_widget_hide(mimeview->icon_mainbox);
1501                 gtk_widget_show(mimeview->ctree_mainbox);
1502                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1503
1504                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1505                                         button);
1506                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1507                                    button, FALSE, FALSE, 0);
1508                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
1509         } else {
1510                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1511                               GTK_SHADOW_NONE);
1512                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1513                 gtk_widget_hide(mimeview->ctree_mainbox);
1514                 gtk_widget_show(mimeview->icon_mainbox);
1515                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1516
1517                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1518                                         button);
1519                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1520                                    button, FALSE, FALSE, 0);
1521                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1522                 if (mimeview->opened)
1523                         icon_list_toggle_by_mime_info
1524                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
1525                                                                        mimeview->opened));
1526
1527                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
1528         }
1529         gtk_widget_grab_focus(button);
1530         gtk_widget_unref(button);
1531
1532 }
1533
1534 void mimeview_update (MimeView *mimeview) {
1535         if (mimeview && mimeview->mimeinfo) {
1536                 icon_list_clear(mimeview);
1537                 icon_list_create(mimeview, mimeview->mimeinfo);
1538         }
1539 }