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