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;
135 MimeView *mimeview_create(void)
142 GtkWidget *scrolledwin;
144 GtkWidget *mime_vbox;
145 GtkWidget *popupmenu;
146 GtkItemFactory *popupfactory;
147 gchar *titles[N_MIMEVIEW_COLS];
151 debug_print("Creating MIME view...\n");
152 mimeview = g_new0(MimeView, 1);
154 titles[COL_MIMETYPE] = _("MIME Type");
155 titles[COL_SIZE] = _("Size");
156 titles[COL_NAME] = _("Name");
158 notebook = gtk_notebook_new();
159 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
161 vbox = gtk_vbox_new(FALSE, 0);
162 gtk_container_add(GTK_CONTAINER(notebook), vbox);
163 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
166 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
167 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
168 GTK_POLICY_AUTOMATIC,
170 gtk_widget_set_usize(scrolledwin, -1, 80);
172 ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
173 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
174 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
175 gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
177 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
178 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
179 for (i = 0; i < N_MIMEVIEW_COLS; i++)
180 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
182 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
184 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
185 GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
186 gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
187 GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
188 gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
189 GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
190 gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
191 GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
192 gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
193 GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
195 mime_vbox = gtk_vbox_new(FALSE, 0);
197 paned = gtk_vpaned_new();
198 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
199 gtk_paned_add2(GTK_PANED(paned), mime_vbox);
200 gtk_container_add(GTK_CONTAINER(notebook), paned);
201 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
204 gtk_widget_show_all(notebook);
206 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
208 n_entries = sizeof(mimeview_popup_entries) /
209 sizeof(mimeview_popup_entries[0]);
210 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
211 "<MimeView>", &popupfactory, mimeview);
213 mimeview->notebook = notebook;
214 mimeview->vbox = vbox;
215 mimeview->paned = paned;
216 mimeview->scrolledwin = scrolledwin;
217 mimeview->ctree = ctree;
218 mimeview->mime_vbox = mime_vbox;
219 mimeview->popupmenu = popupmenu;
220 mimeview->popupfactory = popupfactory;
223 mimeviews = g_slist_prepend(mimeviews, mimeview);
228 void mimeview_init(MimeView *mimeview)
233 * Check whether the message is OpenPGP signed
236 static gboolean mimeview_is_signed(MimeView *mimeview)
238 MimeInfo *partinfo = NULL;
240 debug_print("mimeview_is signed of %p\n", mimeview);
242 if (!mimeview) return FALSE;
243 if (!mimeview->opened) return FALSE;
245 debug_print("mimeview_is_signed: open\n" );
247 if (!mimeview->file) return FALSE;
249 debug_print("mimeview_is_signed: file\n" );
251 partinfo = gtk_ctree_node_get_row_data
252 (GTK_CTREE(mimeview->ctree), mimeview->opened);
253 g_return_val_if_fail(partinfo != NULL, FALSE);
255 /* walk the tree and see whether there is a signature somewhere */
257 if (rfc2015_has_signature(partinfo))
259 } while ((partinfo = partinfo->parent) != NULL);
261 debug_print("mimeview_is_signed: FALSE\n" );
266 static void set_unchecked_signature(MimeInfo *mimeinfo)
268 MimeInfo *sig_partinfo;
270 sig_partinfo = rfc2015_find_signature(mimeinfo);
271 if (sig_partinfo == NULL) return;
273 g_free(sig_partinfo->sigstatus);
274 sig_partinfo->sigstatus =
275 g_strdup(_("Select \"Check signature\" to check"));
277 g_free(sig_partinfo->sigstatus_full);
278 sig_partinfo->sigstatus_full = NULL;
280 #endif /* USE_GPGME */
282 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
285 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
290 mimeview_clear(mimeview);
291 textview_clear(mimeview->messageview->textview);
293 g_return_if_fail(file != NULL);
294 g_return_if_fail(mimeinfo != NULL);
296 mimeview->mimeinfo = mimeinfo;
298 if ((fp = fopen(file, "rb")) == NULL) {
299 FILE_OP_ERROR(file, "fopen");
302 mimeview->file = g_strdup(file);
304 /* skip MIME part headers */
305 if (mimeinfo->mime_type == MIME_MULTIPART) {
306 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
308 while (fgets(buf, sizeof(buf), fp) != NULL)
309 if (buf[0] == '\r' || buf[0] == '\n') break;
312 procmime_scan_multipart_message(mimeinfo, fp);
314 if ((prefs_common.auto_check_signatures)
316 rfc2015_check_signature(mimeinfo, fp);
318 set_unchecked_signature(mimeinfo);
321 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
324 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
326 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
327 mimeview_selected, mimeview);
329 /* search first text part */
330 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
331 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
334 partinfo = gtk_ctree_node_get_row_data(ctree, node);
336 (partinfo->mime_type == MIME_TEXT ||
337 partinfo->mime_type == MIME_TEXT_HTML))
341 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
344 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
347 gtk_ctree_select(ctree, node);
348 gtkut_ctree_set_focus_row(ctree, node);
349 gtk_widget_grab_focus(mimeview->ctree);
353 void mimeview_destroy(MimeView *mimeview)
357 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
358 MimeViewer *viewer = (MimeViewer *) cur->data;
359 viewer->destroy_viewer(viewer);
361 g_slist_free(mimeview->viewers);
363 procmime_mimeinfo_free_all(mimeview->mimeinfo);
364 g_free(mimeview->file);
367 mimeviews = g_slist_remove(mimeviews, mimeview);
370 static void mimeview_set_multipart_tree(MimeView *mimeview,
372 GtkCTreeNode *parent)
374 GtkCTreeNode *current = parent;
376 g_return_if_fail(mimeinfo != NULL);
378 if (!mimeinfo->sub && mimeinfo->parent)
379 current = mimeview_append_part(mimeview, mimeinfo, parent);
380 if (mimeinfo->sub && !mimeinfo->sub->children &&
381 mimeinfo->sub->mime_type != MIME_TEXT &&
382 mimeinfo->sub->mime_type != MIME_TEXT_HTML) {
383 mimeview_append_part(mimeview, mimeinfo->sub, parent);
388 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
390 if (mimeinfo->children)
391 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
394 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
397 static gchar *get_part_name(MimeInfo *partinfo)
400 if (partinfo->sigstatus)
401 return partinfo->sigstatus;
405 return partinfo->name;
406 else if (partinfo->filename)
407 return partinfo->filename;
408 else if (partinfo->description)
409 return partinfo->description;
414 static gchar *get_part_description(MimeInfo *partinfo)
416 if (partinfo->description)
417 return partinfo->description;
418 else if (partinfo->name)
419 return partinfo->name;
420 else if (partinfo->filename)
421 return partinfo->filename;
426 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
428 GtkCTreeNode *parent)
430 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
432 gchar *str[N_MIMEVIEW_COLS];
435 partinfo->content_type ? partinfo->content_type : "";
436 str[COL_SIZE] = to_human_readable(partinfo->size);
437 if (prefs_common.attach_desc)
438 str[COL_NAME] = get_part_description(partinfo);
440 str[COL_NAME] = get_part_name(partinfo);
442 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
443 NULL, NULL, NULL, NULL,
445 gtk_ctree_node_set_row_data(ctree, node, partinfo);
450 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
458 if (!partinfo) return;
461 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
463 fname = pi ? pi->plaintextfile : mimeview->file;
465 fname = mimeview->file;
466 #endif /* USE_GPGME */
469 if ((fp = fopen(fname, "rb")) == NULL) {
470 FILE_OP_ERROR(fname, "fopen");
474 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
475 FILE_OP_ERROR(mimeview->file, "fseek");
480 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
481 textview_show_part(mimeview->textview, partinfo, fp);
486 static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
490 if (!partinfo) return;
492 filename = procmime_get_tmp_file_name(partinfo);
494 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
496 (_("Can't get the part of multipart message."));
498 mimeview_change_view_type(mimeview, MIMEVIEW_IMAGE);
499 /* Workaround for the GTK+ bug with handling scroll adjustments
501 imageview_clear(mimeview->imageview);
502 imageview_show_image(mimeview->imageview, partinfo, filename,
503 prefs_common.resize_image);
510 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
513 MimeViewerFactory *factory = NULL;
514 MimeViewer *viewer = NULL;
516 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
517 MimeViewerFactory *curfactory = cur->data;
519 if(!fnmatch(curfactory->content_type, content_type, 0)) {
520 factory = curfactory;
527 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
528 MimeViewer *curviewer = cur->data;
530 if (curviewer->factory == factory)
533 viewer = factory->create_viewer();
534 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
539 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
543 viewer = get_viewer_for_content_type(mimeview, partinfo->content_type);
544 if (viewer == NULL) {
545 if (mimeview->mimeviewer != NULL)
546 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
547 mimeview->mimeviewer = NULL;
551 if (mimeview->mimeviewer != viewer) {
552 if (mimeview->mimeviewer != NULL)
553 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
554 mimeview->mimeviewer = viewer;
555 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
557 viewer->show_mimepart(viewer, mimeview->file, partinfo);
562 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
564 TextView *textview = mimeview->textview;
565 ImageView *imageview = mimeview->imageview;
568 if ((mimeview->type != MIMEVIEW_VIEWER) &&
569 (mimeview->type == type)) return;
571 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
573 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
574 GTK_WIDGET(children->data));
575 g_list_free(children);
580 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
581 GTK_WIDGET_PTR(imageview));
584 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
585 GTK_WIDGET_PTR(textview));
587 case MIMEVIEW_VIEWER:
588 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
589 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
595 mimeview->type = type;
598 static void mimeview_clear(MimeView *mimeview)
600 GtkCList *clist = GTK_CLIST(mimeview->ctree);
602 procmime_mimeinfo_free_all(mimeview->mimeinfo);
603 mimeview->mimeinfo = NULL;
605 gtk_clist_clear(clist);
606 textview_clear(mimeview->textview);
607 imageview_clear(mimeview->imageview);
608 if (mimeview->mimeviewer != NULL)
609 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
611 mimeview->opened = NULL;
613 g_free(mimeview->file);
614 mimeview->file = NULL;
616 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
619 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
624 if (mimeview->opened == node) return;
625 mimeview->opened = node;
626 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
628 partinfo = gtk_ctree_node_get_row_data(ctree, node);
629 if (!partinfo) return;
631 /* ungrab the mouse event */
632 if (GTK_WIDGET_HAS_GRAB(ctree)) {
633 gtk_grab_remove(GTK_WIDGET(ctree));
634 if (gdk_pointer_is_grabbed())
635 gdk_pointer_ungrab(GDK_CURRENT_TIME);
638 mimeview->textview->default_text = FALSE;
640 if (!mimeview_show_part(mimeview, partinfo)) {
641 switch (partinfo->mime_type) {
644 case MIME_TEXT_ENRICHED:
645 case MIME_MESSAGE_RFC822:
647 mimeview_show_message_part(mimeview, partinfo);
650 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
652 mimeview->textview->default_text = TRUE;
653 if (prefs_common.display_img)
654 mimeview_show_image_part(mimeview, partinfo);
656 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
657 textview_show_mime_part(mimeview->textview, partinfo);
662 mimeview->textview->default_text = TRUE;
663 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
665 if (g_strcasecmp(partinfo->content_type,
666 "application/pgp-signature") == 0)
667 textview_show_signature_part(mimeview->textview,
671 textview_show_mime_part(mimeview->textview, partinfo);
677 static void mimeview_start_drag(GtkWidget *widget, gint button,
678 GdkEvent *event, MimeView *mimeview)
681 GdkDragContext *context;
685 g_return_if_fail(mimeview != NULL);
686 ctree = GTK_CTREE(mimeview->ctree);
687 partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
688 if (partinfo->filename == NULL && partinfo->name == NULL) return;
690 list = gtk_target_list_new(mimeview_mime_types, 1);
691 context = gtk_drag_begin(widget, list,
692 GDK_ACTION_COPY, button, event);
693 gtk_drag_set_icon_default(context);
696 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
699 GtkCList *clist = GTK_CLIST(widget);
703 if (!event) return FALSE;
705 if (event->button == 2 || event->button == 3) {
706 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
709 gtk_clist_unselect_all(clist);
710 gtk_clist_select_row(clist, row, column);
711 gtkut_clist_set_focus_row(clist, row);
714 if (event->button == 2 ||
715 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
716 /* call external program for image, audio or html */
717 mimeview_launch(mimeview);
718 } else if (event->button == 3) {
719 partinfo = gtk_ctree_node_get_row_data
720 (GTK_CTREE(mimeview->ctree), mimeview->opened);
721 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
722 partinfo->mime_type == MIME_TEXT_HTML ||
723 partinfo->mime_type == MIME_TEXT_ENRICHED ||
724 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
725 partinfo->mime_type == MIME_IMAGE ||
726 partinfo->mime_type == MIME_MULTIPART))
727 menu_set_sensitive(mimeview->popupfactory,
728 "/Display as text", FALSE);
730 menu_set_sensitive(mimeview->popupfactory,
731 "/Display as text", TRUE);
733 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
734 menu_set_sensitive(mimeview->popupfactory,
737 menu_set_sensitive(mimeview->popupfactory,
740 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
741 if (partinfo && (partinfo->mime_type == MIME_IMAGE))
742 menu_set_sensitive(mimeview->popupfactory,
743 "/Display image", TRUE);
746 menu_set_sensitive(mimeview->popupfactory,
748 mimeview_is_signed(mimeview));
751 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
752 NULL, NULL, NULL, NULL,
753 event->button, event->time);
759 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
761 mimeview_key_pressed(mimeview->ctree, event, mimeview);
764 #define BREAK_ON_MODIFIER_KEY() \
765 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
767 #define KEY_PRESS_EVENT_STOP() \
768 if (gtk_signal_n_emissions_by_name \
769 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
770 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
771 "key_press_event"); \
774 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
777 SummaryView *summaryview;
778 GtkCTree *ctree = GTK_CTREE(widget);
781 if (!event) return FALSE;
782 if (!mimeview->opened) return FALSE;
784 switch (event->keyval) {
786 if (textview_scroll_page(mimeview->textview, FALSE))
789 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
791 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
792 gtk_sctree_select(GTK_SCTREE(ctree), node);
797 textview_scroll_page(mimeview->textview, TRUE);
800 textview_scroll_one_line(mimeview->textview,
801 (event->state & GDK_MOD1_MASK) != 0);
805 BREAK_ON_MODIFIER_KEY();
806 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
807 KEY_PRESS_EVENT_STOP();
809 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
810 GTK_SCROLL_STEP_FORWARD, 0.0);
814 BREAK_ON_MODIFIER_KEY();
815 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
816 KEY_PRESS_EVENT_STOP();
818 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
819 GTK_SCROLL_STEP_BACKWARD, 0.0);
822 BREAK_ON_MODIFIER_KEY();
823 KEY_PRESS_EVENT_STOP();
824 mimeview_save_as(mimeview);
827 BREAK_ON_MODIFIER_KEY();
828 KEY_PRESS_EVENT_STOP();
829 mimeview_display_as_text(mimeview);
832 BREAK_ON_MODIFIER_KEY();
833 KEY_PRESS_EVENT_STOP();
834 mimeview_show_image(mimeview);
837 BREAK_ON_MODIFIER_KEY();
838 KEY_PRESS_EVENT_STOP();
839 mimeview_launch(mimeview);
845 if (!mimeview->messageview->mainwin) return FALSE;
846 summaryview = mimeview->messageview->mainwin->summaryview;
847 summary_pass_key_press_event(summaryview, event);
851 static void mimeview_drag_data_get(GtkWidget *widget,
852 GdkDragContext *drag_context,
853 GtkSelectionData *selection_data,
858 gchar *filename, *uriname;
861 if (!mimeview->opened) return;
862 if (!mimeview->file) return;
864 partinfo = gtk_ctree_node_get_row_data
865 (GTK_CTREE(mimeview->ctree), mimeview->opened);
866 if (!partinfo) return;
867 if (!partinfo->filename && !partinfo->name) return;
869 filename = partinfo->filename ? partinfo->filename : partinfo->name;
870 filename = g_basename(filename);
871 if (*filename == '\0') return;
873 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
876 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
878 (_("Can't save the part of multipart message."));
880 uriname = g_strconcat("file:/", filename, NULL);
881 gtk_selection_data_set(selection_data, selection_data->target, 8,
882 uriname, strlen(uriname));
888 static void mimeview_save_all(MimeView *mimeview)
891 gchar *defname = NULL;
893 MimeInfo *attachment;
896 if (!mimeview->opened) return;
897 if (!mimeview->file) return;
899 partinfo = gtk_ctree_node_get_row_data
900 (GTK_CTREE(mimeview->ctree), mimeview->opened);
901 g_return_if_fail(partinfo != NULL);
903 dirname = filesel_select_file(_("Save as"), defname);
904 if (!dirname) return;
906 /* return to first children */
907 if (!partinfo->parent->children) return; /* multipart container? */
908 attachment = partinfo->parent->children->next;
909 /* for each attachment, extract it in the selected dir. */
910 while (attachment != NULL) {
911 static guint subst_cnt = 1;
913 gchar *attachname = g_strdup(get_part_name(attachment));
914 AlertValue aval = G_ALERTDEFAULT;
917 if (!attachname || !strlen(attachname))
918 attachname = g_strdup_printf("noname.%d",subst_cnt++);
919 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
920 g_snprintf(buf, sizeof(buf), "%s%s",
922 (attachname[0] == G_DIR_SEPARATOR)
925 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
926 attachdir = g_dirname(buf);
927 make_dir_hier(attachdir);
930 if (is_file_exist(buf)) {
931 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
933 aval = alertpanel(_("Overwrite"), res, _("OK"),
939 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
940 alertpanel_error(_("Can't save the part of multipart message."));
941 attachment = attachment->next;
945 static void mimeview_display_as_text(MimeView *mimeview)
949 if (!mimeview->opened) return;
951 partinfo = gtk_ctree_node_get_row_data
952 (GTK_CTREE(mimeview->ctree), mimeview->opened);
953 mimeview_show_message_part(mimeview, partinfo);
956 static void mimeview_show_image(MimeView *mimeview)
960 if (!mimeview->opened) return;
962 partinfo = gtk_ctree_node_get_row_data
963 (GTK_CTREE(mimeview->ctree), mimeview->opened);
964 mimeview_show_image_part(mimeview, partinfo);
967 static void mimeview_save_as(MimeView *mimeview)
970 gchar *defname = NULL;
974 if (!mimeview->opened) return;
975 if (!mimeview->file) return;
977 partinfo = gtk_ctree_node_get_row_data
978 (GTK_CTREE(mimeview->ctree), mimeview->opened);
979 g_return_if_fail(partinfo != NULL);
981 if (partinfo->filename)
982 defname = partinfo->filename;
983 else if (partinfo->name) {
984 Xstrdup_a(defname, partinfo->name, return);
985 subst_for_filename(defname);
988 filename = filesel_select_file(_("Save as"), defname);
989 if (!filename) return;
990 if (is_file_exist(filename)) {
992 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
994 aval = alertpanel(_("Overwrite"), res, _("OK"),
997 if (G_ALERTDEFAULT != aval) return;
1000 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
1002 (_("Can't save the part of multipart message."));
1005 static void mimeview_launch(MimeView *mimeview)
1010 if (!mimeview->opened) return;
1011 if (!mimeview->file) return;
1013 partinfo = gtk_ctree_node_get_row_data
1014 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1015 g_return_if_fail(partinfo != NULL);
1017 filename = procmime_get_tmp_file_name(partinfo);
1019 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
1021 (_("Can't save the part of multipart message."));
1023 mimeview_view_file(filename, partinfo, NULL);
1028 static void mimeview_open_with(MimeView *mimeview)
1034 if (!mimeview->opened) return;
1035 if (!mimeview->file) return;
1037 partinfo = gtk_ctree_node_get_row_data
1038 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1039 g_return_if_fail(partinfo != NULL);
1041 filename = procmime_get_tmp_file_name(partinfo);
1043 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1045 (_("Can't save the part of multipart message."));
1050 if (!prefs_common.mime_open_cmd_history)
1051 prefs_common.mime_open_cmd_history =
1052 add_history(NULL, prefs_common.mime_open_cmd);
1054 cmd = input_dialog_combo
1056 _("Enter the command line to open file:\n"
1057 "(`%s' will be replaced with file name)"),
1058 prefs_common.mime_open_cmd,
1059 prefs_common.mime_open_cmd_history,
1062 mimeview_view_file(filename, partinfo, cmd);
1063 g_free(prefs_common.mime_open_cmd);
1064 prefs_common.mime_open_cmd = cmd;
1065 prefs_common.mime_open_cmd_history =
1066 add_history(prefs_common.mime_open_cmd_history, cmd);
1072 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1073 const gchar *cmdline)
1075 static gchar *default_image_cmdline = "display '%s'";
1076 static gchar *default_audio_cmdline = "play '%s'";
1077 static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1078 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1082 const gchar *def_cmd;
1088 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1090 } else if (MIME_IMAGE == partinfo->mime_type) {
1091 cmd = prefs_common.mime_image_viewer;
1092 def_cmd = default_image_cmdline;
1093 } else if (MIME_AUDIO == partinfo->mime_type) {
1094 cmd = prefs_common.mime_audio_player;
1095 def_cmd = default_audio_cmdline;
1096 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1097 cmd = prefs_common.uri_cmd;
1098 def_cmd = default_html_cmdline;
1100 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1101 partinfo->content_type, "%s");
1106 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1107 !strchr(p + 2, '%'))
1108 g_snprintf(buf, sizeof(buf), cmd, filename);
1111 g_warning("MIME viewer command line is invalid: `%s'", cmd);
1113 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1118 execute_command_line(buf, TRUE);
1122 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1128 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1129 g_return_if_fail(partinfo != NULL);
1131 part_name = get_part_name(partinfo);
1132 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1135 static void mimeview_update_names(MimeView *mimeview)
1137 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1139 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1142 static void mimeview_update_signature_info(MimeView *mimeview)
1146 if (!mimeview) return;
1147 if (!mimeview->opened) return;
1149 partinfo = gtk_ctree_node_get_row_data
1150 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1151 if (!partinfo) return;
1153 if (g_strcasecmp(partinfo->content_type,
1154 "application/pgp-signature") == 0) {
1155 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1156 textview_show_signature_part(mimeview->textview, partinfo);
1160 void mimeview_check_signature(MimeView *mimeview)
1165 g_return_if_fail (mimeview_is_signed(mimeview));
1166 g_return_if_fail (gpg_started);
1168 mimeinfo = gtk_ctree_node_get_row_data
1169 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1170 g_return_if_fail(mimeinfo != NULL);
1171 g_return_if_fail(mimeview->file != NULL);
1173 while (mimeinfo->parent)
1174 mimeinfo = mimeinfo->parent;
1176 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1177 FILE_OP_ERROR(mimeview->file, "fopen");
1181 rfc2015_check_signature(mimeinfo, fp);
1184 mimeview_update_names(mimeview);
1185 mimeview_update_signature_info(mimeview);
1187 textview_show_message(mimeview->messageview->textview, mimeinfo,
1190 #endif /* USE_GPGME */
1192 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1194 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1197 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1199 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1202 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1204 GSList *mimeview_list, *viewer_list;
1206 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1207 MimeView *mimeview = (MimeView *) mimeview_list->data;
1209 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1210 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1211 mimeview->mimeviewer = NULL;
1214 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1215 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1217 mimeviewer->destroy_viewer(mimeviewer);
1218 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1222 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);