2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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.
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.
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.
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/gtkvpaned.h>
33 #include <gtk/gtktext.h>
34 #include <gtk/gtksignal.h>
35 #include <gtk/gtkmenu.h>
36 #include <gtk/gtkdnd.h>
37 #include <gtk/gtkselection.h>
46 #include "summaryview.h"
49 #include "alertpanel.h"
50 #include "inputdialog.h"
53 #include "prefs_common.h"
64 #define N_MIMEVIEW_COLS 3
66 static void mimeview_set_multipart_tree (MimeView *mimeview,
68 GtkCTreeNode *parent);
69 static GtkCTreeNode *mimeview_append_part (MimeView *mimeview,
71 GtkCTreeNode *parent);
72 static void mimeview_show_message_part (MimeView *mimeview,
74 static void mimeview_show_image_part (MimeView *mimeview,
76 static void mimeview_change_view_type (MimeView *mimeview,
78 static void mimeview_clear (MimeView *mimeview);
80 static void mimeview_selected (GtkCTree *ctree,
84 static void mimeview_start_drag (GtkWidget *widget,
88 static gint mimeview_button_pressed (GtkWidget *widget,
89 GdkEventButton *event,
91 static gint mimeview_key_pressed (GtkWidget *widget,
95 static void mimeview_drag_data_get (GtkWidget *widget,
96 GdkDragContext *drag_context,
97 GtkSelectionData *selection_data,
102 static void mimeview_display_as_text (MimeView *mimeview);
103 static void mimeview_show_image (MimeView *mimeview);
104 static void mimeview_save_as (MimeView *mimeview);
105 static void mimeview_launch (MimeView *mimeview);
106 static void mimeview_open_with (MimeView *mimeview);
107 static void mimeview_view_file (const gchar *filename,
109 const gchar *cmdline);
111 static void mimeview_check_signature (MimeView *mimeview);
114 static GtkItemFactoryEntry mimeview_popup_entries[] =
116 {N_("/_Open"), NULL, mimeview_launch, 0, NULL},
117 {N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
118 {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
119 {N_("/_Display image"), NULL, mimeview_show_image, 0, NULL},
120 {N_("/_Save as..."), NULL, mimeview_save_as, 0, NULL}
123 {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
127 static GtkTargetEntry mimeview_mime_types[] =
129 {"text/uri-list", 0, 0}
132 MimeView *mimeview_create(void)
139 GtkWidget *scrolledwin;
141 GtkWidget *mime_vbox;
142 GtkWidget *popupmenu;
143 GtkItemFactory *popupfactory;
144 gchar *titles[N_MIMEVIEW_COLS];
148 debug_print(_("Creating MIME view...\n"));
149 mimeview = g_new0(MimeView, 1);
151 titles[COL_MIMETYPE] = _("MIME Type");
152 titles[COL_SIZE] = _("Size");
153 titles[COL_NAME] = _("Name");
155 notebook = gtk_notebook_new();
156 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
158 vbox = gtk_vbox_new(FALSE, 0);
159 gtk_container_add(GTK_CONTAINER(notebook), vbox);
160 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
163 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
164 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
165 GTK_POLICY_AUTOMATIC,
167 gtk_widget_set_usize(scrolledwin, -1, 80);
169 ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
170 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
171 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
172 gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
174 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
175 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
176 for (i = 0; i < N_MIMEVIEW_COLS; i++)
177 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
179 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
181 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
182 GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
183 gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
184 GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
185 gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
186 GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
187 gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
188 GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
189 gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
190 GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
192 mime_vbox = gtk_vbox_new(FALSE, 0);
194 paned = gtk_vpaned_new();
195 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
196 gtk_paned_add2(GTK_PANED(paned), mime_vbox);
197 gtk_container_add(GTK_CONTAINER(notebook), paned);
198 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
201 gtk_widget_show_all(notebook);
203 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
205 n_entries = sizeof(mimeview_popup_entries) /
206 sizeof(mimeview_popup_entries[0]);
207 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
208 "<MimeView>", &popupfactory, mimeview);
210 mimeview->notebook = notebook;
211 mimeview->vbox = vbox;
212 mimeview->paned = paned;
213 mimeview->scrolledwin = scrolledwin;
214 mimeview->ctree = ctree;
215 mimeview->mime_vbox = mime_vbox;
216 mimeview->popupmenu = popupmenu;
217 mimeview->popupfactory = popupfactory;
223 void mimeview_init(MimeView *mimeview)
228 * Check whether the message is OpenPGP signed
231 static gboolean mimeview_is_signed(MimeView *mimeview)
233 MimeInfo *partinfo = NULL;
235 debug_print("mimeview_is signed of %p\n", mimeview);
237 if (!mimeview) return FALSE;
238 if (!mimeview->opened) return FALSE;
240 debug_print("mimeview_is_signed: open\n" );
242 if (!mimeview->file) return FALSE;
244 debug_print("mimeview_is_signed: file\n" );
246 partinfo = gtk_ctree_node_get_row_data
247 (GTK_CTREE(mimeview->ctree), mimeview->opened);
248 g_return_val_if_fail(partinfo != NULL, FALSE);
250 /* walk the tree and see whether there is a signature somewhere */
252 if (rfc2015_has_signature(partinfo))
254 } while ((partinfo = partinfo->parent) != NULL);
256 debug_print("mimeview_is_signed: FALSE\n" );
261 static void set_unchecked_signature(MimeInfo *mimeinfo)
263 MimeInfo *sig_partinfo;
265 sig_partinfo = rfc2015_find_signature(mimeinfo);
266 if (sig_partinfo == NULL) return;
268 g_free(sig_partinfo->sigstatus);
269 sig_partinfo->sigstatus =
270 g_strdup(_("Select \"Check signature\" to check"));
272 g_free(sig_partinfo->sigstatus_full);
273 sig_partinfo->sigstatus_full = NULL;
275 #endif /* USE_GPGME */
277 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
280 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
285 mimeview_clear(mimeview);
286 textview_clear(mimeview->messageview->textview);
288 g_return_if_fail(file != NULL);
289 g_return_if_fail(mimeinfo != NULL);
291 mimeview->mimeinfo = mimeinfo;
293 if ((fp = fopen(file, "rb")) == NULL) {
294 FILE_OP_ERROR(file, "fopen");
297 mimeview->file = g_strdup(file);
299 /* skip MIME part headers */
300 if (mimeinfo->mime_type == MIME_MULTIPART) {
301 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
303 while (fgets(buf, sizeof(buf), fp) != NULL)
304 if (buf[0] == '\r' || buf[0] == '\n') break;
307 procmime_scan_multipart_message(mimeinfo, fp);
309 if (prefs_common.auto_check_signatures)
310 rfc2015_check_signature(mimeinfo, fp);
312 set_unchecked_signature(mimeinfo);
315 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
318 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
320 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
321 mimeview_selected, mimeview);
323 /* search first text part */
324 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
325 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
328 partinfo = gtk_ctree_node_get_row_data(ctree, node);
330 (partinfo->mime_type == MIME_TEXT ||
331 partinfo->mime_type == MIME_TEXT_HTML))
335 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
338 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
341 gtk_ctree_select(ctree, node);
342 gtkut_ctree_set_focus_row(ctree, node);
343 gtk_widget_grab_focus(mimeview->ctree);
347 void mimeview_destroy(MimeView *mimeview)
349 procmime_mimeinfo_free_all(mimeview->mimeinfo);
350 g_free(mimeview->file);
354 static void mimeview_set_multipart_tree(MimeView *mimeview,
356 GtkCTreeNode *parent)
358 GtkCTreeNode *current = parent;
360 g_return_if_fail(mimeinfo != NULL);
362 if (!mimeinfo->sub && mimeinfo->parent)
363 current = mimeview_append_part(mimeview, mimeinfo, parent);
366 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
368 if (mimeinfo->children)
369 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
372 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
375 static gchar *get_part_name(MimeInfo *partinfo)
378 if (partinfo->sigstatus)
379 return partinfo->sigstatus;
383 return partinfo->name;
384 else if (partinfo->filename)
385 return partinfo->filename;
390 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
392 GtkCTreeNode *parent)
394 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
396 gchar *str[N_MIMEVIEW_COLS];
399 partinfo->content_type ? partinfo->content_type : "";
400 str[COL_SIZE] = to_human_readable(partinfo->size);
401 str[COL_NAME] = get_part_name(partinfo);
403 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
404 NULL, NULL, NULL, NULL,
406 gtk_ctree_node_set_row_data(ctree, node, partinfo);
411 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
419 if (!partinfo) return;
422 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
424 fname = pi ? pi->plaintextfile : mimeview->file;
426 fname = mimeview->file;
427 #endif /* USE_GPGME */
430 if ((fp = fopen(fname, "rb")) == NULL) {
431 FILE_OP_ERROR(fname, "fopen");
435 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
436 FILE_OP_ERROR(mimeview->file, "fseek");
441 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
442 textview_show_part(mimeview->textview, partinfo, fp);
447 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
451 if (!partinfo) return;
453 filename = procmime_get_tmp_file_name(partinfo);
455 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
457 (_("Can't get the part of multipart message."));
459 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
460 /* Workaround for the GTK+ bug with handling scroll adjustments
462 imageview_clear(mimeview->imageview);
463 imageview_show_image(mimeview->imageview, partinfo, filename);
470 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
472 TextView *textview = mimeview->textview;
473 ImageView *imageview = mimeview->imageview;
476 if (mimeview->type == type) return;
478 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
480 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
481 GTK_WIDGET(children->data));
482 g_list_free(children);
487 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
488 GTK_WIDGET_PTR(imageview));
491 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
492 GTK_WIDGET_PTR(textview));
498 mimeview->type = type;
501 static void mimeview_clear(MimeView *mimeview)
503 GtkCList *clist = GTK_CLIST(mimeview->ctree);
505 procmime_mimeinfo_free_all(mimeview->mimeinfo);
506 mimeview->mimeinfo = NULL;
508 gtk_clist_clear(clist);
509 textview_clear(mimeview->textview);
510 imageview_clear(mimeview->imageview);
512 mimeview->opened = NULL;
514 g_free(mimeview->file);
515 mimeview->file = NULL;
517 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
520 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
525 if (mimeview->opened == node) return;
526 mimeview->opened = node;
527 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
529 partinfo = gtk_ctree_node_get_row_data(ctree, node);
530 if (!partinfo) return;
532 /* ungrab the mouse event */
533 if (GTK_WIDGET_HAS_GRAB(ctree)) {
534 gtk_grab_remove(GTK_WIDGET(ctree));
535 if (gdk_pointer_is_grabbed())
536 gdk_pointer_ungrab(GDK_CURRENT_TIME);
539 switch (partinfo->mime_type) {
542 case MIME_TEXT_ENRICHED:
543 case MIME_MESSAGE_RFC822:
545 mimeview_show_message_part(mimeview, partinfo);
547 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
549 if (prefs_common.display_img)
550 mimeview_show_image_part(mimeview, partinfo);
552 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
553 textview_show_mime_part(mimeview->textview, partinfo);
558 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
560 if (g_strcasecmp(partinfo->content_type,
561 "application/pgp-signature") == 0)
562 textview_show_signature_part(mimeview->textview,
566 textview_show_mime_part(mimeview->textview, partinfo);
571 static void mimeview_start_drag(GtkWidget *widget, gint button,
572 GdkEvent *event, MimeView *mimeview)
575 GdkDragContext *context;
579 g_return_if_fail(mimeview != NULL);
580 ctree = GTK_CTREE(mimeview->ctree);
581 partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
582 if (partinfo->filename == NULL && partinfo->name == NULL) return;
584 list = gtk_target_list_new(mimeview_mime_types, 1);
585 context = gtk_drag_begin(widget, list,
586 GDK_ACTION_COPY, button, event);
587 gtk_drag_set_icon_default(context);
590 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
593 GtkCList *clist = GTK_CLIST(widget);
597 if (!event) return FALSE;
599 if (event->button == 2 || event->button == 3) {
600 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
603 gtk_clist_unselect_all(clist);
604 gtk_clist_select_row(clist, row, column);
605 gtkut_clist_set_focus_row(clist, row);
608 if (event->button == 2 ||
609 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
610 /* call external program for image, audio or html */
611 mimeview_launch(mimeview);
612 } else if (event->button == 3) {
613 partinfo = gtk_ctree_node_get_row_data
614 (GTK_CTREE(mimeview->ctree), mimeview->opened);
615 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
616 partinfo->mime_type == MIME_TEXT_HTML ||
617 partinfo->mime_type == MIME_TEXT_ENRICHED ||
618 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
619 partinfo->mime_type == MIME_IMAGE ||
620 partinfo->mime_type == MIME_MULTIPART))
621 menu_set_sensitive(mimeview->popupfactory,
622 "/Display as text", FALSE);
624 menu_set_sensitive(mimeview->popupfactory,
625 "/Display as text", TRUE);
627 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
628 menu_set_sensitive(mimeview->popupfactory,
631 menu_set_sensitive(mimeview->popupfactory,
634 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
635 if (partinfo && (partinfo->mime_type == MIME_IMAGE))
636 menu_set_sensitive(mimeview->popupfactory,
637 "/Display image", TRUE);
640 menu_set_sensitive(mimeview->popupfactory,
642 mimeview_is_signed(mimeview));
645 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
646 NULL, NULL, NULL, NULL,
647 event->button, event->time);
653 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
655 mimeview_key_pressed(mimeview->ctree, event, mimeview);
658 #define BREAK_ON_MODIFIER_KEY() \
659 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
661 #define KEY_PRESS_EVENT_STOP() \
662 if (gtk_signal_n_emissions_by_name \
663 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
664 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
665 "key_press_event"); \
668 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
671 SummaryView *summaryview;
672 GtkCTree *ctree = GTK_CTREE(widget);
675 if (!event) return FALSE;
676 if (!mimeview->opened) return FALSE;
678 switch (event->keyval) {
680 if (textview_scroll_page(mimeview->textview, FALSE))
683 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
685 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
686 gtk_sctree_select(GTK_SCTREE(ctree), node);
691 textview_scroll_page(mimeview->textview, TRUE);
694 textview_scroll_one_line(mimeview->textview,
695 (event->state & GDK_MOD1_MASK) != 0);
699 BREAK_ON_MODIFIER_KEY();
700 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
701 KEY_PRESS_EVENT_STOP();
703 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
704 GTK_SCROLL_STEP_FORWARD, 0.0);
708 BREAK_ON_MODIFIER_KEY();
709 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
710 KEY_PRESS_EVENT_STOP();
712 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
713 GTK_SCROLL_STEP_BACKWARD, 0.0);
716 BREAK_ON_MODIFIER_KEY();
717 KEY_PRESS_EVENT_STOP();
718 mimeview_save_as(mimeview);
721 BREAK_ON_MODIFIER_KEY();
722 KEY_PRESS_EVENT_STOP();
723 mimeview_display_as_text(mimeview);
726 BREAK_ON_MODIFIER_KEY();
727 KEY_PRESS_EVENT_STOP();
728 mimeview_show_image(mimeview);
731 BREAK_ON_MODIFIER_KEY();
732 KEY_PRESS_EVENT_STOP();
733 mimeview_launch(mimeview);
739 if (!mimeview->messageview->mainwin) return FALSE;
740 summaryview = mimeview->messageview->mainwin->summaryview;
741 summary_pass_key_press_event(summaryview, event);
745 static void mimeview_drag_data_get(GtkWidget *widget,
746 GdkDragContext *drag_context,
747 GtkSelectionData *selection_data,
752 gchar *filename, *uriname;
755 if (!mimeview->opened) return;
756 if (!mimeview->file) return;
758 partinfo = gtk_ctree_node_get_row_data
759 (GTK_CTREE(mimeview->ctree), mimeview->opened);
760 if (!partinfo) return;
761 if (!partinfo->filename && !partinfo->name) return;
763 filename = partinfo->filename ? partinfo->filename : partinfo->name;
764 filename = g_basename(filename);
765 if (*filename == '\0') return;
767 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
770 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
772 (_("Can't save the part of multipart message."));
774 uriname = g_strconcat("file:/", filename, NULL);
775 gtk_selection_data_set(selection_data, selection_data->target, 8,
776 uriname, strlen(uriname));
782 static void mimeview_display_as_text(MimeView *mimeview)
786 if (!mimeview->opened) return;
788 partinfo = gtk_ctree_node_get_row_data
789 (GTK_CTREE(mimeview->ctree), mimeview->opened);
790 mimeview_show_message_part(mimeview, partinfo);
793 static void mimeview_show_image(MimeView *mimeview)
797 if (!mimeview->opened) return;
799 partinfo = gtk_ctree_node_get_row_data
800 (GTK_CTREE(mimeview->ctree), mimeview->opened);
801 mimeview_show_image_part(mimeview, partinfo);
804 static void mimeview_save_as(MimeView *mimeview)
807 gchar *defname = NULL;
810 if (!mimeview->opened) return;
811 if (!mimeview->file) return;
813 partinfo = gtk_ctree_node_get_row_data
814 (GTK_CTREE(mimeview->ctree), mimeview->opened);
815 g_return_if_fail(partinfo != NULL);
817 if (partinfo->filename)
818 defname = partinfo->filename;
819 else if (partinfo->name) {
820 Xstrdup_a(defname, partinfo->name, return);
821 subst_for_filename(defname);
824 filename = filesel_select_file(_("Save as"), defname);
825 if (!filename) return;
826 if (is_file_exist(filename)) {
829 aval = alertpanel(_("Overwrite"),
830 _("Overwrite existing file?"),
831 _("OK"), _("Cancel"), NULL);
832 if (G_ALERTDEFAULT != aval) return;
835 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
837 (_("Can't save the part of multipart message."));
840 static void mimeview_launch(MimeView *mimeview)
845 if (!mimeview->opened) return;
846 if (!mimeview->file) return;
848 partinfo = gtk_ctree_node_get_row_data
849 (GTK_CTREE(mimeview->ctree), mimeview->opened);
850 g_return_if_fail(partinfo != NULL);
852 filename = procmime_get_tmp_file_name(partinfo);
854 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
856 (_("Can't save the part of multipart message."));
858 mimeview_view_file(filename, partinfo, NULL);
863 static void mimeview_open_with(MimeView *mimeview)
869 if (!mimeview->opened) return;
870 if (!mimeview->file) return;
872 partinfo = gtk_ctree_node_get_row_data
873 (GTK_CTREE(mimeview->ctree), mimeview->opened);
874 g_return_if_fail(partinfo != NULL);
876 filename = procmime_get_tmp_file_name(partinfo);
878 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
880 (_("Can't save the part of multipart message."));
885 if (!prefs_common.mime_open_cmd_history)
886 prefs_common.mime_open_cmd_history =
887 add_history(NULL, prefs_common.mime_open_cmd);
889 cmd = input_dialog_combo
891 _("Enter the command line to open file:\n"
892 "(`%s' will be replaced with file name)"),
893 prefs_common.mime_open_cmd,
894 prefs_common.mime_open_cmd_history,
897 mimeview_view_file(filename, partinfo, cmd);
898 g_free(prefs_common.mime_open_cmd);
899 prefs_common.mime_open_cmd = cmd;
900 prefs_common.mime_open_cmd_history =
901 add_history(prefs_common.mime_open_cmd_history, cmd);
907 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
908 const gchar *cmdline)
910 static gchar *default_image_cmdline = "display '%s'";
911 static gchar *default_audio_cmdline = "play '%s'";
912 static gchar *default_html_cmdline =
913 "netscape -remote 'openURL(%s,raise)'";
914 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
918 const gchar *def_cmd;
924 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
926 } else if (MIME_IMAGE == partinfo->mime_type) {
927 cmd = prefs_common.mime_image_viewer;
928 def_cmd = default_image_cmdline;
929 } else if (MIME_AUDIO == partinfo->mime_type) {
930 cmd = prefs_common.mime_audio_player;
931 def_cmd = default_audio_cmdline;
932 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
933 cmd = prefs_common.uri_cmd;
934 def_cmd = default_html_cmdline;
936 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
937 partinfo->content_type, "%s");
942 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
944 g_snprintf(buf, sizeof(buf), cmd, filename);
947 g_warning(_("MIME viewer command line is invalid: `%s'"), cmd);
949 g_snprintf(buf, sizeof(buf), def_cmd, filename);
954 execute_command_line(buf, TRUE);
958 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
964 partinfo = gtk_ctree_node_get_row_data(ctree, node);
965 g_return_if_fail(partinfo != NULL);
967 part_name = get_part_name(partinfo);
968 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
971 static void mimeview_update_names(MimeView *mimeview)
973 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
975 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
978 static void mimeview_update_signature_info(MimeView *mimeview)
982 if (!mimeview) return;
983 if (!mimeview->opened) return;
985 partinfo = gtk_ctree_node_get_row_data
986 (GTK_CTREE(mimeview->ctree), mimeview->opened);
987 if (!partinfo) return;
989 if (g_strcasecmp(partinfo->content_type,
990 "application/pgp-signature") == 0) {
991 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
992 textview_show_signature_part(mimeview->textview, partinfo);
996 static void mimeview_check_signature(MimeView *mimeview)
1002 g_return_if_fail (mimeview_is_signed(mimeview));
1004 mimeinfo = gtk_ctree_node_get_row_data
1005 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1006 g_return_if_fail(mimeinfo != NULL);
1007 g_return_if_fail(mimeview->file != NULL);
1009 while (mimeinfo->parent)
1010 mimeinfo = mimeinfo->parent;
1012 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1013 FILE_OP_ERROR(mimeview->file, "fopen");
1018 if (mimeinfo->mime_type == MIME_MULTIPART) {
1019 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
1020 FILE_OP_ERROR(mimeview->file, "fseek");
1021 while (fgets(buf, sizeof(buf), fp) != NULL)
1022 if (buf[0] == '\r' || buf[0] == '\n') break;
1025 procmime_scan_multipart_message(mimeinfo, fp);
1026 rfc2015_check_signature(mimeinfo, fp);
1029 mimeview_update_names(mimeview);
1030 mimeview_update_signature_info(mimeview);
1032 #endif /* USE_GPGME */