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_vbox;
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_vbox = gtk_vbox_new(FALSE, 0);
199 paned = gtk_vpaned_new();
200 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
201 gtk_paned_add2(GTK_PANED(paned), mime_vbox);
202 gtk_container_add(GTK_CONTAINER(notebook), paned);
203 icon = stock_pixmap_widget(mainwin->window, STOCK_PIXMAP_CLIP);
204 gtk_widget_show(icon);
205 gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), paned, icon);
206 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_RIGHT);
208 gtk_widget_show_all(notebook);
210 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
212 n_entries = sizeof(mimeview_popup_entries) /
213 sizeof(mimeview_popup_entries[0]);
214 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
215 "<MimeView>", &popupfactory, mimeview);
217 mimeview->notebook = notebook;
218 mimeview->vbox = vbox;
219 mimeview->paned = paned;
220 mimeview->scrolledwin = scrolledwin;
221 mimeview->ctree = ctree;
222 mimeview->mime_vbox = mime_vbox;
223 mimeview->popupmenu = popupmenu;
224 mimeview->popupfactory = popupfactory;
227 mimeviews = g_slist_prepend(mimeviews, mimeview);
232 void mimeview_init(MimeView *mimeview)
234 textview_init(mimeview->textview);
238 * Check whether the message is OpenPGP signed
241 static gboolean mimeview_is_signed(MimeView *mimeview)
243 MimeInfo *partinfo = NULL;
245 debug_print("mimeview_is signed of %p\n", mimeview);
247 if (!mimeview) return FALSE;
248 if (!mimeview->opened) return FALSE;
250 debug_print("mimeview_is_signed: open\n" );
252 if (!mimeview->file) return FALSE;
254 debug_print("mimeview_is_signed: file\n" );
256 partinfo = gtk_ctree_node_get_row_data
257 (GTK_CTREE(mimeview->ctree), mimeview->opened);
258 g_return_val_if_fail(partinfo != NULL, FALSE);
260 /* walk the tree and see whether there is a signature somewhere */
262 if (rfc2015_has_signature(partinfo))
264 } while ((partinfo = partinfo->parent) != NULL);
266 debug_print("mimeview_is_signed: FALSE\n" );
271 static void set_unchecked_signature(MimeInfo *mimeinfo)
273 MimeInfo *sig_partinfo;
275 sig_partinfo = rfc2015_find_signature(mimeinfo);
276 if (sig_partinfo == NULL) return;
278 g_free(sig_partinfo->sigstatus);
279 sig_partinfo->sigstatus =
280 g_strdup(_("Right-click here to verify the signature"));
282 g_free(sig_partinfo->sigstatus_full);
283 sig_partinfo->sigstatus_full = NULL;
285 #endif /* USE_GPGME */
287 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
290 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
294 mimeview_clear(mimeview);
295 textview_clear(mimeview->messageview->textview);
297 g_return_if_fail(file != NULL);
298 g_return_if_fail(mimeinfo != NULL);
300 mimeview->mimeinfo = mimeinfo;
302 mimeview->file = g_strdup(file);
305 if (prefs_common.auto_check_signatures && gpg_started) {
306 if ((fp = fopen(file, "rb")) == NULL) {
307 FILE_OP_ERROR(file, "fopen");
310 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))
335 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
338 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
341 gtk_ctree_select(ctree, node);
342 gtkut_ctree_set_focus_row(ctree, node);
343 gtk_widget_grab_focus(mimeview->ctree);
347 void mimeview_destroy(MimeView *mimeview)
351 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
352 MimeViewer *viewer = (MimeViewer *) cur->data;
353 viewer->destroy_viewer(viewer);
355 g_slist_free(mimeview->viewers);
357 procmime_mimeinfo_free_all(mimeview->mimeinfo);
358 g_free(mimeview->file);
361 mimeviews = g_slist_remove(mimeviews, mimeview);
364 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
366 if (gtk_notebook_get_current_page
367 (GTK_NOTEBOOK(mimeview->notebook)) == 0)
370 return gtk_ctree_node_get_row_data
371 (GTK_CTREE(mimeview->ctree), mimeview->opened);
374 static void mimeview_set_multipart_tree(MimeView *mimeview,
376 GtkCTreeNode *parent)
380 g_return_if_fail(mimeinfo != NULL);
382 if (mimeinfo->children)
383 mimeinfo = mimeinfo->children;
385 while (mimeinfo != NULL) {
386 node = mimeview_append_part(mimeview, mimeinfo, parent);
388 if (mimeinfo->children)
389 mimeview_set_multipart_tree(mimeview, mimeinfo, node);
390 else if (mimeinfo->sub &&
391 mimeinfo->sub->mime_type != MIME_TEXT &&
392 mimeinfo->sub->mime_type != MIME_TEXT_HTML)
393 mimeview_set_multipart_tree(mimeview, mimeinfo->sub,
395 mimeinfo = mimeinfo->next;
399 static gchar *get_part_name(MimeInfo *partinfo)
402 if (partinfo->sigstatus)
403 return partinfo->sigstatus;
407 return partinfo->name;
408 else if (partinfo->filename)
409 return partinfo->filename;
410 else if (partinfo->description)
411 return partinfo->description;
416 static gchar *get_part_description(MimeInfo *partinfo)
418 if (partinfo->description)
419 return partinfo->description;
420 else if (partinfo->name)
421 return partinfo->name;
422 else if (partinfo->filename)
423 return partinfo->filename;
428 static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
430 GtkCTreeNode *parent)
432 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
434 gchar *str[N_MIMEVIEW_COLS];
437 partinfo->content_type ? partinfo->content_type : "";
438 str[COL_SIZE] = to_human_readable(partinfo->size);
439 if (prefs_common.attach_desc)
440 str[COL_NAME] = get_part_description(partinfo);
442 str[COL_NAME] = get_part_name(partinfo);
444 node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
445 NULL, NULL, NULL, NULL,
447 gtk_ctree_node_set_row_data(ctree, node, partinfo);
452 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
460 if (!partinfo) return;
463 for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
465 fname = pi ? pi->plaintextfile : mimeview->file;
467 fname = mimeview->file;
468 #endif /* USE_GPGME */
471 if ((fp = fopen(fname, "rb")) == NULL) {
472 FILE_OP_ERROR(fname, "fopen");
476 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
477 FILE_OP_ERROR(mimeview->file, "fseek");
482 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
483 textview_show_part(mimeview->textview, partinfo, fp);
488 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
491 MimeViewerFactory *factory = NULL;
492 MimeViewer *viewer = NULL;
495 * FNM_CASEFOLD is a GNU extension
496 * if its not defined copy the string to the stack and
497 * convert the copy to lower case
500 #define FNM_CASEFOLD 0
501 Xstrdup_a(content_type, content_type, return NULL);
502 g_strdown((gchar *)content_type);
505 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
506 MimeViewerFactory *curfactory = cur->data;
509 while (curfactory->content_types[i] != NULL) {
510 debug_print("%s\n", curfactory->content_types[i]);
511 if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
512 factory = curfactory;
523 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
524 MimeViewer *curviewer = cur->data;
526 if (curviewer->factory == factory)
529 viewer = factory->create_viewer();
530 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
535 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
537 gchar *content_type = NULL;
538 MimeViewer *viewer = NULL;
540 if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
541 (partinfo->name != NULL)) {
542 content_type = procmime_get_mime_type(partinfo->name);
544 content_type = g_strdup(partinfo->content_type);
547 if (content_type != NULL) {
548 viewer = get_viewer_for_content_type(mimeview, content_type);
549 g_free(content_type);
555 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
559 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
560 if (viewer == NULL) {
561 if (mimeview->mimeviewer != NULL)
562 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
563 mimeview->mimeviewer = NULL;
567 if (mimeview->mimeviewer != viewer) {
568 if (mimeview->mimeviewer != NULL)
569 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
570 mimeview->mimeviewer = viewer;
571 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
573 viewer->show_mimepart(viewer, mimeview->file, partinfo);
578 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
580 TextView *textview = mimeview->textview;
583 if ((mimeview->type != MIMEVIEW_VIEWER) &&
584 (mimeview->type == type)) return;
586 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
588 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
589 GTK_WIDGET(children->data));
590 g_list_free(children);
595 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
596 GTK_WIDGET_PTR(textview));
598 case MIMEVIEW_VIEWER:
599 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
600 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
606 mimeview->type = type;
609 static void mimeview_clear(MimeView *mimeview)
611 GtkCList *clist = GTK_CLIST(mimeview->ctree);
613 procmime_mimeinfo_free_all(mimeview->mimeinfo);
614 mimeview->mimeinfo = NULL;
616 gtk_clist_clear(clist);
617 textview_clear(mimeview->textview);
618 if (mimeview->mimeviewer != NULL)
619 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
621 mimeview->opened = NULL;
623 g_free(mimeview->file);
624 mimeview->file = NULL;
626 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
629 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
634 if (mimeview->opened == node) return;
635 mimeview->opened = node;
636 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
638 partinfo = gtk_ctree_node_get_row_data(ctree, node);
639 if (!partinfo) return;
641 /* ungrab the mouse event */
642 if (GTK_WIDGET_HAS_GRAB(ctree)) {
643 gtk_grab_remove(GTK_WIDGET(ctree));
644 if (gdk_pointer_is_grabbed())
645 gdk_pointer_ungrab(GDK_CURRENT_TIME);
648 mimeview->textview->default_text = FALSE;
650 if (!mimeview_show_part(mimeview, partinfo)) {
651 switch (partinfo->mime_type) {
654 case MIME_TEXT_ENRICHED:
655 case MIME_MESSAGE_RFC822:
657 mimeview_show_message_part(mimeview, partinfo);
661 mimeview->textview->default_text = TRUE;
662 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
664 if (g_strcasecmp(partinfo->content_type,
665 "application/pgp-signature") == 0)
666 textview_show_signature_part(mimeview->textview,
670 textview_show_mime_part(mimeview->textview, partinfo);
676 static void mimeview_start_drag(GtkWidget *widget, gint button,
677 GdkEvent *event, MimeView *mimeview)
680 GdkDragContext *context;
683 g_return_if_fail(mimeview != NULL);
685 partinfo = mimeview_get_selected_part(mimeview);
686 if (partinfo->filename == NULL && partinfo->name == NULL) return;
688 list = gtk_target_list_new(mimeview_mime_types, 1);
689 context = gtk_drag_begin(widget, list,
690 GDK_ACTION_COPY, button, event);
691 gtk_drag_set_icon_default(context);
694 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
697 GtkCList *clist = GTK_CLIST(widget);
701 if (!event) return FALSE;
703 if (event->button == 2 || event->button == 3) {
704 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
707 gtk_clist_unselect_all(clist);
708 gtk_clist_select_row(clist, row, column);
709 gtkut_clist_set_focus_row(clist, row);
712 if (event->button == 2 ||
713 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
714 /* call external program for image, audio or html */
715 mimeview_launch(mimeview);
716 } else if (event->button == 3) {
717 partinfo = mimeview_get_selected_part(mimeview);
718 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
719 partinfo->mime_type == MIME_TEXT_HTML ||
720 partinfo->mime_type == MIME_TEXT_ENRICHED ||
721 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
722 partinfo->mime_type == MIME_IMAGE ||
723 partinfo->mime_type == MIME_MULTIPART))
724 menu_set_sensitive(mimeview->popupfactory,
725 "/Display as text", FALSE);
727 menu_set_sensitive(mimeview->popupfactory,
728 "/Display as text", TRUE);
730 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
731 menu_set_sensitive(mimeview->popupfactory,
734 menu_set_sensitive(mimeview->popupfactory,
738 menu_set_sensitive(mimeview->popupfactory,
740 mimeview_is_signed(mimeview));
743 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
744 NULL, NULL, NULL, NULL,
745 event->button, event->time);
751 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
753 mimeview_key_pressed(mimeview->ctree, event, mimeview);
756 #define BREAK_ON_MODIFIER_KEY() \
757 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
759 #define KEY_PRESS_EVENT_STOP() \
760 if (gtk_signal_n_emissions_by_name \
761 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
762 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
763 "key_press_event"); \
766 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
769 SummaryView *summaryview;
770 GtkCTree *ctree = GTK_CTREE(widget);
773 if (!event) return FALSE;
774 if (!mimeview->opened) return FALSE;
776 switch (event->keyval) {
778 if (textview_scroll_page(mimeview->textview, FALSE))
781 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
783 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
784 gtk_sctree_select(GTK_SCTREE(ctree), node);
789 textview_scroll_page(mimeview->textview, TRUE);
792 textview_scroll_one_line(mimeview->textview,
793 (event->state & GDK_MOD1_MASK) != 0);
797 BREAK_ON_MODIFIER_KEY();
798 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
799 KEY_PRESS_EVENT_STOP();
801 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
802 GTK_SCROLL_STEP_FORWARD, 0.0);
806 BREAK_ON_MODIFIER_KEY();
807 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
808 KEY_PRESS_EVENT_STOP();
810 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
811 GTK_SCROLL_STEP_BACKWARD, 0.0);
814 BREAK_ON_MODIFIER_KEY();
815 KEY_PRESS_EVENT_STOP();
816 mimeview_save_as(mimeview);
819 BREAK_ON_MODIFIER_KEY();
820 KEY_PRESS_EVENT_STOP();
821 mimeview_display_as_text(mimeview);
824 BREAK_ON_MODIFIER_KEY();
825 KEY_PRESS_EVENT_STOP();
826 mimeview_launch(mimeview);
832 if (!mimeview->messageview->mainwin) return FALSE;
833 summaryview = mimeview->messageview->mainwin->summaryview;
834 summary_pass_key_press_event(summaryview, event);
838 static void mimeview_drag_data_get(GtkWidget *widget,
839 GdkDragContext *drag_context,
840 GtkSelectionData *selection_data,
845 gchar *filename, *uriname;
848 if (!mimeview->opened) return;
849 if (!mimeview->file) return;
851 partinfo = mimeview_get_selected_part(mimeview);
852 if (!partinfo) return;
853 if (!partinfo->filename && !partinfo->name) return;
855 filename = partinfo->filename ? partinfo->filename : partinfo->name;
856 filename = g_basename(filename);
857 if (*filename == '\0') return;
859 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
862 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
864 (_("Can't save the part of multipart message."));
866 uriname = g_strconcat("file:/", filename, NULL);
867 gtk_selection_data_set(selection_data, selection_data->target, 8,
868 uriname, strlen(uriname));
874 static void mimeview_save_all(MimeView *mimeview)
877 gchar *defname = NULL;
879 MimeInfo *attachment;
882 if (!mimeview->opened) return;
883 if (!mimeview->file) return;
885 partinfo = mimeview_get_selected_part(mimeview);
886 g_return_if_fail(partinfo != NULL);
888 dirname = filesel_select_file(_("Save as"), defname);
889 if (!dirname) return;
891 /* return to first children */
892 if (!partinfo->parent->children) return; /* multipart container? */
893 attachment = partinfo->parent->children->next;
894 /* for each attachment, extract it in the selected dir. */
895 while (attachment != NULL) {
896 static guint subst_cnt = 1;
898 gchar *attachname = g_strdup(get_part_name(attachment));
899 AlertValue aval = G_ALERTDEFAULT;
902 if (!attachname || !strlen(attachname))
903 attachname = g_strdup_printf("noname.%d",subst_cnt++);
904 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
905 g_snprintf(buf, sizeof(buf), "%s%s",
907 (attachname[0] == G_DIR_SEPARATOR)
910 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
911 attachdir = g_dirname(buf);
912 make_dir_hier(attachdir);
915 if (is_file_exist(buf)) {
916 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
918 aval = alertpanel(_("Overwrite"), res, _("OK"),
924 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
925 alertpanel_error(_("Can't save the part of multipart message."));
926 attachment = attachment->next;
930 static void mimeview_display_as_text(MimeView *mimeview)
934 if (!mimeview->opened) return;
936 partinfo = mimeview_get_selected_part(mimeview);
937 g_return_if_fail(partinfo != NULL);
938 mimeview_show_message_part(mimeview, partinfo);
941 static void mimeview_save_as(MimeView *mimeview)
944 gchar *defname = NULL;
948 if (!mimeview->opened) return;
949 if (!mimeview->file) return;
951 partinfo = mimeview_get_selected_part(mimeview);
952 g_return_if_fail(partinfo != NULL);
954 if (partinfo->filename)
955 defname = partinfo->filename;
956 else if (partinfo->name) {
957 Xstrdup_a(defname, partinfo->name, return);
958 subst_for_filename(defname);
961 filename = filesel_select_file(_("Save as"), defname);
962 if (!filename) return;
963 if (is_file_exist(filename)) {
965 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
967 aval = alertpanel(_("Overwrite"), res, _("OK"),
970 if (G_ALERTDEFAULT != aval) return;
973 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
975 (_("Can't save the part of multipart message."));
978 static void mimeview_launch(MimeView *mimeview)
983 if (!mimeview->opened) return;
984 if (!mimeview->file) return;
986 partinfo = mimeview_get_selected_part(mimeview);
987 g_return_if_fail(partinfo != NULL);
989 filename = procmime_get_tmp_file_name(partinfo);
991 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
993 (_("Can't save the part of multipart message."));
995 mimeview_view_file(filename, partinfo, NULL);
1000 static void mimeview_open_with(MimeView *mimeview)
1006 if (!mimeview->opened) return;
1007 if (!mimeview->file) return;
1009 partinfo = mimeview_get_selected_part(mimeview);
1010 g_return_if_fail(partinfo != NULL);
1012 filename = procmime_get_tmp_file_name(partinfo);
1014 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
1016 (_("Can't save the part of multipart message."));
1021 if (!prefs_common.mime_open_cmd_history)
1022 prefs_common.mime_open_cmd_history =
1023 add_history(NULL, prefs_common.mime_open_cmd);
1025 cmd = input_dialog_combo
1027 _("Enter the command line to open file:\n"
1028 "(`%s' will be replaced with file name)"),
1029 prefs_common.mime_open_cmd,
1030 prefs_common.mime_open_cmd_history,
1033 mimeview_view_file(filename, partinfo, cmd);
1034 g_free(prefs_common.mime_open_cmd);
1035 prefs_common.mime_open_cmd = cmd;
1036 prefs_common.mime_open_cmd_history =
1037 add_history(prefs_common.mime_open_cmd_history, cmd);
1043 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1044 const gchar *cmdline)
1046 static gchar *default_image_cmdline = "display '%s'";
1047 static gchar *default_audio_cmdline = "play '%s'";
1048 static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1049 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1053 const gchar *def_cmd;
1059 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1061 } else if (MIME_IMAGE == partinfo->mime_type) {
1062 cmd = prefs_common.mime_image_viewer;
1063 def_cmd = default_image_cmdline;
1064 } else if (MIME_AUDIO == partinfo->mime_type) {
1065 cmd = prefs_common.mime_audio_player;
1066 def_cmd = default_audio_cmdline;
1067 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1068 cmd = prefs_common.uri_cmd;
1069 def_cmd = default_html_cmdline;
1071 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1072 partinfo->content_type, "%s");
1077 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1078 !strchr(p + 2, '%'))
1079 g_snprintf(buf, sizeof(buf), cmd, filename);
1082 g_warning("MIME viewer command line is invalid: `%s'", cmd);
1084 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1089 execute_command_line(buf, TRUE);
1093 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1099 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1100 g_return_if_fail(partinfo != NULL);
1102 part_name = get_part_name(partinfo);
1103 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1106 static void mimeview_update_names(MimeView *mimeview)
1108 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1110 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1113 static void mimeview_update_signature_info(MimeView *mimeview)
1117 if (!mimeview) return;
1118 if (!mimeview->opened) return;
1120 partinfo = mimeview_get_selected_part(mimeview);
1121 if (!partinfo) return;
1123 if (g_strcasecmp(partinfo->content_type,
1124 "application/pgp-signature") == 0) {
1125 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1126 textview_show_signature_part(mimeview->textview, partinfo);
1130 void mimeview_check_signature(MimeView *mimeview)
1135 g_return_if_fail (mimeview_is_signed(mimeview));
1136 g_return_if_fail (gpg_started);
1138 mimeinfo = gtk_ctree_node_get_row_data
1139 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1140 g_return_if_fail(mimeinfo != NULL);
1141 g_return_if_fail(mimeview->file != NULL);
1143 while (mimeinfo->parent)
1144 mimeinfo = mimeinfo->parent;
1146 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1147 FILE_OP_ERROR(mimeview->file, "fopen");
1151 rfc2015_check_signature(mimeinfo, fp);
1154 mimeview_update_names(mimeview);
1155 mimeview_update_signature_info(mimeview);
1157 textview_show_message(mimeview->messageview->textview, mimeinfo,
1160 #endif /* USE_GPGME */
1162 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1164 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1167 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1169 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1172 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1174 GSList *mimeview_list, *viewer_list;
1176 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1177 MimeView *mimeview = (MimeView *) mimeview_list->data;
1179 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1180 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1181 mimeview->mimeviewer = NULL;
1184 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1185 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1187 mimeviewer->destroy_viewer(mimeviewer);
1188 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1192 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);