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"
68 #define N_MIMEVIEW_COLS 3
70 static void mimeview_set_multipart_tree (MimeView *mimeview,
72 GtkCTreeNode *parent);
73 static GtkCTreeNode *mimeview_append_part (MimeView *mimeview,
75 GtkCTreeNode *parent);
76 static void mimeview_show_message_part (MimeView *mimeview,
78 static void mimeview_change_view_type (MimeView *mimeview,
80 static void mimeview_clear (MimeView *mimeview);
82 static void mimeview_selected (GtkCTree *ctree,
86 static void mimeview_start_drag (GtkWidget *widget,
90 static gint mimeview_button_pressed (GtkWidget *widget,
91 GdkEventButton *event,
93 static gint mimeview_key_pressed (GtkWidget *widget,
97 static void mimeview_drag_data_get (GtkWidget *widget,
98 GdkDragContext *drag_context,
99 GtkSelectionData *selection_data,
104 static void mimeview_display_as_text (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_("/_Save as..."), NULL, mimeview_save_as, 0, NULL},
119 {N_("/Save _all..."), NULL, mimeview_save_all, 0, NULL}
122 {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
126 static GtkTargetEntry mimeview_mime_types[] =
128 {"text/uri-list", 0, 0}
131 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;
222 mimeviews = g_slist_prepend(mimeviews, mimeview);
227 void mimeview_init(MimeView *mimeview)
232 * Check whether the message is OpenPGP signed
235 static gboolean mimeview_is_signed(MimeView *mimeview)
237 MimeInfo *partinfo = NULL;
239 debug_print("mimeview_is signed of %p\n", mimeview);
241 if (!mimeview) return FALSE;
242 if (!mimeview->opened) return FALSE;
244 debug_print("mimeview_is_signed: open\n" );
246 if (!mimeview->file) return FALSE;
248 debug_print("mimeview_is_signed: file\n" );
250 partinfo = gtk_ctree_node_get_row_data
251 (GTK_CTREE(mimeview->ctree), mimeview->opened);
252 g_return_val_if_fail(partinfo != NULL, FALSE);
254 /* walk the tree and see whether there is a signature somewhere */
256 if (rfc2015_has_signature(partinfo))
258 } while ((partinfo = partinfo->parent) != NULL);
260 debug_print("mimeview_is_signed: FALSE\n" );
265 static void set_unchecked_signature(MimeInfo *mimeinfo)
267 MimeInfo *sig_partinfo;
269 sig_partinfo = rfc2015_find_signature(mimeinfo);
270 if (sig_partinfo == NULL) return;
272 g_free(sig_partinfo->sigstatus);
273 sig_partinfo->sigstatus =
274 g_strdup(_("Right-click here to verify the signature"));
276 g_free(sig_partinfo->sigstatus_full);
277 sig_partinfo->sigstatus_full = NULL;
279 #endif /* USE_GPGME */
281 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
284 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
288 mimeview_clear(mimeview);
289 textview_clear(mimeview->messageview->textview);
291 g_return_if_fail(file != NULL);
292 g_return_if_fail(mimeinfo != NULL);
294 mimeview->mimeinfo = mimeinfo;
296 mimeview->file = g_strdup(file);
299 if (prefs_common.auto_check_signatures && gpg_started) {
300 if ((fp = fopen(file, "rb")) == NULL) {
301 FILE_OP_ERROR(file, "fopen");
304 rfc2015_check_signature(mimeinfo, fp);
307 set_unchecked_signature(mimeinfo);
310 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
313 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
315 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
316 mimeview_selected, mimeview);
318 /* search first text part */
319 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
320 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
323 partinfo = gtk_ctree_node_get_row_data(ctree, node);
325 (partinfo->mime_type == MIME_TEXT ||
326 partinfo->mime_type == MIME_TEXT_HTML))
329 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
332 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
335 gtk_ctree_select(ctree, node);
336 gtkut_ctree_set_focus_row(ctree, node);
337 gtk_widget_grab_focus(mimeview->ctree);
341 void mimeview_destroy(MimeView *mimeview)
345 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
346 MimeViewer *viewer = (MimeViewer *) cur->data;
347 viewer->destroy_viewer(viewer);
349 g_slist_free(mimeview->viewers);
351 procmime_mimeinfo_free_all(mimeview->mimeinfo);
352 g_free(mimeview->file);
355 mimeviews = g_slist_remove(mimeviews, mimeview);
358 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
360 if (gtk_notebook_get_current_page
361 (GTK_NOTEBOOK(mimeview->notebook)) == 0)
364 return gtk_ctree_node_get_row_data
365 (GTK_CTREE(mimeview->ctree), mimeview->opened);
368 static void mimeview_set_multipart_tree(MimeView *mimeview,
370 GtkCTreeNode *parent)
374 g_return_if_fail(mimeinfo != NULL);
376 if (mimeinfo->children)
377 mimeinfo = mimeinfo->children;
379 while (mimeinfo != NULL) {
380 node = mimeview_append_part(mimeview, mimeinfo, parent);
382 if (mimeinfo->children)
383 mimeview_set_multipart_tree(mimeview, mimeinfo, node);
384 else if (mimeinfo->sub &&
385 mimeinfo->sub->mime_type != MIME_TEXT &&
386 mimeinfo->sub->mime_type != MIME_TEXT_HTML)
387 mimeview_set_multipart_tree(mimeview, mimeinfo->sub,
389 mimeinfo = mimeinfo->next;
393 static gchar *get_part_name(MimeInfo *partinfo)
396 if (partinfo->sigstatus)
397 return partinfo->sigstatus;
401 return partinfo->name;
402 else if (partinfo->filename)
403 return partinfo->filename;
404 else if (partinfo->description)
405 return partinfo->description;
410 static gchar *get_part_description(MimeInfo *partinfo)
412 if (partinfo->description)
413 return partinfo->description;
414 else if (partinfo->name)
415 return partinfo->name;
416 else if (partinfo->filename)
417 return partinfo->filename;
422 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
424 GtkCTreeNode *parent)
426 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
428 gchar *str[N_MIMEVIEW_COLS];
431 partinfo->content_type ? partinfo->content_type : "";
432 str[COL_SIZE] = to_human_readable(partinfo->size);
433 if (prefs_common.attach_desc)
434 str[COL_NAME] = get_part_description(partinfo);
436 str[COL_NAME] = get_part_name(partinfo);
438 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
439 NULL, NULL, NULL, NULL,
441 gtk_ctree_node_set_row_data(ctree, node, partinfo);
446 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
454 if (!partinfo) return;
457 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
459 fname = pi ? pi->plaintextfile : mimeview->file;
461 fname = mimeview->file;
462 #endif /* USE_GPGME */
465 if ((fp = fopen(fname, "rb")) == NULL) {
466 FILE_OP_ERROR(fname, "fopen");
470 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
471 FILE_OP_ERROR(mimeview->file, "fseek");
476 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
477 textview_show_part(mimeview->textview, partinfo, fp);
482 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
485 MimeViewerFactory *factory = NULL;
486 MimeViewer *viewer = NULL;
489 * FNM_CASEFOLD is a GNU extension
490 * if its not defined copy the string to the stack and
491 * convert the copy to lower case
494 #define FNM_CASEFOLD 0
495 Xstrdup_a(content_type, content_type, return NULL);
496 g_strdown((gchar *)content_type);
499 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
500 MimeViewerFactory *curfactory = cur->data;
503 while (curfactory->content_types[i] != NULL) {
504 debug_print("%s\n", curfactory->content_types[i]);
505 if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
506 factory = curfactory;
517 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
518 MimeViewer *curviewer = cur->data;
520 if (curviewer->factory == factory)
523 viewer = factory->create_viewer();
524 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
529 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
531 gchar *content_type = NULL;
532 MimeViewer *viewer = NULL;
534 if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
535 (partinfo->name != NULL)) {
536 content_type = procmime_get_mime_type(partinfo->name);
538 content_type = g_strdup(partinfo->content_type);
541 if (content_type != NULL) {
542 viewer = get_viewer_for_content_type(mimeview, content_type);
543 g_free(content_type);
549 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
553 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
554 if (viewer == NULL) {
555 if (mimeview->mimeviewer != NULL)
556 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
557 mimeview->mimeviewer = NULL;
561 if (mimeview->mimeviewer != viewer) {
562 if (mimeview->mimeviewer != NULL)
563 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
564 mimeview->mimeviewer = viewer;
565 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
567 viewer->show_mimepart(viewer, mimeview->file, partinfo);
572 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
574 TextView *textview = mimeview->textview;
577 if ((mimeview->type != MIMEVIEW_VIEWER) &&
578 (mimeview->type == type)) return;
580 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
582 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
583 GTK_WIDGET(children->data));
584 g_list_free(children);
589 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
590 GTK_WIDGET_PTR(textview));
592 case MIMEVIEW_VIEWER:
593 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
594 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
600 mimeview->type = type;
603 static void mimeview_clear(MimeView *mimeview)
605 GtkCList *clist = GTK_CLIST(mimeview->ctree);
607 procmime_mimeinfo_free_all(mimeview->mimeinfo);
608 mimeview->mimeinfo = NULL;
610 gtk_clist_clear(clist);
611 textview_clear(mimeview->textview);
612 if (mimeview->mimeviewer != NULL)
613 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
615 mimeview->opened = NULL;
617 g_free(mimeview->file);
618 mimeview->file = NULL;
620 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
623 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
628 if (mimeview->opened == node) return;
629 mimeview->opened = node;
630 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
632 partinfo = gtk_ctree_node_get_row_data(ctree, node);
633 if (!partinfo) return;
635 /* ungrab the mouse event */
636 if (GTK_WIDGET_HAS_GRAB(ctree)) {
637 gtk_grab_remove(GTK_WIDGET(ctree));
638 if (gdk_pointer_is_grabbed())
639 gdk_pointer_ungrab(GDK_CURRENT_TIME);
642 mimeview->textview->default_text = FALSE;
644 if (!mimeview_show_part(mimeview, partinfo)) {
645 switch (partinfo->mime_type) {
648 case MIME_TEXT_ENRICHED:
649 case MIME_MESSAGE_RFC822:
651 mimeview_show_message_part(mimeview, partinfo);
655 mimeview->textview->default_text = TRUE;
656 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
658 if (g_strcasecmp(partinfo->content_type,
659 "application/pgp-signature") == 0)
660 textview_show_signature_part(mimeview->textview,
664 textview_show_mime_part(mimeview->textview, partinfo);
670 static void mimeview_start_drag(GtkWidget *widget, gint button,
671 GdkEvent *event, MimeView *mimeview)
674 GdkDragContext *context;
677 g_return_if_fail(mimeview != NULL);
679 partinfo = mimeview_get_selected_part(mimeview);
680 if (partinfo->filename == NULL && partinfo->name == NULL) return;
682 list = gtk_target_list_new(mimeview_mime_types, 1);
683 context = gtk_drag_begin(widget, list,
684 GDK_ACTION_COPY, button, event);
685 gtk_drag_set_icon_default(context);
688 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
691 GtkCList *clist = GTK_CLIST(widget);
695 if (!event) return FALSE;
697 if (event->button == 2 || event->button == 3) {
698 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
701 gtk_clist_unselect_all(clist);
702 gtk_clist_select_row(clist, row, column);
703 gtkut_clist_set_focus_row(clist, row);
706 if (event->button == 2 ||
707 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
708 /* call external program for image, audio or html */
709 mimeview_launch(mimeview);
710 } else if (event->button == 3) {
711 partinfo = mimeview_get_selected_part(mimeview);
712 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
713 partinfo->mime_type == MIME_TEXT_HTML ||
714 partinfo->mime_type == MIME_TEXT_ENRICHED ||
715 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
716 partinfo->mime_type == MIME_IMAGE ||
717 partinfo->mime_type == MIME_MULTIPART))
718 menu_set_sensitive(mimeview->popupfactory,
719 "/Display as text", FALSE);
721 menu_set_sensitive(mimeview->popupfactory,
722 "/Display as text", TRUE);
724 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
725 menu_set_sensitive(mimeview->popupfactory,
728 menu_set_sensitive(mimeview->popupfactory,
732 menu_set_sensitive(mimeview->popupfactory,
734 mimeview_is_signed(mimeview));
737 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
738 NULL, NULL, NULL, NULL,
739 event->button, event->time);
745 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
747 mimeview_key_pressed(mimeview->ctree, event, mimeview);
750 #define BREAK_ON_MODIFIER_KEY() \
751 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
753 #define KEY_PRESS_EVENT_STOP() \
754 if (gtk_signal_n_emissions_by_name \
755 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
756 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
757 "key_press_event"); \
760 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
763 SummaryView *summaryview;
764 GtkCTree *ctree = GTK_CTREE(widget);
767 if (!event) return FALSE;
768 if (!mimeview->opened) return FALSE;
770 switch (event->keyval) {
772 if (textview_scroll_page(mimeview->textview, FALSE))
775 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
777 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
778 gtk_sctree_select(GTK_SCTREE(ctree), node);
783 textview_scroll_page(mimeview->textview, TRUE);
786 textview_scroll_one_line(mimeview->textview,
787 (event->state & GDK_MOD1_MASK) != 0);
791 BREAK_ON_MODIFIER_KEY();
792 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
793 KEY_PRESS_EVENT_STOP();
795 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
796 GTK_SCROLL_STEP_FORWARD, 0.0);
800 BREAK_ON_MODIFIER_KEY();
801 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
802 KEY_PRESS_EVENT_STOP();
804 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
805 GTK_SCROLL_STEP_BACKWARD, 0.0);
808 BREAK_ON_MODIFIER_KEY();
809 KEY_PRESS_EVENT_STOP();
810 mimeview_save_as(mimeview);
813 BREAK_ON_MODIFIER_KEY();
814 KEY_PRESS_EVENT_STOP();
815 mimeview_display_as_text(mimeview);
818 BREAK_ON_MODIFIER_KEY();
819 KEY_PRESS_EVENT_STOP();
820 mimeview_launch(mimeview);
826 if (!mimeview->messageview->mainwin) return FALSE;
827 summaryview = mimeview->messageview->mainwin->summaryview;
828 summary_pass_key_press_event(summaryview, event);
832 static void mimeview_drag_data_get(GtkWidget *widget,
833 GdkDragContext *drag_context,
834 GtkSelectionData *selection_data,
839 gchar *filename, *uriname;
842 if (!mimeview->opened) return;
843 if (!mimeview->file) return;
845 partinfo = mimeview_get_selected_part(mimeview);
846 if (!partinfo) return;
847 if (!partinfo->filename && !partinfo->name) return;
849 filename = partinfo->filename ? partinfo->filename : partinfo->name;
850 filename = g_basename(filename);
851 if (*filename == '\0') return;
853 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
856 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
858 (_("Can't save the part of multipart message."));
860 uriname = g_strconcat("file:/", filename, NULL);
861 gtk_selection_data_set(selection_data, selection_data->target, 8,
862 uriname, strlen(uriname));
868 static void mimeview_save_all(MimeView *mimeview)
871 gchar *defname = NULL;
873 MimeInfo *attachment;
876 if (!mimeview->opened) return;
877 if (!mimeview->file) return;
879 partinfo = mimeview_get_selected_part(mimeview);
880 g_return_if_fail(partinfo != NULL);
882 dirname = filesel_select_file(_("Save as"), defname);
883 if (!dirname) return;
885 /* return to first children */
886 if (!partinfo->parent->children) return; /* multipart container? */
887 attachment = partinfo->parent->children->next;
888 /* for each attachment, extract it in the selected dir. */
889 while (attachment != NULL) {
890 static guint subst_cnt = 1;
892 gchar *attachname = g_strdup(get_part_name(attachment));
893 AlertValue aval = G_ALERTDEFAULT;
896 if (!attachname || !strlen(attachname))
897 attachname = g_strdup_printf("noname.%d",subst_cnt++);
898 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
899 g_snprintf(buf, sizeof(buf), "%s%s",
901 (attachname[0] == G_DIR_SEPARATOR)
904 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
905 attachdir = g_dirname(buf);
906 make_dir_hier(attachdir);
909 if (is_file_exist(buf)) {
910 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
912 aval = alertpanel(_("Overwrite"), res, _("OK"),
918 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
919 alertpanel_error(_("Can't save the part of multipart message."));
920 attachment = attachment->next;
924 static void mimeview_display_as_text(MimeView *mimeview)
928 if (!mimeview->opened) return;
930 partinfo = mimeview_get_selected_part(mimeview);
931 g_return_if_fail(partinfo != NULL);
932 mimeview_show_message_part(mimeview, partinfo);
935 static void mimeview_save_as(MimeView *mimeview)
938 gchar *defname = NULL;
942 if (!mimeview->opened) return;
943 if (!mimeview->file) return;
945 partinfo = mimeview_get_selected_part(mimeview);
946 g_return_if_fail(partinfo != NULL);
948 if (partinfo->filename)
949 defname = partinfo->filename;
950 else if (partinfo->name) {
951 Xstrdup_a(defname, partinfo->name, return);
952 subst_for_filename(defname);
955 filename = filesel_select_file(_("Save as"), defname);
956 if (!filename) return;
957 if (is_file_exist(filename)) {
959 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
961 aval = alertpanel(_("Overwrite"), res, _("OK"),
964 if (G_ALERTDEFAULT != aval) return;
967 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
969 (_("Can't save the part of multipart message."));
972 static void mimeview_launch(MimeView *mimeview)
977 if (!mimeview->opened) return;
978 if (!mimeview->file) return;
980 partinfo = mimeview_get_selected_part(mimeview);
981 g_return_if_fail(partinfo != NULL);
983 filename = procmime_get_tmp_file_name(partinfo);
985 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
987 (_("Can't save the part of multipart message."));
989 mimeview_view_file(filename, partinfo, NULL);
994 static void mimeview_open_with(MimeView *mimeview)
1000 if (!mimeview->opened) return;
1001 if (!mimeview->file) return;
1003 partinfo = mimeview_get_selected_part(mimeview);
1004 g_return_if_fail(partinfo != NULL);
1006 filename = procmime_get_tmp_file_name(partinfo);
1008 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1010 (_("Can't save the part of multipart message."));
1015 if (!prefs_common.mime_open_cmd_history)
1016 prefs_common.mime_open_cmd_history =
1017 add_history(NULL, prefs_common.mime_open_cmd);
1019 cmd = input_dialog_combo
1021 _("Enter the command line to open file:\n"
1022 "(`%s' will be replaced with file name)"),
1023 prefs_common.mime_open_cmd,
1024 prefs_common.mime_open_cmd_history,
1027 mimeview_view_file(filename, partinfo, cmd);
1028 g_free(prefs_common.mime_open_cmd);
1029 prefs_common.mime_open_cmd = cmd;
1030 prefs_common.mime_open_cmd_history =
1031 add_history(prefs_common.mime_open_cmd_history, cmd);
1037 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1038 const gchar *cmdline)
1040 static gchar *default_image_cmdline = "display '%s'";
1041 static gchar *default_audio_cmdline = "play '%s'";
1042 static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1043 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1047 const gchar *def_cmd;
1053 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1055 } else if (MIME_IMAGE == partinfo->mime_type) {
1056 cmd = prefs_common.mime_image_viewer;
1057 def_cmd = default_image_cmdline;
1058 } else if (MIME_AUDIO == partinfo->mime_type) {
1059 cmd = prefs_common.mime_audio_player;
1060 def_cmd = default_audio_cmdline;
1061 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1062 cmd = prefs_common.uri_cmd;
1063 def_cmd = default_html_cmdline;
1065 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1066 partinfo->content_type, "%s");
1071 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1072 !strchr(p + 2, '%'))
1073 g_snprintf(buf, sizeof(buf), cmd, filename);
1076 g_warning("MIME viewer command line is invalid: `%s'", cmd);
1078 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1083 execute_command_line(buf, TRUE);
1087 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1093 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1094 g_return_if_fail(partinfo != NULL);
1096 part_name = get_part_name(partinfo);
1097 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1100 static void mimeview_update_names(MimeView *mimeview)
1102 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1104 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1107 static void mimeview_update_signature_info(MimeView *mimeview)
1111 if (!mimeview) return;
1112 if (!mimeview->opened) return;
1114 partinfo = mimeview_get_selected_part(mimeview);
1115 if (!partinfo) return;
1117 if (g_strcasecmp(partinfo->content_type,
1118 "application/pgp-signature") == 0) {
1119 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1120 textview_show_signature_part(mimeview->textview, partinfo);
1124 void mimeview_check_signature(MimeView *mimeview)
1129 g_return_if_fail (mimeview_is_signed(mimeview));
1130 g_return_if_fail (gpg_started);
1132 mimeinfo = gtk_ctree_node_get_row_data
1133 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1134 g_return_if_fail(mimeinfo != NULL);
1135 g_return_if_fail(mimeview->file != NULL);
1137 while (mimeinfo->parent)
1138 mimeinfo = mimeinfo->parent;
1140 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1141 FILE_OP_ERROR(mimeview->file, "fopen");
1145 rfc2015_check_signature(mimeinfo, fp);
1148 mimeview_update_names(mimeview);
1149 mimeview_update_signature_info(mimeview);
1151 textview_show_message(mimeview->messageview->textview, mimeinfo,
1154 #endif /* USE_GPGME */
1156 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1158 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1161 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1163 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1166 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1168 GSList *mimeview_list, *viewer_list;
1170 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1171 MimeView *mimeview = (MimeView *) mimeview_list->data;
1173 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1174 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1175 mimeview->mimeviewer = NULL;
1178 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1179 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1181 mimeviewer->destroy_viewer(mimeviewer);
1182 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1186 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);