2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
36 #include <sys/types.h>
43 #include "summaryview.h"
46 #include "alertpanel.h"
47 #include "inputdialog.h"
50 #include "prefs_common.h"
51 #include "procheader.h"
52 #include "stock_pixmap.h"
53 #include "gtk/gtkvscrollbutton.h"
54 #include "gtk/logwindow.h"
56 #include "manage_window.h"
58 #include "file-utils.h"
68 #define N_MIMEVIEW_COLS 4
70 static void mimeview_set_multipart_tree (MimeView *mimeview,
73 static void mimeview_show_message_part (MimeView *mimeview,
75 static void mimeview_change_view_type (MimeView *mimeview,
77 static gchar *mimeview_get_filename_for_part (MimeInfo *partinfo,
80 static gboolean mimeview_write_part (const gchar *filename,
82 gboolean handle_error);
84 static void mimeview_selected (GtkTreeSelection *selection,
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 gboolean mimeview_scrolled (GtkWidget *widget,
101 GdkEventScroll *event,
104 static void mimeview_save_all (MimeView *mimeview);
106 static void mimeview_open_part_with (MimeView *mimeview,
110 static void mimeview_send_to (MimeView *mimeview,
112 static void mimeview_view_file (const gchar *filename,
116 static gboolean icon_clicked_cb (GtkWidget *button,
117 GdkEventButton *event,
119 static void icon_selected (MimeView *mimeview,
122 static gint icon_key_pressed (GtkWidget *button,
125 static void icon_list_append_icon (MimeView *mimeview,
127 static void icon_list_create (MimeView *mimeview,
129 static void icon_list_clear (MimeView *mimeview);
130 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
132 static void ctree_size_allocate_cb (GtkWidget *widget,
133 GtkAllocation *allocation,
135 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
137 static gboolean part_button_pressed (MimeView *mimeview,
138 GdkEventButton *event,
140 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
141 GtkAllocation *layout_size,
143 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview);
144 static const gchar *get_part_name(MimeInfo *partinfo);
145 static const gchar *get_part_description(MimeInfo *partinfo);
147 static void mimeview_launch_cb(GtkAction *action, gpointer data)
149 MimeView *mimeview = (MimeView *)data;
150 mimeview_launch(mimeview, mimeview_get_part_to_use(mimeview));
154 static void mimeview_open_with_cb(GtkAction *action, gpointer data)
156 mimeview_open_with((MimeView *)data);
160 static void mimeview_copy_cb(GtkAction *action, gpointer data)
162 MimeView *mimeview = (MimeView *)data;
163 MimeInfo *mimeinfo = mimeview_get_part_to_use(mimeview);
165 if (mimeinfo == NULL)
168 if (mimeinfo->type == MIMETYPE_IMAGE) {
169 GError *error = NULL;
170 GdkPixbuf *pixbuf = procmime_get_part_as_pixbuf(mimeinfo, &error);
172 g_warning("could not copy: %s", error->message);
175 gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
178 g_object_unref(pixbuf);
180 void *data = procmime_get_part_as_string(mimeinfo, FALSE);
181 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
182 data, mimeinfo->length);
187 static void mimeview_send_to_cb(GtkAction *action, gpointer data)
189 MimeView *mimeview = (MimeView *)data;
190 mimeview_send_to(mimeview, mimeview_get_part_to_use(mimeview));
193 static void mimeview_display_as_text_cb(GtkAction *action, gpointer data)
195 mimeview_display_as_text((MimeView *)data);
198 static void mimeview_save_as_cb(GtkAction *action, gpointer data)
200 mimeview_save_as((MimeView *)data);
203 static void mimeview_save_all_cb(GtkAction *action, gpointer data)
205 mimeview_save_all((MimeView *)data);
208 static void mimeview_select_next_part_cb(GtkAction *action, gpointer data)
210 mimeview_select_next_part((MimeView *)data);
213 static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
215 mimeview_select_prev_part((MimeView *)data);
218 static GtkActionEntry mimeview_menu_actions[] = {
219 { "MimeView", NULL, "MimeView", NULL, NULL, NULL },
220 { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
221 #if (!defined G_OS_WIN32)
222 { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
224 { "MimeView/Copy", NULL, N_("Copy"), NULL, "Copy", G_CALLBACK(mimeview_copy_cb) },
225 { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
226 { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
227 { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
228 { "MimeView/SaveAll", NULL, N_("Save _all..."), NULL, "Save all parts", G_CALLBACK(mimeview_save_all_cb) },
229 { "MimeView/NextPart", NULL, N_("Next part"), NULL, "Next part", G_CALLBACK(mimeview_select_next_part_cb) },
230 { "MimeView/PrevPart", NULL, N_("Previous part"), NULL, "Previous part", G_CALLBACK(mimeview_select_prev_part_cb) }
233 static GtkTargetEntry mimeview_mime_types[] =
235 {"text/uri-list", 0, 0}
238 GSList *mimeviewer_factories;
241 static GdkCursor *hand_cursor = NULL;
243 static gboolean mimeview_visi_notify(GtkWidget *widget,
244 GdkEventVisibility *event,
247 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
251 static gboolean mimeview_leave_notify(GtkWidget *widget,
252 GdkEventCrossing *event,
255 gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
259 static gboolean mimeview_enter_notify(GtkWidget *widget,
260 GdkEventCrossing *event,
263 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
267 MimeView *mimeview_create(MainWindow *mainwin)
272 GtkWidget *scrolledwin;
274 GtkWidget *mime_notebook;
275 GtkWidget *popupmenu;
276 GtkWidget *ctree_mainbox;
278 GtkWidget *mime_toggle;
279 GtkWidget *icon_mainbox;
280 GtkWidget *icon_scroll;
281 GtkWidget *icon_vbox;
283 GtkWidget *scrollbutton;
285 NoticeView *siginfoview;
288 GtkCellRenderer *renderer;
289 GtkTreeViewColumn *column;
290 GtkTreeSelection *selection;
292 gchar *titles[N_MIMEVIEW_COLS];
296 hand_cursor = gdk_cursor_new(GDK_HAND2);
298 debug_print("Creating MIME view...\n");
299 mimeview = g_new0(MimeView, 1);
301 titles[COL_MIMETYPE] = _("MIME Type");
302 titles[COL_SIZE] = _("Size");
303 titles[COL_NAME] = _("Name");
305 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
306 gtk_widget_show(scrolledwin);
307 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
308 GTK_POLICY_AUTOMATIC,
309 GTK_POLICY_AUTOMATIC);
311 model = gtk_tree_store_new(N_MIMEVIEW_COLS,
317 ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
318 g_object_unref(model);
319 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
320 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
321 prefs_common.use_stripes_everywhere);
322 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ctree),
323 prefs_common.show_col_headers);
325 renderer = gtk_cell_renderer_text_new();
326 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
327 -1, titles[COL_MIMETYPE],renderer,
328 "text", COL_MIMETYPE, NULL);
329 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
331 renderer = gtk_cell_renderer_text_new();
332 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
333 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
334 -1, titles[COL_SIZE], renderer,
335 "text", COL_SIZE, NULL);
336 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
337 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
339 renderer = gtk_cell_renderer_text_new();
340 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
341 -1, titles[COL_NAME], renderer,
342 "text", COL_NAME, NULL);
343 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
344 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
346 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
347 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
349 gtk_widget_show(ctree);
350 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
351 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
352 mimeview_mime_types, 1, GDK_ACTION_COPY);
354 g_signal_connect(G_OBJECT(selection), "changed",
355 G_CALLBACK(mimeview_selected), mimeview);
356 g_signal_connect(G_OBJECT(ctree), "button_release_event",
357 G_CALLBACK(mimeview_button_pressed), mimeview);
358 g_signal_connect(G_OBJECT(ctree), "key_press_event",
359 G_CALLBACK(mimeview_key_pressed), mimeview);
360 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
361 G_CALLBACK(mimeview_drag_data_get), mimeview);
363 mime_notebook = gtk_notebook_new();
364 gtk_widget_show(mime_notebook);
365 gtk_widget_set_can_focus(mime_notebook, FALSE);
366 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
367 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
369 icon_vbox = gtk_vbox_new(FALSE, 2);
370 gtk_widget_show(icon_vbox);
371 icon_scroll = gtk_layout_new(NULL, NULL);
372 gtk_widget_show(icon_scroll);
373 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
374 scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
375 gtk_widget_show(scrollbutton);
377 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
378 G_CALLBACK(mimeview_scrolled), mimeview);
380 mime_toggle = gtk_event_box_new();
381 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
383 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
384 G_CALLBACK(mimeview_visi_notify), mimeview);
385 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
386 G_CALLBACK(mimeview_leave_notify), mimeview);
387 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
388 G_CALLBACK(mimeview_enter_notify), mimeview);
390 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
391 gtk_widget_show(mime_toggle);
392 mimeview->ctree_mode = FALSE;
393 arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
394 gtk_widget_show(arrow);
395 gtk_widget_size_request(arrow, &r);
396 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
397 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
398 G_CALLBACK(mime_toggle_button_cb), mimeview);
400 icon_mainbox = gtk_vbox_new(FALSE, 0);
401 gtk_widget_show(icon_mainbox);
402 gtk_widget_set_size_request(icon_mainbox, 32, -1);
403 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
404 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
405 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
406 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
407 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
409 ctree_mainbox = gtk_hbox_new(FALSE, 0);
410 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
411 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
412 G_CALLBACK(ctree_size_allocate_cb), mimeview);
414 mimeview->ui_manager = gtk_ui_manager_new();
415 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
416 "MimeView", mimeview_menu_actions,
417 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
419 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
420 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
421 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
422 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
423 "/Menus/MimeView/", "Open", "MimeView/Open",
424 GTK_UI_MANAGER_MENUITEM);
425 #if (!defined G_OS_WIN32)
426 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
427 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
428 GTK_UI_MANAGER_MENUITEM);
430 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
431 "/Menus/MimeView/", "Copy", "MimeView/Copy",
432 GTK_UI_MANAGER_MENUITEM);
433 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
434 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
435 GTK_UI_MANAGER_MENUITEM);
436 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
437 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
438 GTK_UI_MANAGER_MENUITEM);
439 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
440 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
441 GTK_UI_MANAGER_MENUITEM);
442 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
443 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
444 GTK_UI_MANAGER_MENUITEM);
445 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
446 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
447 GTK_UI_MANAGER_MENUITEM);
448 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
449 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
450 GTK_UI_MANAGER_MENUITEM);
452 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
453 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
456 vbox = gtk_vbox_new(FALSE, 0);
457 gtk_widget_show(vbox);
458 siginfoview = noticeview_create(mainwin);
459 noticeview_hide(siginfoview);
460 noticeview_set_icon_clickable(siginfoview, TRUE);
461 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
462 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
464 paned = gtk_vpaned_new();
465 gtk_widget_show(paned);
466 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
467 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
469 hbox = gtk_hbox_new(FALSE, 0);
470 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
471 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
473 gtk_widget_show(hbox);
474 gtk_widget_hide(ctree_mainbox);
476 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
478 mimeview->hbox = hbox;
479 mimeview->paned = paned;
480 mimeview->scrolledwin = scrolledwin;
481 mimeview->ctree = ctree;
482 mimeview->mime_notebook = mime_notebook;
483 mimeview->popupmenu = popupmenu;
485 mimeview->ctree_mainbox = ctree_mainbox;
486 mimeview->icon_scroll = icon_scroll;
487 mimeview->icon_vbox = icon_vbox;
488 mimeview->icon_mainbox = icon_mainbox;
489 mimeview->icon_count = 0;
490 mimeview->mainwin = mainwin;
491 mimeview->mime_toggle = mime_toggle;
492 mimeview->siginfoview = siginfoview;
493 mimeview->scrollbutton = scrollbutton;
494 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
496 mimeviews = g_slist_prepend(mimeviews, mimeview);
501 void mimeview_init(MimeView *mimeview)
503 textview_init(mimeview->textview);
505 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
506 GTK_WIDGET_PTR(mimeview->textview));
509 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
512 if (privacy_mimeinfo_is_signed(mimeinfo))
514 mimeinfo = procmime_mimeinfo_next(mimeinfo);
520 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
523 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
525 mimeview_clear(mimeview);
527 cm_return_if_fail(file != NULL);
528 cm_return_if_fail(mimeinfo != NULL);
530 mimeview->mimeinfo = mimeinfo;
532 mimeview->file = g_strdup(file);
534 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
537 /* check if the mail's signed - it can change the mail structure */
539 if (any_part_is_signed(mimeinfo))
540 debug_print("signed mail\n");
542 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
543 gtk_tree_view_expand_all(ctree);
544 icon_list_clear(mimeview);
545 icon_list_create(mimeview, mimeinfo);
547 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
548 mimeview_selected, mimeview);
552 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
555 static void mimeview_free_mimeinfo(MimeView *mimeview)
557 gboolean defer = FALSE;
559 defer = (mimeview->check_data != NULL);
561 mimeview->check_data->free_after_use = TRUE;
563 if (mimeview->mimeinfo != NULL && !defer) {
564 procmime_mimeinfo_free_all(&mimeview->mimeinfo);
565 mimeview->mimeinfo = NULL;
568 debug_print("deferring free(mimeinfo) and cancelling check\n");
569 mimeview_check_sig_cancel_now(mimeview);
574 void mimeview_destroy(MimeView *mimeview)
578 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
579 MimeViewer *viewer = (MimeViewer *) cur->data;
580 viewer->destroy_viewer(viewer);
582 g_slist_free(mimeview->viewers);
583 gtk_target_list_unref(mimeview->target_list);
586 if (mimeview->check_data) {
587 mimeview->check_data->destroy_mimeview = TRUE;
588 debug_print("deferring destroy\n");
592 mimeview_free_mimeinfo(mimeview);
593 gtk_tree_path_free(mimeview->opened);
594 g_free(mimeview->file);
596 mimeviews = g_slist_remove(mimeviews, mimeview);
601 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
603 return gtkut_tree_view_get_selected_pointer(
604 GTK_TREE_VIEW(mimeview->ctree), COL_DATA,
608 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
610 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
614 gtk_tree_model_get_iter(model, &iter, path);
615 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
619 gboolean mimeview_tree_is_empty(MimeView *mimeview)
621 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
623 return !gtk_tree_model_get_iter_first(model, &iter);
626 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
628 GtkTreeIter iter, parent;
631 gtk_tree_model_get_iter(model, &iter, path);
633 if (gtk_tree_model_iter_has_child(model, &iter)) {
634 gtk_tree_path_down(path);
638 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
640 if (!gtk_tree_model_iter_next(model, &iter)) {
642 GtkTreeIter saved_parent = parent;
643 gtk_tree_path_up(path);
644 if (gtk_tree_model_iter_next(model, &parent)) {
645 gtk_tree_path_next(path);
648 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
653 gtk_tree_path_next(path);
660 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
662 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
663 GtkTreeIter iter, child, parent;
666 gtk_tree_model_get_iter(model, &iter, path);
667 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
669 if (gtk_tree_path_prev(path)) {
670 gtk_tree_model_get_iter(model, &iter, path);
672 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
673 gtk_tree_path_down(path);
675 while (gtk_tree_model_iter_next(model, &child))
676 gtk_tree_path_next(path);
683 gtk_tree_path_up(path);
690 gint mimeview_get_selected_part_num(MimeView *mimeview)
692 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
697 if (!gtk_tree_model_get_iter_first(model, &iter))
699 path = gtk_tree_model_get_path(model, &iter);
702 if (!gtk_tree_path_compare(mimeview->opened, path)) {
703 gtk_tree_path_free(path);
708 } while (mimeview_tree_next(model, path));
710 gtk_tree_path_free(path);
715 void mimeview_select_part_num(MimeView *mimeview, gint i)
717 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
720 GtkTreeSelection *selection;
726 if (!gtk_tree_model_get_iter_first(model, &iter))
728 path = gtk_tree_model_get_path(model, &iter);
731 if (!mimeview_tree_next(model, path)) {
732 gtk_tree_path_free(path);
738 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
739 gtk_tree_selection_select_path(selection, path);
740 gtk_tree_path_free(path);
743 static void mimeview_set_multipart_tree(MimeView *mimeview,
747 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
748 GTK_TREE_VIEW(mimeview->ctree)));
750 gchar *content_type, *length, *name;
752 cm_return_if_fail(mimeinfo != NULL);
754 while (mimeinfo != NULL) {
755 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
756 content_type = g_strdup_printf("%s/%s",
757 procmime_get_media_type_str(mimeinfo->type),
760 content_type = g_strdup("UNKNOWN");
762 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
764 if (prefs_common.attach_desc)
765 name = g_strdup(get_part_description(mimeinfo));
767 name = g_strdup(get_part_name(mimeinfo));
769 gtk_tree_store_append(model, &iter, parent);
770 gtk_tree_store_set(model, &iter,
771 COL_MIMETYPE, content_type,
774 COL_DATA, mimeinfo, -1);
775 g_free(content_type);
779 if (mimeinfo->node->children)
780 mimeview_set_multipart_tree(mimeview,
781 (MimeInfo *) mimeinfo->node->children->data, &iter);
782 mimeinfo = mimeinfo->node->next != NULL ?
783 (MimeInfo *) mimeinfo->node->next->data : NULL;
787 static const gchar *get_real_part_name(MimeInfo *partinfo)
789 const gchar *name = NULL;
791 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
793 name = procmime_mimeinfo_get_parameter(partinfo, "name");
798 static const gchar *get_part_name(MimeInfo *partinfo)
802 name = get_real_part_name(partinfo);
809 static const gchar *get_part_description(MimeInfo *partinfo)
811 if (partinfo->description)
812 return partinfo->description;
814 return get_part_name(partinfo);
817 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
822 if (!partinfo) return;
824 fname = mimeview->file;
827 if ((fp = claws_fopen(fname, "rb")) == NULL) {
828 FILE_OP_ERROR(fname, "claws_fopen");
832 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
833 FILE_OP_ERROR(mimeview->file, "fseek");
838 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
839 textview_show_part(mimeview->textview, partinfo, fp);
844 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
847 MimeViewerFactory *factory = NULL;
848 MimeViewer *viewer = NULL;
849 gchar *real_contenttype = NULL, *tmp;
851 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
853 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
854 MimeViewerFactory *curfactory = cur->data;
857 while (curfactory->content_types[i] != NULL) {
858 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
859 if (g_pattern_match_simple(tmp, real_contenttype)) {
860 debug_print("%s\n", curfactory->content_types[i]);
861 factory = curfactory;
871 g_free(real_contenttype);
875 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
876 MimeViewer *curviewer = cur->data;
878 if (curviewer->factory == factory)
881 viewer = factory->create_viewer();
882 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
883 GTK_WIDGET(viewer->get_widget(viewer)));
885 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
890 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
892 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
895 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
897 gchar *content_type = NULL;
898 MimeViewer *viewer = NULL;
900 if ((partinfo->type == MIMETYPE_APPLICATION) &&
901 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
902 const gchar *filename;
904 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
905 if (filename == NULL)
906 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
907 if (filename != NULL)
908 content_type = procmime_get_mime_type(filename);
910 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
913 if (content_type != NULL) {
914 viewer = get_viewer_for_content_type(mimeview, content_type);
915 g_free(content_type);
921 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
925 if (mimeview->messageview->partial_display_shown) {
926 noticeview_hide(mimeview->messageview->noticeview);
927 mimeview->messageview->partial_display_shown = FALSE;
930 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
931 if (viewer == NULL) {
932 if (mimeview->mimeviewer != NULL)
933 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
934 mimeview->mimeviewer = NULL;
938 if (mimeview->mimeviewer != NULL)
939 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
941 if (mimeview->mimeviewer != viewer)
942 mimeview->mimeviewer = viewer;
944 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
945 viewer->mimeview = mimeview;
946 viewer->show_mimepart(viewer, mimeview->file, partinfo);
951 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
953 TextView *textview = mimeview->textview;
954 GtkWidget *focused = NULL;
956 if (mainwindow_get_mainwindow())
957 focused = gtkut_get_focused_child(
958 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
960 if ((mimeview->type != MIMEVIEW_VIEWER) &&
961 (mimeview->type == type)) return;
965 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
966 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
967 GTK_WIDGET_PTR(textview)));
969 case MIMEVIEW_VIEWER:
970 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
971 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
972 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
978 gtk_widget_grab_focus(focused);
979 mimeview->type = type;
982 void mimeview_clear(MimeView *mimeview)
989 if (g_slist_find(mimeviews, mimeview) == NULL)
992 noticeview_hide(mimeview->siginfoview);
994 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
995 gtk_tree_store_clear(GTK_TREE_STORE(model));
997 textview_clear(mimeview->textview);
998 if (mimeview->mimeviewer != NULL)
999 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
1001 mimeview_free_mimeinfo(mimeview);
1003 mimeview->mimeinfo = NULL;
1005 gtk_tree_path_free(mimeview->opened);
1006 mimeview->opened = NULL;
1008 g_free(mimeview->file);
1009 mimeview->file = NULL;
1011 icon_list_clear(mimeview);
1012 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1015 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
1016 GtkUIManager *ui_manager;
1018 if (messageview->window != NULL)
1019 ui_manager = messageview->ui_manager;
1021 ui_manager = messageview->mainwin->ui_manager;
1023 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
1026 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1027 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1029 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
1030 gboolean special, SignatureStatus code)
1032 gchar *text = NULL, *button_text = NULL;
1034 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1035 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1037 cm_return_if_fail(mimeview != NULL);
1038 cm_return_if_fail(mimeinfo != NULL);
1043 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1046 case SIGNATURE_UNCHECKED:
1047 button_text = _("Check signature");
1048 func = check_signature_cb;
1049 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1052 button_text = _("View full information");
1053 func = display_full_info_cb;
1054 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1056 case SIGNATURE_WARN:
1057 button_text = _("View full information");
1058 func = display_full_info_cb;
1059 icon = STOCK_PIXMAP_PRIVACY_WARN;
1061 case SIGNATURE_KEY_EXPIRED:
1062 button_text = _("View full information");
1063 func = display_full_info_cb;
1064 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1066 case SIGNATURE_INVALID:
1067 button_text = _("View full information");
1068 func = display_full_info_cb;
1069 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1071 case SIGNATURE_CHECK_FAILED:
1072 case SIGNATURE_CHECK_TIMEOUT:
1073 button_text = _("Check again");
1074 func = check_signature_cb;
1075 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1080 if (mycode == SIGNATURE_UNCHECKED) {
1081 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1082 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1084 if (*shortcut == '\0')
1085 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1087 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1091 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1092 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1093 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1094 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1096 if (*shortcut == '\0')
1097 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1099 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1103 noticeview_set_text(mimeview->siginfoview, text);
1104 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1107 noticeview_set_button_text(mimeview->siginfoview, NULL);
1108 noticeview_set_button_press_callback(
1109 mimeview->siginfoview,
1111 (gpointer) mimeview);
1112 noticeview_set_icon(mimeview->siginfoview, icon);
1113 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1115 icon_list_clear(mimeview);
1116 icon_list_create(mimeview, mimeview->mimeinfo);
1121 /* reset all thread stuff, and do the cleanups we've been left to do */
1122 static void mimeview_check_data_reset(MimeView *mimeview)
1125 gboolean must_destroy;
1127 if (!mimeview->check_data)
1130 must_free = mimeview->check_data->free_after_use;
1131 must_destroy = mimeview->check_data->destroy_mimeview;
1133 if (mimeview->check_data->cancel_th_init) {
1134 debug_print("killing canceller thread\n");
1135 mimeview->check_data->cancel_th_init = FALSE;
1136 pthread_cancel(mimeview->check_data->cancel_th);
1140 debug_print("freeing deferred mimeinfo\n");
1141 procmime_mimeinfo_free_all(&mimeview->check_data->siginfo);
1144 g_free(mimeview->check_data);
1145 mimeview->check_data = NULL;
1148 debug_print("freeing deferred mimeview\n");
1149 mimeview_free_mimeinfo(mimeview);
1150 gtk_tree_path_free(mimeview->opened);
1151 g_free(mimeview->file);
1153 mimeviews = g_slist_remove(mimeviews, mimeview);
1157 /* GUI update once the checker thread is done or killed */
1158 static gboolean mimeview_check_sig_thread_cb(void *data)
1160 MimeView *mimeview = (MimeView *) data;
1161 MimeInfo *mimeinfo = mimeview->siginfo;
1163 debug_print("mimeview_check_sig_thread_cb\n");
1165 if (mimeinfo == NULL) {
1166 /* message changed !? */
1167 g_warning("no more siginfo!");
1171 if (!mimeview->check_data) {
1172 g_warning("nothing to check");
1176 if (mimeview->check_data->siginfo != mimeinfo) {
1177 /* message changed !? */
1178 g_warning("different siginfo!");
1182 if (mimeview->check_data->destroy_mimeview ||
1183 mimeview->check_data->free_after_use) {
1184 debug_print("not bothering, we're changing message\n");
1189 if (mimeview->check_data->timeout)
1190 update_signature_noticeview(mimeview, mimeview->siginfo,
1191 TRUE, SIGNATURE_CHECK_TIMEOUT);
1193 update_signature_noticeview(mimeview, mimeview->siginfo,
1197 mimeview_check_data_reset(mimeview);
1201 /* sig checker thread */
1202 static void *mimeview_check_sig_worker_thread(void *data)
1204 MimeView *mimeview = (MimeView *)data;
1205 MimeInfo *mimeinfo = mimeview->siginfo;
1207 debug_print("checking...\n");
1209 if (!mimeview->check_data)
1212 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1213 privacy_mimeinfo_check_signature(mimeinfo);
1214 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1215 mimeview->check_data->cancel_th_init = FALSE;
1216 pthread_cancel(mimeview->check_data->cancel_th);
1219 /* that's strange! we changed message without
1220 * getting killed. */
1221 g_warning("different siginfo!");
1222 mimeview_check_data_reset(mimeview);
1226 /* use g_timeout so that GUI updates is done from the
1228 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1233 /* killer thread - acts when the checker didn't work fast
1235 static void *mimeview_check_sig_cancel_thread(void *data)
1237 MimeView *mimeview = (MimeView *)data;
1239 if (!mimeview->check_data)
1240 return NULL; /* nothing to kill ! */
1242 /* wait for a few seconds... */
1243 debug_print("waiting a while\n");
1245 g_usleep(5 * 1000 * 1000);
1247 if (!mimeview->check_data)
1248 return NULL; /* nothing to kill, it's done in time :) */
1250 /* too late, go away checker thread */
1251 debug_print("killing checker thread\n");
1252 if (mimeview->check_data->th_init) {
1253 mimeview->check_data->th_init = FALSE;
1254 pthread_cancel(mimeview->check_data->th);
1257 /* tell upstream it was a timeout */
1258 mimeview->check_data->timeout = TRUE;
1259 /* use g_timeout so that GUI updates is done from the
1261 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1266 /* get rid of the checker thread right now - used when changing the
1267 * displayed message for example. */
1268 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1270 if (!mimeview->check_data)
1272 debug_print("killing checker thread NOW\n");
1273 if (mimeview->check_data->th_init) {
1274 mimeview->check_data->th_init = FALSE;
1275 pthread_cancel(mimeview->check_data->th);
1278 /* tell upstream it was a timeout */
1279 mimeview->check_data->timeout = TRUE;
1280 mimeview_check_sig_thread_cb(mimeview);
1284 /* creates a thread to check the signature, and a second one
1285 * to kill the first one after a timeout */
1286 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1289 pthread_attr_t detach, detach2;
1291 if (mimeview->check_data) {
1292 g_warning("already checking it");
1296 mimeview->check_data = g_new0(SigCheckData, 1);
1297 mimeview->check_data->siginfo = mimeview->siginfo;
1298 debug_print("creating thread\n");
1300 /* init thread attributes and create the checker thread */
1301 if (pthread_attr_init(&detach) != 0 ||
1302 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1303 pthread_attr_init(&detach2) != 0 ||
1304 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1305 pthread_create(&th, &detach,
1306 mimeview_check_sig_worker_thread,
1308 /* arh. We'll do it synchronously. */
1309 g_warning("can't create checked thread");
1310 g_free(mimeview->check_data);
1311 mimeview->check_data = NULL;
1314 mimeview->check_data->th = th;
1315 mimeview->check_data->th_init = TRUE;
1318 /* create the killer thread */
1319 if (pthread_create(&th2, &detach2,
1320 mimeview_check_sig_cancel_thread,
1322 g_warning("can't create killer thread");
1323 g_free(mimeview->check_data);
1324 mimeview->check_data = NULL;
1327 mimeview->check_data->cancel_th = th2;
1328 mimeview->check_data->cancel_th_init = TRUE;
1333 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1335 MimeView *mimeview = (MimeView *) user_data;
1336 MimeInfo *mimeinfo = mimeview->siginfo;
1338 if (mimeinfo == NULL)
1341 if (mimeview->check_data)
1344 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1347 /* let's do it non-blocking */
1348 mimeview_check_sig_in_thread(mimeview);
1349 if (!mimeview->check_data) /* let's check syncronously */
1352 debug_print("checking without thread\n");
1353 privacy_mimeinfo_check_signature(mimeinfo);
1354 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1358 void mimeview_check_signature(MimeView *mimeview)
1360 check_signature_cb(NULL, mimeview);
1363 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1365 MimeView *mimeview = (MimeView *) user_data;
1366 gtk_tree_path_free(mimeview->opened);
1367 mimeview->opened = NULL;
1368 mimeview_selected(gtk_tree_view_get_selection(
1369 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1372 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1374 MimeView *mimeview = (MimeView *) user_data;
1377 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1378 textview_set_text(mimeview->textview, siginfo);
1380 noticeview_set_button_text(mimeview->siginfoview, NULL);
1381 noticeview_set_button_press_callback(
1382 mimeview->siginfoview,
1383 G_CALLBACK(redisplay_email),
1384 (gpointer) mimeview);
1385 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1388 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1391 MimeInfo *first_text;
1393 cm_return_if_fail(mimeview != NULL);
1394 cm_return_if_fail(selected != NULL);
1396 if (selected->type == MIMETYPE_MESSAGE
1397 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1398 /* if the first text part is signed, check that */
1399 first_text = selected;
1400 while (first_text && first_text->type != MIMETYPE_TEXT) {
1401 first_text = procmime_mimeinfo_next(first_text);
1404 update_signature_info(mimeview, first_text);
1410 while (siginfo != NULL) {
1411 if (privacy_mimeinfo_is_signed(siginfo))
1413 siginfo = procmime_mimeinfo_parent(siginfo);
1415 mimeview->siginfo = siginfo;
1417 /* This shortcut boolean is there to correctly set the menu's
1418 * CheckSignature item sensitivity without killing performance
1419 * each time the menu sensitiveness is updated (a lot).
1421 mimeview->signed_part = (siginfo != NULL);
1423 if (siginfo == NULL) {
1424 noticeview_hide(mimeview->siginfoview);
1428 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1429 noticeview_show(mimeview->siginfoview);
1432 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1434 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1435 GtkTreeModel *model;
1439 MainWindow *mainwin;
1441 selection = gtk_tree_view_get_selection(ctree);
1442 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1445 path = gtk_tree_model_get_path(model, &iter);
1447 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1448 gtk_tree_path_free(path);
1452 gtk_tree_path_free(mimeview->opened);
1453 mimeview->opened = path;
1454 mimeview->spec_part = NULL;
1455 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1457 partinfo = mimeview_get_node_part(mimeview, path);
1458 if (!partinfo) return;
1460 /* ungrab the mouse event */
1461 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1462 gtk_grab_remove(GTK_WIDGET(ctree));
1463 if (gdk_pointer_is_grabbed())
1464 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1467 mimeview->textview->default_text = FALSE;
1469 update_signature_info(mimeview, partinfo);
1471 if (!mimeview_show_part(mimeview, partinfo)) {
1472 switch (partinfo->type) {
1474 case MIMETYPE_MESSAGE:
1475 case MIMETYPE_MULTIPART:
1476 mimeview_show_message_part(mimeview, partinfo);
1480 mimeview->textview->default_text = TRUE;
1481 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1482 textview_clear(mimeview->textview);
1483 textview_show_mime_part(mimeview->textview, partinfo);
1487 mainwin = mainwindow_get_mainwindow();
1489 main_window_set_menu_sensitive(mainwin);
1491 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1492 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1493 mimeview_check_signature(mimeview);
1497 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1500 GtkTreeSelection *selection;
1503 if (!event) return FALSE;
1505 if (event->button == 2 || event->button == 3) {
1506 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1507 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1510 gtk_tree_selection_select_iter(selection, &iter);
1512 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1517 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1520 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1521 if (event->direction == GDK_SCROLL_UP) {
1522 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1524 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1526 gtk_vscrollbutton_scroll(scrollbutton);
1530 /* from gdkevents.c */
1531 #define DOUBLE_CLICK_TIME 250
1533 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1536 static MimeInfo *lastinfo;
1537 static guint32 lasttime;
1539 if (event->button == 2 ||
1540 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1541 /* call external program for image, audio or html */
1542 mimeview_launch(mimeview, partinfo);
1544 } else if (event->button == 3) {
1545 MainWindow *mainwin = mainwindow_get_mainwindow();
1547 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1548 partinfo->type == MIMETYPE_IMAGE ||
1549 partinfo->type == MIMETYPE_MULTIPART))
1550 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1552 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1554 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1555 partinfo->type == MIMETYPE_IMAGE ||
1556 partinfo->type == MIMETYPE_TEXT))
1557 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", TRUE);
1559 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", FALSE);
1562 partinfo->type == MIMETYPE_APPLICATION &&
1563 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1564 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1567 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1569 main_window_set_menu_sensitive(mainwin);
1570 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1571 "pop_partinfo", partinfo);
1573 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1574 NULL, NULL, NULL, NULL,
1575 event->button, event->time);
1579 lastinfo = partinfo;
1580 lasttime = event->time;
1585 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1587 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1590 void mimeview_select_next_part(MimeView *mimeview)
1592 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1593 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1594 GtkTreeSelection *selection;
1597 MimeInfo *partinfo = NULL;
1600 if (!mimeview->opened) return;
1602 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1603 path = gtk_tree_model_get_path(model, &iter);
1605 has_next = mimeview_tree_next(model, path);
1607 has_next = gtk_tree_model_get_iter_first(model, &iter);
1608 gtk_tree_path_free(path);
1609 path = gtk_tree_model_get_path(model, &iter);
1613 partinfo = mimeview_get_node_part(mimeview, path);
1614 if (partinfo->type == MIMETYPE_MULTIPART ||
1615 (!prefs_common.show_inline_attachments && partinfo->id))
1617 selection = gtk_tree_view_get_selection(ctree);
1618 gtk_tree_selection_select_path(selection, path);
1619 icon_list_toggle_by_mime_info(mimeview, partinfo);
1622 gtk_tree_path_free(path);
1625 void mimeview_select_prev_part(MimeView *mimeview)
1627 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1628 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1629 GtkTreeSelection *selection;
1632 MimeInfo *partinfo = NULL;
1635 if (!mimeview->opened) return;
1637 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1638 path = gtk_tree_model_get_path(model, &iter);
1640 has_prev = mimeview_tree_prev(mimeview, path);
1642 while (mimeview_tree_next(model, path)) {}
1647 partinfo = mimeview_get_node_part(mimeview, path);
1648 if (partinfo->type == MIMETYPE_MULTIPART ||
1649 (!prefs_common.show_inline_attachments && partinfo->id))
1651 selection = gtk_tree_view_get_selection(ctree);
1652 gtk_tree_selection_select_path(selection, path);
1653 icon_list_toggle_by_mime_info(mimeview, partinfo);
1656 gtk_tree_path_free(path);
1659 #define BREAK_ON_MODIFIER_KEY() \
1660 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1662 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1665 SummaryView *summaryview;
1667 if (!event) return FALSE;
1668 if (!mimeview->opened) return FALSE;
1670 summaryview = mimeview->messageview->mainwin->summaryview;
1672 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1675 switch (event->keyval) {
1678 textview_scroll_max(mimeview->textview,
1679 (event->keyval == GDK_KEY_Home));
1681 case GDK_KEY_Page_Down:
1683 if (mimeview_scroll_page(mimeview,
1684 (event->state & GDK_SHIFT_MASK) != 0))
1687 if (!(event->state & GDK_SHIFT_MASK))
1688 mimeview_select_next_part(mimeview);
1690 case GDK_KEY_Page_Up:
1691 case GDK_KEY_BackSpace:
1692 mimeview_scroll_page(mimeview, TRUE);
1694 case GDK_KEY_Return:
1695 case GDK_KEY_KP_Enter:
1696 mimeview_scroll_one_line(mimeview,
1697 (event->state & GDK_MOD1_MASK) != 0);
1701 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1707 if (mimeview->messageview->new_window) return FALSE;
1709 return summary_pass_key_press_event(summaryview, event);
1712 static void mimeview_drag_data_get(GtkWidget *widget,
1713 GdkDragContext *drag_context,
1714 GtkSelectionData *selection_data,
1719 gchar *filename = NULL, *uriname, *tmp;
1724 if (!mimeview->opened) return;
1725 if (!mimeview->file) return;
1727 partinfo = mimeview_get_selected_part(mimeview);
1728 if (!partinfo) return;
1730 if (strlen(get_part_name(partinfo)) > 0) {
1731 filename = g_path_get_basename(get_part_name(partinfo));
1732 if (*filename == '\0') return;
1733 } else if (partinfo->type == MIMETYPE_MESSAGE
1734 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1736 GPtrArray *headers = NULL;
1739 fp = claws_fopen(partinfo->data.filename, "rb");
1740 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1741 headers = procheader_get_header_array_asis(fp);
1744 for (i = 0; i < headers->len; i++) {
1745 Header *header = g_ptr_array_index(headers, i);
1746 if (procheader_headername_equal(header->name, "Subject")) {
1747 unfold_line(header->body);
1748 name = g_strconcat(header->body, ".txt", NULL);
1749 subst_for_filename(name);
1752 procheader_header_array_destroy(headers);
1758 filename = g_path_get_basename(name);
1761 if (filename == NULL)
1762 filename = g_path_get_basename("Unnamed part");
1764 if (!g_utf8_validate(filename, -1, NULL))
1765 tmp = conv_codeset_strdup(filename,
1766 conv_get_locale_charset_str(),
1769 tmp = g_strdup(filename);
1772 g_warning("filename not in UTF-8");
1773 tmp = g_strdup("Unnamed part");
1775 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1779 if (is_file_exist(filename)) {
1781 gchar *prefix = NULL;
1782 gchar *new_name = NULL;
1783 if (strrchr(tmp, '.')) {
1784 prefix = g_strdup(tmp);
1785 ext = g_strdup(strrchr(tmp, '.'));
1786 *(strrchr(prefix, '.')) = '\0';
1788 prefix = g_strdup(tmp);
1792 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1796 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1799 goto check_new_file;
1804 if ((err = procmime_get_part(filename, partinfo)) < 0)
1806 (_("Couldn't save the part of multipart message: %s"),
1809 tmp = g_filename_to_uri(filename, NULL, NULL);
1810 uriname = g_strconcat(tmp, "\r\n", NULL);
1813 gtk_selection_data_set(selection_data,
1814 gtk_selection_data_get_target(selection_data), 8,
1815 (guchar *)uriname, strlen(uriname));
1822 * Returns a filename (with path) for an attachment
1823 * \param partinfo The attachment to save
1824 * \param basedir The target directory
1825 * \param number Used for dummy filename if attachment is unnamed
1827 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1828 const gchar *basedir,
1834 filename = g_strdup(get_part_name(partinfo));
1835 if (!filename || !*filename) {
1837 filename = g_strdup_printf("noname.%d", number);
1840 if (!g_utf8_validate(filename, -1, NULL)) {
1841 gchar *tmp = conv_filename_to_utf8(filename);
1846 subst_for_filename(filename);
1848 fullname = g_strconcat
1849 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1850 ? &filename[1] : filename, NULL);
1853 filename = conv_filename_from_utf8(fullname);
1859 * Write a single attachment to file
1860 * \param filename Filename with path
1861 * \param partinfo Attachment to save
1863 static gboolean mimeview_write_part(const gchar *filename,
1865 gboolean handle_error)
1870 dir= g_path_get_dirname(filename);
1871 if (!is_dir_exist(dir))
1875 if (is_file_exist(filename)) {
1880 if (!g_utf8_validate(filename, -1, NULL))
1881 tmp = conv_filename_to_utf8(filename);
1883 tmp = g_strdup(filename);
1885 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1888 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
1889 GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST);
1891 if (G_ALERTALTERNATE != aval) return FALSE;
1894 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1895 debug_print("error saving MIME part: %d\n", err);
1898 (_("Couldn't save the part of multipart message: %s"),
1906 static AlertValue mimeview_save_all_error_ask(gint n)
1908 gchar *message = g_strdup_printf(
1909 _("An error has occurred while saving message part #%d. "
1910 "Do you want to cancel operation or skip error and "
1912 AlertValue av = alertpanel_full(_("Error saving all message parts"),
1913 message, GTK_STOCK_CANCEL, _("Skip"), _("Skip all"),
1914 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1919 static void mimeview_save_all_info(gint errors, gint total)
1922 gchar *msg = g_strdup_printf(
1923 ngettext("%d file saved successfully.",
1924 "%d files saved successfully.",
1927 alertpanel_notice("%s", msg);
1930 gchar *msg1 = g_strdup_printf(
1931 ngettext("%d file saved successfully",
1932 "%d files saved successfully",
1935 gchar *msg2 = g_strdup_printf(
1936 ngettext("%s, %d file failed.",
1937 "%s, %d files failed.",
1940 alertpanel_warning("%s", msg2);
1947 * Menu callback: Save all attached files
1948 * \param mimeview Current display
1950 static void mimeview_save_all(MimeView *mimeview)
1954 gchar *startdir = NULL;
1955 gint number = 1, errors = 0;
1956 gboolean skip_errors = FALSE;
1958 if (!mimeview->opened) return;
1959 if (!mimeview->file) return;
1960 if (!mimeview->mimeinfo) return;
1962 partinfo = mimeview->mimeinfo;
1963 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1964 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1966 startdir = g_strdup(get_home_dir());
1968 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1969 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1975 if (!is_dir_exist (dirname)) {
1976 alertpanel_error(_("'%s' is not a directory."), dirname);
1982 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1983 dirname[strlen(dirname)-1] = '\0';
1985 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1986 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1987 partinfo = procmime_mimeinfo_next(partinfo);
1988 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1989 partinfo = procmime_mimeinfo_next(partinfo);
1990 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1991 partinfo = procmime_mimeinfo_next(partinfo);
1994 while (partinfo != NULL) {
1995 if (partinfo->type != MIMETYPE_MESSAGE &&
1996 partinfo->type != MIMETYPE_MULTIPART &&
1997 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1998 || get_real_part_name(partinfo) != NULL)) {
1999 gchar *filename = mimeview_get_filename_for_part(
2000 partinfo, dirname, number++);
2002 gboolean ok = mimeview_write_part(filename, partinfo, FALSE);
2007 AlertValue av = mimeview_save_all_error_ask(number - 1);
2008 skip_errors = (av == G_ALERTOTHER);
2009 if (av == G_ALERTDEFAULT) /* cancel */
2014 partinfo = procmime_mimeinfo_next(partinfo);
2017 g_free(prefs_common.attach_save_dir);
2019 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
2020 -1, NULL, NULL, NULL);
2023 mimeview_save_all_info(errors, number - 1);
2026 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
2028 MimeInfo *partinfo = NULL;
2029 if (mimeview->spec_part) {
2030 partinfo = mimeview->spec_part;
2031 mimeview->spec_part = NULL;
2033 partinfo = (MimeInfo *) g_object_get_data
2034 (G_OBJECT(mimeview->popupmenu),
2036 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2037 "pop_partinfo", NULL);
2039 partinfo = mimeview_get_selected_part(mimeview);
2046 * Menu callback: Save the selected attachment
2047 * \param mimeview Current display
2049 void mimeview_save_as(MimeView *mimeview)
2052 gchar *filepath = NULL;
2053 gchar *filedir = NULL;
2055 gchar *partname = NULL;
2057 if (!mimeview->opened) return;
2058 if (!mimeview->file) return;
2060 partinfo = mimeview_get_part_to_use(mimeview);
2062 cm_return_if_fail(partinfo != NULL);
2064 if (get_part_name(partinfo) == NULL) {
2067 partname = g_strdup(get_part_name(partinfo));
2069 if (!g_utf8_validate(partname, -1, NULL)) {
2070 gchar *tmp = conv_filename_to_utf8(partname);
2072 tmp = conv_codeset_strdup(partname,
2073 conv_get_locale_charset_str(),
2082 subst_for_filename(partname);
2084 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2085 filepath = g_strconcat(prefs_common.attach_save_dir,
2086 G_DIR_SEPARATOR_S, partname, NULL);
2088 filepath = g_strdup(partname);
2092 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2093 filename = filesel_select_file_save(_("Save as"), filepath);
2099 mimeview_write_part(filename, partinfo, TRUE);
2101 filedir = g_path_get_dirname(filename);
2102 if (filedir && strcmp(filedir, ".")) {
2103 g_free(prefs_common.attach_save_dir);
2104 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2111 void mimeview_display_as_text(MimeView *mimeview)
2115 if (!mimeview->opened) return;
2117 partinfo = mimeview_get_part_to_use(mimeview);
2118 mimeview_select_mimepart_icon(mimeview, partinfo);
2119 cm_return_if_fail(partinfo != NULL);
2120 mimeview_show_message_part(mimeview, partinfo);
2123 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2128 if (!mimeview->opened) return;
2129 if (!mimeview->file) return;
2132 partinfo = mimeview_get_part_to_use(mimeview);
2134 cm_return_if_fail(partinfo != NULL);
2136 filename = procmime_get_tmp_file_name(partinfo);
2138 if ((err = procmime_get_part(filename, partinfo)) < 0)
2140 (_("Couldn't save the part of multipart message: %s"),
2143 mimeview_view_file(filename, partinfo, NULL, mimeview);
2149 void mimeview_open_with(MimeView *mimeview)
2153 if (!mimeview) return;
2154 if (!mimeview->opened) return;
2155 if (!mimeview->file) return;
2157 partinfo = mimeview_get_part_to_use(mimeview);
2159 mimeview_open_part_with(mimeview, partinfo, FALSE);
2162 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2166 gchar *mime_command = NULL;
2167 gchar *content_type = NULL;
2170 cm_return_if_fail(partinfo != NULL);
2172 filename = procmime_get_tmp_file_name(partinfo);
2174 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2176 (_("Couldn't save the part of multipart message: %s"),
2182 if (!prefs_common.mime_open_cmd_history)
2183 prefs_common.mime_open_cmd_history =
2184 add_history(NULL, prefs_common.mime_open_cmd);
2186 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2187 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2188 /* guess content-type from filename */
2189 content_type = procmime_get_mime_type(filename);
2191 if (content_type == NULL) {
2192 content_type = procmime_get_content_type_str(partinfo->type,
2196 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2197 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2198 mime_command = g_strdup(prefs_common_get_uri_cmd());
2199 g_free(content_type);
2200 content_type = NULL;
2201 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2202 || !prefs_common_get_ext_editor_cmd()[0]) {
2203 mime_command = mailcap_get_command_for_type(content_type, filename);
2205 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2206 g_free(content_type);
2207 content_type = NULL;
2209 if (mime_command == NULL) {
2210 /* try with extension this time */
2211 g_free(content_type);
2212 content_type = procmime_get_mime_type(filename);
2213 mime_command = mailcap_get_command_for_type(content_type, filename);
2216 if (mime_command == NULL)
2220 gboolean remember = FALSE;
2221 if (content_type != NULL)
2222 cmd = input_dialog_combo_remember
2224 _("Enter the command-line to open file:\n"
2225 "('%s' will be replaced with file name)"),
2226 mime_command ? mime_command : prefs_common.mime_open_cmd,
2227 prefs_common.mime_open_cmd_history, &remember);
2229 cmd = input_dialog_combo
2231 _("Enter the command-line to open file:\n"
2232 "('%s' will be replaced with file name)"),
2233 mime_command ? mime_command : prefs_common.mime_open_cmd,
2234 prefs_common.mime_open_cmd_history);
2235 if (cmd && remember) {
2236 mailcap_update_default(content_type, cmd);
2238 g_free(mime_command);
2243 mimeview_view_file(filename, partinfo, cmd, mimeview);
2244 g_free(prefs_common.mime_open_cmd);
2245 prefs_common.mime_open_cmd = cmd;
2246 prefs_common.mime_open_cmd_history =
2247 add_history(prefs_common.mime_open_cmd_history, cmd);
2250 g_free(content_type);
2255 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2257 GList *attach_file = NULL;
2258 AttachInfo *ainfo = NULL;
2262 if (!mimeview->opened) return;
2263 if (!mimeview->file) return;
2265 cm_return_if_fail(partinfo != NULL);
2267 filename = procmime_get_tmp_file_name(partinfo);
2269 if (!(err = procmime_get_part(filename, partinfo))) {
2270 ainfo = g_new0(AttachInfo, 1);
2271 ainfo->file = filename;
2272 ainfo->name = g_strdup(get_part_name(partinfo));
2273 ainfo->content_type = procmime_get_content_type_str(
2274 partinfo->type, partinfo->subtype);
2275 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2276 partinfo, "charset"));
2277 attach_file = g_list_append(attach_file, ainfo);
2279 compose_new(NULL, NULL, attach_file);
2281 g_free(ainfo->name);
2282 g_free(ainfo->content_type);
2283 g_free(ainfo->charset);
2285 g_list_free(attach_file);
2288 (_("Couldn't save the part of multipart message: %s"),
2293 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2294 const gchar *cmd, MimeView *mimeview)
2298 gchar buf[BUFFSIZE];
2301 mimeview_open_part_with(mimeview, partinfo, TRUE);
2303 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2304 !strchr(p + 2, '%')) {
2305 g_snprintf(buf, sizeof(buf), cmd, filename);
2306 if (!prefs_common.save_parts_readwrite)
2307 g_chmod(filename, S_IRUSR);
2309 g_chmod(filename, S_IRUSR|S_IWUSR);
2311 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2312 mimeview_open_part_with(mimeview, partinfo, FALSE);
2314 if (execute_command_line(buf, TRUE, NULL) != 0) {
2315 if (!prefs_common.save_parts_readwrite)
2316 g_chmod(filename, S_IRUSR|S_IWUSR);
2317 mimeview_open_part_with(mimeview, partinfo, FALSE);
2321 SHFILEINFO file_info;
2322 GError *error = NULL;
2323 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2325 if (error != NULL) {
2326 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2328 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2329 g_error_free(error);
2333 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2334 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2335 _("This attachment is an executable file. Executing "
2336 "untrusted binaries is dangerous and could compromise "
2337 "your computer.\n\n"
2338 "Do you want to run this file?"), GTK_STOCK_CANCEL,
2340 NULL, ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
2341 if (val == G_ALERTALTERNATE) {
2342 debug_print("executing binary\n");
2343 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2346 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2354 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2356 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2359 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2361 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2364 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2366 GSList *mimeview_list, *viewer_list;
2368 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2369 MimeView *mimeview = (MimeView *) mimeview_list->data;
2371 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2372 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2373 mimeview->mimeviewer = NULL;
2376 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2377 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2379 mimeviewer->destroy_viewer(mimeviewer);
2380 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2384 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2387 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2392 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2393 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2395 if (event->button == 1) {
2396 icon_selected(mimeview, num, partinfo);
2397 gtk_widget_grab_focus(button);
2398 icon_list_toggle_by_mime_info(mimeview, partinfo);
2400 part_button_pressed(mimeview, event, partinfo);
2405 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2407 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2410 MimeInfo *curr = NULL;
2412 if (!gtk_tree_model_get_iter_first(model, &iter))
2414 path = gtk_tree_model_get_path(model, &iter);
2417 gtk_tree_model_get_iter(model, &iter, path);
2418 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2419 if (curr == partinfo) {
2420 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2421 GTK_TREE_VIEW(mimeview->ctree));
2422 gtk_tree_selection_select_iter(sel, &iter);
2423 gtk_tree_path_free(path);
2426 } while (mimeview_tree_next(model, path));
2428 gtk_tree_path_free(path);
2431 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2433 icon_list_toggle_by_mime_info(mimeview, partinfo);
2434 icon_selected(mimeview, -1, partinfo);
2437 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2440 SummaryView *summaryview;
2442 if (!event) return FALSE;
2444 switch (event->keyval) {
2446 if (mimeview_scroll_page(mimeview, FALSE))
2449 mimeview_select_next_part(mimeview);
2453 case GDK_KEY_BackSpace:
2454 mimeview_scroll_page(mimeview, TRUE);
2456 case GDK_KEY_Return:
2457 case GDK_KEY_KP_Enter:
2458 mimeview_scroll_one_line(mimeview,
2459 (event->state & GDK_MOD1_MASK) != 0);
2462 BREAK_ON_MODIFIER_KEY();
2463 mimeview_save_as(mimeview);
2466 BREAK_ON_MODIFIER_KEY();
2467 mimeview_display_as_text(mimeview);
2470 BREAK_ON_MODIFIER_KEY();
2471 mimeview_launch(mimeview, NULL);
2475 BREAK_ON_MODIFIER_KEY();
2476 mimeview_open_with(mimeview);
2480 BREAK_ON_MODIFIER_KEY();
2481 mimeview_check_signature(mimeview);
2484 BREAK_ON_MODIFIER_KEY();
2485 mimeview_select_next_part(mimeview);
2491 if (!mimeview->messageview->mainwin) return FALSE;
2492 summaryview = mimeview->messageview->mainwin->summaryview;
2493 return summary_pass_key_press_event(summaryview, event);
2496 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2498 MimeView *mimeview = (MimeView *)data;
2499 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2501 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2502 "pop_partinfo", partinfo);
2503 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
2504 NULL, NULL, NULL, NULL,
2505 0, gtk_get_current_event_time());
2509 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2511 GtkWidget *pixmap = NULL;
2516 const gchar *desc = NULL;
2517 gchar *sigshort = NULL;
2518 gchar *content_type;
2521 MimeInfo *siginfo = NULL;
2522 MimeInfo *encrypted = NULL;
2527 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2530 vbox = mimeview->icon_vbox;
2531 mimeview->icon_count++;
2532 button = gtk_event_box_new();
2534 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2535 G_CALLBACK(mimeview_visi_notify), mimeview);
2536 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2537 G_CALLBACK(mimeview_leave_notify), mimeview);
2538 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2539 G_CALLBACK(mimeview_enter_notify), mimeview);
2541 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2542 g_object_set_data(G_OBJECT(button), "icon_number",
2543 GINT_TO_POINTER(mimeview->icon_count));
2544 g_object_set_data(G_OBJECT(button), "partinfo",
2547 switch (mimeinfo->type) {
2550 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2551 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2552 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2553 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2554 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2555 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2556 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2557 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2558 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2560 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2562 case MIMETYPE_MESSAGE:
2563 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2565 case MIMETYPE_APPLICATION:
2566 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2567 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2568 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2569 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2570 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2571 stockp = STOCK_PIXMAP_MIME_PDF;
2572 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2573 stockp = STOCK_PIXMAP_MIME_PS;
2575 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2577 case MIMETYPE_IMAGE:
2578 stockp = STOCK_PIXMAP_MIME_IMAGE;
2580 case MIMETYPE_AUDIO:
2581 stockp = STOCK_PIXMAP_MIME_AUDIO;
2584 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2588 partinfo = mimeinfo;
2589 while (partinfo != NULL) {
2590 if (privacy_mimeinfo_is_signed(partinfo)) {
2594 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2595 encrypted = partinfo;
2598 partinfo = procmime_mimeinfo_parent(partinfo);
2601 if (siginfo != NULL) {
2602 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2603 case SIGNATURE_UNCHECKED:
2604 case SIGNATURE_CHECK_FAILED:
2605 case SIGNATURE_CHECK_TIMEOUT:
2606 pixmap = stock_pixmap_widget_with_overlay(stockp,
2607 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2610 pixmap = stock_pixmap_widget_with_overlay(stockp,
2611 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2613 case SIGNATURE_WARN:
2614 case SIGNATURE_KEY_EXPIRED:
2615 pixmap = stock_pixmap_widget_with_overlay(stockp,
2616 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2618 case SIGNATURE_INVALID:
2619 pixmap = stock_pixmap_widget_with_overlay(stockp,
2620 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2623 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2624 } else if (encrypted != NULL) {
2625 pixmap = stock_pixmap_widget_with_overlay(stockp,
2626 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2628 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2629 OVERLAY_NONE, 6, 3);
2631 gtk_container_add(GTK_CONTAINER(button), pixmap);
2633 if (prefs_common.attach_desc)
2634 desc = get_part_description(mimeinfo);
2636 desc = get_part_name(mimeinfo);
2639 content_type = procmime_get_content_type_str(mimeinfo->type,
2642 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2643 "\n<b>", _("Size:"), " </b>",
2644 to_human_readable((goffset)mimeinfo->length), NULL);
2645 g_free(content_type);
2646 if (desc && *desc) {
2647 gchar *tmp = NULL, *escaped = NULL;
2648 if (!g_utf8_validate(desc, -1, NULL)) {
2649 tmp = conv_filename_to_utf8(desc);
2651 tmp = g_strdup(desc);
2653 escaped = g_markup_escape_text(tmp,-1);
2655 tiptmp = g_strconcat(tip, "\n<b>",
2656 prefs_common.attach_desc && mimeinfo->description ?
2657 _("Description:") : _("Filename:"),
2658 " </b>", escaped, NULL);
2664 if (sigshort && *sigshort) {
2665 gchar *sigshort_escaped =
2666 g_markup_escape_text(sigshort, -1);
2668 tiptmp = g_strjoin("\n", tip, sigshort_escaped, NULL);
2670 g_free(sigshort_escaped);
2675 gtk_widget_set_tooltip_markup(button, tip);
2677 gtk_widget_show_all(button);
2678 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2679 mimeview_mime_types, 1, GDK_ACTION_COPY);
2681 g_signal_connect(G_OBJECT(button), "popup-menu",
2682 G_CALLBACK(icon_popup_menu), mimeview);
2683 g_signal_connect(G_OBJECT(button), "button_release_event",
2684 G_CALLBACK(icon_clicked_cb), mimeview);
2685 g_signal_connect(G_OBJECT(button), "key_press_event",
2686 G_CALLBACK(icon_key_pressed), mimeview);
2687 g_signal_connect(G_OBJECT(button), "drag_data_get",
2688 G_CALLBACK(mimeview_drag_data_get), mimeview);
2689 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2691 gtk_widget_size_request(pixmap, &r);
2692 gtk_widget_set_size_request(button, -1, r.height + 4);
2697 static void icon_list_clear (MimeView *mimeview)
2699 GList *child, *orig;
2702 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2703 for (child = orig; child != NULL; child = g_list_next(child)) {
2704 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
2705 GTK_WIDGET(child->data));
2708 mimeview->icon_count = 0;
2709 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2710 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2711 gtk_adjustment_changed(adj);
2715 *\brief Used to 'click' the next or previous icon.
2717 *\return true if the icon 'number' exists and was selected.
2719 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2720 GtkAllocation *size, MimeView *mimeview)
2722 GtkAllocation vbox_size;
2723 GtkAllocation layout_size;
2728 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2730 gtk_widget_get_allocation(mimeview->icon_vbox, &vbox_size);
2731 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2733 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2734 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2736 MAX(vbox_size.height, layout_size.height));
2737 gtk_adjustment_set_step_increment(adj, 10);
2740 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2742 GtkRequisition size;
2743 GtkRequisition requisition;
2745 cm_return_if_fail(mimeinfo != NULL);
2747 while (mimeinfo != NULL) {
2748 if (mimeinfo->type != MIMETYPE_MULTIPART)
2749 icon_list_append_icon(mimeview, mimeinfo);
2750 if (mimeinfo->node->children != NULL)
2751 icon_list_create(mimeview,
2752 (MimeInfo *) mimeinfo->node->children->data);
2753 mimeinfo = mimeinfo->node->next != NULL
2754 ? (MimeInfo *) mimeinfo->node->next->data
2757 gtk_widget_size_request(mimeview->icon_vbox, &size);
2758 gtk_widget_get_requisition(mimeview->icon_mainbox, &requisition);
2759 if (size.width > requisition.width) {
2760 gtk_widget_set_size_request(mimeview->icon_mainbox,
2763 if (mimeview->opened)
2764 icon_list_toggle_by_mime_info(mimeview,
2765 mimeview_get_node_part(mimeview, mimeview->opened));
2768 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2771 GList *children, *child;
2773 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2774 for (child = children; child != NULL; child = g_list_next(child)) {
2775 gboolean *highlight = NULL;
2776 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2778 if (!GTK_IS_EVENT_BOX(child->data))
2781 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2782 *highlight = (g_object_get_data(G_OBJECT(child->data),
2783 "partinfo") == (gpointer)mimeinfo);
2785 gtk_widget_queue_draw(icon);
2787 g_list_free(children);
2790 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2793 prefs_common.mimeview_tree_height = allocation->height;
2796 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2799 g_object_ref(button);
2801 mimeview_leave_notify(button, NULL, NULL);
2803 mimeview->ctree_mode = !mimeview->ctree_mode;
2804 if (mimeview->ctree_mode) {
2805 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2807 gtk_widget_hide(mimeview->icon_mainbox);
2808 gtk_widget_show(mimeview->ctree_mainbox);
2809 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2810 prefs_common.mimeview_tree_height);
2812 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2814 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2815 button, FALSE, FALSE, 0);
2817 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2819 gtk_widget_hide(mimeview->ctree_mainbox);
2820 gtk_widget_show(mimeview->icon_mainbox);
2821 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2823 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2825 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2826 button, FALSE, FALSE, 0);
2827 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2828 if (mimeview->opened)
2829 icon_list_toggle_by_mime_info(mimeview,
2830 mimeview_get_node_part(mimeview, mimeview->opened));
2831 summary_grab_focus(mimeview->mainwin->summaryview);
2833 g_object_unref(button);
2837 void mimeview_update (MimeView *mimeview)
2839 if (mimeview && mimeview->mimeinfo) {
2840 icon_list_clear(mimeview);
2841 icon_list_create(mimeview, mimeview->mimeinfo);
2845 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2847 MessageView *msgview = NULL;
2848 MainWindow *mainwin = NULL;
2853 msgview = mimeview->messageview;
2857 mainwin = msgview->mainwin;
2861 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2862 "pop_partinfo", NULL);
2864 if (!strcmp(cmd, "sc://view_log"))
2865 log_window_show(mainwin->logwin);
2866 else if (!strcmp(cmd, "sc://save_as"))
2867 mimeview_save_as(mimeview);
2868 else if (!strcmp(cmd, "sc://display_as_text"))
2869 mimeview_display_as_text(mimeview);
2871 else if (!strcmp(cmd, "sc://open_with"))
2872 mimeview_open_with(mimeview);
2874 else if (!strcmp(cmd, "sc://open"))
2875 mimeview_launch(mimeview, NULL);
2876 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2877 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2878 icon_selected(mimeview, -1, (MimeInfo *)data);
2879 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2880 mimeview_launch(mimeview, (MimeInfo *)data);
2881 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2882 mimeview->spec_part = (MimeInfo *)data;
2883 part_button_pressed(mimeview, event, (MimeInfo *)data);
2884 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2885 const gchar *tagname = cmd + strlen("sc://search_tags:");
2886 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2887 gtk_toggle_button_set_active(
2888 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2890 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2891 ADVANCED_SEARCH_EXTENDED, buf);
2896 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2898 if (mimeview->type == MIMEVIEW_TEXT)
2899 return textview_scroll_page(mimeview->textview, up);
2900 else if (mimeview->mimeviewer) {
2901 MimeViewer *mimeviewer = mimeview->mimeviewer;
2902 if (mimeviewer->scroll_page)
2903 return mimeviewer->scroll_page(mimeviewer, up);
2908 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2910 if (mimeview->type == MIMEVIEW_TEXT)
2911 textview_scroll_one_line(mimeview->textview, up);
2912 else if (mimeview->mimeviewer) {
2913 MimeViewer *mimeviewer = mimeview->mimeviewer;
2914 if (mimeviewer->scroll_one_line)
2915 mimeviewer->scroll_one_line(mimeviewer, up);