2004-10-01 [colin] 0.9.12cvs117.1
[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_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_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_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_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         while (partinfo != NULL) {
1078                 if (partinfo->type != MIMETYPE_MESSAGE &&
1079                     partinfo->type != MIMETYPE_MULTIPART &&
1080                     partinfo->disposition == DISPOSITIONTYPE_ATTACHMENT) {
1081                         gchar *filename = mimeview_get_filename_for_part
1082                                 (partinfo, dirname, number++);
1083
1084                         mimeview_write_part(filename, partinfo);
1085                         g_free(filename);
1086                 }
1087                 partinfo = procmime_mimeinfo_next(partinfo);
1088         }
1089
1090         if (prefs_common.attach_save_dir)
1091                 g_free(prefs_common.attach_save_dir);
1092
1093         prefs_common.attach_save_dir = g_strdup(dirname);
1094
1095         if (startdir) g_free(startdir);
1096 }
1097
1098 /**
1099  * Menu callback: Save the selected attachment
1100  * \param mimeview Current display
1101  */
1102 static void mimeview_save_as(MimeView *mimeview)
1103 {
1104         gchar *filename;
1105         gchar *filepath = NULL;
1106         gchar *filedir = NULL;
1107         MimeInfo *partinfo;
1108         const gchar *partname = NULL;
1109
1110         if (!mimeview->opened) return;
1111         if (!mimeview->file) return;
1112
1113         partinfo = mimeview_get_selected_part(mimeview);
1114         if (!partinfo) { 
1115                 partinfo = (MimeInfo *) g_object_get_data
1116                          (G_OBJECT(mimeview->popupmenu),
1117                          "pop_partinfo");
1118                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1119                                   "pop_partinfo", NULL);
1120         }                        
1121         g_return_if_fail(partinfo != NULL);
1122         
1123         if ((partname = get_part_name(partinfo)) == NULL) {
1124                 return;
1125         }
1126
1127         if (prefs_common.attach_save_dir)
1128                 filepath = g_strconcat(prefs_common.attach_save_dir,
1129                                        G_DIR_SEPARATOR_S, partname, NULL);
1130         else
1131                 filepath = g_strdup(partname);
1132
1133         filename = filesel_select_file_save(_("Save as"), filepath);
1134         if (!filename) {
1135                 g_free(filepath);
1136                 return;
1137         }
1138
1139         mimeview_write_part(filename, partinfo);
1140
1141         filedir = g_dirname(filename);
1142         if (filedir && strcmp(filedir, ".")) {
1143                 if (prefs_common.attach_save_dir)
1144                         g_free(prefs_common.attach_save_dir);
1145                 prefs_common.attach_save_dir = g_strdup(filedir);
1146         }
1147
1148         g_free(filedir);
1149         g_free(filepath);
1150 }
1151
1152 static void mimeview_display_as_text(MimeView *mimeview)
1153 {
1154         MimeInfo *partinfo;
1155
1156         if (!mimeview->opened) return;
1157
1158         partinfo = mimeview_get_selected_part(mimeview);
1159         if (!partinfo)  {
1160                 partinfo = (MimeInfo *) g_object_get_data
1161                         (G_OBJECT(mimeview->popupmenu),
1162                          "pop_partinfo");
1163                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1164                                   "pop_partinfo", NULL);
1165         
1166         }                        
1167         g_return_if_fail(partinfo != NULL);
1168         mimeview_show_message_part(mimeview, partinfo);
1169 }
1170
1171 static void mimeview_launch(MimeView *mimeview)
1172 {
1173         MimeInfo *partinfo;
1174         gchar *filename;
1175
1176         if (!mimeview->opened) return;
1177         if (!mimeview->file) return;
1178
1179         partinfo = mimeview_get_selected_part(mimeview);
1180         if (!partinfo) { 
1181                 partinfo = (MimeInfo *) g_object_get_data
1182                         (G_OBJECT(mimeview->popupmenu),
1183                          "pop_partinfo");
1184                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1185                                   "pop_partinfo", NULL);
1186         }                        
1187         g_return_if_fail(partinfo != NULL);
1188
1189         filename = procmime_get_tmp_file_name(partinfo);
1190
1191         if (procmime_get_part(filename, partinfo) < 0)
1192                 alertpanel_error
1193                         (_("Can't save the part of multipart message."));
1194         else
1195                 mimeview_view_file(filename, partinfo, NULL);
1196
1197         g_free(filename);
1198 }
1199
1200 static void mimeview_open_with(MimeView *mimeview)
1201 {
1202         MimeInfo *partinfo;
1203         gchar *filename;
1204         gchar *cmd;
1205
1206         if (!mimeview->opened) return;
1207         if (!mimeview->file) return;
1208
1209         partinfo = mimeview_get_selected_part(mimeview);
1210         if (!partinfo) { 
1211                 partinfo = (MimeInfo *) g_object_get_data
1212                         (G_OBJECT(mimeview->popupmenu),
1213                          "pop_partinfo");
1214                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1215                                   "pop_partinfo", NULL);
1216         }                        
1217         g_return_if_fail(partinfo != NULL);
1218
1219         filename = procmime_get_tmp_file_name(partinfo);
1220
1221         if (procmime_get_part(filename, partinfo) < 0) {
1222                 alertpanel_error
1223                         (_("Can't save the part of multipart message."));
1224                 g_free(filename);
1225                 return;
1226         }
1227
1228         if (!prefs_common.mime_open_cmd_history)
1229                 prefs_common.mime_open_cmd_history =
1230                         add_history(NULL, prefs_common.mime_open_cmd);
1231
1232         cmd = input_dialog_combo
1233                 (_("Open with"),
1234                  _("Enter the command line to open file:\n"
1235                    "(`%s' will be replaced with file name)"),
1236                  prefs_common.mime_open_cmd,
1237                  prefs_common.mime_open_cmd_history,
1238                  TRUE);
1239         if (cmd) {
1240                 mimeview_view_file(filename, partinfo, cmd);
1241                 g_free(prefs_common.mime_open_cmd);
1242                 prefs_common.mime_open_cmd = cmd;
1243                 prefs_common.mime_open_cmd_history =
1244                         add_history(prefs_common.mime_open_cmd_history, cmd);
1245         }
1246
1247         g_free(filename);
1248 }
1249
1250 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1251                                const gchar *cmdline)
1252 {
1253         static gchar *default_image_cmdline = "display '%s'";
1254         static gchar *default_audio_cmdline = "play '%s'";
1255         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1256         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1257         gchar buf[1024];
1258         gchar m_buf[1024];
1259         const gchar *cmd;
1260         const gchar *def_cmd;
1261         const gchar *p;
1262
1263         if (cmdline) {
1264                 cmd = cmdline;
1265                 def_cmd = NULL;
1266         } else if (MIMETYPE_APPLICATION == partinfo->type &&
1267                    !g_strcasecmp(partinfo->subtype, "octet-stream")) {
1268                 return;
1269         } else if (MIMETYPE_IMAGE == partinfo->type) {
1270                 cmd = prefs_common.mime_image_viewer;
1271                 def_cmd = default_image_cmdline;
1272         } else if (MIMETYPE_AUDIO == partinfo->type) {
1273                 cmd = prefs_common.mime_audio_player;
1274                 def_cmd = default_audio_cmdline;
1275         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1276                 cmd = prefs_common.uri_cmd;
1277                 def_cmd = default_html_cmdline;
1278         } else {
1279                 gchar *content_type;
1280                 
1281                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
1282                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1283                            content_type, "%s");
1284                 g_free(content_type);
1285                 cmd = m_buf;
1286                 def_cmd = NULL;
1287         }
1288
1289         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1290             !strchr(p + 2, '%'))
1291                 g_snprintf(buf, sizeof(buf), cmd, filename);
1292         else {
1293                 if (cmd)
1294                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1295                 if (def_cmd)
1296                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1297                 else
1298                         return;
1299         }
1300
1301         execute_command_line(buf, TRUE);
1302 }
1303
1304 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1305 {
1306         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1307 }
1308
1309 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1310 {
1311         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1312 }
1313
1314 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1315 {
1316         GSList *mimeview_list, *viewer_list;
1317
1318         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1319                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1320                 
1321                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1322                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1323                         mimeview->mimeviewer = NULL;
1324                 }
1325
1326                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1327                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1328
1329                         mimeviewer->destroy_viewer(mimeviewer);
1330                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1331                 }
1332         }
1333
1334         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1335 }
1336
1337 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1338 {
1339         gint      num;
1340         MimeInfo *partinfo;
1341
1342         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1343         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1344
1345         icon_selected(mimeview, num, partinfo);
1346         gtk_widget_grab_focus(button);
1347         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1348                 toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1349                 if (event->button == 2 || event->button == 3)
1350                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
1351                                                      TRUE);
1352         }
1353
1354         part_button_pressed(mimeview, event, partinfo);
1355
1356         return FALSE;
1357 }
1358
1359 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1360 {
1361         GtkCTreeNode *node;
1362         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1363         if (node)
1364                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1365 }               
1366
1367 #undef  KEY_PRESS_EVENT_STOP
1368 #define KEY_PRESS_EVENT_STOP() \
1369         g_signal_stop_emission_by_name(G_OBJECT(button), \
1370                                        "key_press_event");
1371
1372 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1373                              MimeView *mimeview)
1374 {
1375         gint          num;
1376         MimeInfo     *partinfo;
1377         SummaryView  *summaryview;
1378         TextView     *textview;
1379
1380         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1381         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1382         
1383         if (!event) return FALSE;
1384
1385         textview = mimeview->textview;
1386
1387         switch (event->keyval) {
1388         case GDK_space:
1389                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1390                         /* stop the button being untoggled */
1391                         KEY_PRESS_EVENT_STOP();
1392                         if (textview_scroll_page(textview, FALSE))
1393                                 return TRUE;
1394
1395                         if (icon_list_select_by_number(mimeview, num + 1))
1396                                 return TRUE;
1397                 } else {
1398                         icon_selected(mimeview, num, partinfo);
1399                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1400                         return TRUE;
1401                 }
1402
1403                 break;
1404         case GDK_BackSpace:
1405                 textview_scroll_page(textview, TRUE);
1406                 return TRUE;
1407         case GDK_Return:
1408                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1409                         KEY_PRESS_EVENT_STOP();
1410                         textview_scroll_one_line(textview,
1411                                                  (event->state & GDK_MOD1_MASK) != 0);
1412                         return TRUE;
1413                 } else {
1414                         icon_selected(mimeview, num, partinfo);
1415                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1416                         return TRUE;
1417                 }
1418
1419         case GDK_n:
1420         case GDK_N:
1421                 BREAK_ON_MODIFIER_KEY();
1422                 if (icon_list_select_by_number(mimeview, num + 1)) {
1423                         KEY_PRESS_EVENT_STOP();
1424                         return TRUE;
1425                 }
1426                 break;
1427                 
1428         case GDK_p:
1429         case GDK_P:
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_y:
1438                 BREAK_ON_MODIFIER_KEY();
1439                 KEY_PRESS_EVENT_STOP();
1440                 mimeview_save_as(mimeview);
1441                 return TRUE;
1442         case GDK_t:
1443                 BREAK_ON_MODIFIER_KEY();
1444                 KEY_PRESS_EVENT_STOP();
1445                 mimeview_display_as_text(mimeview);
1446                 return TRUE;    
1447         case GDK_l:
1448                 BREAK_ON_MODIFIER_KEY();
1449                 KEY_PRESS_EVENT_STOP();
1450                 mimeview_launch(mimeview);
1451                 return TRUE;
1452         case GDK_o:
1453                 BREAK_ON_MODIFIER_KEY();
1454                 KEY_PRESS_EVENT_STOP();
1455                 mimeview_open_with(mimeview);
1456                 return TRUE;
1457         default:
1458                 break;
1459         }
1460
1461         if (!mimeview->messageview->mainwin) return FALSE;
1462         summaryview = mimeview->messageview->mainwin->summaryview;
1463         summary_pass_key_press_event(summaryview, event);
1464         return TRUE;
1465 }
1466
1467 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1468 {
1469         GList *child;
1470         
1471         child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
1472         for (; child != NULL; child = g_list_next(child)) {
1473                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1474                     GTK_TOGGLE_BUTTON(child->data) != button &&
1475                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1476                         gtk_toggle_button_set_active
1477                                 (GTK_TOGGLE_BUTTON(child->data),
1478                                  FALSE);
1479         }
1480 }
1481
1482 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1483 {
1484         GtkWidget *pixmap;
1485         GtkWidget *vbox;
1486         GtkWidget *button;
1487         gchar *tip;
1488         gchar *tiptmp;
1489         const gchar *desc = NULL; 
1490         gchar *sigshort = NULL;
1491         gchar *content_type;
1492         StockPixmap stockp;
1493         MimeInfo *partinfo;
1494         MimeInfo *siginfo = NULL;
1495         MimeInfo *encrypted = NULL;
1496         
1497         vbox = mimeview->icon_vbox;
1498         mimeview->icon_count++;
1499         button = gtk_toggle_button_new();
1500         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1501         g_object_set_data(G_OBJECT(button), "icon_number", 
1502                           GINT_TO_POINTER(mimeview->icon_count));
1503         g_object_set_data(G_OBJECT(button), "partinfo", 
1504                           mimeinfo);
1505         
1506         switch (mimeinfo->type) {
1507                 
1508         case MIMETYPE_TEXT:
1509                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "html"))
1510                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1511                 else if  (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "enriched"))
1512                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1513                 else
1514                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1515                 break;
1516         case MIMETYPE_MESSAGE:
1517                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1518                 break;
1519         case MIMETYPE_APPLICATION:
1520                 stockp = STOCK_PIXMAP_MIME_APPLICATION;
1521                 break;
1522         case MIMETYPE_IMAGE:
1523                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1524                 break;
1525         case MIMETYPE_AUDIO:
1526                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1527                 break;
1528         default:
1529                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1530                 break;
1531         }
1532         
1533         partinfo = mimeinfo;
1534         while (partinfo != NULL) {
1535                 if (privacy_mimeinfo_is_signed(partinfo)) {
1536                         siginfo = partinfo;
1537                         break;
1538                 }
1539                 if (privacy_mimeinfo_is_encrypted(partinfo)) {
1540                         encrypted = partinfo;
1541                         break;
1542                 }
1543                 partinfo = procmime_mimeinfo_parent(partinfo);
1544         }       
1545
1546         if (siginfo != NULL) {
1547                 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
1548                 case SIGNATURE_UNCHECKED:
1549                 case SIGNATURE_CHECK_FAILED:
1550                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1551                             STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1552                         break;
1553                 case SIGNATURE_OK:
1554                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1555                             STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1556                         break;
1557                 case SIGNATURE_WARN:
1558                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1559                             STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
1560                         break;
1561                 case SIGNATURE_INVALID:
1562                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1563                             STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1564                         break;
1565                 }
1566                 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
1567         } else if (encrypted != NULL) {
1568                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1569                             STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);         
1570         } else {
1571                 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
1572                                                           OVERLAY_NONE, 6, 3);
1573         }
1574         gtk_container_add(GTK_CONTAINER(button), pixmap);
1575         
1576         if (!desc) {
1577                 if (prefs_common.attach_desc)
1578                         desc = get_part_description(mimeinfo);
1579                 else
1580                         desc = get_part_name(mimeinfo);
1581         }
1582
1583         content_type = procmime_get_content_type_str(mimeinfo->type,
1584                                                      mimeinfo->subtype);
1585
1586         tip = g_strjoin("\n", content_type,
1587                         to_human_readable(mimeinfo->length), NULL);
1588         g_free(content_type);
1589         if (desc && *desc) {
1590                 tiptmp = g_strjoin("\n", desc, tip, NULL);
1591                 g_free(tip);
1592                 tip = tiptmp;
1593         }
1594         if (sigshort && *sigshort) {
1595                 tiptmp = g_strjoin("\n", tip, sigshort, NULL);
1596                 g_free(tip);
1597                 tip = tiptmp;
1598         }
1599         g_free(sigshort);
1600
1601         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1602         g_free(tip);
1603         gtk_widget_show_all(button);
1604         g_signal_connect(G_OBJECT(button), "button_release_event", 
1605                          G_CALLBACK(icon_clicked_cb), mimeview);
1606         g_signal_connect(G_OBJECT(button), "key_press_event", 
1607                          G_CALLBACK(icon_key_pressed), mimeview);
1608         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1609
1610 }
1611
1612 static void icon_list_clear (MimeView *mimeview)
1613 {
1614         GList     *child;
1615         GtkAdjustment *adj;
1616         
1617         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1618         for (; child != NULL; child = g_list_next(child)) {
1619                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1620                                        GTK_WIDGET(child->data));
1621         }
1622         mimeview->icon_count = 0;
1623         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1624         adj->value = adj->lower;
1625 }
1626
1627 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
1628                                           MimeInfo      *mimeinfo)
1629 {
1630         GList *child;
1631         
1632         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1633         for (; child != NULL; child = g_list_next(child)) {
1634                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1635                     g_object_get_data(G_OBJECT(child->data),
1636                                       "partinfo") == (gpointer)mimeinfo) {
1637                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1638                         gtk_toggle_button_set_active
1639                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1640                 }                                
1641         }
1642 }
1643
1644 /*!
1645  *\brief        Used to 'click' the next or previous icon.
1646  *
1647  *\return       true if the icon 'number' exists and was selected.
1648  */
1649 static gboolean icon_list_select_by_number(MimeView     *mimeview,
1650                                            gint          number)
1651 {
1652         GList *child;
1653
1654         if (number == 0) return FALSE;
1655         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1656         for (; child != NULL; child = g_list_next(child)) {
1657                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1658                     GPOINTER_TO_INT(g_object_get_data(G_OBJECT(child->data),
1659                                         "icon_number")) == number) {
1660                         icon_selected(mimeview, number,
1661                                       (MimeInfo*)g_object_get_data(G_OBJECT(child->data),
1662                                                                    "partinfo"));
1663                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1664                         gtk_toggle_button_set_active
1665                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1666                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
1667                 
1668                         return TRUE;
1669                 }                                
1670         }
1671         return FALSE;
1672 }
1673
1674 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1675                                          GtkAllocation *size, MimeView *mimeview)
1676 {
1677         GtkAllocation *mainbox_size;
1678         GtkAllocation *vbox_size;
1679         GtkAllocation *layout_size;
1680         GtkAdjustment *adj;
1681         
1682         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1683
1684         mainbox_size = &mimeview->icon_mainbox->allocation;
1685         vbox_size = &mimeview->icon_vbox->allocation;
1686         layout_size = &mimeview->icon_scroll->allocation;
1687                 
1688         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1689                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1690                             MAX(vbox_size->height, layout_size->height));
1691         adj->step_increment = 5;
1692 }
1693
1694 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1695 {
1696         GtkRequisition size;
1697
1698         g_return_if_fail(mimeinfo != NULL);
1699
1700         while (mimeinfo != NULL) {
1701                 if (mimeinfo->type != MIMETYPE_MULTIPART)
1702                         icon_list_append_icon(mimeview, mimeinfo);
1703                 if (mimeinfo->node->children != NULL)
1704                         icon_list_create(mimeview, 
1705                                 (MimeInfo *) mimeinfo->node->children->data);
1706                 mimeinfo = mimeinfo->node->next != NULL 
1707                          ? (MimeInfo *) mimeinfo->node->next->data 
1708                          : NULL;
1709         }
1710         gtk_widget_size_request(mimeview->icon_vbox, &size);
1711         if (size.width > mimeview->icon_mainbox->requisition.width) {
1712                 gtk_widget_set_size_request(mimeview->icon_mainbox, 
1713                                             size.width, -1);
1714         }
1715
1716 }
1717
1718 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1719 {
1720         gtk_widget_ref(button); 
1721
1722         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1723                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1724                                         GTK_SHADOW_NONE);
1725                 gtk_widget_hide(mimeview->icon_mainbox);
1726                 gtk_widget_show(mimeview->ctree_mainbox);
1727                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1728
1729                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1730                                         button);
1731                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1732                                    button, FALSE, FALSE, 0);
1733                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
1734         } else {
1735                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1736                               GTK_SHADOW_NONE);
1737                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1738                 gtk_widget_hide(mimeview->ctree_mainbox);
1739                 gtk_widget_show(mimeview->icon_mainbox);
1740                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1741
1742                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1743                                         button);
1744                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1745                                    button, FALSE, FALSE, 0);
1746                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1747                 if (mimeview->opened)
1748                         icon_list_toggle_by_mime_info
1749                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
1750                                                                        mimeview->opened));
1751
1752                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
1753         }
1754         gtk_widget_grab_focus(button);
1755         gtk_widget_unref(button);
1756
1757 }
1758
1759 void mimeview_update (MimeView *mimeview) {
1760         if (mimeview && mimeview->mimeinfo) {
1761                 icon_list_clear(mimeview);
1762                 icon_list_create(mimeview, mimeview->mimeinfo);
1763         }
1764 }