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_save_all (MimeView *mimeview);
106 static void mimeview_launch (MimeView *mimeview);
107 static void mimeview_open_with (MimeView *mimeview);
108 static void mimeview_view_file (const gchar *filename,
110 const gchar *cmdline);
112 static GtkItemFactoryEntry mimeview_popup_entries[] =
114 {N_("/_Open"), NULL, mimeview_launch, 0, NULL},
115 {N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
116 {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
117 {N_("/_Display image"), NULL, mimeview_show_image, 0, NULL},
118 {N_("/_Save as..."), NULL, mimeview_save_as, 0, NULL},
119 {N_("/Save _all..."), NULL, mimeview_save_all, 0, NULL}
122 {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
126 static GtkTargetEntry mimeview_mime_types[] =
128 {"text/uri-list", 0, 0}
131 MimeView *mimeview_create(void)
138 GtkWidget *scrolledwin;
140 GtkWidget *mime_vbox;
141 GtkWidget *popupmenu;
142 GtkItemFactory *popupfactory;
143 gchar *titles[N_MIMEVIEW_COLS];
147 debug_print("Creating MIME view...\n");
148 mimeview = g_new0(MimeView, 1);
150 titles[COL_MIMETYPE] = _("MIME Type");
151 titles[COL_SIZE] = _("Size");
152 titles[COL_NAME] = _("Name");
154 notebook = gtk_notebook_new();
155 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
157 vbox = gtk_vbox_new(FALSE, 0);
158 gtk_container_add(GTK_CONTAINER(notebook), vbox);
159 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
162 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
163 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
164 GTK_POLICY_AUTOMATIC,
166 gtk_widget_set_usize(scrolledwin, -1, 80);
168 ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
169 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
170 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
171 gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
173 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
174 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
175 for (i = 0; i < N_MIMEVIEW_COLS; i++)
176 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
178 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
180 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
181 GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
182 gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
183 GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
184 gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
185 GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
186 gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
187 GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
188 gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
189 GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
191 mime_vbox = gtk_vbox_new(FALSE, 0);
193 paned = gtk_vpaned_new();
194 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
195 gtk_paned_add2(GTK_PANED(paned), mime_vbox);
196 gtk_container_add(GTK_CONTAINER(notebook), paned);
197 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
200 gtk_widget_show_all(notebook);
202 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
204 n_entries = sizeof(mimeview_popup_entries) /
205 sizeof(mimeview_popup_entries[0]);
206 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
207 "<MimeView>", &popupfactory, mimeview);
209 mimeview->notebook = notebook;
210 mimeview->vbox = vbox;
211 mimeview->paned = paned;
212 mimeview->scrolledwin = scrolledwin;
213 mimeview->ctree = ctree;
214 mimeview->mime_vbox = mime_vbox;
215 mimeview->popupmenu = popupmenu;
216 mimeview->popupfactory = popupfactory;
222 void mimeview_init(MimeView *mimeview)
227 * Check whether the message is OpenPGP signed
230 static gboolean mimeview_is_signed(MimeView *mimeview)
232 MimeInfo *partinfo = NULL;
234 debug_print("mimeview_is signed of %p\n", mimeview);
236 if (!mimeview) return FALSE;
237 if (!mimeview->opened) return FALSE;
239 debug_print("mimeview_is_signed: open\n" );
241 if (!mimeview->file) return FALSE;
243 debug_print("mimeview_is_signed: file\n" );
245 partinfo = gtk_ctree_node_get_row_data
246 (GTK_CTREE(mimeview->ctree), mimeview->opened);
247 g_return_val_if_fail(partinfo != NULL, FALSE);
249 /* walk the tree and see whether there is a signature somewhere */
251 if (rfc2015_has_signature(partinfo))
253 } while ((partinfo = partinfo->parent) != NULL);
255 debug_print("mimeview_is_signed: FALSE\n" );
260 static void set_unchecked_signature(MimeInfo *mimeinfo)
262 MimeInfo *sig_partinfo;
264 sig_partinfo = rfc2015_find_signature(mimeinfo);
265 if (sig_partinfo == NULL) return;
267 g_free(sig_partinfo->sigstatus);
268 sig_partinfo->sigstatus =
269 g_strdup(_("Select \"Check signature\" to check"));
271 g_free(sig_partinfo->sigstatus_full);
272 sig_partinfo->sigstatus_full = NULL;
274 #endif /* USE_GPGME */
276 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
279 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
284 mimeview_clear(mimeview);
285 textview_clear(mimeview->messageview->textview);
287 g_return_if_fail(file != NULL);
288 g_return_if_fail(mimeinfo != NULL);
290 mimeview->mimeinfo = mimeinfo;
292 if ((fp = fopen(file, "rb")) == NULL) {
293 FILE_OP_ERROR(file, "fopen");
296 mimeview->file = g_strdup(file);
298 /* skip MIME part headers */
299 if (mimeinfo->mime_type == MIME_MULTIPART) {
300 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
302 while (fgets(buf, sizeof(buf), fp) != NULL)
303 if (buf[0] == '\r' || buf[0] == '\n') break;
306 procmime_scan_multipart_message(mimeinfo, fp);
308 if (prefs_common.auto_check_signatures)
309 rfc2015_check_signature(mimeinfo, fp);
311 set_unchecked_signature(mimeinfo);
314 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
317 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
319 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
320 mimeview_selected, mimeview);
322 /* search first text part */
323 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
324 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
327 partinfo = gtk_ctree_node_get_row_data(ctree, node);
329 (partinfo->mime_type == MIME_TEXT ||
330 partinfo->mime_type == MIME_TEXT_HTML))
334 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
337 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
340 gtk_ctree_select(ctree, node);
341 gtkut_ctree_set_focus_row(ctree, node);
342 gtk_widget_grab_focus(mimeview->ctree);
346 void mimeview_destroy(MimeView *mimeview)
348 procmime_mimeinfo_free_all(mimeview->mimeinfo);
349 g_free(mimeview->file);
353 static void mimeview_set_multipart_tree(MimeView *mimeview,
355 GtkCTreeNode *parent)
357 GtkCTreeNode *current = parent;
359 g_return_if_fail(mimeinfo != NULL);
361 if (!mimeinfo->sub && mimeinfo->parent)
362 current = mimeview_append_part(mimeview, mimeinfo, parent);
363 if (mimeinfo->sub && !mimeinfo->sub->children &&
364 mimeinfo->sub->mime_type != MIME_TEXT &&
365 mimeinfo->sub->mime_type != MIME_TEXT_HTML) {
366 mimeview_append_part(mimeview, mimeinfo->sub, parent);
371 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
373 if (mimeinfo->children)
374 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
377 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
380 static gchar *get_part_name(MimeInfo *partinfo)
383 if (partinfo->sigstatus)
384 return partinfo->sigstatus;
388 return partinfo->name;
389 else if (partinfo->filename)
390 return partinfo->filename;
395 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
397 GtkCTreeNode *parent)
399 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
401 gchar *str[N_MIMEVIEW_COLS];
404 partinfo->content_type ? partinfo->content_type : "";
405 str[COL_SIZE] = to_human_readable(partinfo->size);
406 str[COL_NAME] = get_part_name(partinfo);
408 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
409 NULL, NULL, NULL, NULL,
411 gtk_ctree_node_set_row_data(ctree, node, partinfo);
416 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
424 if (!partinfo) return;
427 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
429 fname = pi ? pi->plaintextfile : mimeview->file;
431 fname = mimeview->file;
432 #endif /* USE_GPGME */
435 if ((fp = fopen(fname, "rb")) == NULL) {
436 FILE_OP_ERROR(fname, "fopen");
440 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
441 FILE_OP_ERROR(mimeview->file, "fseek");
446 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
447 textview_show_part(mimeview->textview, partinfo, fp);
452 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
456 if (!partinfo) return;
458 filename = procmime_get_tmp_file_name(partinfo);
460 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
462 (_("Can't get the part of multipart message."));
464 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
465 /* Workaround for the GTK+ bug with handling scroll adjustments
467 imageview_clear(mimeview->imageview);
468 imageview_show_image(mimeview->imageview, partinfo, filename);
475 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
477 TextView *textview = mimeview->textview;
478 ImageView *imageview = mimeview->imageview;
481 if (mimeview->type == type) return;
483 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
485 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
486 GTK_WIDGET(children->data));
487 g_list_free(children);
492 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
493 GTK_WIDGET_PTR(imageview));
496 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
497 GTK_WIDGET_PTR(textview));
503 mimeview->type = type;
506 static void mimeview_clear(MimeView *mimeview)
508 GtkCList *clist = GTK_CLIST(mimeview->ctree);
510 procmime_mimeinfo_free_all(mimeview->mimeinfo);
511 mimeview->mimeinfo = NULL;
513 gtk_clist_clear(clist);
514 textview_clear(mimeview->textview);
515 imageview_clear(mimeview->imageview);
517 mimeview->opened = NULL;
519 g_free(mimeview->file);
520 mimeview->file = NULL;
522 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
525 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
530 if (mimeview->opened == node) return;
531 mimeview->opened = node;
532 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
534 partinfo = gtk_ctree_node_get_row_data(ctree, node);
535 if (!partinfo) return;
537 /* ungrab the mouse event */
538 if (GTK_WIDGET_HAS_GRAB(ctree)) {
539 gtk_grab_remove(GTK_WIDGET(ctree));
540 if (gdk_pointer_is_grabbed())
541 gdk_pointer_ungrab(GDK_CURRENT_TIME);
544 mimeview->textview->default_text = FALSE;
546 switch (partinfo->mime_type) {
549 case MIME_TEXT_ENRICHED:
550 case MIME_MESSAGE_RFC822:
552 mimeview_show_message_part(mimeview, partinfo);
555 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
557 mimeview->textview->default_text = TRUE;
558 if (prefs_common.display_img)
559 mimeview_show_image_part(mimeview, partinfo);
561 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
562 textview_show_mime_part(mimeview->textview, partinfo);
567 mimeview->textview->default_text = TRUE;
568 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
570 if (g_strcasecmp(partinfo->content_type,
571 "application/pgp-signature") == 0)
572 textview_show_signature_part(mimeview->textview,
576 textview_show_mime_part(mimeview->textview, partinfo);
581 static void mimeview_start_drag(GtkWidget *widget, gint button,
582 GdkEvent *event, MimeView *mimeview)
585 GdkDragContext *context;
589 g_return_if_fail(mimeview != NULL);
590 ctree = GTK_CTREE(mimeview->ctree);
591 partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
592 if (partinfo->filename == NULL && partinfo->name == NULL) return;
594 list = gtk_target_list_new(mimeview_mime_types, 1);
595 context = gtk_drag_begin(widget, list,
596 GDK_ACTION_COPY, button, event);
597 gtk_drag_set_icon_default(context);
600 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
603 GtkCList *clist = GTK_CLIST(widget);
607 if (!event) return FALSE;
609 if (event->button == 2 || event->button == 3) {
610 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
613 gtk_clist_unselect_all(clist);
614 gtk_clist_select_row(clist, row, column);
615 gtkut_clist_set_focus_row(clist, row);
618 if (event->button == 2 ||
619 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
620 /* call external program for image, audio or html */
621 mimeview_launch(mimeview);
622 } else if (event->button == 3) {
623 partinfo = gtk_ctree_node_get_row_data
624 (GTK_CTREE(mimeview->ctree), mimeview->opened);
625 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
626 partinfo->mime_type == MIME_TEXT_HTML ||
627 partinfo->mime_type == MIME_TEXT_ENRICHED ||
628 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
629 partinfo->mime_type == MIME_IMAGE ||
630 partinfo->mime_type == MIME_MULTIPART))
631 menu_set_sensitive(mimeview->popupfactory,
632 "/Display as text", FALSE);
634 menu_set_sensitive(mimeview->popupfactory,
635 "/Display as text", TRUE);
637 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
638 menu_set_sensitive(mimeview->popupfactory,
641 menu_set_sensitive(mimeview->popupfactory,
644 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
645 if (partinfo && (partinfo->mime_type == MIME_IMAGE))
646 menu_set_sensitive(mimeview->popupfactory,
647 "/Display image", TRUE);
650 menu_set_sensitive(mimeview->popupfactory,
652 mimeview_is_signed(mimeview));
655 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
656 NULL, NULL, NULL, NULL,
657 event->button, event->time);
663 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
665 mimeview_key_pressed(mimeview->ctree, event, mimeview);
668 #define BREAK_ON_MODIFIER_KEY() \
669 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
671 #define KEY_PRESS_EVENT_STOP() \
672 if (gtk_signal_n_emissions_by_name \
673 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
674 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
675 "key_press_event"); \
678 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
681 SummaryView *summaryview;
682 GtkCTree *ctree = GTK_CTREE(widget);
685 if (!event) return FALSE;
686 if (!mimeview->opened) return FALSE;
688 switch (event->keyval) {
690 if (textview_scroll_page(mimeview->textview, FALSE))
693 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
695 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
696 gtk_sctree_select(GTK_SCTREE(ctree), node);
701 textview_scroll_page(mimeview->textview, TRUE);
704 textview_scroll_one_line(mimeview->textview,
705 (event->state & GDK_MOD1_MASK) != 0);
709 BREAK_ON_MODIFIER_KEY();
710 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
711 KEY_PRESS_EVENT_STOP();
713 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
714 GTK_SCROLL_STEP_FORWARD, 0.0);
718 BREAK_ON_MODIFIER_KEY();
719 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
720 KEY_PRESS_EVENT_STOP();
722 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
723 GTK_SCROLL_STEP_BACKWARD, 0.0);
726 BREAK_ON_MODIFIER_KEY();
727 KEY_PRESS_EVENT_STOP();
728 mimeview_save_as(mimeview);
731 BREAK_ON_MODIFIER_KEY();
732 KEY_PRESS_EVENT_STOP();
733 mimeview_display_as_text(mimeview);
736 BREAK_ON_MODIFIER_KEY();
737 KEY_PRESS_EVENT_STOP();
738 mimeview_show_image(mimeview);
741 BREAK_ON_MODIFIER_KEY();
742 KEY_PRESS_EVENT_STOP();
743 mimeview_launch(mimeview);
749 if (!mimeview->messageview->mainwin) return FALSE;
750 summaryview = mimeview->messageview->mainwin->summaryview;
751 summary_pass_key_press_event(summaryview, event);
755 static void mimeview_drag_data_get(GtkWidget *widget,
756 GdkDragContext *drag_context,
757 GtkSelectionData *selection_data,
762 gchar *filename, *uriname;
765 if (!mimeview->opened) return;
766 if (!mimeview->file) return;
768 partinfo = gtk_ctree_node_get_row_data
769 (GTK_CTREE(mimeview->ctree), mimeview->opened);
770 if (!partinfo) return;
771 if (!partinfo->filename && !partinfo->name) return;
773 filename = partinfo->filename ? partinfo->filename : partinfo->name;
774 filename = g_basename(filename);
775 if (*filename == '\0') return;
777 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
780 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
782 (_("Can't save the part of multipart message."));
784 uriname = g_strconcat("file:/", filename, NULL);
785 gtk_selection_data_set(selection_data, selection_data->target, 8,
786 uriname, strlen(uriname));
792 static void mimeview_save_all(MimeView *mimeview)
795 gchar *defname = NULL;
797 MimeInfo *attachment;
800 if (!mimeview->opened) return;
801 if (!mimeview->file) return;
803 partinfo = gtk_ctree_node_get_row_data
804 (GTK_CTREE(mimeview->ctree), mimeview->opened);
805 g_return_if_fail(partinfo != NULL);
807 dirname = filesel_select_file(_("Save as"), defname);
808 if (!dirname) return;
810 /* return to first children */
811 attachment = partinfo->parent->children->next;
812 /* for each attachment, extract it in the selected dir. */
813 while(attachment != NULL)
815 static guint subst_cnt = 1;
817 gchar *attachname = (attachment->filename)
818 ? g_strdup(attachment->filename)
819 : g_strdup_printf("noname.%d",subst_cnt++);
821 subst_chars(attachname, "/\\", G_DIR_SEPARATOR);
822 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
823 g_snprintf(buf, sizeof(buf), "%s%s",
825 (attachname[0] == G_DIR_SEPARATOR)
828 attachdir = g_dirname(buf);
829 make_dir_hier(attachdir);
833 if (is_file_exist(buf)) {
836 aval = alertpanel(_("Overwrite"),
837 _("Overwrite existing file?"),
838 _("OK"), _("Cancel"), NULL);
839 if (G_ALERTDEFAULT != aval) return;
841 if (procmime_get_part(buf, mimeview->file, attachment) < 0)
842 alertpanel_error(_("Can't save the part of multipart message."));
843 attachment = attachment->next;
847 static void mimeview_display_as_text(MimeView *mimeview)
851 if (!mimeview->opened) return;
853 partinfo = gtk_ctree_node_get_row_data
854 (GTK_CTREE(mimeview->ctree), mimeview->opened);
855 mimeview_show_message_part(mimeview, partinfo);
858 static void mimeview_show_image(MimeView *mimeview)
862 if (!mimeview->opened) return;
864 partinfo = gtk_ctree_node_get_row_data
865 (GTK_CTREE(mimeview->ctree), mimeview->opened);
866 mimeview_show_image_part(mimeview, partinfo);
869 static void mimeview_save_as(MimeView *mimeview)
872 gchar *defname = NULL;
875 if (!mimeview->opened) return;
876 if (!mimeview->file) return;
878 partinfo = gtk_ctree_node_get_row_data
879 (GTK_CTREE(mimeview->ctree), mimeview->opened);
880 g_return_if_fail(partinfo != NULL);
882 if (partinfo->filename)
883 defname = partinfo->filename;
884 else if (partinfo->name) {
885 Xstrdup_a(defname, partinfo->name, return);
886 subst_for_filename(defname);
889 filename = filesel_select_file(_("Save as"), defname);
890 if (!filename) return;
891 if (is_file_exist(filename)) {
894 aval = alertpanel(_("Overwrite"),
895 _("Overwrite existing file?"),
896 _("OK"), _("Cancel"), NULL);
897 if (G_ALERTDEFAULT != aval) return;
900 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
902 (_("Can't save the part of multipart message."));
905 static void mimeview_launch(MimeView *mimeview)
910 if (!mimeview->opened) return;
911 if (!mimeview->file) return;
913 partinfo = gtk_ctree_node_get_row_data
914 (GTK_CTREE(mimeview->ctree), mimeview->opened);
915 g_return_if_fail(partinfo != NULL);
917 filename = procmime_get_tmp_file_name(partinfo);
919 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
921 (_("Can't save the part of multipart message."));
923 mimeview_view_file(filename, partinfo, NULL);
928 static void mimeview_open_with(MimeView *mimeview)
934 if (!mimeview->opened) return;
935 if (!mimeview->file) return;
937 partinfo = gtk_ctree_node_get_row_data
938 (GTK_CTREE(mimeview->ctree), mimeview->opened);
939 g_return_if_fail(partinfo != NULL);
941 filename = procmime_get_tmp_file_name(partinfo);
943 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
945 (_("Can't save the part of multipart message."));
950 if (!prefs_common.mime_open_cmd_history)
951 prefs_common.mime_open_cmd_history =
952 add_history(NULL, prefs_common.mime_open_cmd);
954 cmd = input_dialog_combo
956 _("Enter the command line to open file:\n"
957 "(`%s' will be replaced with file name)"),
958 prefs_common.mime_open_cmd,
959 prefs_common.mime_open_cmd_history,
962 mimeview_view_file(filename, partinfo, cmd);
963 g_free(prefs_common.mime_open_cmd);
964 prefs_common.mime_open_cmd = cmd;
965 prefs_common.mime_open_cmd_history =
966 add_history(prefs_common.mime_open_cmd_history, cmd);
972 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
973 const gchar *cmdline)
975 static gchar *default_image_cmdline = "display '%s'";
976 static gchar *default_audio_cmdline = "play '%s'";
977 static gchar *default_html_cmdline =
978 "netscape -remote 'openURL(%s,raise)'";
979 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
983 const gchar *def_cmd;
989 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
991 } else if (MIME_IMAGE == partinfo->mime_type) {
992 cmd = prefs_common.mime_image_viewer;
993 def_cmd = default_image_cmdline;
994 } else if (MIME_AUDIO == partinfo->mime_type) {
995 cmd = prefs_common.mime_audio_player;
996 def_cmd = default_audio_cmdline;
997 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
998 cmd = prefs_common.uri_cmd;
999 def_cmd = default_html_cmdline;
1001 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1002 partinfo->content_type, "%s");
1007 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1008 !strchr(p + 2, '%'))
1009 g_snprintf(buf, sizeof(buf), cmd, filename);
1012 g_warning(_("MIME viewer command line is invalid: `%s'"), cmd);
1014 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1019 execute_command_line(buf, TRUE);
1023 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1029 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1030 g_return_if_fail(partinfo != NULL);
1032 part_name = get_part_name(partinfo);
1033 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1036 static void mimeview_update_names(MimeView *mimeview)
1038 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1040 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1043 static void mimeview_update_signature_info(MimeView *mimeview)
1047 if (!mimeview) return;
1048 if (!mimeview->opened) return;
1050 partinfo = gtk_ctree_node_get_row_data
1051 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1052 if (!partinfo) return;
1054 if (g_strcasecmp(partinfo->content_type,
1055 "application/pgp-signature") == 0) {
1056 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1057 textview_show_signature_part(mimeview->textview, partinfo);
1061 void mimeview_check_signature(MimeView *mimeview)
1066 g_return_if_fail (mimeview_is_signed(mimeview));
1068 mimeinfo = gtk_ctree_node_get_row_data
1069 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1070 g_return_if_fail(mimeinfo != NULL);
1071 g_return_if_fail(mimeview->file != NULL);
1073 while (mimeinfo->parent)
1074 mimeinfo = mimeinfo->parent;
1076 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1077 FILE_OP_ERROR(mimeview->file, "fopen");
1081 rfc2015_check_signature(mimeinfo, fp);
1084 mimeview_update_names(mimeview);
1085 mimeview_update_signature_info(mimeview);
1087 textview_show_message(mimeview->messageview->textview, mimeinfo,
1090 #endif /* USE_GPGME */