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 void mimeview_check_signature (MimeView *mimeview);
115 static GtkItemFactoryEntry mimeview_popup_entries[] =
117 {N_("/_Open"), NULL, mimeview_launch, 0, NULL},
118 {N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
119 {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
120 {N_("/_Display image"), NULL, mimeview_show_image, 0, NULL},
121 {N_("/_Save as..."), NULL, mimeview_save_as, 0, NULL},
122 {N_("/Save _all..."), NULL, mimeview_save_all, 0, NULL}
125 {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
129 static GtkTargetEntry mimeview_mime_types[] =
131 {"text/uri-list", 0, 0}
134 MimeView *mimeview_create(void)
141 GtkWidget *scrolledwin;
143 GtkWidget *mime_vbox;
144 GtkWidget *popupmenu;
145 GtkItemFactory *popupfactory;
146 gchar *titles[N_MIMEVIEW_COLS];
150 debug_print(_("Creating MIME view...\n"));
151 mimeview = g_new0(MimeView, 1);
153 titles[COL_MIMETYPE] = _("MIME Type");
154 titles[COL_SIZE] = _("Size");
155 titles[COL_NAME] = _("Name");
157 notebook = gtk_notebook_new();
158 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
160 vbox = gtk_vbox_new(FALSE, 0);
161 gtk_container_add(GTK_CONTAINER(notebook), vbox);
162 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
165 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
166 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
167 GTK_POLICY_AUTOMATIC,
169 gtk_widget_set_usize(scrolledwin, -1, 80);
171 ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
172 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
173 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
174 gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
176 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
177 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
178 for (i = 0; i < N_MIMEVIEW_COLS; i++)
179 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
181 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
183 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
184 GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
185 gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
186 GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
187 gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
188 GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
189 gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
190 GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
191 gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
192 GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
194 mime_vbox = gtk_vbox_new(FALSE, 0);
196 paned = gtk_vpaned_new();
197 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
198 gtk_paned_add2(GTK_PANED(paned), mime_vbox);
199 gtk_container_add(GTK_CONTAINER(notebook), paned);
200 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
203 gtk_widget_show_all(notebook);
205 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
207 n_entries = sizeof(mimeview_popup_entries) /
208 sizeof(mimeview_popup_entries[0]);
209 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
210 "<MimeView>", &popupfactory, mimeview);
212 mimeview->notebook = notebook;
213 mimeview->vbox = vbox;
214 mimeview->paned = paned;
215 mimeview->scrolledwin = scrolledwin;
216 mimeview->ctree = ctree;
217 mimeview->mime_vbox = mime_vbox;
218 mimeview->popupmenu = popupmenu;
219 mimeview->popupfactory = popupfactory;
225 void mimeview_init(MimeView *mimeview)
230 * Check whether the message is OpenPGP signed
233 static gboolean mimeview_is_signed(MimeView *mimeview)
235 MimeInfo *partinfo = NULL;
237 debug_print("mimeview_is signed of %p\n", mimeview);
239 if (!mimeview) return FALSE;
240 if (!mimeview->opened) return FALSE;
242 debug_print("mimeview_is_signed: open\n" );
244 if (!mimeview->file) return FALSE;
246 debug_print("mimeview_is_signed: file\n" );
248 partinfo = gtk_ctree_node_get_row_data
249 (GTK_CTREE(mimeview->ctree), mimeview->opened);
250 g_return_val_if_fail(partinfo != NULL, FALSE);
252 /* walk the tree and see whether there is a signature somewhere */
254 if (rfc2015_has_signature(partinfo))
256 } while ((partinfo = partinfo->parent) != NULL);
258 debug_print("mimeview_is_signed: FALSE\n" );
263 static void set_unchecked_signature(MimeInfo *mimeinfo)
265 MimeInfo *sig_partinfo;
267 sig_partinfo = rfc2015_find_signature(mimeinfo);
268 if (sig_partinfo == NULL) return;
270 g_free(sig_partinfo->sigstatus);
271 sig_partinfo->sigstatus =
272 g_strdup(_("Select \"Check signature\" to check"));
274 g_free(sig_partinfo->sigstatus_full);
275 sig_partinfo->sigstatus_full = NULL;
277 #endif /* USE_GPGME */
279 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
282 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
287 mimeview_clear(mimeview);
288 textview_clear(mimeview->messageview->textview);
290 g_return_if_fail(file != NULL);
291 g_return_if_fail(mimeinfo != NULL);
293 mimeview->mimeinfo = mimeinfo;
295 if ((fp = fopen(file, "rb")) == NULL) {
296 FILE_OP_ERROR(file, "fopen");
299 mimeview->file = g_strdup(file);
301 /* skip MIME part headers */
302 if (mimeinfo->mime_type == MIME_MULTIPART) {
303 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
305 while (fgets(buf, sizeof(buf), fp) != NULL)
306 if (buf[0] == '\r' || buf[0] == '\n') break;
309 procmime_scan_multipart_message(mimeinfo, fp);
311 if (prefs_common.auto_check_signatures)
312 rfc2015_check_signature(mimeinfo, fp);
314 set_unchecked_signature(mimeinfo);
317 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
320 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
322 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
323 mimeview_selected, mimeview);
325 /* search first text part */
326 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
327 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
330 partinfo = gtk_ctree_node_get_row_data(ctree, node);
332 (partinfo->mime_type == MIME_TEXT ||
333 partinfo->mime_type == MIME_TEXT_HTML))
337 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
340 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
343 gtk_ctree_select(ctree, node);
344 gtkut_ctree_set_focus_row(ctree, node);
345 gtk_widget_grab_focus(mimeview->ctree);
349 void mimeview_destroy(MimeView *mimeview)
351 procmime_mimeinfo_free_all(mimeview->mimeinfo);
352 g_free(mimeview->file);
356 static void mimeview_set_multipart_tree(MimeView *mimeview,
358 GtkCTreeNode *parent)
360 GtkCTreeNode *current = parent;
362 g_return_if_fail(mimeinfo != NULL);
364 if (!mimeinfo->sub && mimeinfo->parent)
365 current = mimeview_append_part(mimeview, mimeinfo, parent);
366 if (mimeinfo->sub && !mimeinfo->sub->children &&
367 mimeinfo->sub->mime_type != MIME_TEXT &&
368 mimeinfo->sub->mime_type != MIME_TEXT_HTML) {
369 mimeview_append_part(mimeview, mimeinfo->sub, parent);
374 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
376 if (mimeinfo->children)
377 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
380 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
383 static gchar *get_part_name(MimeInfo *partinfo)
386 if (partinfo->sigstatus)
387 return partinfo->sigstatus;
391 return partinfo->name;
392 else if (partinfo->filename)
393 return partinfo->filename;
398 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
400 GtkCTreeNode *parent)
402 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
404 gchar *str[N_MIMEVIEW_COLS];
407 partinfo->content_type ? partinfo->content_type : "";
408 str[COL_SIZE] = to_human_readable(partinfo->size);
409 str[COL_NAME] = get_part_name(partinfo);
411 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
412 NULL, NULL, NULL, NULL,
414 gtk_ctree_node_set_row_data(ctree, node, partinfo);
419 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
427 if (!partinfo) return;
430 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
432 fname = pi ? pi->plaintextfile : mimeview->file;
434 fname = mimeview->file;
435 #endif /* USE_GPGME */
438 if ((fp = fopen(fname, "rb")) == NULL) {
439 FILE_OP_ERROR(fname, "fopen");
443 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
444 FILE_OP_ERROR(mimeview->file, "fseek");
449 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
450 textview_show_part(mimeview->textview, partinfo, fp);
455 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
459 if (!partinfo) return;
461 filename = procmime_get_tmp_file_name(partinfo);
463 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
465 (_("Can't get the part of multipart message."));
467 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
468 /* Workaround for the GTK+ bug with handling scroll adjustments
470 imageview_clear(mimeview->imageview);
471 imageview_show_image(mimeview->imageview, partinfo, filename);
478 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
480 TextView *textview = mimeview->textview;
481 ImageView *imageview = mimeview->imageview;
484 if (mimeview->type == type) return;
486 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
488 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
489 GTK_WIDGET(children->data));
490 g_list_free(children);
495 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
496 GTK_WIDGET_PTR(imageview));
499 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
500 GTK_WIDGET_PTR(textview));
506 mimeview->type = type;
509 static void mimeview_clear(MimeView *mimeview)
511 GtkCList *clist = GTK_CLIST(mimeview->ctree);
513 procmime_mimeinfo_free_all(mimeview->mimeinfo);
514 mimeview->mimeinfo = NULL;
516 gtk_clist_clear(clist);
517 textview_clear(mimeview->textview);
518 imageview_clear(mimeview->imageview);
520 mimeview->opened = NULL;
522 g_free(mimeview->file);
523 mimeview->file = NULL;
525 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
528 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
533 if (mimeview->opened == node) return;
534 mimeview->opened = node;
535 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
537 partinfo = gtk_ctree_node_get_row_data(ctree, node);
538 if (!partinfo) return;
540 /* ungrab the mouse event */
541 if (GTK_WIDGET_HAS_GRAB(ctree)) {
542 gtk_grab_remove(GTK_WIDGET(ctree));
543 if (gdk_pointer_is_grabbed())
544 gdk_pointer_ungrab(GDK_CURRENT_TIME);
547 mimeview->textview->default_text = FALSE;
549 switch (partinfo->mime_type) {
552 case MIME_TEXT_ENRICHED:
553 case MIME_MESSAGE_RFC822:
555 mimeview_show_message_part(mimeview, partinfo);
558 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
560 mimeview->textview->default_text = TRUE;
561 if (prefs_common.display_img)
562 mimeview_show_image_part(mimeview, partinfo);
564 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
565 textview_show_mime_part(mimeview->textview, partinfo);
570 mimeview->textview->default_text = TRUE;
571 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
573 if (g_strcasecmp(partinfo->content_type,
574 "application/pgp-signature") == 0)
575 textview_show_signature_part(mimeview->textview,
579 textview_show_mime_part(mimeview->textview, partinfo);
584 static void mimeview_start_drag(GtkWidget *widget, gint button,
585 GdkEvent *event, MimeView *mimeview)
588 GdkDragContext *context;
592 g_return_if_fail(mimeview != NULL);
593 ctree = GTK_CTREE(mimeview->ctree);
594 partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
595 if (partinfo->filename == NULL && partinfo->name == NULL) return;
597 list = gtk_target_list_new(mimeview_mime_types, 1);
598 context = gtk_drag_begin(widget, list,
599 GDK_ACTION_COPY, button, event);
600 gtk_drag_set_icon_default(context);
603 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
606 GtkCList *clist = GTK_CLIST(widget);
610 if (!event) return FALSE;
612 if (event->button == 2 || event->button == 3) {
613 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
616 gtk_clist_unselect_all(clist);
617 gtk_clist_select_row(clist, row, column);
618 gtkut_clist_set_focus_row(clist, row);
621 if (event->button == 2 ||
622 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
623 /* call external program for image, audio or html */
624 mimeview_launch(mimeview);
625 } else if (event->button == 3) {
626 partinfo = gtk_ctree_node_get_row_data
627 (GTK_CTREE(mimeview->ctree), mimeview->opened);
628 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
629 partinfo->mime_type == MIME_TEXT_HTML ||
630 partinfo->mime_type == MIME_TEXT_ENRICHED ||
631 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
632 partinfo->mime_type == MIME_IMAGE ||
633 partinfo->mime_type == MIME_MULTIPART))
634 menu_set_sensitive(mimeview->popupfactory,
635 "/Display as text", FALSE);
637 menu_set_sensitive(mimeview->popupfactory,
638 "/Display as text", TRUE);
640 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
641 menu_set_sensitive(mimeview->popupfactory,
644 menu_set_sensitive(mimeview->popupfactory,
647 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
648 if (partinfo && (partinfo->mime_type == MIME_IMAGE))
649 menu_set_sensitive(mimeview->popupfactory,
650 "/Display image", TRUE);
653 menu_set_sensitive(mimeview->popupfactory,
655 mimeview_is_signed(mimeview));
658 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
659 NULL, NULL, NULL, NULL,
660 event->button, event->time);
666 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
668 mimeview_key_pressed(mimeview->ctree, event, mimeview);
671 #define BREAK_ON_MODIFIER_KEY() \
672 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
674 #define KEY_PRESS_EVENT_STOP() \
675 if (gtk_signal_n_emissions_by_name \
676 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
677 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
678 "key_press_event"); \
681 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
684 SummaryView *summaryview;
685 GtkCTree *ctree = GTK_CTREE(widget);
688 if (!event) return FALSE;
689 if (!mimeview->opened) return FALSE;
691 switch (event->keyval) {
693 if (textview_scroll_page(mimeview->textview, FALSE))
696 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
698 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
699 gtk_sctree_select(GTK_SCTREE(ctree), node);
704 textview_scroll_page(mimeview->textview, TRUE);
707 textview_scroll_one_line(mimeview->textview,
708 (event->state & GDK_MOD1_MASK) != 0);
712 BREAK_ON_MODIFIER_KEY();
713 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
714 KEY_PRESS_EVENT_STOP();
716 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
717 GTK_SCROLL_STEP_FORWARD, 0.0);
721 BREAK_ON_MODIFIER_KEY();
722 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
723 KEY_PRESS_EVENT_STOP();
725 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
726 GTK_SCROLL_STEP_BACKWARD, 0.0);
729 BREAK_ON_MODIFIER_KEY();
730 KEY_PRESS_EVENT_STOP();
731 mimeview_save_as(mimeview);
734 BREAK_ON_MODIFIER_KEY();
735 KEY_PRESS_EVENT_STOP();
736 mimeview_display_as_text(mimeview);
739 BREAK_ON_MODIFIER_KEY();
740 KEY_PRESS_EVENT_STOP();
741 mimeview_show_image(mimeview);
744 BREAK_ON_MODIFIER_KEY();
745 KEY_PRESS_EVENT_STOP();
746 mimeview_launch(mimeview);
752 if (!mimeview->messageview->mainwin) return FALSE;
753 summaryview = mimeview->messageview->mainwin->summaryview;
754 summary_pass_key_press_event(summaryview, event);
758 static void mimeview_drag_data_get(GtkWidget *widget,
759 GdkDragContext *drag_context,
760 GtkSelectionData *selection_data,
765 gchar *filename, *uriname;
768 if (!mimeview->opened) return;
769 if (!mimeview->file) return;
771 partinfo = gtk_ctree_node_get_row_data
772 (GTK_CTREE(mimeview->ctree), mimeview->opened);
773 if (!partinfo) return;
774 if (!partinfo->filename && !partinfo->name) return;
776 filename = partinfo->filename ? partinfo->filename : partinfo->name;
777 filename = g_basename(filename);
778 if (*filename == '\0') return;
780 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
783 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
785 (_("Can't save the part of multipart message."));
787 uriname = g_strconcat("file:/", filename, NULL);
788 gtk_selection_data_set(selection_data, selection_data->target, 8,
789 uriname, strlen(uriname));
795 static void mimeview_save_all(MimeView *mimeview)
798 gchar *defname = NULL;
800 MimeInfo *attachment;
803 if (!mimeview->opened) return;
804 if (!mimeview->file) return;
806 partinfo = gtk_ctree_node_get_row_data
807 (GTK_CTREE(mimeview->ctree), mimeview->opened);
808 g_return_if_fail(partinfo != NULL);
810 dirname = filesel_select_file(_("Save as"), defname);
811 if (!dirname) return;
813 /* return to first children */
814 attachment = partinfo->parent->children->next;
815 /* for each attachment, extract it in the selected dir. */
816 while(attachment != NULL)
818 if(attachment->filename) {
820 gchar *attachname = g_strdup(attachment->filename);
822 subst_chars(attachname, "/\\", G_DIR_SEPARATOR);
823 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
824 g_snprintf(buf, sizeof(buf), "%s%s",
826 (attachname[0] == G_DIR_SEPARATOR)
829 attachdir = g_dirname(buf);
830 make_dir_hier(g_dirname(buf));
834 if (is_file_exist(buf)) {
837 aval = alertpanel(_("Overwrite"),
838 _("Overwrite existing file?"),
839 _("OK"), _("Cancel"), NULL);
840 if (G_ALERTDEFAULT != aval) return;
842 if (procmime_get_part(buf, mimeview->file, attachment) < 0)
843 alertpanel_error(_("Can't save the part of multipart message."));
845 attachment = attachment->next;
850 static void mimeview_display_as_text(MimeView *mimeview)
854 if (!mimeview->opened) return;
856 partinfo = gtk_ctree_node_get_row_data
857 (GTK_CTREE(mimeview->ctree), mimeview->opened);
858 mimeview_show_message_part(mimeview, partinfo);
861 static void mimeview_show_image(MimeView *mimeview)
865 if (!mimeview->opened) return;
867 partinfo = gtk_ctree_node_get_row_data
868 (GTK_CTREE(mimeview->ctree), mimeview->opened);
869 mimeview_show_image_part(mimeview, partinfo);
872 static void mimeview_save_as(MimeView *mimeview)
875 gchar *defname = NULL;
878 if (!mimeview->opened) return;
879 if (!mimeview->file) return;
881 partinfo = gtk_ctree_node_get_row_data
882 (GTK_CTREE(mimeview->ctree), mimeview->opened);
883 g_return_if_fail(partinfo != NULL);
885 if (partinfo->filename)
886 defname = partinfo->filename;
887 else if (partinfo->name) {
888 Xstrdup_a(defname, partinfo->name, return);
889 subst_for_filename(defname);
892 filename = filesel_select_file(_("Save as"), defname);
893 if (!filename) return;
894 if (is_file_exist(filename)) {
897 aval = alertpanel(_("Overwrite"),
898 _("Overwrite existing file?"),
899 _("OK"), _("Cancel"), NULL);
900 if (G_ALERTDEFAULT != aval) return;
903 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
905 (_("Can't save the part of multipart message."));
908 static void mimeview_launch(MimeView *mimeview)
913 if (!mimeview->opened) return;
914 if (!mimeview->file) return;
916 partinfo = gtk_ctree_node_get_row_data
917 (GTK_CTREE(mimeview->ctree), mimeview->opened);
918 g_return_if_fail(partinfo != NULL);
920 filename = procmime_get_tmp_file_name(partinfo);
922 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
924 (_("Can't save the part of multipart message."));
926 mimeview_view_file(filename, partinfo, NULL);
931 static void mimeview_open_with(MimeView *mimeview)
937 if (!mimeview->opened) return;
938 if (!mimeview->file) return;
940 partinfo = gtk_ctree_node_get_row_data
941 (GTK_CTREE(mimeview->ctree), mimeview->opened);
942 g_return_if_fail(partinfo != NULL);
944 filename = procmime_get_tmp_file_name(partinfo);
946 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
948 (_("Can't save the part of multipart message."));
953 if (!prefs_common.mime_open_cmd_history)
954 prefs_common.mime_open_cmd_history =
955 add_history(NULL, prefs_common.mime_open_cmd);
957 cmd = input_dialog_combo
959 _("Enter the command line to open file:\n"
960 "(`%s' will be replaced with file name)"),
961 prefs_common.mime_open_cmd,
962 prefs_common.mime_open_cmd_history,
965 mimeview_view_file(filename, partinfo, cmd);
966 g_free(prefs_common.mime_open_cmd);
967 prefs_common.mime_open_cmd = cmd;
968 prefs_common.mime_open_cmd_history =
969 add_history(prefs_common.mime_open_cmd_history, cmd);
975 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
976 const gchar *cmdline)
978 static gchar *default_image_cmdline = "display '%s'";
979 static gchar *default_audio_cmdline = "play '%s'";
980 static gchar *default_html_cmdline =
981 "netscape -remote 'openURL(%s,raise)'";
982 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
986 const gchar *def_cmd;
992 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
994 } else if (MIME_IMAGE == partinfo->mime_type) {
995 cmd = prefs_common.mime_image_viewer;
996 def_cmd = default_image_cmdline;
997 } else if (MIME_AUDIO == partinfo->mime_type) {
998 cmd = prefs_common.mime_audio_player;
999 def_cmd = default_audio_cmdline;
1000 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1001 cmd = prefs_common.uri_cmd;
1002 def_cmd = default_html_cmdline;
1004 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1005 partinfo->content_type, "%s");
1010 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1011 !strchr(p + 2, '%'))
1012 g_snprintf(buf, sizeof(buf), cmd, filename);
1015 g_warning(_("MIME viewer command line is invalid: `%s'"), cmd);
1017 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1022 execute_command_line(buf, TRUE);
1026 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1032 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1033 g_return_if_fail(partinfo != NULL);
1035 part_name = get_part_name(partinfo);
1036 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1039 static void mimeview_update_names(MimeView *mimeview)
1041 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1043 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1046 static void mimeview_update_signature_info(MimeView *mimeview)
1050 if (!mimeview) return;
1051 if (!mimeview->opened) return;
1053 partinfo = gtk_ctree_node_get_row_data
1054 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1055 if (!partinfo) return;
1057 if (g_strcasecmp(partinfo->content_type,
1058 "application/pgp-signature") == 0) {
1059 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1060 textview_show_signature_part(mimeview->textview, partinfo);
1064 static void mimeview_check_signature(MimeView *mimeview)
1067 gchar buf[BUFFSIZE];
1070 g_return_if_fail (mimeview_is_signed(mimeview));
1072 mimeinfo = gtk_ctree_node_get_row_data
1073 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1074 g_return_if_fail(mimeinfo != NULL);
1075 g_return_if_fail(mimeview->file != NULL);
1077 while (mimeinfo->parent)
1078 mimeinfo = mimeinfo->parent;
1080 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1081 FILE_OP_ERROR(mimeview->file, "fopen");
1086 if (mimeinfo->mime_type == MIME_MULTIPART) {
1087 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
1088 FILE_OP_ERROR(mimeview->file, "fseek");
1089 while (fgets(buf, sizeof(buf), fp) != NULL)
1090 if (buf[0] == '\r' || buf[0] == '\n') break;
1093 procmime_scan_multipart_message(mimeinfo, fp);
1094 rfc2015_check_signature(mimeinfo, fp);
1097 mimeview_update_names(mimeview);
1098 mimeview_update_signature_info(mimeview);
1100 #endif /* USE_GPGME */