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>
47 #include "summaryview.h"
50 #include "alertpanel.h"
51 #include "inputdialog.h"
54 #include "prefs_common.h"
65 #define N_MIMEVIEW_COLS 3
67 static void mimeview_set_multipart_tree (MimeView *mimeview,
69 GtkCTreeNode *parent);
70 static GtkCTreeNode *mimeview_append_part (MimeView *mimeview,
72 GtkCTreeNode *parent);
73 static void mimeview_show_message_part (MimeView *mimeview,
75 static void mimeview_show_image_part (MimeView *mimeview,
77 static void mimeview_change_view_type (MimeView *mimeview,
79 static void mimeview_clear (MimeView *mimeview);
81 static void mimeview_selected (GtkCTree *ctree,
85 static void mimeview_start_drag (GtkWidget *widget,
89 static gint mimeview_button_pressed (GtkWidget *widget,
90 GdkEventButton *event,
92 static gint mimeview_key_pressed (GtkWidget *widget,
96 static void mimeview_drag_data_get (GtkWidget *widget,
97 GdkDragContext *drag_context,
98 GtkSelectionData *selection_data,
103 static void mimeview_display_as_text (MimeView *mimeview);
104 static void mimeview_show_image (MimeView *mimeview);
105 static void mimeview_save_as (MimeView *mimeview);
106 static void mimeview_save_all (MimeView *mimeview);
107 static void mimeview_launch (MimeView *mimeview);
108 static void mimeview_open_with (MimeView *mimeview);
109 static void mimeview_view_file (const gchar *filename,
111 const gchar *cmdline);
113 static GtkItemFactoryEntry mimeview_popup_entries[] =
115 {N_("/_Open"), NULL, mimeview_launch, 0, NULL},
116 {N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
117 {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
118 {N_("/_Display image"), NULL, mimeview_show_image, 0, NULL},
119 {N_("/_Save as..."), NULL, mimeview_save_as, 0, NULL},
120 {N_("/Save _all..."), NULL, mimeview_save_all, 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 GSList *mimeviewer_factories;
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)
313 rfc2015_check_signature(mimeinfo, fp);
315 set_unchecked_signature(mimeinfo);
318 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
321 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
323 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
324 mimeview_selected, mimeview);
326 /* search first text part */
327 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
328 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
331 partinfo = gtk_ctree_node_get_row_data(ctree, node);
333 (partinfo->mime_type == MIME_TEXT ||
334 partinfo->mime_type == MIME_TEXT_HTML))
338 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
341 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
344 gtk_ctree_select(ctree, node);
345 gtkut_ctree_set_focus_row(ctree, node);
346 gtk_widget_grab_focus(mimeview->ctree);
350 void mimeview_destroy(MimeView *mimeview)
354 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
355 MimeViewer *viewer = (MimeViewer *) cur->data;
356 viewer->destroy_viewer(viewer);
358 g_slist_free(mimeview->viewers);
360 procmime_mimeinfo_free_all(mimeview->mimeinfo);
361 g_free(mimeview->file);
365 static void mimeview_set_multipart_tree(MimeView *mimeview,
367 GtkCTreeNode *parent)
369 GtkCTreeNode *current = parent;
371 g_return_if_fail(mimeinfo != NULL);
373 if (!mimeinfo->sub && mimeinfo->parent)
374 current = mimeview_append_part(mimeview, mimeinfo, parent);
375 if (mimeinfo->sub && !mimeinfo->sub->children &&
376 mimeinfo->sub->mime_type != MIME_TEXT &&
377 mimeinfo->sub->mime_type != MIME_TEXT_HTML) {
378 mimeview_append_part(mimeview, mimeinfo->sub, parent);
383 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
385 if (mimeinfo->children)
386 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
389 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
392 static gchar *get_part_name(MimeInfo *partinfo)
395 if (partinfo->sigstatus)
396 return partinfo->sigstatus;
400 return partinfo->name;
401 else if (partinfo->filename)
402 return partinfo->filename;
403 else if (partinfo->description)
404 return partinfo->description;
409 static gchar *get_part_description(MimeInfo *partinfo)
411 if (partinfo->description)
412 return partinfo->description;
413 else if (partinfo->name)
414 return partinfo->name;
415 else if (partinfo->filename)
416 return partinfo->filename;
421 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
423 GtkCTreeNode *parent)
425 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
427 gchar *str[N_MIMEVIEW_COLS];
430 partinfo->content_type ? partinfo->content_type : "";
431 str[COL_SIZE] = to_human_readable(partinfo->size);
432 if (prefs_common.attach_desc)
433 str[COL_NAME] = get_part_description(partinfo);
435 str[COL_NAME] = get_part_name(partinfo);
437 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
438 NULL, NULL, NULL, NULL,
440 gtk_ctree_node_set_row_data(ctree, node, partinfo);
445 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
453 if (!partinfo) return;
456 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
458 fname = pi ? pi->plaintextfile : mimeview->file;
460 fname = mimeview->file;
461 #endif /* USE_GPGME */
464 if ((fp = fopen(fname, "rb")) == NULL) {
465 FILE_OP_ERROR(fname, "fopen");
469 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
470 FILE_OP_ERROR(mimeview->file, "fseek");
475 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
476 textview_show_part(mimeview->textview, partinfo, fp);
481 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
485 if (!partinfo) return;
487 filename = procmime_get_tmp_file_name(partinfo);
489 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
491 (_("Can't get the part of multipart message."));
493 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
494 /* Workaround for the GTK+ bug with handling scroll adjustments
496 imageview_clear(mimeview->imageview);
497 imageview_show_image(mimeview->imageview, partinfo, filename,
498 prefs_common.resize_image);
505 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
508 MimeViewerFactory *factory = NULL;
509 MimeViewer *viewer = NULL;
511 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
512 MimeViewerFactory *curfactory = cur->data;
514 if(!fnmatch(curfactory->content_type, content_type, 0)) {
515 factory = curfactory;
522 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
523 MimeViewer *curviewer = cur->data;
525 if (curviewer->factory == factory)
528 viewer = factory->create_viewer();
529 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
534 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
538 viewer = get_viewer_for_content_type(mimeview, partinfo->content_type);
539 if (viewer == NULL) {
540 if (mimeview->mimeviewer != NULL)
541 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
542 mimeview->mimeviewer = NULL;
546 if (mimeview->mimeviewer != viewer) {
547 if (mimeview->mimeviewer != NULL)
548 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
549 mimeview->mimeviewer = viewer;
550 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
552 viewer->show_mimepart(viewer, mimeview->file, partinfo);
557 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
559 TextView *textview = mimeview->textview;
560 ImageView *imageview = mimeview->imageview;
563 if ((mimeview->type != MIMEVIEW_VIEWER) &&
564 (mimeview->type == type)) return;
566 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
568 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
569 GTK_WIDGET(children->data));
570 g_list_free(children);
575 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
576 GTK_WIDGET_PTR(imageview));
579 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
580 GTK_WIDGET_PTR(textview));
582 case MIMEVIEW_VIEWER:
583 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
584 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
590 mimeview->type = type;
593 static void mimeview_clear(MimeView *mimeview)
595 GtkCList *clist = GTK_CLIST(mimeview->ctree);
597 procmime_mimeinfo_free_all(mimeview->mimeinfo);
598 mimeview->mimeinfo = NULL;
600 gtk_clist_clear(clist);
601 textview_clear(mimeview->textview);
602 imageview_clear(mimeview->imageview);
603 if (mimeview->mimeviewer != NULL)
604 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
606 mimeview->opened = NULL;
608 g_free(mimeview->file);
609 mimeview->file = NULL;
611 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
614 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
619 if (mimeview->opened == node) return;
620 mimeview->opened = node;
621 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
623 partinfo = gtk_ctree_node_get_row_data(ctree, node);
624 if (!partinfo) return;
626 /* ungrab the mouse event */
627 if (GTK_WIDGET_HAS_GRAB(ctree)) {
628 gtk_grab_remove(GTK_WIDGET(ctree));
629 if (gdk_pointer_is_grabbed())
630 gdk_pointer_ungrab(GDK_CURRENT_TIME);
633 mimeview->textview->default_text = FALSE;
635 if (!mimeview_show_part(mimeview, partinfo)) {
636 switch (partinfo->mime_type) {
639 case MIME_TEXT_ENRICHED:
640 case MIME_MESSAGE_RFC822:
642 mimeview_show_message_part(mimeview, partinfo);
645 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
647 mimeview->textview->default_text = TRUE;
648 if (prefs_common.display_img)
649 mimeview_show_image_part(mimeview, partinfo);
651 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
652 textview_show_mime_part(mimeview->textview, partinfo);
657 mimeview->textview->default_text = TRUE;
658 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
660 if (g_strcasecmp(partinfo->content_type,
661 "application/pgp-signature") == 0)
662 textview_show_signature_part(mimeview->textview,
666 textview_show_mime_part(mimeview->textview, partinfo);
672 static void mimeview_start_drag(GtkWidget *widget, gint button,
673 GdkEvent *event, MimeView *mimeview)
676 GdkDragContext *context;
680 g_return_if_fail(mimeview != NULL);
681 ctree = GTK_CTREE(mimeview->ctree);
682 partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
683 if (partinfo->filename == NULL && partinfo->name == NULL) return;
685 list = gtk_target_list_new(mimeview_mime_types, 1);
686 context = gtk_drag_begin(widget, list,
687 GDK_ACTION_COPY, button, event);
688 gtk_drag_set_icon_default(context);
691 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
694 GtkCList *clist = GTK_CLIST(widget);
698 if (!event) return FALSE;
700 if (event->button == 2 || event->button == 3) {
701 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
704 gtk_clist_unselect_all(clist);
705 gtk_clist_select_row(clist, row, column);
706 gtkut_clist_set_focus_row(clist, row);
709 if (event->button == 2 ||
710 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
711 /* call external program for image, audio or html */
712 mimeview_launch(mimeview);
713 } else if (event->button == 3) {
714 partinfo = gtk_ctree_node_get_row_data
715 (GTK_CTREE(mimeview->ctree), mimeview->opened);
716 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
717 partinfo->mime_type == MIME_TEXT_HTML ||
718 partinfo->mime_type == MIME_TEXT_ENRICHED ||
719 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
720 partinfo->mime_type == MIME_IMAGE ||
721 partinfo->mime_type == MIME_MULTIPART))
722 menu_set_sensitive(mimeview->popupfactory,
723 "/Display as text", FALSE);
725 menu_set_sensitive(mimeview->popupfactory,
726 "/Display as text", TRUE);
728 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
729 menu_set_sensitive(mimeview->popupfactory,
732 menu_set_sensitive(mimeview->popupfactory,
735 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
736 if (partinfo && (partinfo->mime_type == MIME_IMAGE))
737 menu_set_sensitive(mimeview->popupfactory,
738 "/Display image", TRUE);
741 menu_set_sensitive(mimeview->popupfactory,
743 mimeview_is_signed(mimeview));
746 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
747 NULL, NULL, NULL, NULL,
748 event->button, event->time);
754 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
756 mimeview_key_pressed(mimeview->ctree, event, mimeview);
759 #define BREAK_ON_MODIFIER_KEY() \
760 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
762 #define KEY_PRESS_EVENT_STOP() \
763 if (gtk_signal_n_emissions_by_name \
764 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
765 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
766 "key_press_event"); \
769 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
772 SummaryView *summaryview;
773 GtkCTree *ctree = GTK_CTREE(widget);
776 if (!event) return FALSE;
777 if (!mimeview->opened) return FALSE;
779 switch (event->keyval) {
781 if (textview_scroll_page(mimeview->textview, FALSE))
784 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
786 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
787 gtk_sctree_select(GTK_SCTREE(ctree), node);
792 textview_scroll_page(mimeview->textview, TRUE);
795 textview_scroll_one_line(mimeview->textview,
796 (event->state & GDK_MOD1_MASK) != 0);
800 BREAK_ON_MODIFIER_KEY();
801 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
802 KEY_PRESS_EVENT_STOP();
804 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
805 GTK_SCROLL_STEP_FORWARD, 0.0);
809 BREAK_ON_MODIFIER_KEY();
810 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
811 KEY_PRESS_EVENT_STOP();
813 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
814 GTK_SCROLL_STEP_BACKWARD, 0.0);
817 BREAK_ON_MODIFIER_KEY();
818 KEY_PRESS_EVENT_STOP();
819 mimeview_save_as(mimeview);
822 BREAK_ON_MODIFIER_KEY();
823 KEY_PRESS_EVENT_STOP();
824 mimeview_display_as_text(mimeview);
827 BREAK_ON_MODIFIER_KEY();
828 KEY_PRESS_EVENT_STOP();
829 mimeview_show_image(mimeview);
832 BREAK_ON_MODIFIER_KEY();
833 KEY_PRESS_EVENT_STOP();
834 mimeview_launch(mimeview);
840 if (!mimeview->messageview->mainwin) return FALSE;
841 summaryview = mimeview->messageview->mainwin->summaryview;
842 summary_pass_key_press_event(summaryview, event);
846 static void mimeview_drag_data_get(GtkWidget *widget,
847 GdkDragContext *drag_context,
848 GtkSelectionData *selection_data,
853 gchar *filename, *uriname;
856 if (!mimeview->opened) return;
857 if (!mimeview->file) return;
859 partinfo = gtk_ctree_node_get_row_data
860 (GTK_CTREE(mimeview->ctree), mimeview->opened);
861 if (!partinfo) return;
862 if (!partinfo->filename && !partinfo->name) return;
864 filename = partinfo->filename ? partinfo->filename : partinfo->name;
865 filename = g_basename(filename);
866 if (*filename == '\0') return;
868 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
871 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
873 (_("Can't save the part of multipart message."));
875 uriname = g_strconcat("file:/", filename, NULL);
876 gtk_selection_data_set(selection_data, selection_data->target, 8,
877 uriname, strlen(uriname));
883 static void mimeview_save_all(MimeView *mimeview)
886 gchar *defname = NULL;
888 MimeInfo *attachment;
891 if (!mimeview->opened) return;
892 if (!mimeview->file) return;
894 partinfo = gtk_ctree_node_get_row_data
895 (GTK_CTREE(mimeview->ctree), mimeview->opened);
896 g_return_if_fail(partinfo != NULL);
898 dirname = filesel_select_file(_("Save as"), defname);
899 if (!dirname) return;
901 /* return to first children */
902 if (!partinfo->parent->children) return; /* multipart container? */
903 attachment = partinfo->parent->children->next;
904 /* for each attachment, extract it in the selected dir. */
905 while (attachment != NULL) {
906 static guint subst_cnt = 1;
908 gchar *attachname = g_strdup(get_part_name(attachment));
909 AlertValue aval = G_ALERTDEFAULT;
912 if (!attachname || !strlen(attachname))
913 attachname = g_strdup_printf("noname.%d",subst_cnt++);
914 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
915 g_snprintf(buf, sizeof(buf), "%s%s",
917 (attachname[0] == G_DIR_SEPARATOR)
920 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
921 attachdir = g_dirname(buf);
922 make_dir_hier(attachdir);
925 if (is_file_exist(buf)) {
926 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
928 aval = alertpanel(_("Overwrite"), res, _("OK"),
934 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
935 alertpanel_error(_("Can't save the part of multipart message."));
936 attachment = attachment->next;
940 static void mimeview_display_as_text(MimeView *mimeview)
944 if (!mimeview->opened) return;
946 partinfo = gtk_ctree_node_get_row_data
947 (GTK_CTREE(mimeview->ctree), mimeview->opened);
948 mimeview_show_message_part(mimeview, partinfo);
951 static void mimeview_show_image(MimeView *mimeview)
955 if (!mimeview->opened) return;
957 partinfo = gtk_ctree_node_get_row_data
958 (GTK_CTREE(mimeview->ctree), mimeview->opened);
959 mimeview_show_image_part(mimeview, partinfo);
962 static void mimeview_save_as(MimeView *mimeview)
965 gchar *defname = NULL;
969 if (!mimeview->opened) return;
970 if (!mimeview->file) return;
972 partinfo = gtk_ctree_node_get_row_data
973 (GTK_CTREE(mimeview->ctree), mimeview->opened);
974 g_return_if_fail(partinfo != NULL);
976 if (partinfo->filename)
977 defname = partinfo->filename;
978 else if (partinfo->name) {
979 Xstrdup_a(defname, partinfo->name, return);
980 subst_for_filename(defname);
983 filename = filesel_select_file(_("Save as"), defname);
984 if (!filename) return;
985 if (is_file_exist(filename)) {
987 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
989 aval = alertpanel(_("Overwrite"), res, _("OK"),
992 if (G_ALERTDEFAULT != aval) return;
995 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
997 (_("Can't save the part of multipart message."));
1000 static void mimeview_launch(MimeView *mimeview)
1005 if (!mimeview->opened) return;
1006 if (!mimeview->file) return;
1008 partinfo = gtk_ctree_node_get_row_data
1009 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1010 g_return_if_fail(partinfo != NULL);
1012 filename = procmime_get_tmp_file_name(partinfo);
1014 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
1016 (_("Can't save the part of multipart message."));
1018 mimeview_view_file(filename, partinfo, NULL);
1023 static void mimeview_open_with(MimeView *mimeview)
1029 if (!mimeview->opened) return;
1030 if (!mimeview->file) return;
1032 partinfo = gtk_ctree_node_get_row_data
1033 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1034 g_return_if_fail(partinfo != NULL);
1036 filename = procmime_get_tmp_file_name(partinfo);
1038 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1040 (_("Can't save the part of multipart message."));
1045 if (!prefs_common.mime_open_cmd_history)
1046 prefs_common.mime_open_cmd_history =
1047 add_history(NULL, prefs_common.mime_open_cmd);
1049 cmd = input_dialog_combo
1051 _("Enter the command line to open file:\n"
1052 "(`%s' will be replaced with file name)"),
1053 prefs_common.mime_open_cmd,
1054 prefs_common.mime_open_cmd_history,
1057 mimeview_view_file(filename, partinfo, cmd);
1058 g_free(prefs_common.mime_open_cmd);
1059 prefs_common.mime_open_cmd = cmd;
1060 prefs_common.mime_open_cmd_history =
1061 add_history(prefs_common.mime_open_cmd_history, cmd);
1067 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1068 const gchar *cmdline)
1070 static gchar *default_image_cmdline = "display '%s'";
1071 static gchar *default_audio_cmdline = "play '%s'";
1072 static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1073 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1077 const gchar *def_cmd;
1083 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1085 } else if (MIME_IMAGE == partinfo->mime_type) {
1086 cmd = prefs_common.mime_image_viewer;
1087 def_cmd = default_image_cmdline;
1088 } else if (MIME_AUDIO == partinfo->mime_type) {
1089 cmd = prefs_common.mime_audio_player;
1090 def_cmd = default_audio_cmdline;
1091 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1092 cmd = prefs_common.uri_cmd;
1093 def_cmd = default_html_cmdline;
1095 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1096 partinfo->content_type, "%s");
1101 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1102 !strchr(p + 2, '%'))
1103 g_snprintf(buf, sizeof(buf), cmd, filename);
1106 g_warning("MIME viewer command line is invalid: `%s'", cmd);
1108 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1113 execute_command_line(buf, TRUE);
1117 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1123 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1124 g_return_if_fail(partinfo != NULL);
1126 part_name = get_part_name(partinfo);
1127 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1130 static void mimeview_update_names(MimeView *mimeview)
1132 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1134 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1137 static void mimeview_update_signature_info(MimeView *mimeview)
1141 if (!mimeview) return;
1142 if (!mimeview->opened) return;
1144 partinfo = gtk_ctree_node_get_row_data
1145 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1146 if (!partinfo) return;
1148 if (g_strcasecmp(partinfo->content_type,
1149 "application/pgp-signature") == 0) {
1150 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1151 textview_show_signature_part(mimeview->textview, partinfo);
1155 void mimeview_check_signature(MimeView *mimeview)
1160 g_return_if_fail (mimeview_is_signed(mimeview));
1161 g_return_if_fail (gpg_started);
1163 mimeinfo = gtk_ctree_node_get_row_data
1164 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1165 g_return_if_fail(mimeinfo != NULL);
1166 g_return_if_fail(mimeview->file != NULL);
1168 while (mimeinfo->parent)
1169 mimeinfo = mimeinfo->parent;
1171 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1172 FILE_OP_ERROR(mimeview->file, "fopen");
1176 rfc2015_check_signature(mimeinfo, fp);
1179 mimeview_update_names(mimeview);
1180 mimeview_update_signature_info(mimeview);
1182 textview_show_message(mimeview->messageview->textview, mimeinfo,
1185 #endif /* USE_GPGME */
1187 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1189 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1192 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1194 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);