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