2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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>
40 #ifndef HAVE_APACHE_FNMATCH
41 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
51 #include "summaryview.h"
54 #include "alertpanel.h"
55 #include "inputdialog.h"
58 #include "prefs_common.h"
60 #include "stock_pixmap.h"
69 #define N_MIMEVIEW_COLS 3
71 static void mimeview_set_multipart_tree (MimeView *mimeview,
73 GtkCTreeNode *parent);
74 static GtkCTreeNode *mimeview_append_part (MimeView *mimeview,
76 GtkCTreeNode *parent);
77 static void mimeview_show_message_part (MimeView *mimeview,
79 static void mimeview_change_view_type (MimeView *mimeview,
81 static void mimeview_clear (MimeView *mimeview);
83 static void mimeview_selected (GtkCTree *ctree,
87 static void mimeview_start_drag (GtkWidget *widget,
91 static gint mimeview_button_pressed (GtkWidget *widget,
92 GdkEventButton *event,
94 static gint mimeview_key_pressed (GtkWidget *widget,
98 static void mimeview_drag_data_get (GtkWidget *widget,
99 GdkDragContext *drag_context,
100 GtkSelectionData *selection_data,
105 static void mimeview_display_as_text (MimeView *mimeview);
106 static void mimeview_save_as (MimeView *mimeview);
107 static void mimeview_save_all (MimeView *mimeview);
108 static void mimeview_launch (MimeView *mimeview);
109 static void mimeview_open_with (MimeView *mimeview);
110 static void mimeview_view_file (const gchar *filename,
112 const gchar *cmdline);
114 static GtkItemFactoryEntry mimeview_popup_entries[] =
116 {N_("/_Open"), NULL, mimeview_launch, 0, NULL},
117 {N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
118 {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
119 {N_("/_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(MainWindow *mainwin)
142 GtkWidget *scrolledwin;
144 GtkWidget *mime_notebook;
145 GtkWidget *popupmenu;
147 GtkItemFactory *popupfactory;
148 gchar *titles[N_MIMEVIEW_COLS];
152 debug_print("Creating MIME view...\n");
153 mimeview = g_new0(MimeView, 1);
155 titles[COL_MIMETYPE] = _("MIME Type");
156 titles[COL_SIZE] = _("Size");
157 titles[COL_NAME] = _("Name");
159 notebook = gtk_notebook_new();
160 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
162 vbox = gtk_vbox_new(FALSE, 0);
163 gtk_container_add(GTK_CONTAINER(notebook), vbox);
164 icon = stock_pixmap_widget(mainwin->window, STOCK_PIXMAP_MESSAGEVIEW_TEXT);
165 gtk_widget_show(icon);
166 gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), vbox, icon);
168 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
169 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
170 GTK_POLICY_AUTOMATIC,
172 gtk_widget_set_usize(scrolledwin, -1, 80);
174 ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
175 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
176 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
177 gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
179 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
180 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
181 for (i = 0; i < N_MIMEVIEW_COLS; i++)
182 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
184 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
186 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
187 GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
188 gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
189 GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
190 gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
191 GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
192 gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
193 GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
194 gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
195 GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
197 mime_notebook = gtk_notebook_new();
198 gtk_widget_show(mime_notebook);
199 GTK_WIDGET_UNSET_FLAGS(mime_notebook, GTK_CAN_FOCUS);
200 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
201 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
203 paned = gtk_vpaned_new();
204 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
205 gtk_paned_add2(GTK_PANED(paned), mime_notebook);
206 gtk_container_add(GTK_CONTAINER(notebook), paned);
207 icon = stock_pixmap_widget(mainwin->window, STOCK_PIXMAP_CLIP);
208 gtk_widget_show(icon);
209 gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), paned, icon);
210 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_RIGHT);
212 gtk_widget_show_all(notebook);
214 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
216 n_entries = sizeof(mimeview_popup_entries) /
217 sizeof(mimeview_popup_entries[0]);
218 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
219 "<MimeView>", &popupfactory, mimeview);
221 mimeview->notebook = notebook;
222 mimeview->vbox = vbox;
223 mimeview->paned = paned;
224 mimeview->scrolledwin = scrolledwin;
225 mimeview->ctree = ctree;
226 mimeview->mime_notebook = mime_notebook;
227 mimeview->popupmenu = popupmenu;
228 mimeview->popupfactory = popupfactory;
231 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
233 mimeviews = g_slist_prepend(mimeviews, mimeview);
238 void mimeview_init(MimeView *mimeview)
240 textview_init(mimeview->textview);
242 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
243 GTK_WIDGET_PTR(mimeview->textview));
247 * Check whether the message is OpenPGP signed
250 static gboolean mimeview_is_signed(MimeView *mimeview)
252 MimeInfo *partinfo = NULL;
254 debug_print("mimeview_is signed of %p\n", mimeview);
256 if (!mimeview) return FALSE;
257 if (!mimeview->opened) return FALSE;
259 debug_print("mimeview_is_signed: open\n" );
261 if (!mimeview->file) return FALSE;
263 debug_print("mimeview_is_signed: file\n" );
265 partinfo = gtk_ctree_node_get_row_data
266 (GTK_CTREE(mimeview->ctree), mimeview->opened);
267 g_return_val_if_fail(partinfo != NULL, FALSE);
269 /* walk the tree and see whether there is a signature somewhere */
271 if (rfc2015_has_signature(partinfo))
273 } while ((partinfo = partinfo->parent) != NULL);
275 debug_print("mimeview_is_signed: FALSE\n" );
280 static void set_unchecked_signature(MimeInfo *mimeinfo)
282 MimeInfo *sig_partinfo;
284 sig_partinfo = rfc2015_find_signature(mimeinfo);
285 if (sig_partinfo == NULL) return;
287 g_free(sig_partinfo->sigstatus);
288 sig_partinfo->sigstatus =
289 g_strdup(_("Right-click here to verify the signature"));
291 g_free(sig_partinfo->sigstatus_full);
292 sig_partinfo->sigstatus_full = NULL;
294 #endif /* USE_GPGME */
296 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
299 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
303 mimeview_clear(mimeview);
304 textview_clear(mimeview->messageview->textview);
306 g_return_if_fail(file != NULL);
307 g_return_if_fail(mimeinfo != NULL);
309 mimeview->mimeinfo = mimeinfo;
311 mimeview->file = g_strdup(file);
314 if (prefs_common.auto_check_signatures && gpg_started) {
315 if ((fp = fopen(file, "rb")) == NULL) {
316 FILE_OP_ERROR(file, "fopen");
319 rfc2015_check_signature(mimeinfo, fp);
322 set_unchecked_signature(mimeinfo);
325 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
328 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
330 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
331 mimeview_selected, mimeview);
333 /* search first text part */
334 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
335 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
338 partinfo = gtk_ctree_node_get_row_data(ctree, node);
340 (partinfo->mime_type == MIME_TEXT ||
341 partinfo->mime_type == MIME_TEXT_HTML))
344 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
347 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
350 gtk_ctree_select(ctree, node);
351 gtkut_ctree_set_focus_row(ctree, node);
352 gtk_widget_grab_focus(mimeview->ctree);
356 void mimeview_destroy(MimeView *mimeview)
360 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
361 MimeViewer *viewer = (MimeViewer *) cur->data;
362 gtk_container_remove(GTK_CONTAINER(mimeview->mime_notebook),
363 GTK_WIDGET(viewer->get_widget(viewer)));
364 viewer->destroy_viewer(viewer);
366 g_slist_free(mimeview->viewers);
367 gtk_target_list_unref(mimeview->target_list);
369 procmime_mimeinfo_free_all(mimeview->mimeinfo);
370 g_free(mimeview->file);
373 mimeviews = g_slist_remove(mimeviews, mimeview);
376 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
378 if (gtk_notebook_get_current_page
379 (GTK_NOTEBOOK(mimeview->notebook)) == 0)
382 return gtk_ctree_node_get_row_data
383 (GTK_CTREE(mimeview->ctree), mimeview->opened);
386 static void mimeview_set_multipart_tree(MimeView *mimeview,
388 GtkCTreeNode *parent)
392 g_return_if_fail(mimeinfo != NULL);
394 if (mimeinfo->children)
395 mimeinfo = mimeinfo->children;
397 while (mimeinfo != NULL) {
398 node = mimeview_append_part(mimeview, mimeinfo, parent);
400 if (mimeinfo->children)
401 mimeview_set_multipart_tree(mimeview, mimeinfo, node);
402 else if (mimeinfo->sub &&
403 mimeinfo->sub->mime_type != MIME_TEXT &&
404 mimeinfo->sub->mime_type != MIME_TEXT_HTML)
405 mimeview_set_multipart_tree(mimeview, mimeinfo->sub,
407 mimeinfo = mimeinfo->next;
411 static gchar *get_part_name(MimeInfo *partinfo)
414 if (partinfo->sigstatus)
415 return partinfo->sigstatus;
419 return partinfo->name;
420 else if (partinfo->filename)
421 return partinfo->filename;
422 else if (partinfo->description)
423 return partinfo->description;
428 static gchar *get_part_description(MimeInfo *partinfo)
430 if (partinfo->description)
431 return partinfo->description;
432 else if (partinfo->name)
433 return partinfo->name;
434 else if (partinfo->filename)
435 return partinfo->filename;
440 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
442 GtkCTreeNode *parent)
444 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
446 gchar *str[N_MIMEVIEW_COLS];
449 partinfo->content_type ? partinfo->content_type : "";
450 str[COL_SIZE] = to_human_readable(partinfo->size);
451 if (prefs_common.attach_desc)
452 str[COL_NAME] = get_part_description(partinfo);
454 str[COL_NAME] = get_part_name(partinfo);
456 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
457 NULL, NULL, NULL, NULL,
459 gtk_ctree_node_set_row_data(ctree, node, partinfo);
464 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
472 if (!partinfo) return;
475 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
477 fname = pi ? pi->plaintextfile : mimeview->file;
479 fname = mimeview->file;
480 #endif /* USE_GPGME */
483 if ((fp = fopen(fname, "rb")) == NULL) {
484 FILE_OP_ERROR(fname, "fopen");
488 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
489 FILE_OP_ERROR(mimeview->file, "fseek");
494 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
495 textview_show_part(mimeview->textview, partinfo, fp);
500 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
503 MimeViewerFactory *factory = NULL;
504 MimeViewer *viewer = NULL;
507 * FNM_CASEFOLD is a GNU extension
508 * if its not defined copy the string to the stack and
509 * convert the copy to lower case
512 #define FNM_CASEFOLD 0
513 Xstrdup_a(content_type, content_type, return NULL);
514 g_strdown((gchar *)content_type);
517 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
518 MimeViewerFactory *curfactory = cur->data;
521 while (curfactory->content_types[i] != NULL) {
522 debug_print("%s\n", curfactory->content_types[i]);
523 if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
524 factory = curfactory;
535 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
536 MimeViewer *curviewer = cur->data;
538 if (curviewer->factory == factory)
541 viewer = factory->create_viewer();
542 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
543 GTK_WIDGET(viewer->get_widget(viewer)));
545 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
550 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
552 gchar *content_type = NULL;
553 MimeViewer *viewer = NULL;
555 if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
556 (partinfo->name != NULL)) {
557 content_type = procmime_get_mime_type(partinfo->name);
559 content_type = g_strdup(partinfo->content_type);
562 if (content_type != NULL) {
563 viewer = get_viewer_for_content_type(mimeview, content_type);
564 g_free(content_type);
570 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
574 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
575 if (viewer == NULL) {
576 if (mimeview->mimeviewer != NULL)
577 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
578 mimeview->mimeviewer = NULL;
582 if (mimeview->mimeviewer != viewer) {
583 if (mimeview->mimeviewer != NULL)
584 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
585 mimeview->mimeviewer = viewer;
586 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
588 viewer->show_mimepart(viewer, mimeview->file, partinfo);
593 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
595 TextView *textview = mimeview->textview;
597 if ((mimeview->type != MIMEVIEW_VIEWER) &&
598 (mimeview->type == type)) return;
602 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
603 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
604 GTK_WIDGET_PTR(textview)));
606 case MIMEVIEW_VIEWER:
607 gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
608 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
609 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
615 mimeview->type = type;
618 static void mimeview_clear(MimeView *mimeview)
620 GtkCList *clist = GTK_CLIST(mimeview->ctree);
622 procmime_mimeinfo_free_all(mimeview->mimeinfo);
623 mimeview->mimeinfo = NULL;
625 gtk_clist_clear(clist);
626 textview_clear(mimeview->textview);
627 if (mimeview->mimeviewer != NULL)
628 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
630 mimeview->opened = NULL;
632 g_free(mimeview->file);
633 mimeview->file = NULL;
635 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
638 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
643 if (mimeview->opened == node) return;
644 mimeview->opened = node;
645 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
647 partinfo = gtk_ctree_node_get_row_data(ctree, node);
648 if (!partinfo) return;
650 /* ungrab the mouse event */
651 if (GTK_WIDGET_HAS_GRAB(ctree)) {
652 gtk_grab_remove(GTK_WIDGET(ctree));
653 if (gdk_pointer_is_grabbed())
654 gdk_pointer_ungrab(GDK_CURRENT_TIME);
657 mimeview->textview->default_text = FALSE;
659 if (!mimeview_show_part(mimeview, partinfo)) {
660 switch (partinfo->mime_type) {
663 case MIME_TEXT_ENRICHED:
664 case MIME_MESSAGE_RFC822:
666 mimeview_show_message_part(mimeview, partinfo);
670 mimeview->textview->default_text = TRUE;
671 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
673 if (g_strcasecmp(partinfo->content_type,
674 "application/pgp-signature") == 0)
675 textview_show_signature_part(mimeview->textview,
679 textview_show_mime_part(mimeview->textview, partinfo);
685 static void mimeview_start_drag(GtkWidget *widget, gint button,
686 GdkEvent *event, MimeView *mimeview)
688 GdkDragContext *context;
691 g_return_if_fail(mimeview != NULL);
693 partinfo = mimeview_get_selected_part(mimeview);
694 if (partinfo->filename == NULL && partinfo->name == NULL) return;
696 context = gtk_drag_begin(widget, mimeview->target_list,
697 GDK_ACTION_COPY, button, event);
698 gtk_drag_set_icon_default(context);
701 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
704 GtkCList *clist = GTK_CLIST(widget);
708 if (!event) return FALSE;
710 if (event->button == 2 || event->button == 3) {
711 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
714 gtk_clist_unselect_all(clist);
715 gtk_clist_select_row(clist, row, column);
716 gtkut_clist_set_focus_row(clist, row);
719 if (event->button == 2 ||
720 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
721 /* call external program for image, audio or html */
722 mimeview_launch(mimeview);
723 } else if (event->button == 3) {
724 partinfo = mimeview_get_selected_part(mimeview);
725 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
726 partinfo->mime_type == MIME_TEXT_HTML ||
727 partinfo->mime_type == MIME_TEXT_ENRICHED ||
728 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
729 partinfo->mime_type == MIME_IMAGE ||
730 partinfo->mime_type == MIME_MULTIPART))
731 menu_set_sensitive(mimeview->popupfactory,
732 "/Display as text", FALSE);
734 menu_set_sensitive(mimeview->popupfactory,
735 "/Display as text", TRUE);
737 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
738 menu_set_sensitive(mimeview->popupfactory,
741 menu_set_sensitive(mimeview->popupfactory,
745 menu_set_sensitive(mimeview->popupfactory,
747 mimeview_is_signed(mimeview));
750 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
751 NULL, NULL, NULL, NULL,
752 event->button, event->time);
758 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
760 mimeview_key_pressed(mimeview->ctree, event, mimeview);
763 #define BREAK_ON_MODIFIER_KEY() \
764 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
766 #define KEY_PRESS_EVENT_STOP() \
767 if (gtk_signal_n_emissions_by_name \
768 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
769 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
770 "key_press_event"); \
773 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
776 SummaryView *summaryview;
777 GtkCTree *ctree = GTK_CTREE(widget);
780 if (!event) return FALSE;
781 if (!mimeview->opened) return FALSE;
783 switch (event->keyval) {
785 if (textview_scroll_page(mimeview->textview, FALSE))
788 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
790 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
791 gtk_sctree_select(GTK_SCTREE(ctree), node);
796 textview_scroll_page(mimeview->textview, TRUE);
799 textview_scroll_one_line(mimeview->textview,
800 (event->state & GDK_MOD1_MASK) != 0);
804 BREAK_ON_MODIFIER_KEY();
805 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
806 KEY_PRESS_EVENT_STOP();
808 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
809 GTK_SCROLL_STEP_FORWARD, 0.0);
813 BREAK_ON_MODIFIER_KEY();
814 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
815 KEY_PRESS_EVENT_STOP();
817 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
818 GTK_SCROLL_STEP_BACKWARD, 0.0);
821 BREAK_ON_MODIFIER_KEY();
822 KEY_PRESS_EVENT_STOP();
823 mimeview_save_as(mimeview);
826 BREAK_ON_MODIFIER_KEY();
827 KEY_PRESS_EVENT_STOP();
828 mimeview_display_as_text(mimeview);
831 BREAK_ON_MODIFIER_KEY();
832 KEY_PRESS_EVENT_STOP();
833 mimeview_launch(mimeview);
839 if (!mimeview->messageview->mainwin) return FALSE;
840 summaryview = mimeview->messageview->mainwin->summaryview;
841 summary_pass_key_press_event(summaryview, event);
845 static void mimeview_drag_data_get(GtkWidget *widget,
846 GdkDragContext *drag_context,
847 GtkSelectionData *selection_data,
852 gchar *filename, *uriname;
855 if (!mimeview->opened) return;
856 if (!mimeview->file) return;
858 partinfo = mimeview_get_selected_part(mimeview);
859 if (!partinfo) return;
860 if (!partinfo->filename && !partinfo->name) return;
862 filename = partinfo->filename ? partinfo->filename : partinfo->name;
863 filename = g_basename(filename);
864 if (*filename == '\0') return;
866 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
869 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
871 (_("Can't save the part of multipart message."));
873 uriname = g_strconcat("file:/", filename, NULL);
874 gtk_selection_data_set(selection_data, selection_data->target, 8,
875 uriname, strlen(uriname));
881 static void mimeview_save_all(MimeView *mimeview)
884 gchar *defname = NULL;
886 MimeInfo *attachment;
889 if (!mimeview->opened) return;
890 if (!mimeview->file) return;
892 partinfo = mimeview_get_selected_part(mimeview);
893 g_return_if_fail(partinfo != NULL);
895 dirname = filesel_select_file(_("Save as"), defname);
896 if (!dirname) return;
898 /* return to first children */
899 if (!partinfo->parent->children) return; /* multipart container? */
900 attachment = partinfo->parent->children->next;
901 /* for each attachment, extract it in the selected dir. */
902 while (attachment != NULL) {
903 static guint subst_cnt = 1;
905 gchar *attachname = g_strdup(get_part_name(attachment));
906 AlertValue aval = G_ALERTDEFAULT;
909 if (!attachname || !strlen(attachname))
910 attachname = g_strdup_printf("noname.%d",subst_cnt++);
911 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
912 g_snprintf(buf, sizeof(buf), "%s%s",
914 (attachname[0] == G_DIR_SEPARATOR)
917 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
918 attachdir = g_dirname(buf);
919 make_dir_hier(attachdir);
922 if (is_file_exist(buf)) {
923 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
925 aval = alertpanel(_("Overwrite"), res, _("OK"),
931 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
932 alertpanel_error(_("Can't save the part of multipart message."));
933 attachment = attachment->next;
937 static void mimeview_display_as_text(MimeView *mimeview)
941 if (!mimeview->opened) return;
943 partinfo = mimeview_get_selected_part(mimeview);
944 g_return_if_fail(partinfo != NULL);
945 mimeview_show_message_part(mimeview, partinfo);
948 static void mimeview_save_as(MimeView *mimeview)
951 gchar *defname = NULL;
955 if (!mimeview->opened) return;
956 if (!mimeview->file) return;
958 partinfo = mimeview_get_selected_part(mimeview);
959 g_return_if_fail(partinfo != NULL);
961 if (partinfo->filename)
962 defname = partinfo->filename;
963 else if (partinfo->name) {
964 Xstrdup_a(defname, partinfo->name, return);
965 subst_for_filename(defname);
968 filename = filesel_select_file(_("Save as"), defname);
969 if (!filename) return;
970 if (is_file_exist(filename)) {
972 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
974 aval = alertpanel(_("Overwrite"), res, _("OK"),
977 if (G_ALERTDEFAULT != aval) return;
980 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
982 (_("Can't save the part of multipart message."));
985 static void mimeview_launch(MimeView *mimeview)
990 if (!mimeview->opened) return;
991 if (!mimeview->file) return;
993 partinfo = mimeview_get_selected_part(mimeview);
994 g_return_if_fail(partinfo != NULL);
996 filename = procmime_get_tmp_file_name(partinfo);
998 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
1000 (_("Can't save the part of multipart message."));
1002 mimeview_view_file(filename, partinfo, NULL);
1007 static void mimeview_open_with(MimeView *mimeview)
1013 if (!mimeview->opened) return;
1014 if (!mimeview->file) return;
1016 partinfo = mimeview_get_selected_part(mimeview);
1017 g_return_if_fail(partinfo != NULL);
1019 filename = procmime_get_tmp_file_name(partinfo);
1021 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1023 (_("Can't save the part of multipart message."));
1028 if (!prefs_common.mime_open_cmd_history)
1029 prefs_common.mime_open_cmd_history =
1030 add_history(NULL, prefs_common.mime_open_cmd);
1032 cmd = input_dialog_combo
1034 _("Enter the command line to open file:\n"
1035 "(`%s' will be replaced with file name)"),
1036 prefs_common.mime_open_cmd,
1037 prefs_common.mime_open_cmd_history,
1040 mimeview_view_file(filename, partinfo, cmd);
1041 g_free(prefs_common.mime_open_cmd);
1042 prefs_common.mime_open_cmd = cmd;
1043 prefs_common.mime_open_cmd_history =
1044 add_history(prefs_common.mime_open_cmd_history, cmd);
1050 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1051 const gchar *cmdline)
1053 static gchar *default_image_cmdline = "display '%s'";
1054 static gchar *default_audio_cmdline = "play '%s'";
1055 static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1056 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1060 const gchar *def_cmd;
1066 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1068 } else if (MIME_IMAGE == partinfo->mime_type) {
1069 cmd = prefs_common.mime_image_viewer;
1070 def_cmd = default_image_cmdline;
1071 } else if (MIME_AUDIO == partinfo->mime_type) {
1072 cmd = prefs_common.mime_audio_player;
1073 def_cmd = default_audio_cmdline;
1074 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1075 cmd = prefs_common.uri_cmd;
1076 def_cmd = default_html_cmdline;
1078 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1079 partinfo->content_type, "%s");
1084 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1085 !strchr(p + 2, '%'))
1086 g_snprintf(buf, sizeof(buf), cmd, filename);
1089 g_warning("MIME viewer command line is invalid: `%s'", cmd);
1091 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1096 execute_command_line(buf, TRUE);
1100 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1106 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1107 g_return_if_fail(partinfo != NULL);
1109 part_name = get_part_name(partinfo);
1110 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1113 static void mimeview_update_names(MimeView *mimeview)
1115 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1117 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1120 static void mimeview_update_signature_info(MimeView *mimeview)
1124 if (!mimeview) return;
1125 if (!mimeview->opened) return;
1127 partinfo = mimeview_get_selected_part(mimeview);
1128 if (!partinfo) return;
1130 if (g_strcasecmp(partinfo->content_type,
1131 "application/pgp-signature") == 0) {
1132 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1133 textview_show_signature_part(mimeview->textview, partinfo);
1137 void mimeview_check_signature(MimeView *mimeview)
1142 g_return_if_fail (mimeview_is_signed(mimeview));
1143 g_return_if_fail (gpg_started);
1145 mimeinfo = gtk_ctree_node_get_row_data
1146 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1147 g_return_if_fail(mimeinfo != NULL);
1148 g_return_if_fail(mimeview->file != NULL);
1150 while (mimeinfo->parent)
1151 mimeinfo = mimeinfo->parent;
1153 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1154 FILE_OP_ERROR(mimeview->file, "fopen");
1158 rfc2015_check_signature(mimeinfo, fp);
1161 mimeview_update_names(mimeview);
1162 mimeview_update_signature_info(mimeview);
1164 textview_show_message(mimeview->messageview->textview, mimeinfo,
1167 #endif /* USE_GPGME */
1169 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1171 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1174 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1176 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1179 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1181 GSList *mimeview_list, *viewer_list;
1183 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1184 MimeView *mimeview = (MimeView *) mimeview_list->data;
1186 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1187 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1188 mimeview->mimeviewer = NULL;
1191 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1192 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1194 mimeviewer->destroy_viewer(mimeviewer);
1195 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1199 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);