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