2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtknotebook.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkctree.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkvpaned.h>
33 #include <gtk/gtktext.h>
34 #include <gtk/gtksignal.h>
35 #include <gtk/gtkmenu.h>
36 #include <gtk/gtkdnd.h>
37 #include <gtk/gtkselection.h>
47 #include "summaryview.h"
50 #include "alertpanel.h"
51 #include "inputdialog.h"
54 #include "prefs_common.h"
65 #define N_MIMEVIEW_COLS 3
67 static void mimeview_set_multipart_tree (MimeView *mimeview,
69 GtkCTreeNode *parent);
70 static GtkCTreeNode *mimeview_append_part (MimeView *mimeview,
72 GtkCTreeNode *parent);
73 static void mimeview_show_message_part (MimeView *mimeview,
75 static void mimeview_change_view_type (MimeView *mimeview,
77 static void mimeview_clear (MimeView *mimeview);
79 static void mimeview_selected (GtkCTree *ctree,
83 static void mimeview_start_drag (GtkWidget *widget,
87 static gint mimeview_button_pressed (GtkWidget *widget,
88 GdkEventButton *event,
90 static gint mimeview_key_pressed (GtkWidget *widget,
94 static void mimeview_drag_data_get (GtkWidget *widget,
95 GdkDragContext *drag_context,
96 GtkSelectionData *selection_data,
101 static void mimeview_display_as_text (MimeView *mimeview);
102 static void mimeview_save_as (MimeView *mimeview);
103 static void mimeview_save_all (MimeView *mimeview);
104 static void mimeview_launch (MimeView *mimeview);
105 static void mimeview_open_with (MimeView *mimeview);
106 static void mimeview_view_file (const gchar *filename,
108 const gchar *cmdline);
110 static GtkItemFactoryEntry mimeview_popup_entries[] =
112 {N_("/_Open"), NULL, mimeview_launch, 0, NULL},
113 {N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
114 {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
115 {N_("/_Save as..."), NULL, mimeview_save_as, 0, NULL},
116 {N_("/Save _all..."), NULL, mimeview_save_all, 0, NULL}
119 {N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
123 static GtkTargetEntry mimeview_mime_types[] =
125 {"text/uri-list", 0, 0}
128 GSList *mimeviewer_factories;
131 MimeView *mimeview_create(void)
138 GtkWidget *scrolledwin;
140 GtkWidget *mime_vbox;
141 GtkWidget *popupmenu;
142 GtkItemFactory *popupfactory;
143 gchar *titles[N_MIMEVIEW_COLS];
147 debug_print("Creating MIME view...\n");
148 mimeview = g_new0(MimeView, 1);
150 titles[COL_MIMETYPE] = _("MIME Type");
151 titles[COL_SIZE] = _("Size");
152 titles[COL_NAME] = _("Name");
154 notebook = gtk_notebook_new();
155 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
157 vbox = gtk_vbox_new(FALSE, 0);
158 gtk_container_add(GTK_CONTAINER(notebook), vbox);
159 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), vbox,
162 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
163 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
164 GTK_POLICY_AUTOMATIC,
166 gtk_widget_set_usize(scrolledwin, -1, 80);
168 ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
169 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
170 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
171 gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
173 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
174 gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 64);
175 for (i = 0; i < N_MIMEVIEW_COLS; i++)
176 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
178 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
180 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
181 GTK_SIGNAL_FUNC(mimeview_selected), mimeview);
182 gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
183 GTK_SIGNAL_FUNC(mimeview_button_pressed), mimeview);
184 gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
185 GTK_SIGNAL_FUNC(mimeview_key_pressed), mimeview);
186 gtk_signal_connect(GTK_OBJECT (ctree),"start_drag",
187 GTK_SIGNAL_FUNC (mimeview_start_drag), mimeview);
188 gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
189 GTK_SIGNAL_FUNC(mimeview_drag_data_get), mimeview);
191 mime_vbox = gtk_vbox_new(FALSE, 0);
193 paned = gtk_vpaned_new();
194 gtk_paned_add1(GTK_PANED(paned), scrolledwin);
195 gtk_paned_add2(GTK_PANED(paned), mime_vbox);
196 gtk_container_add(GTK_CONTAINER(notebook), paned);
197 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), paned,
200 gtk_widget_show_all(notebook);
202 gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
204 n_entries = sizeof(mimeview_popup_entries) /
205 sizeof(mimeview_popup_entries[0]);
206 popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
207 "<MimeView>", &popupfactory, mimeview);
209 mimeview->notebook = notebook;
210 mimeview->vbox = vbox;
211 mimeview->paned = paned;
212 mimeview->scrolledwin = scrolledwin;
213 mimeview->ctree = ctree;
214 mimeview->mime_vbox = mime_vbox;
215 mimeview->popupmenu = popupmenu;
216 mimeview->popupfactory = popupfactory;
219 mimeviews = g_slist_prepend(mimeviews, mimeview);
224 void mimeview_init(MimeView *mimeview)
229 * Check whether the message is OpenPGP signed
232 static gboolean mimeview_is_signed(MimeView *mimeview)
234 MimeInfo *partinfo = NULL;
236 debug_print("mimeview_is signed of %p\n", mimeview);
238 if (!mimeview) return FALSE;
239 if (!mimeview->opened) return FALSE;
241 debug_print("mimeview_is_signed: open\n" );
243 if (!mimeview->file) return FALSE;
245 debug_print("mimeview_is_signed: file\n" );
247 partinfo = gtk_ctree_node_get_row_data
248 (GTK_CTREE(mimeview->ctree), mimeview->opened);
249 g_return_val_if_fail(partinfo != NULL, FALSE);
251 /* walk the tree and see whether there is a signature somewhere */
253 if (rfc2015_has_signature(partinfo))
255 } while ((partinfo = partinfo->parent) != NULL);
257 debug_print("mimeview_is_signed: FALSE\n" );
262 static void set_unchecked_signature(MimeInfo *mimeinfo)
264 MimeInfo *sig_partinfo;
266 sig_partinfo = rfc2015_find_signature(mimeinfo);
267 if (sig_partinfo == NULL) return;
269 g_free(sig_partinfo->sigstatus);
270 sig_partinfo->sigstatus =
271 g_strdup(_("Select \"Check signature\" to check"));
273 g_free(sig_partinfo->sigstatus_full);
274 sig_partinfo->sigstatus_full = NULL;
276 #endif /* USE_GPGME */
278 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
281 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
286 mimeview_clear(mimeview);
287 textview_clear(mimeview->messageview->textview);
289 g_return_if_fail(file != NULL);
290 g_return_if_fail(mimeinfo != NULL);
292 mimeview->mimeinfo = mimeinfo;
294 if ((fp = fopen(file, "rb")) == NULL) {
295 FILE_OP_ERROR(file, "fopen");
298 mimeview->file = g_strdup(file);
300 /* skip MIME part headers */
301 if (mimeinfo->mime_type == MIME_MULTIPART) {
302 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
304 while (fgets(buf, sizeof(buf), fp) != NULL)
305 if (buf[0] == '\r' || buf[0] == '\n') break;
308 procmime_scan_multipart_message(mimeinfo, fp);
310 if ((prefs_common.auto_check_signatures)
312 rfc2015_check_signature(mimeinfo, fp);
314 set_unchecked_signature(mimeinfo);
317 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree), mimeview_selected,
320 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
322 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
323 mimeview_selected, mimeview);
325 /* search first text part */
326 for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
327 node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
330 partinfo = gtk_ctree_node_get_row_data(ctree, node);
332 (partinfo->mime_type == MIME_TEXT ||
333 partinfo->mime_type == MIME_TEXT_HTML))
337 textview_show_message(mimeview->messageview->textview, mimeinfo, file);
340 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
343 gtk_ctree_select(ctree, node);
344 gtkut_ctree_set_focus_row(ctree, node);
345 gtk_widget_grab_focus(mimeview->ctree);
349 void mimeview_destroy(MimeView *mimeview)
353 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
354 MimeViewer *viewer = (MimeViewer *) cur->data;
355 viewer->destroy_viewer(viewer);
357 g_slist_free(mimeview->viewers);
359 procmime_mimeinfo_free_all(mimeview->mimeinfo);
360 g_free(mimeview->file);
363 mimeviews = g_slist_remove(mimeviews, mimeview);
366 static void mimeview_set_multipart_tree(MimeView *mimeview,
368 GtkCTreeNode *parent)
370 GtkCTreeNode *current = parent;
372 g_return_if_fail(mimeinfo != NULL);
374 if (!mimeinfo->sub && mimeinfo->parent)
375 current = mimeview_append_part(mimeview, mimeinfo, parent);
376 if (mimeinfo->sub && !mimeinfo->sub->children &&
377 mimeinfo->sub->mime_type != MIME_TEXT &&
378 mimeinfo->sub->mime_type != MIME_TEXT_HTML) {
379 mimeview_append_part(mimeview, mimeinfo->sub, parent);
384 mimeview_set_multipart_tree(mimeview, mimeinfo->sub, current);
386 if (mimeinfo->children)
387 mimeview_set_multipart_tree(mimeview, mimeinfo->children, current);
390 mimeview_set_multipart_tree(mimeview, mimeinfo->next, parent);
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;
488 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
489 MimeViewerFactory *curfactory = cur->data;
491 if(!fnmatch(curfactory->content_type, content_type, 0)) {
492 factory = curfactory;
499 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
500 MimeViewer *curviewer = cur->data;
502 if (curviewer->factory == factory)
505 viewer = factory->create_viewer();
506 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
511 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
513 gchar *content_type = NULL;
514 MimeViewer *viewer = NULL;
516 if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
517 (partinfo->name != NULL)) {
518 content_type = procmime_get_mime_type(partinfo->name);
520 content_type = g_strdup(partinfo->content_type);
523 if (content_type != NULL) {
524 viewer = get_viewer_for_content_type(mimeview, content_type);
525 g_free(content_type);
531 static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
535 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
536 if (viewer == NULL) {
537 if (mimeview->mimeviewer != NULL)
538 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
539 mimeview->mimeviewer = NULL;
543 if (mimeview->mimeviewer != viewer) {
544 if (mimeview->mimeviewer != NULL)
545 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
546 mimeview->mimeviewer = viewer;
547 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
549 viewer->show_mimepart(viewer, mimeview->file, partinfo);
554 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
556 TextView *textview = mimeview->textview;
559 if ((mimeview->type != MIMEVIEW_VIEWER) &&
560 (mimeview->type == type)) return;
562 children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
564 gtkut_container_remove(GTK_CONTAINER(mimeview->mime_vbox),
565 GTK_WIDGET(children->data));
566 g_list_free(children);
571 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
572 GTK_WIDGET_PTR(textview));
574 case MIMEVIEW_VIEWER:
575 gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
576 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
582 mimeview->type = type;
585 static void mimeview_clear(MimeView *mimeview)
587 GtkCList *clist = GTK_CLIST(mimeview->ctree);
589 procmime_mimeinfo_free_all(mimeview->mimeinfo);
590 mimeview->mimeinfo = NULL;
592 gtk_clist_clear(clist);
593 textview_clear(mimeview->textview);
594 if (mimeview->mimeviewer != NULL)
595 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
597 mimeview->opened = NULL;
599 g_free(mimeview->file);
600 mimeview->file = NULL;
602 /* gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0); */
605 static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
610 if (mimeview->opened == node) return;
611 mimeview->opened = node;
612 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
614 partinfo = gtk_ctree_node_get_row_data(ctree, node);
615 if (!partinfo) return;
617 /* ungrab the mouse event */
618 if (GTK_WIDGET_HAS_GRAB(ctree)) {
619 gtk_grab_remove(GTK_WIDGET(ctree));
620 if (gdk_pointer_is_grabbed())
621 gdk_pointer_ungrab(GDK_CURRENT_TIME);
624 mimeview->textview->default_text = FALSE;
626 if (!mimeview_show_part(mimeview, partinfo)) {
627 switch (partinfo->mime_type) {
630 case MIME_TEXT_ENRICHED:
631 case MIME_MESSAGE_RFC822:
633 mimeview_show_message_part(mimeview, partinfo);
637 mimeview->textview->default_text = TRUE;
638 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
640 if (g_strcasecmp(partinfo->content_type,
641 "application/pgp-signature") == 0)
642 textview_show_signature_part(mimeview->textview,
646 textview_show_mime_part(mimeview->textview, partinfo);
652 static void mimeview_start_drag(GtkWidget *widget, gint button,
653 GdkEvent *event, MimeView *mimeview)
656 GdkDragContext *context;
660 g_return_if_fail(mimeview != NULL);
661 ctree = GTK_CTREE(mimeview->ctree);
662 partinfo = gtk_ctree_node_get_row_data(ctree, mimeview->opened);
663 if (partinfo->filename == NULL && partinfo->name == NULL) return;
665 list = gtk_target_list_new(mimeview_mime_types, 1);
666 context = gtk_drag_begin(widget, list,
667 GDK_ACTION_COPY, button, event);
668 gtk_drag_set_icon_default(context);
671 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
674 GtkCList *clist = GTK_CLIST(widget);
678 if (!event) return FALSE;
680 if (event->button == 2 || event->button == 3) {
681 if (!gtk_clist_get_selection_info(clist, event->x, event->y,
684 gtk_clist_unselect_all(clist);
685 gtk_clist_select_row(clist, row, column);
686 gtkut_clist_set_focus_row(clist, row);
689 if (event->button == 2 ||
690 (event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
691 /* call external program for image, audio or html */
692 mimeview_launch(mimeview);
693 } else if (event->button == 3) {
694 partinfo = gtk_ctree_node_get_row_data
695 (GTK_CTREE(mimeview->ctree), mimeview->opened);
696 if (partinfo && (partinfo->mime_type == MIME_TEXT ||
697 partinfo->mime_type == MIME_TEXT_HTML ||
698 partinfo->mime_type == MIME_TEXT_ENRICHED ||
699 partinfo->mime_type == MIME_MESSAGE_RFC822 ||
700 partinfo->mime_type == MIME_IMAGE ||
701 partinfo->mime_type == MIME_MULTIPART))
702 menu_set_sensitive(mimeview->popupfactory,
703 "/Display as text", FALSE);
705 menu_set_sensitive(mimeview->popupfactory,
706 "/Display as text", TRUE);
708 partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
709 menu_set_sensitive(mimeview->popupfactory,
712 menu_set_sensitive(mimeview->popupfactory,
716 menu_set_sensitive(mimeview->popupfactory,
718 mimeview_is_signed(mimeview));
721 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
722 NULL, NULL, NULL, NULL,
723 event->button, event->time);
729 void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
731 mimeview_key_pressed(mimeview->ctree, event, mimeview);
734 #define BREAK_ON_MODIFIER_KEY() \
735 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
737 #define KEY_PRESS_EVENT_STOP() \
738 if (gtk_signal_n_emissions_by_name \
739 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
740 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
741 "key_press_event"); \
744 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
747 SummaryView *summaryview;
748 GtkCTree *ctree = GTK_CTREE(widget);
751 if (!event) return FALSE;
752 if (!mimeview->opened) return FALSE;
754 switch (event->keyval) {
756 if (textview_scroll_page(mimeview->textview, FALSE))
759 node = GTK_CTREE_NODE_NEXT(mimeview->opened);
761 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
762 gtk_sctree_select(GTK_SCTREE(ctree), node);
767 textview_scroll_page(mimeview->textview, TRUE);
770 textview_scroll_one_line(mimeview->textview,
771 (event->state & GDK_MOD1_MASK) != 0);
775 BREAK_ON_MODIFIER_KEY();
776 if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
777 KEY_PRESS_EVENT_STOP();
779 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
780 GTK_SCROLL_STEP_FORWARD, 0.0);
784 BREAK_ON_MODIFIER_KEY();
785 if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
786 KEY_PRESS_EVENT_STOP();
788 gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
789 GTK_SCROLL_STEP_BACKWARD, 0.0);
792 BREAK_ON_MODIFIER_KEY();
793 KEY_PRESS_EVENT_STOP();
794 mimeview_save_as(mimeview);
797 BREAK_ON_MODIFIER_KEY();
798 KEY_PRESS_EVENT_STOP();
799 mimeview_display_as_text(mimeview);
802 BREAK_ON_MODIFIER_KEY();
803 KEY_PRESS_EVENT_STOP();
804 mimeview_launch(mimeview);
810 if (!mimeview->messageview->mainwin) return FALSE;
811 summaryview = mimeview->messageview->mainwin->summaryview;
812 summary_pass_key_press_event(summaryview, event);
816 static void mimeview_drag_data_get(GtkWidget *widget,
817 GdkDragContext *drag_context,
818 GtkSelectionData *selection_data,
823 gchar *filename, *uriname;
826 if (!mimeview->opened) return;
827 if (!mimeview->file) return;
829 partinfo = gtk_ctree_node_get_row_data
830 (GTK_CTREE(mimeview->ctree), mimeview->opened);
831 if (!partinfo) return;
832 if (!partinfo->filename && !partinfo->name) return;
834 filename = partinfo->filename ? partinfo->filename : partinfo->name;
835 filename = g_basename(filename);
836 if (*filename == '\0') return;
838 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
841 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
843 (_("Can't save the part of multipart message."));
845 uriname = g_strconcat("file:/", filename, NULL);
846 gtk_selection_data_set(selection_data, selection_data->target, 8,
847 uriname, strlen(uriname));
853 static void mimeview_save_all(MimeView *mimeview)
856 gchar *defname = NULL;
858 MimeInfo *attachment;
861 if (!mimeview->opened) return;
862 if (!mimeview->file) return;
864 partinfo = gtk_ctree_node_get_row_data
865 (GTK_CTREE(mimeview->ctree), mimeview->opened);
866 g_return_if_fail(partinfo != NULL);
868 dirname = filesel_select_file(_("Save as"), defname);
869 if (!dirname) return;
871 /* return to first children */
872 if (!partinfo->parent->children) return; /* multipart container? */
873 attachment = partinfo->parent->children->next;
874 /* for each attachment, extract it in the selected dir. */
875 while (attachment != NULL) {
876 static guint subst_cnt = 1;
878 gchar *attachname = g_strdup(get_part_name(attachment));
879 AlertValue aval = G_ALERTDEFAULT;
882 if (!attachname || !strlen(attachname))
883 attachname = g_strdup_printf("noname.%d",subst_cnt++);
884 subst_chars(attachname, ":?*&|<>\t\r\n", '_');
885 g_snprintf(buf, sizeof(buf), "%s%s",
887 (attachname[0] == G_DIR_SEPARATOR)
890 subst_chars(buf, "/\\", G_DIR_SEPARATOR);
891 attachdir = g_dirname(buf);
892 make_dir_hier(attachdir);
895 if (is_file_exist(buf)) {
896 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
898 aval = alertpanel(_("Overwrite"), res, _("OK"),
904 if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
905 alertpanel_error(_("Can't save the part of multipart message."));
906 attachment = attachment->next;
910 static void mimeview_display_as_text(MimeView *mimeview)
914 if (!mimeview->opened) return;
916 partinfo = gtk_ctree_node_get_row_data
917 (GTK_CTREE(mimeview->ctree), mimeview->opened);
918 mimeview_show_message_part(mimeview, partinfo);
921 static void mimeview_save_as(MimeView *mimeview)
924 gchar *defname = NULL;
928 if (!mimeview->opened) return;
929 if (!mimeview->file) return;
931 partinfo = gtk_ctree_node_get_row_data
932 (GTK_CTREE(mimeview->ctree), mimeview->opened);
933 g_return_if_fail(partinfo != NULL);
935 if (partinfo->filename)
936 defname = partinfo->filename;
937 else if (partinfo->name) {
938 Xstrdup_a(defname, partinfo->name, return);
939 subst_for_filename(defname);
942 filename = filesel_select_file(_("Save as"), defname);
943 if (!filename) return;
944 if (is_file_exist(filename)) {
946 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
948 aval = alertpanel(_("Overwrite"), res, _("OK"),
951 if (G_ALERTDEFAULT != aval) return;
954 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
956 (_("Can't save the part of multipart message."));
959 static void mimeview_launch(MimeView *mimeview)
964 if (!mimeview->opened) return;
965 if (!mimeview->file) return;
967 partinfo = gtk_ctree_node_get_row_data
968 (GTK_CTREE(mimeview->ctree), mimeview->opened);
969 g_return_if_fail(partinfo != NULL);
971 filename = procmime_get_tmp_file_name(partinfo);
973 if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
975 (_("Can't save the part of multipart message."));
977 mimeview_view_file(filename, partinfo, NULL);
982 static void mimeview_open_with(MimeView *mimeview)
988 if (!mimeview->opened) return;
989 if (!mimeview->file) return;
991 partinfo = gtk_ctree_node_get_row_data
992 (GTK_CTREE(mimeview->ctree), mimeview->opened);
993 g_return_if_fail(partinfo != NULL);
995 filename = procmime_get_tmp_file_name(partinfo);
997 if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
999 (_("Can't save the part of multipart message."));
1004 if (!prefs_common.mime_open_cmd_history)
1005 prefs_common.mime_open_cmd_history =
1006 add_history(NULL, prefs_common.mime_open_cmd);
1008 cmd = input_dialog_combo
1010 _("Enter the command line to open file:\n"
1011 "(`%s' will be replaced with file name)"),
1012 prefs_common.mime_open_cmd,
1013 prefs_common.mime_open_cmd_history,
1016 mimeview_view_file(filename, partinfo, cmd);
1017 g_free(prefs_common.mime_open_cmd);
1018 prefs_common.mime_open_cmd = cmd;
1019 prefs_common.mime_open_cmd_history =
1020 add_history(prefs_common.mime_open_cmd_history, cmd);
1026 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
1027 const gchar *cmdline)
1029 static gchar *default_image_cmdline = "display '%s'";
1030 static gchar *default_audio_cmdline = "play '%s'";
1031 static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
1032 static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
1036 const gchar *def_cmd;
1042 } else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
1044 } else if (MIME_IMAGE == partinfo->mime_type) {
1045 cmd = prefs_common.mime_image_viewer;
1046 def_cmd = default_image_cmdline;
1047 } else if (MIME_AUDIO == partinfo->mime_type) {
1048 cmd = prefs_common.mime_audio_player;
1049 def_cmd = default_audio_cmdline;
1050 } else if (MIME_TEXT_HTML == partinfo->mime_type) {
1051 cmd = prefs_common.uri_cmd;
1052 def_cmd = default_html_cmdline;
1054 g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
1055 partinfo->content_type, "%s");
1060 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
1061 !strchr(p + 2, '%'))
1062 g_snprintf(buf, sizeof(buf), cmd, filename);
1065 g_warning("MIME viewer command line is invalid: `%s'", cmd);
1067 g_snprintf(buf, sizeof(buf), def_cmd, filename);
1072 execute_command_line(buf, TRUE);
1076 static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
1082 partinfo = gtk_ctree_node_get_row_data(ctree, node);
1083 g_return_if_fail(partinfo != NULL);
1085 part_name = get_part_name(partinfo);
1086 gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
1089 static void mimeview_update_names(MimeView *mimeview)
1091 GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
1093 gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
1096 static void mimeview_update_signature_info(MimeView *mimeview)
1100 if (!mimeview) return;
1101 if (!mimeview->opened) return;
1103 partinfo = gtk_ctree_node_get_row_data
1104 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1105 if (!partinfo) return;
1107 if (g_strcasecmp(partinfo->content_type,
1108 "application/pgp-signature") == 0) {
1109 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1110 textview_show_signature_part(mimeview->textview, partinfo);
1114 void mimeview_check_signature(MimeView *mimeview)
1119 g_return_if_fail (mimeview_is_signed(mimeview));
1120 g_return_if_fail (gpg_started);
1122 mimeinfo = gtk_ctree_node_get_row_data
1123 (GTK_CTREE(mimeview->ctree), mimeview->opened);
1124 g_return_if_fail(mimeinfo != NULL);
1125 g_return_if_fail(mimeview->file != NULL);
1127 while (mimeinfo->parent)
1128 mimeinfo = mimeinfo->parent;
1130 if ((fp = fopen(mimeview->file, "rb")) == NULL) {
1131 FILE_OP_ERROR(mimeview->file, "fopen");
1135 rfc2015_check_signature(mimeinfo, fp);
1138 mimeview_update_names(mimeview);
1139 mimeview_update_signature_info(mimeview);
1141 textview_show_message(mimeview->messageview->textview, mimeinfo,
1144 #endif /* USE_GPGME */
1146 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
1148 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
1151 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
1153 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
1156 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
1158 GSList *mimeview_list, *viewer_list;
1160 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
1161 MimeView *mimeview = (MimeView *) mimeview_list->data;
1163 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
1164 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1165 mimeview->mimeviewer = NULL;
1168 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
1169 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
1171 mimeviewer->destroy_viewer(mimeviewer);
1172 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
1176 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);