c665fb7303e5a9d3f77a1045e9f05ef7175ac402
[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 #warning FIXME_GTK2
850 #if 0
851 #define KEY_PRESS_EVENT_STOP() \
852         if (gtk_signal_n_emissions_by_name \
853                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
854                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
855                                              "key_press_event"); \
856         }
857 #else
858 #define KEY_PRESS_EVENT_STOP()
859 #endif
860
861 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
862                                  MimeView *mimeview)
863 {
864         SummaryView *summaryview;
865         GtkCTree *ctree = GTK_CTREE(widget);
866         GtkCTreeNode *node;
867
868         if (!event) return FALSE;
869         if (!mimeview->opened) return FALSE;
870
871         switch (event->keyval) {
872         case GDK_space:
873                 if (textview_scroll_page(mimeview->textview, FALSE))
874                         return TRUE;
875
876                 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
877                 if (node) {
878                         gtk_sctree_unselect_all(GTK_SCTREE(ctree));
879                         gtk_sctree_select(GTK_SCTREE(ctree), node);
880                         return TRUE;
881                 }
882                 break;
883         case GDK_BackSpace:
884                 textview_scroll_page(mimeview->textview, TRUE);
885                 return TRUE;
886         case GDK_Return:
887                 textview_scroll_one_line(mimeview->textview,
888                                          (event->state & GDK_MOD1_MASK) != 0);
889                 return TRUE;
890         case GDK_n:
891         case GDK_N:
892                 BREAK_ON_MODIFIER_KEY();
893                 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
894                 KEY_PRESS_EVENT_STOP();
895                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
896                                         GTK_SCROLL_STEP_FORWARD, 0.0);
897                 return TRUE;
898         case GDK_p:
899         case GDK_P:
900                 BREAK_ON_MODIFIER_KEY();
901                 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
902                 KEY_PRESS_EVENT_STOP();
903                 g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
904                                         GTK_SCROLL_STEP_BACKWARD, 0.0);
905                 return TRUE;
906         case GDK_y:
907                 BREAK_ON_MODIFIER_KEY();
908                 KEY_PRESS_EVENT_STOP();
909                 mimeview_save_as(mimeview);
910                 return TRUE;
911         case GDK_t:
912                 BREAK_ON_MODIFIER_KEY();
913                 KEY_PRESS_EVENT_STOP();
914                 mimeview_display_as_text(mimeview);
915                 return TRUE;    
916         case GDK_l:
917                 BREAK_ON_MODIFIER_KEY();
918                 KEY_PRESS_EVENT_STOP();
919                 mimeview_launch(mimeview);
920                 return TRUE;
921         case GDK_o:
922                 BREAK_ON_MODIFIER_KEY();
923                 KEY_PRESS_EVENT_STOP();
924                 mimeview_open_with(mimeview);
925                 return TRUE;
926         default:
927                 break;
928         }
929
930         if (!mimeview->messageview->mainwin) return FALSE;
931         summaryview = mimeview->messageview->mainwin->summaryview;
932         summary_pass_key_press_event(summaryview, event);
933         return TRUE;
934 }
935
936 static void mimeview_drag_data_get(GtkWidget        *widget,
937                                    GdkDragContext   *drag_context,
938                                    GtkSelectionData *selection_data,
939                                    guint             info,
940                                    guint             time,
941                                    MimeView         *mimeview)
942 {
943         gchar *filename, *uriname;
944         MimeInfo *partinfo;
945
946         if (!mimeview->opened) return;
947         if (!mimeview->file) return;
948
949         partinfo = mimeview_get_selected_part(mimeview);
950         if (!partinfo) return;
951
952         filename = g_basename(get_part_name(partinfo));
953         if (*filename == '\0') return;
954
955         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
956                                filename, NULL);
957
958         if (procmime_get_part(filename, partinfo) < 0)
959                 alertpanel_error
960                         (_("Can't save the part of multipart message."));
961
962         uriname = g_strconcat("file:/", filename, NULL);
963         gtk_selection_data_set(selection_data, selection_data->target, 8,
964                                uriname, strlen(uriname));
965
966         g_free(uriname);
967         g_free(filename);
968 }
969
970 /**
971  * Returns a filename (with path) for an attachment
972  * \param partinfo The attachment to save
973  * \param basedir The target directory
974  * \param number Used for dummy filename if attachment is unnamed
975  */
976 gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
977                                       const gchar *basedir,
978                                       gint number)
979 {
980         gchar *fullname;
981         gchar *filename;
982
983         filename = g_strdup(get_part_name(partinfo));
984         if (!filename || !*filename)
985                 filename = g_strdup_printf("noname.%d", number);
986         subst_for_shellsafe_filename(filename);
987
988         fullname = g_strconcat
989                 (basedir, G_DIR_SEPARATOR_S, (filename[0] == G_DIR_SEPARATOR)
990                  ? &filename[1] : filename, NULL);
991         subst_chars(fullname, "/\\", G_DIR_SEPARATOR);
992
993         g_free(filename);
994         return fullname;
995 }
996
997 /**
998  * Write a single attachment to file
999  * \param filename Filename with path
1000  * \param partinfo Attachment to save
1001  */
1002 static gboolean mimeview_write_part(const gchar *filename,
1003                                     MimeInfo *partinfo)
1004 {
1005         gchar *dir;
1006         
1007         dir= g_dirname(filename);
1008         if (!is_dir_exist(dir))
1009                 make_dir_hier(dir);
1010         g_free(dir);
1011
1012         if (is_file_exist(filename)) {
1013                 AlertValue aval;
1014                 gchar *res;
1015                 
1016                 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1017                                       filename);
1018                 aval = alertpanel(_("Overwrite"), res, _("OK"), 
1019                                   _("Cancel"), NULL);
1020                 g_free(res);                                      
1021                 if (G_ALERTDEFAULT != aval) return FALSE;
1022         }
1023
1024         if (procmime_get_part(filename, partinfo) < 0) {
1025                 alertpanel_error
1026                         (_("Can't save the part of multipart message."));
1027                 return FALSE;
1028         }
1029
1030         return TRUE;
1031 }
1032
1033 /**
1034  * Menu callback: Save all attached files
1035  * \param mimeview Current display
1036  */
1037 static void mimeview_save_all(MimeView *mimeview)
1038 {
1039         MimeInfo *partinfo;
1040         gchar *dirname;
1041         gchar *startdir = NULL;
1042         gint number = 1;
1043
1044         if (!mimeview->opened) return;
1045         if (!mimeview->file) return;
1046         if (!mimeview->mimeinfo) return;
1047
1048         partinfo = mimeview->mimeinfo;
1049         if (prefs_common.attach_save_dir)
1050                 startdir = g_strconcat(prefs_common.attach_save_dir,
1051                                        G_DIR_SEPARATOR_S, NULL);
1052
1053         dirname = filesel_select_file(_("Select destination folder"), startdir);
1054         if (!dirname) {
1055                 if (startdir) g_free(startdir);
1056                 return;
1057         }
1058
1059         if (!is_dir_exist (dirname)) {
1060                 alertpanel_error(_("`%s' is not a directory."),
1061                                  dirname);
1062                 if (startdir) g_free(startdir);
1063                 return;
1064         }
1065
1066         if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1067                 dirname[strlen(dirname)-1] = '\0';
1068
1069         while (partinfo != NULL) {
1070                 if (partinfo->type != MIMETYPE_MESSAGE &&
1071                     partinfo->type != MIMETYPE_MULTIPART &&
1072                     partinfo->disposition != DISPOSITIONTYPE_INLINE) {
1073                         gchar *filename = mimeview_get_filename_for_part
1074                                 (partinfo, dirname, number++);
1075
1076                         mimeview_write_part(filename, partinfo);
1077                         g_free(filename);
1078                 }
1079                 partinfo = procmime_mimeinfo_next(partinfo);
1080         }
1081
1082         if (prefs_common.attach_save_dir)
1083                 g_free(prefs_common.attach_save_dir);
1084
1085         prefs_common.attach_save_dir = g_strdup(dirname);
1086
1087         if (startdir) g_free(startdir);
1088 }
1089
1090 /**
1091  * Menu callback: Save the selected attachment
1092  * \param mimeview Current display
1093  */
1094 static void mimeview_save_as(MimeView *mimeview)
1095 {
1096         gchar *filename;
1097         gchar *defname = NULL;
1098         gchar *filepath = NULL;
1099         gchar *filedir = NULL;
1100         MimeInfo *partinfo;
1101         const gchar *partname = NULL;
1102
1103         if (!mimeview->opened) return;
1104         if (!mimeview->file) return;
1105
1106         partinfo = mimeview_get_selected_part(mimeview);
1107         if (!partinfo) { 
1108                 partinfo = (MimeInfo *) g_object_get_data
1109                          (G_OBJECT(mimeview->popupmenu),
1110                          "pop_partinfo");
1111                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1112                                   "pop_partinfo", NULL);
1113         }                        
1114         g_return_if_fail(partinfo != NULL);
1115         
1116         if ((partname = get_part_name(partinfo)) != NULL) {
1117                 Xstrdup_a(defname, partname, return);
1118                 subst_for_shellsafe_filename(defname);
1119         }
1120
1121         if (prefs_common.attach_save_dir)
1122                 filepath = g_strconcat(prefs_common.attach_save_dir,
1123                                        G_DIR_SEPARATOR_S, defname, NULL);
1124         else
1125                 filepath = g_strdup(defname);
1126
1127         filename = filesel_select_file(_("Save as"), filepath);
1128         if (!filename) {
1129                 g_free(filepath);
1130                 return;
1131         }
1132
1133         mimeview_write_part(filename, partinfo);
1134
1135         filedir = g_dirname(filename);
1136         if (filedir && strcmp(filedir, ".")) {
1137                 if (prefs_common.attach_save_dir)
1138                         g_free(prefs_common.attach_save_dir);
1139                 prefs_common.attach_save_dir = g_strdup(filedir);
1140         }
1141
1142         g_free(filedir);
1143         g_free(filepath);
1144 }
1145
1146 static void mimeview_display_as_text(MimeView *mimeview)
1147 {
1148         MimeInfo *partinfo;
1149
1150         if (!mimeview->opened) return;
1151
1152         partinfo = mimeview_get_selected_part(mimeview);
1153         if (!partinfo)  {
1154                 partinfo = (MimeInfo *) g_object_get_data
1155                         (G_OBJECT(mimeview->popupmenu),
1156                          "pop_partinfo");
1157                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1158                                   "pop_partinfo", NULL);
1159         
1160         }                        
1161         g_return_if_fail(partinfo != NULL);
1162         mimeview_show_message_part(mimeview, partinfo);
1163 }
1164
1165 static void mimeview_launch(MimeView *mimeview)
1166 {
1167         MimeInfo *partinfo;
1168         gchar *filename;
1169
1170         if (!mimeview->opened) return;
1171         if (!mimeview->file) return;
1172
1173         partinfo = mimeview_get_selected_part(mimeview);
1174         if (!partinfo) { 
1175                 partinfo = (MimeInfo *) g_object_get_data
1176                         (G_OBJECT(mimeview->popupmenu),
1177                          "pop_partinfo");
1178                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1179                                   "pop_partinfo", NULL);
1180         }                        
1181         g_return_if_fail(partinfo != NULL);
1182
1183         filename = procmime_get_tmp_file_name(partinfo);
1184
1185         if (procmime_get_part(filename, partinfo) < 0)
1186                 alertpanel_error
1187                         (_("Can't save the part of multipart message."));
1188         else
1189                 mimeview_view_file(filename, partinfo, NULL);
1190
1191         g_free(filename);
1192 }
1193
1194 static void mimeview_open_with(MimeView *mimeview)
1195 {
1196         MimeInfo *partinfo;
1197         gchar *filename;
1198         gchar *cmd;
1199
1200         if (!mimeview->opened) return;
1201         if (!mimeview->file) return;
1202
1203         partinfo = mimeview_get_selected_part(mimeview);
1204         if (!partinfo) { 
1205                 partinfo = (MimeInfo *) g_object_get_data
1206                         (G_OBJECT(mimeview->popupmenu),
1207                          "pop_partinfo");
1208                 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1209                                   "pop_partinfo", NULL);
1210         }                        
1211         g_return_if_fail(partinfo != NULL);
1212
1213         filename = procmime_get_tmp_file_name(partinfo);
1214
1215         if (procmime_get_part(filename, partinfo) < 0) {
1216                 alertpanel_error
1217                         (_("Can't save the part of multipart message."));
1218                 g_free(filename);
1219                 return;
1220         }
1221
1222         if (!prefs_common.mime_open_cmd_history)
1223                 prefs_common.mime_open_cmd_history =
1224                         add_history(NULL, prefs_common.mime_open_cmd);
1225
1226         cmd = input_dialog_combo
1227                 (_("Open with"),
1228                  _("Enter the command line to open file:\n"
1229                    "(`%s' will be replaced with file name)"),
1230                  prefs_common.mime_open_cmd,
1231                  prefs_common.mime_open_cmd_history,
1232                  TRUE);
1233         if (cmd) {
1234                 mimeview_view_file(filename, partinfo, cmd);
1235                 g_free(prefs_common.mime_open_cmd);
1236                 prefs_common.mime_open_cmd = cmd;
1237                 prefs_common.mime_open_cmd_history =
1238                         add_history(prefs_common.mime_open_cmd_history, cmd);
1239         }
1240
1241         g_free(filename);
1242 }
1243
1244 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1245                                const gchar *cmdline)
1246 {
1247         static gchar *default_image_cmdline = "display '%s'";
1248         static gchar *default_audio_cmdline = "play '%s'";
1249         static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1250         static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1251         gchar buf[1024];
1252         gchar m_buf[1024];
1253         const gchar *cmd;
1254         const gchar *def_cmd;
1255         const gchar *p;
1256
1257         if (cmdline) {
1258                 cmd = cmdline;
1259                 def_cmd = NULL;
1260         } else if (MIMETYPE_APPLICATION == partinfo->type &&
1261                    !g_strcasecmp(partinfo->subtype, "octet-stream")) {
1262                 return;
1263         } else if (MIMETYPE_IMAGE == partinfo->type) {
1264                 cmd = prefs_common.mime_image_viewer;
1265                 def_cmd = default_image_cmdline;
1266         } else if (MIMETYPE_AUDIO == partinfo->type) {
1267                 cmd = prefs_common.mime_audio_player;
1268                 def_cmd = default_audio_cmdline;
1269         } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
1270                 cmd = prefs_common.uri_cmd;
1271                 def_cmd = default_html_cmdline;
1272         } else {
1273                 gchar *content_type;
1274                 
1275                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
1276                 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1277                            content_type, "%s");
1278                 g_free(content_type);
1279                 cmd = m_buf;
1280                 def_cmd = NULL;
1281         }
1282
1283         if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1284             !strchr(p + 2, '%'))
1285                 g_snprintf(buf, sizeof(buf), cmd, filename);
1286         else {
1287                 if (cmd)
1288                         g_warning("MIME viewer command line is invalid: `%s'", cmd);
1289                 if (def_cmd)
1290                         g_snprintf(buf, sizeof(buf), def_cmd, filename);
1291                 else
1292                         return;
1293         }
1294
1295         execute_command_line(buf, TRUE);
1296 }
1297
1298 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1299 {
1300         mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1301 }
1302
1303 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1304 {
1305         return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1306 }
1307
1308 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1309 {
1310         GSList *mimeview_list, *viewer_list;
1311
1312         for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1313                 MimeView *mimeview = (MimeView *) mimeview_list->data;
1314                 
1315                 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1316                         mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1317                         mimeview->mimeviewer = NULL;
1318                 }
1319
1320                 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1321                         MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1322
1323                         mimeviewer->destroy_viewer(mimeviewer);
1324                         mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1325                 }
1326         }
1327
1328         mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
1329 }
1330
1331 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
1332 {
1333         gint      num;
1334         MimeInfo *partinfo;
1335
1336         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1337         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1338
1339         icon_selected(mimeview, num, partinfo);
1340         gtk_widget_grab_focus(button);
1341         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1342                 toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1343                 if (event->button == 2 || event->button == 3)
1344                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
1345                                                      TRUE);
1346         } else {
1347                 g_signal_stop_emission_by_name(G_OBJECT(button), "button_press_event");
1348         }
1349
1350         part_button_pressed(mimeview, event, partinfo);
1351
1352         return FALSE;
1353 }
1354
1355 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
1356 {
1357         GtkCTreeNode *node;
1358         node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
1359         if (node)
1360                 gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
1361 }               
1362
1363 #warning FIXME_GTK2
1364 #if 0
1365 #undef  KEY_PRESS_EVENT_STOP
1366 #define KEY_PRESS_EVENT_STOP() \
1367         if (gtk_signal_n_emissions_by_name \
1368                 (GTK_OBJECT(button), "key_press_event") > 0) { \
1369                 gtk_signal_emit_stop_by_name(GTK_OBJECT(button), \
1370                                              "key_press_event"); \
1371         }
1372 #endif
1373 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
1374                              MimeView *mimeview)
1375 {
1376         gint          num;
1377         MimeInfo     *partinfo;
1378         SummaryView  *summaryview;
1379         TextView     *textview;
1380
1381         num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
1382         partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
1383         
1384         if (!event) return FALSE;
1385
1386         textview = mimeview->textview;
1387
1388         switch (event->keyval) {
1389         case GDK_space:
1390                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1391                         /* stop the button being untoggled */
1392                         KEY_PRESS_EVENT_STOP();
1393                         if (textview_scroll_page(textview, FALSE))
1394                                 return TRUE;
1395
1396                         if (icon_list_select_by_number(mimeview, num + 1))
1397                                 return TRUE;
1398                 } else {
1399                         icon_selected(mimeview, num, partinfo);
1400                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1401                         return TRUE;
1402                 }
1403
1404                 break;
1405         case GDK_BackSpace:
1406                 textview_scroll_page(textview, TRUE);
1407                 return TRUE;
1408         case GDK_Return:
1409                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1410                         KEY_PRESS_EVENT_STOP();
1411                         textview_scroll_one_line(textview,
1412                                                  (event->state & GDK_MOD1_MASK) != 0);
1413                         return TRUE;
1414                 } else {
1415                         icon_selected(mimeview, num, partinfo);
1416                         toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
1417                         return TRUE;
1418                 }
1419
1420         case GDK_n:
1421         case GDK_N:
1422                 BREAK_ON_MODIFIER_KEY();
1423                 if (icon_list_select_by_number(mimeview, num + 1)) {
1424                         KEY_PRESS_EVENT_STOP();
1425                         return TRUE;
1426                 }
1427                 break;
1428                 
1429         case GDK_p:
1430         case GDK_P:
1431                 BREAK_ON_MODIFIER_KEY();
1432                 if (icon_list_select_by_number(mimeview, num - 1)) {
1433                         KEY_PRESS_EVENT_STOP();
1434                         return TRUE;
1435                 }
1436                 break;
1437
1438         case GDK_y:
1439                 BREAK_ON_MODIFIER_KEY();
1440                 KEY_PRESS_EVENT_STOP();
1441                 mimeview_save_as(mimeview);
1442                 return TRUE;
1443         case GDK_t:
1444                 BREAK_ON_MODIFIER_KEY();
1445                 KEY_PRESS_EVENT_STOP();
1446                 mimeview_display_as_text(mimeview);
1447                 return TRUE;    
1448         case GDK_l:
1449                 BREAK_ON_MODIFIER_KEY();
1450                 KEY_PRESS_EVENT_STOP();
1451                 mimeview_launch(mimeview);
1452                 return TRUE;
1453         case GDK_o:
1454                 BREAK_ON_MODIFIER_KEY();
1455                 KEY_PRESS_EVENT_STOP();
1456                 mimeview_open_with(mimeview);
1457                 return TRUE;
1458         default:
1459                 break;
1460         }
1461
1462         if (!mimeview->messageview->mainwin) return FALSE;
1463         summaryview = mimeview->messageview->mainwin->summaryview;
1464         summary_pass_key_press_event(summaryview, event);
1465         return TRUE;
1466 }
1467
1468 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
1469 {
1470         GList *child;
1471         
1472         child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
1473         for (; child != NULL; child = g_list_next(child)) {
1474                 if (GTK_IS_TOGGLE_BUTTON(child->data) && 
1475                     GTK_TOGGLE_BUTTON(child->data) != button &&
1476                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
1477                         gtk_toggle_button_set_active
1478                                 (GTK_TOGGLE_BUTTON(child->data),
1479                                  FALSE);
1480         }
1481 }
1482
1483 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo) 
1484 {
1485         GtkWidget *pixmap;
1486         GtkWidget *vbox;
1487         GtkWidget *button;
1488         gchar *tip;
1489         gchar *tiptmp;
1490         const gchar *desc = NULL; 
1491         gchar *sigshort = NULL;
1492         gchar *content_type;
1493         StockPixmap stockp;
1494         MimeInfo *partinfo;
1495         MimeInfo *siginfo = NULL;
1496         MimeInfo *encrypted = NULL;
1497         
1498         vbox = mimeview->icon_vbox;
1499         mimeview->icon_count++;
1500         button = gtk_toggle_button_new();
1501         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
1502         g_object_set_data(G_OBJECT(button), "icon_number", 
1503                           GINT_TO_POINTER(mimeview->icon_count));
1504         g_object_set_data(G_OBJECT(button), "partinfo", 
1505                           mimeinfo);
1506         
1507         switch (mimeinfo->type) {
1508                 
1509         case MIMETYPE_TEXT:
1510                 if (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "html"))
1511                         stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
1512                 else if  (mimeinfo->subtype && !g_strcasecmp(mimeinfo->subtype, "enriched"))
1513                         stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
1514                 else
1515                         stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
1516                 break;
1517         case MIMETYPE_MESSAGE:
1518                 stockp = STOCK_PIXMAP_MIME_MESSAGE;
1519                 break;
1520         case MIMETYPE_APPLICATION:
1521                 stockp = STOCK_PIXMAP_MIME_APPLICATION;
1522                 break;
1523         case MIMETYPE_IMAGE:
1524                 stockp = STOCK_PIXMAP_MIME_IMAGE;
1525                 break;
1526         case MIMETYPE_AUDIO:
1527                 stockp = STOCK_PIXMAP_MIME_AUDIO;
1528                 break;
1529         default:
1530                 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
1531                 break;
1532         }
1533         
1534         partinfo = mimeinfo;
1535         while (partinfo != NULL) {
1536                 if (privacy_mimeinfo_is_signed(partinfo)) {
1537                         siginfo = partinfo;
1538                         break;
1539                 }
1540                 if (privacy_mimeinfo_is_encrypted(partinfo)) {
1541                         encrypted = partinfo;
1542                         break;
1543                 }
1544                 partinfo = procmime_mimeinfo_parent(partinfo);
1545         }       
1546
1547         if (siginfo != NULL) {
1548                 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
1549                 case SIGNATURE_UNCHECKED:
1550                 case SIGNATURE_CHECK_FAILED:
1551                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1552                             STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1553                         break;
1554                 case SIGNATURE_OK:
1555                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1556                             STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1557                         break;
1558                 case SIGNATURE_WARN:
1559                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1560                             STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
1561                         break;
1562                 case SIGNATURE_INVALID:
1563                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1564                             STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
1565                         break;
1566                 }
1567                 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
1568         } else if (encrypted != NULL) {
1569                         pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
1570                             STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);         
1571         } else {
1572                 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
1573                                                           OVERLAY_NONE, 6, 3);
1574         }
1575         gtk_container_add(GTK_CONTAINER(button), pixmap);
1576         
1577         if (!desc) {
1578                 if (prefs_common.attach_desc)
1579                         desc = get_part_description(mimeinfo);
1580                 else
1581                         desc = get_part_name(mimeinfo);
1582         }
1583
1584         content_type = procmime_get_content_type_str(mimeinfo->type,
1585                                                      mimeinfo->subtype);
1586
1587         tip = g_strjoin("\n", content_type,
1588                         to_human_readable(mimeinfo->length), NULL);
1589         g_free(content_type);
1590         if (desc && *desc) {
1591                 tiptmp = g_strjoin("\n", desc, tip, NULL);
1592                 g_free(tip);
1593                 tip = tiptmp;
1594         }
1595         if (sigshort && *sigshort) {
1596                 tiptmp = g_strjoin("\n", tip, sigshort, NULL);
1597                 g_free(tip);
1598                 tip = tiptmp;
1599         }
1600         g_free(sigshort);
1601
1602         gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
1603         g_free(tip);
1604         gtk_widget_show_all(button);
1605         g_signal_connect(G_OBJECT(button), "button_press_event", 
1606                          G_CALLBACK(icon_clicked_cb), mimeview);
1607         g_signal_connect(G_OBJECT(button), "key_press_event", 
1608                          G_CALLBACK(icon_key_pressed), mimeview);
1609         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
1610
1611 }
1612
1613 static void icon_list_clear (MimeView *mimeview)
1614 {
1615         GList     *child;
1616         GtkAdjustment *adj;
1617         
1618         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1619         for (; child != NULL; child = g_list_next(child)) {
1620                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
1621                                        GTK_WIDGET(child->data));
1622         }
1623         mimeview->icon_count = 0;
1624         adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1625         adj->value = adj->lower;
1626         g_signal_stop_emission_by_name(G_OBJECT (adj), "value_changed");
1627 }
1628
1629 static void icon_list_toggle_by_mime_info(MimeView      *mimeview,
1630                                           MimeInfo      *mimeinfo)
1631 {
1632         GList *child;
1633         
1634         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1635         for (; child != NULL; child = g_list_next(child)) {
1636                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1637                     g_object_get_data(G_OBJECT(child->data),
1638                                       "partinfo") == (gpointer)mimeinfo) {
1639                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1640                         gtk_toggle_button_set_active
1641                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1642                 }                                
1643         }
1644 }
1645
1646 /*!
1647  *\brief        Used to 'click' the next or previous icon.
1648  *
1649  *\return       true if the icon 'number' exists and was selected.
1650  */
1651 static gboolean icon_list_select_by_number(MimeView     *mimeview,
1652                                            gint          number)
1653 {
1654         GList *child;
1655
1656         if (number == 0) return FALSE;
1657         child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
1658         for (; child != NULL; child = g_list_next(child)) {
1659                 if (GTK_IS_TOGGLE_BUTTON(child->data) &&  
1660                     GPOINTER_TO_INT(g_object_get_data(G_OBJECT(child->data),
1661                                         "icon_number")) == number) {
1662                         icon_selected(mimeview, number,
1663                                       (MimeInfo*)g_object_get_data(G_OBJECT(child->data),
1664                                                                    "partinfo"));
1665                         toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
1666                         gtk_toggle_button_set_active
1667                                 (GTK_TOGGLE_BUTTON(child->data), TRUE);
1668                         gtk_widget_grab_focus(GTK_WIDGET(child->data));
1669                 
1670                         return TRUE;
1671                 }                                
1672         }
1673         return FALSE;
1674 }
1675
1676 static void icon_scroll_size_allocate_cb(GtkWidget *widget, 
1677                                          GtkAllocation *size, MimeView *mimeview)
1678 {
1679         GtkAllocation *mainbox_size;
1680         GtkAllocation *vbox_size;
1681         GtkAllocation *layout_size;
1682         GtkAdjustment *adj;
1683         
1684         adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
1685
1686         mainbox_size = &mimeview->icon_mainbox->allocation;
1687         vbox_size = &mimeview->icon_vbox->allocation;
1688         layout_size = &mimeview->icon_scroll->allocation;
1689         
1690 #warning FIXME_GTK2 /* this code cause hang up. */
1691 #if 0
1692         /* centralise the vbox */
1693         gtk_layout_move(GTK_LAYOUT(mimeview->icon_scroll), mimeview->icon_vbox, 
1694                         (mainbox_size->width - vbox_size->width)/2, 0);
1695 #endif
1696         
1697         gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
1698                             GTK_LAYOUT(mimeview->icon_scroll)->width, 
1699                             MAX(vbox_size->height, layout_size->height));
1700         adj->step_increment = 5;
1701 }
1702
1703 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
1704 {
1705         GtkRequisition size;
1706         gint           width;
1707         g_return_if_fail(mimeinfo != NULL);
1708
1709         while (mimeinfo != NULL) {
1710                 if (mimeinfo->type != MIMETYPE_MULTIPART)
1711                         icon_list_append_icon(mimeview, mimeinfo);
1712                 if (mimeinfo->node->children != NULL)
1713                         icon_list_create(mimeview, (MimeInfo *) mimeinfo->node->children->data);
1714                 mimeinfo = mimeinfo->node->next != NULL ? (MimeInfo *) mimeinfo->node->next->data : NULL;
1715         }
1716         gtk_widget_size_request(mimeview->icon_vbox, &size);
1717         width = size.width + 4;
1718         if (width > mimeview->icon_mainbox->requisition.width) {
1719                 gtk_widget_set_size_request(mimeview->icon_mainbox, 
1720                                             width, -1);
1721         }
1722
1723 }
1724
1725 static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview) 
1726 {
1727         gtk_widget_ref(button); 
1728
1729         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
1730                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT, 
1731                                         GTK_SHADOW_NONE);
1732                 gtk_widget_hide(mimeview->icon_mainbox);
1733                 gtk_widget_show(mimeview->ctree_mainbox);
1734                 gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
1735
1736                 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox), 
1737                                         button);
1738                 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox), 
1739                                    button, FALSE, FALSE, 0);
1740                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 6);
1741         } else {
1742                 gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT, 
1743                               GTK_SHADOW_NONE);
1744                 mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
1745                 gtk_widget_hide(mimeview->ctree_mainbox);
1746                 gtk_widget_show(mimeview->icon_mainbox);
1747                 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
1748
1749                 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox), 
1750                                         button);
1751                 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox), 
1752                                    button, FALSE, FALSE, 0);
1753                 gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
1754                 if (mimeview->opened)
1755                         icon_list_toggle_by_mime_info
1756                                 (mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree), 
1757                                                                        mimeview->opened));
1758
1759                 gtk_paned_set_gutter_size(GTK_PANED(mimeview->paned), 0);
1760         }
1761         gtk_widget_grab_focus(button);
1762         gtk_widget_unref(button);
1763
1764 }
1765
1766 void mimeview_update (MimeView *mimeview) {
1767         if (mimeview && mimeview->mimeinfo) {
1768                 icon_list_clear(mimeview);
1769                 icon_list_create(mimeview, mimeview->mimeinfo);
1770         }
1771 }