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>
47 #include "summaryview.h"
50 #include "alertpanel.h"
51 #include "inputdialog.h"
54 #include "prefs_common.h"
64 #define N_MIMEVIEW_COLS 3
66 static void mimeview_set_multipart_tree (MimeView *mimeview,
68 GtkCTreeNode *parent);
69 static GtkCTreeNode *mimeview_append_part (MimeView *mimeview,
71 GtkCTreeNode *parent);
72 static void mimeview_show_message_part (MimeView *mimeview,
74 static void mimeview_change_view_type (MimeView *mimeview,
76 static void mimeview_clear (MimeView *mimeview);
78 static void mimeview_selected (GtkCTree *ctree,
82 static void mimeview_start_drag (GtkWidget *widget,
86 static gint mimeview_button_pressed (GtkWidget *widget,
87 GdkEventButton *event,
89 static gint mimeview_key_pressed (GtkWidget *widget,
93 static void mimeview_drag_data_get (GtkWidget *widget,
94 GdkDragContext *drag_context,
95 GtkSelectionData *selection_data,
100 static void mimeview_display_as_text (MimeView *mimeview);
101 static void mimeview_save_as (MimeView *mimeview);
102 static void mimeview_save_all (MimeView *mimeview);
103 static void mimeview_launch (MimeView *mimeview);
104 static void mimeview_open_with (MimeView *mimeview);
105 static void mimeview_view_file (const gchar *filename,
107 const gchar *cmdline);
109 static GtkItemFactoryEntry mimeview_popup_entries[] =
111 {N_("/_Open"), NULL, mimeview_launch, 0, NULL},
112 {N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
113 {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
114 {N_("/_Save as..."), NULL, mimeview_save_as, 0, NULL},
115 {N_("/Save _all..."), NULL, mimeview_save_all, 0, NULL}
118 {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
122 static GtkTargetEntry mimeview_mime_types[] =
124 {"text/uri-list", 0, 0}
127 GSList *mimeviewer_factories;
130 MimeView *mimeview_create(void)
137 GtkWidget *scrolledwin;
139 GtkWidget *mime_vbox;
140 GtkWidget *popupmenu;
141 GtkItemFactory *popupfactory;
142 gchar *titles[N_MIMEVIEW_COLS];
146 debug_print("Creating MIME view...\n");
147 mimeview = g_new0(MimeView, 1);
149 titles[COL_MIMETYPE] = _("MIME Type");
150 titles[COL_SIZE] = _("Size");
151 titles[COL_NAME] = _("Name");
153 notebook = gtk_notebook_new();
154 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
156 vbox = gtk_vbox_new(FALSE, 0);
157 gtk_container_add(GTK_CONTAINER(notebook), vbox);
158 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
161 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
162 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
163 GTK_POLICY_AUTOMATIC,
165 gtk_widget_set_usize(scrolledwin, -1, 80);
167 ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
168 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
169 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
170 gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
172 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
173 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
174 for (i = 0; i < N_MIMEVIEW_COLS; i++)
175 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
177 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
179 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
180 GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
181 gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
182 GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
183 gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
184 GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
185 gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
186 GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
187 gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
188 GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
190 mime_vbox = gtk_vbox_new(FALSE, 0);
192 paned = gtk_vpaned_new();
193 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
194 gtk_paned_add2(GTK_PANED(paned), mime_vbox);
195 gtk_container_add(GTK_CONTAINER(notebook), paned);
196 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
199 gtk_widget_show_all(notebook);
201 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
203 n_entries = sizeof(mimeview_popup_entries) /
204 sizeof(mimeview_popup_entries[0]);
205 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
206 "<MimeView>", &popupfactory, mimeview);
208 mimeview->notebook = notebook;
209 mimeview->vbox = vbox;
210 mimeview->paned = paned;
211 mimeview->scrolledwin = scrolledwin;
212 mimeview->ctree = ctree;
213 mimeview->mime_vbox = mime_vbox;
214 mimeview->popupmenu = popupmenu;
215 mimeview->popupfactory = popupfactory;
218 mimeviews = g_slist_prepend(mimeviews, mimeview);
223 void mimeview_init(MimeView *mimeview)
228 * Check whether the message is OpenPGP signed
231 static gboolean mimeview_is_signed(MimeView *mimeview)
233 MimeInfo *partinfo = NULL;
235 debug_print("mimeview_is signed of %p\n", mimeview);
237 if (!mimeview) return FALSE;
238 if (!mimeview->opened) return FALSE;
240 debug_print("mimeview_is_signed: open\n" );
242 if (!mimeview->file) return FALSE;
244 debug_print("mimeview_is_signed: file\n" );
246 partinfo = gtk_ctree_node_get_row_data
247 (GTK_CTREE(mimeview->ctree), mimeview->opened);
248 g_return_val_if_fail(partinfo != NULL, FALSE);
250 /* walk the tree and see whether there is a signature somewhere */
252 if (rfc2015_has_signature(partinfo))
254 } while ((partinfo = partinfo->parent) != NULL);
256 debug_print("mimeview_is_signed: FALSE\n" );
261 static void set_unchecked_signature(MimeInfo *mimeinfo)
263 MimeInfo *sig_partinfo;
265 sig_partinfo = rfc2015_find_signature(mimeinfo);
266 if (sig_partinfo == NULL) return;
268 g_free(sig_partinfo->sigstatus);
269 sig_partinfo->sigstatus =
270 g_strdup(_("Select \"Check signature\" to check"));
272 g_free(sig_partinfo->sigstatus_full);
273 sig_partinfo->sigstatus_full = NULL;
275 #endif /* USE_GPGME */
277 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
280 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
285 mimeview_clear(mimeview);
286 textview_clear(mimeview->messageview->textview);
288 g_return_if_fail(file != NULL);
289 g_return_if_fail(mimeinfo != NULL);
291 mimeview->mimeinfo = mimeinfo;
293 if ((fp = fopen(file, "rb")) == NULL) {
294 FILE_OP_ERROR(file, "fopen");
297 mimeview->file = g_strdup(file);
299 /* skip MIME part headers */
300 if (mimeinfo->mime_type == MIME_MULTIPART) {
301 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
303 while (fgets(buf, sizeof(buf), fp) != NULL)
304 if (buf[0] == '\r' || buf[0] == '\n') break;
307 procmime_scan_multipart_message(mimeinfo, fp);
309 if ((prefs_common.auto_check_signatures)
311 rfc2015_check_signature(mimeinfo, fp);
313 set_unchecked_signature(mimeinfo);
316 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
319 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
321 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
322 mimeview_selected, mimeview);
324 /* search first text part */
325 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
326 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
329 partinfo = gtk_ctree_node_get_row_data(ctree, node);
331 (partinfo->mime_type == MIME_TEXT ||
332 partinfo->mime_type == MIME_TEXT_HTML))
336 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
339 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
342 gtk_ctree_select(ctree, node);
343 gtkut_ctree_set_focus_row(ctree, node);
344 gtk_widget_grab_focus(mimeview->ctree);
348 void mimeview_destroy(MimeView *mimeview)
352 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
353 MimeViewer *viewer = (MimeViewer *) cur->data;
354 viewer->destroy_viewer(viewer);
356 g_slist_free(mimeview->viewers);
358 procmime_mimeinfo_free_all(mimeview->mimeinfo);
359 g_free(mimeview->file);
362 mimeviews = g_slist_remove(mimeviews, mimeview);
365 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
367 if (gtk_notebook_get_current_page
368 (GTK_NOTEBOOK(mimeview->notebook)) == 0)
371 return gtk_ctree_node_get_row_data
372 (GTK_CTREE(mimeview->ctree), mimeview->opened);
375 static void mimeview_set_multipart_tree(MimeView *mimeview,
377 GtkCTreeNode *parent)
379 GtkCTreeNode *current = parent;
381 g_return_if_fail(mimeinfo != NULL);
383 if (!mimeinfo->sub && mimeinfo->parent)
384 current = mimeview_append_part(mimeview, mimeinfo, parent);
385 if (mimeinfo->sub && !mimeinfo->sub->children &&
386 mimeinfo->sub->mime_type != MIME_TEXT &&
387 mimeinfo->sub->mime_type != MIME_TEXT_HTML) {
388 mimeview_append_part(mimeview, mimeinfo->sub, parent);
393 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
395 if (mimeinfo->children)
396 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
399 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
402 static gchar *get_part_name(MimeInfo *partinfo)
405 if (partinfo->sigstatus)
406 return partinfo->sigstatus;
410 return partinfo->name;
411 else if (partinfo->filename)
412 return partinfo->filename;
413 else if (partinfo->description)
414 return partinfo->description;
419 static gchar *get_part_description(MimeInfo *partinfo)
421 if (partinfo->description)
422 return partinfo->description;
423 else if (partinfo->name)
424 return partinfo->name;
425 else if (partinfo->filename)
426 return partinfo->filename;
431 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
433 GtkCTreeNode *parent)
435 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
437 gchar *str[N_MIMEVIEW_COLS];
440 partinfo->content_type ? partinfo->content_type : "";
441 str[COL_SIZE] = to_human_readable(partinfo->size);
442 if (prefs_common.attach_desc)
443 str[COL_NAME] = get_part_description(partinfo);
445 str[COL_NAME] = get_part_name(partinfo);
447 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
448 NULL, NULL, NULL, NULL,
450 gtk_ctree_node_set_row_data(ctree, node, partinfo);
455 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
463 if (!partinfo) return;
466 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
468 fname = pi ? pi->plaintextfile : mimeview->file;
470 fname = mimeview->file;
471 #endif /* USE_GPGME */
474 if ((fp = fopen(fname, "rb")) == NULL) {
475 FILE_OP_ERROR(fname, "fopen");
479 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
480 FILE_OP_ERROR(mimeview->file, "fseek");
485 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
486 textview_show_part(mimeview->textview, partinfo, fp);
491 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
494 MimeViewerFactory *factory = NULL;
495 MimeViewer *viewer = NULL;
497 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
498 MimeViewerFactory *curfactory = cur->data;
500 if(!fnmatch(curfactory->content_type, content_type, 0)) {
501 factory = curfactory;
508 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
509 MimeViewer *curviewer = cur->data;
511 if (curviewer->factory == factory)
514 viewer = factory->create_viewer();
515 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
520 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
522 gchar *content_type = NULL;
523 MimeViewer *viewer = NULL;
525 if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
526 (partinfo->name != NULL)) {
527 content_type = procmime_get_mime_type(partinfo->name);
529 content_type = g_strdup(partinfo->content_type);
532 if (content_type != NULL) {
533 viewer = get_viewer_for_content_type(mimeview, content_type);
534 g_free(content_type);
540 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
544 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
545 if (viewer == NULL) {
546 if (mimeview->mimeviewer != NULL)
547 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
548 mimeview->mimeviewer = NULL;
552 if (mimeview->mimeviewer != viewer) {
553 if (mimeview->mimeviewer != NULL)
554 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
555 mimeview->mimeviewer = viewer;
556 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
558 viewer->show_mimepart(viewer, mimeview->file, partinfo);
563 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
565 TextView *textview = mimeview->textview;
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(textview));
583 case MIMEVIEW_VIEWER:
584 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
585 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
591 mimeview->type = type;
594 static void mimeview_clear(MimeView *mimeview)
596 GtkCList *clist = GTK_CLIST(mimeview->ctree);
598 procmime_mimeinfo_free_all(mimeview->mimeinfo);
599 mimeview->mimeinfo = NULL;
601 gtk_clist_clear(clist);
602 textview_clear(mimeview->textview);
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);
646 mimeview->textview->default_text = TRUE;
647 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
649 if (g_strcasecmp(partinfo->content_type,
650 "application/pgp-signature") == 0)
651 textview_show_signature_part(mimeview->textview,
655 textview_show_mime_part(mimeview->textview, partinfo);
661 static void mimeview_start_drag(GtkWidget *widget, gint button,
662 GdkEvent *event, MimeView *mimeview)
665 GdkDragContext *context;
668 g_return_if_fail(mimeview != NULL);
670 partinfo = mimeview_get_selected_part(mimeview);
671 if (partinfo->filename == NULL && partinfo->name == NULL) return;
673 list = gtk_target_list_new(mimeview_mime_types, 1);
674 context = gtk_drag_begin(widget, list,
675 GDK_ACTION_COPY, button, event);
676 gtk_drag_set_icon_default(context);
679 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
682 GtkCList *clist = GTK_CLIST(widget);
686 if (!event) return FALSE;
688 if (event->button == 2 || event->button == 3) {
689 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
692 gtk_clist_unselect_all(clist);
693 gtk_clist_select_row(clist, row, column);
694 gtkut_clist_set_focus_row(clist, row);
697 if (event->button == 2 ||
698 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
699 /* call external program for image, audio or html */
700 mimeview_launch(mimeview);
701 } else if (event->button == 3) {
702 partinfo = mimeview_get_selected_part(mimeview);
703 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
704 partinfo->mime_type == MIME_TEXT_HTML ||
705 partinfo->mime_type == MIME_TEXT_ENRICHED ||
706 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
707 partinfo->mime_type == MIME_IMAGE ||
708 partinfo->mime_type == MIME_MULTIPART))
709 menu_set_sensitive(mimeview->popupfactory,
710 "/Display as text", FALSE);
712 menu_set_sensitive(mimeview->popupfactory,
713 "/Display as text", TRUE);
715 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
716 menu_set_sensitive(mimeview->popupfactory,
719 menu_set_sensitive(mimeview->popupfactory,
723 menu_set_sensitive(mimeview->popupfactory,
725 mimeview_is_signed(mimeview));
728 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
729 NULL, NULL, NULL, NULL,
730 event->button, event->time);
736 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
738 mimeview_key_pressed(mimeview->ctree, event, mimeview);
741 #define BREAK_ON_MODIFIER_KEY() \
742 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
744 #define KEY_PRESS_EVENT_STOP() \
745 if (gtk_signal_n_emissions_by_name \
746 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
747 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
748 "key_press_event"); \
751 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
754 SummaryView *summaryview;
755 GtkCTree *ctree = GTK_CTREE(widget);
758 if (!event) return FALSE;
759 if (!mimeview->opened) return FALSE;
761 switch (event->keyval) {
763 if (textview_scroll_page(mimeview->textview, FALSE))
766 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
768 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
769 gtk_sctree_select(GTK_SCTREE(ctree), node);
774 textview_scroll_page(mimeview->textview, TRUE);
777 textview_scroll_one_line(mimeview->textview,
778 (event->state & GDK_MOD1_MASK) != 0);
782 BREAK_ON_MODIFIER_KEY();
783 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
784 KEY_PRESS_EVENT_STOP();
786 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
787 GTK_SCROLL_STEP_FORWARD, 0.0);
791 BREAK_ON_MODIFIER_KEY();
792 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
793 KEY_PRESS_EVENT_STOP();
795 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
796 GTK_SCROLL_STEP_BACKWARD, 0.0);
799 BREAK_ON_MODIFIER_KEY();
800 KEY_PRESS_EVENT_STOP();
801 mimeview_save_as(mimeview);
804 BREAK_ON_MODIFIER_KEY();
805 KEY_PRESS_EVENT_STOP();
806 mimeview_display_as_text(mimeview);
809 BREAK_ON_MODIFIER_KEY();
810 KEY_PRESS_EVENT_STOP();
811 mimeview_launch(mimeview);
817 if (!mimeview->messageview->mainwin) return FALSE;
818 summaryview = mimeview->messageview->mainwin->summaryview;
819 summary_pass_key_press_event(summaryview, event);
823 static void mimeview_drag_data_get(GtkWidget *widget,
824 GdkDragContext *drag_context,
825 GtkSelectionData *selection_data,
830 gchar *filename, *uriname;
833 if (!mimeview->opened) return;
834 if (!mimeview->file) return;
836 partinfo = mimeview_get_selected_part(mimeview);
837 if (!partinfo) return;
838 if (!partinfo->filename && !partinfo->name) return;
840 filename = partinfo->filename ? partinfo->filename : partinfo->name;
841 filename = g_basename(filename);
842 if (*filename == '\0') return;
844 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
847 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
849 (_("Can't save the part of multipart message."));
851 uriname = g_strconcat("file:/", filename, NULL);
852 gtk_selection_data_set(selection_data, selection_data->target, 8,
853 uriname, strlen(uriname));
859 static void mimeview_save_all(MimeView *mimeview)
862 gchar *defname = NULL;
864 MimeInfo *attachment;
867 if (!mimeview->opened) return;
868 if (!mimeview->file) return;
870 partinfo = mimeview_get_selected_part(mimeview);
871 g_return_if_fail(partinfo != NULL);
873 dirname = filesel_select_file(_("Save as"), defname);
874 if (!dirname) return;
876 /* return to first children */
877 if (!partinfo->parent->children) return; /* multipart container? */
878 attachment = partinfo->parent->children->next;
879 /* for each attachment, extract it in the selected dir. */
880 while (attachment != NULL) {
881 static guint subst_cnt = 1;
883 gchar *attachname = g_strdup(get_part_name(attachment));
884 AlertValue aval = G_ALERTDEFAULT;
887 if (!attachname || !strlen(attachname))
888 attachname = g_strdup_printf("noname.%d",subst_cnt++);
889 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
890 g_snprintf(buf, sizeof(buf), "%s%s",
892 (attachname[0] == G_DIR_SEPARATOR)
895 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
896 attachdir = g_dirname(buf);
897 make_dir_hier(attachdir);
900 if (is_file_exist(buf)) {
901 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
903 aval = alertpanel(_("Overwrite"), res, _("OK"),
909 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
910 alertpanel_error(_("Can't save the part of multipart message."));
911 attachment = attachment->next;
915 static void mimeview_display_as_text(MimeView *mimeview)
919 if (!mimeview->opened) return;
921 partinfo = mimeview_get_selected_part(mimeview);
922 g_return_if_fail(partinfo != NULL);
923 mimeview_show_message_part(mimeview, partinfo);
926 static void mimeview_save_as(MimeView *mimeview)
929 gchar *defname = NULL;
933 if (!mimeview->opened) return;
934 if (!mimeview->file) return;
936 partinfo = mimeview_get_selected_part(mimeview);
937 g_return_if_fail(partinfo != NULL);
939 if (partinfo->filename)
940 defname = partinfo->filename;
941 else if (partinfo->name) {
942 Xstrdup_a(defname, partinfo->name, return);
943 subst_for_filename(defname);
946 filename = filesel_select_file(_("Save as"), defname);
947 if (!filename) return;
948 if (is_file_exist(filename)) {
950 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
952 aval = alertpanel(_("Overwrite"), res, _("OK"),
955 if (G_ALERTDEFAULT != aval) return;
958 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
960 (_("Can't save the part of multipart message."));
963 static void mimeview_launch(MimeView *mimeview)
968 if (!mimeview->opened) return;
969 if (!mimeview->file) return;
971 partinfo = mimeview_get_selected_part(mimeview);
972 g_return_if_fail(partinfo != NULL);
974 filename = procmime_get_tmp_file_name(partinfo);
976 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
978 (_("Can't save the part of multipart message."));
980 mimeview_view_file(filename, partinfo, NULL);
985 static void mimeview_open_with(MimeView *mimeview)
991 if (!mimeview->opened) return;
992 if (!mimeview->file) return;
994 partinfo = mimeview_get_selected_part(mimeview);
995 g_return_if_fail(partinfo != NULL);
997 filename = procmime_get_tmp_file_name(partinfo);
999 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1001 (_("Can't save the part of multipart message."));
1006 if (!prefs_common.mime_open_cmd_history)
1007 prefs_common.mime_open_cmd_history =
1008 add_history(NULL, prefs_common.mime_open_cmd);
1010 cmd = input_dialog_combo
1012 _("Enter the command line to open file:\n"
1013 "(`%s' will be replaced with file name)"),
1014 prefs_common.mime_open_cmd,
1015 prefs_common.mime_open_cmd_history,
1018 mimeview_view_file(filename, partinfo, cmd);
1019 g_free(prefs_common.mime_open_cmd);
1020 prefs_common.mime_open_cmd = cmd;
1021 prefs_common.mime_open_cmd_history =
1022 add_history(prefs_common.mime_open_cmd_history, cmd);
1028 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1029 const gchar *cmdline)
1031 static gchar *default_image_cmdline = "display '%s'";
1032 static gchar *default_audio_cmdline = "play '%s'";
1033 static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1034 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1038 const gchar *def_cmd;
1044 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1046 } else if (MIME_IMAGE == partinfo->mime_type) {
1047 cmd = prefs_common.mime_image_viewer;
1048 def_cmd = default_image_cmdline;
1049 } else if (MIME_AUDIO == partinfo->mime_type) {
1050 cmd = prefs_common.mime_audio_player;
1051 def_cmd = default_audio_cmdline;
1052 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1053 cmd = prefs_common.uri_cmd;
1054 def_cmd = default_html_cmdline;
1056 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1057 partinfo->content_type, "%s");
1062 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1063 !strchr(p + 2, '%'))
1064 g_snprintf(buf, sizeof(buf), cmd, filename);
1067 g_warning("MIME viewer command line is invalid: `%s'", cmd);
1069 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1074 execute_command_line(buf, TRUE);
1078 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1084 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1085 g_return_if_fail(partinfo != NULL);
1087 part_name = get_part_name(partinfo);
1088 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1091 static void mimeview_update_names(MimeView *mimeview)
1093 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1095 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1098 static void mimeview_update_signature_info(MimeView *mimeview)
1102 if (!mimeview) return;
1103 if (!mimeview->opened) return;
1105 partinfo = mimeview_get_selected_part(mimeview);
1106 if (!partinfo) return;
1108 if (g_strcasecmp(partinfo->content_type,
1109 "application/pgp-signature") == 0) {
1110 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1111 textview_show_signature_part(mimeview->textview, partinfo);
1115 void mimeview_check_signature(MimeView *mimeview)
1120 g_return_if_fail (mimeview_is_signed(mimeview));
1121 g_return_if_fail (gpg_started);
1123 mimeinfo = gtk_ctree_node_get_row_data
1124 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1125 g_return_if_fail(mimeinfo != NULL);
1126 g_return_if_fail(mimeview->file != NULL);
1128 while (mimeinfo->parent)
1129 mimeinfo = mimeinfo->parent;
1131 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1132 FILE_OP_ERROR(mimeview->file, "fopen");
1136 rfc2015_check_signature(mimeinfo, fp);
1139 mimeview_update_names(mimeview);
1140 mimeview_update_signature_info(mimeview);
1142 textview_show_message(mimeview->messageview->textview, mimeinfo,
1145 #endif /* USE_GPGME */
1147 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1149 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1152 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1154 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1157 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1159 GSList *mimeview_list, *viewer_list;
1161 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1162 MimeView *mimeview = (MimeView *) mimeview_list->data;
1164 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1165 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1166 mimeview->mimeviewer = NULL;
1169 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1170 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1172 mimeviewer->destroy_viewer(mimeviewer);
1173 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1177 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);