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