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