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_send_to_cb(GtkAction *action, gpointer data)
162 MimeView *mimeview = (MimeView *)data;
163 mimeview_send_to(mimeview, mimeview_get_part_to_use(mimeview));
166 static void mimeview_display_as_text_cb(GtkAction *action, gpointer data)
168 mimeview_display_as_text((MimeView *)data);
171 static void mimeview_save_as_cb(GtkAction *action, gpointer data)
173 mimeview_save_as((MimeView *)data);
176 static void mimeview_save_all_cb(GtkAction *action, gpointer data)
178 mimeview_save_all((MimeView *)data);
181 static void mimeview_select_next_part_cb(GtkAction *action, gpointer data)
183 mimeview_select_next_part((MimeView *)data);
186 static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
188 mimeview_select_prev_part((MimeView *)data);
191 static GtkActionEntry mimeview_menu_actions[] = {
192 { "MimeView", NULL, "MimeView", NULL, NULL, NULL },
193 { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
194 #if (!defined G_OS_WIN32)
195 { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
197 { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
198 { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
199 { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
200 { "MimeView/SaveAll", NULL, N_("Save _all..."), NULL, "Save all parts", G_CALLBACK(mimeview_save_all_cb) },
201 { "MimeView/NextPart", NULL, N_("Next part"), NULL, "Next part", G_CALLBACK(mimeview_select_next_part_cb) },
202 { "MimeView/PrevPart", NULL, N_("Previous part"), NULL, "Previous part", G_CALLBACK(mimeview_select_prev_part_cb) }
205 static GtkTargetEntry mimeview_mime_types[] =
207 {"text/uri-list", 0, 0}
210 GSList *mimeviewer_factories;
213 static GdkCursor *hand_cursor = NULL;
215 static gboolean mimeview_visi_notify(GtkWidget *widget,
216 GdkEventVisibility *event,
219 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
223 static gboolean mimeview_leave_notify(GtkWidget *widget,
224 GdkEventCrossing *event,
227 gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
231 static gboolean mimeview_enter_notify(GtkWidget *widget,
232 GdkEventCrossing *event,
235 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
239 MimeView *mimeview_create(MainWindow *mainwin)
244 GtkWidget *scrolledwin;
246 GtkWidget *mime_notebook;
247 GtkWidget *popupmenu;
248 GtkWidget *ctree_mainbox;
250 GtkWidget *mime_toggle;
251 GtkWidget *icon_mainbox;
252 GtkWidget *icon_scroll;
253 GtkWidget *icon_vbox;
255 GtkWidget *scrollbutton;
257 NoticeView *siginfoview;
260 GtkCellRenderer *renderer;
261 GtkTreeViewColumn *column;
262 GtkTreeSelection *selection;
264 gchar *titles[N_MIMEVIEW_COLS];
268 hand_cursor = gdk_cursor_new(GDK_HAND2);
270 debug_print("Creating MIME view...\n");
271 mimeview = g_new0(MimeView, 1);
273 titles[COL_MIMETYPE] = _("MIME Type");
274 titles[COL_SIZE] = _("Size");
275 titles[COL_NAME] = _("Name");
277 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
278 gtk_widget_show(scrolledwin);
279 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
280 GTK_POLICY_AUTOMATIC,
281 GTK_POLICY_AUTOMATIC);
283 model = gtk_tree_store_new(N_MIMEVIEW_COLS,
289 ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
290 g_object_unref(model);
291 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
292 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
293 prefs_common.use_stripes_everywhere);
294 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ctree),
295 prefs_common.show_col_headers);
297 renderer = gtk_cell_renderer_text_new();
298 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
299 -1, titles[COL_MIMETYPE],renderer,
300 "text", COL_MIMETYPE, NULL);
301 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
303 renderer = gtk_cell_renderer_text_new();
304 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
305 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
306 -1, titles[COL_SIZE], renderer,
307 "text", COL_SIZE, NULL);
308 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
309 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
311 renderer = gtk_cell_renderer_text_new();
312 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
313 -1, titles[COL_NAME], renderer,
314 "text", COL_NAME, NULL);
315 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
316 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
318 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
319 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
321 gtk_widget_show(ctree);
322 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
323 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
324 mimeview_mime_types, 1, GDK_ACTION_COPY);
326 g_signal_connect(G_OBJECT(selection), "changed",
327 G_CALLBACK(mimeview_selected), mimeview);
328 g_signal_connect(G_OBJECT(ctree), "button_release_event",
329 G_CALLBACK(mimeview_button_pressed), mimeview);
330 g_signal_connect(G_OBJECT(ctree), "key_press_event",
331 G_CALLBACK(mimeview_key_pressed), mimeview);
332 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
333 G_CALLBACK(mimeview_drag_data_get), mimeview);
335 mime_notebook = gtk_notebook_new();
336 gtk_widget_show(mime_notebook);
337 gtk_widget_set_can_focus(mime_notebook, FALSE);
338 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
339 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
341 icon_vbox = gtk_vbox_new(FALSE, 2);
342 gtk_widget_show(icon_vbox);
343 icon_scroll = gtk_layout_new(NULL, NULL);
344 gtk_widget_show(icon_scroll);
345 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
346 scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
347 gtk_widget_show(scrollbutton);
349 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
350 G_CALLBACK(mimeview_scrolled), mimeview);
352 mime_toggle = gtk_event_box_new();
353 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
355 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
356 G_CALLBACK(mimeview_visi_notify), mimeview);
357 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
358 G_CALLBACK(mimeview_leave_notify), mimeview);
359 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
360 G_CALLBACK(mimeview_enter_notify), mimeview);
362 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
363 gtk_widget_show(mime_toggle);
364 mimeview->ctree_mode = FALSE;
365 arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
366 gtk_widget_show(arrow);
367 gtk_widget_size_request(arrow, &r);
368 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
369 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
370 G_CALLBACK(mime_toggle_button_cb), mimeview);
372 icon_mainbox = gtk_vbox_new(FALSE, 0);
373 gtk_widget_show(icon_mainbox);
374 gtk_widget_set_size_request(icon_mainbox, 32, -1);
375 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
376 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
377 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
378 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
379 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
381 ctree_mainbox = gtk_hbox_new(FALSE, 0);
382 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
383 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
384 G_CALLBACK(ctree_size_allocate_cb), mimeview);
386 mimeview->ui_manager = gtk_ui_manager_new();
387 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
388 "MimeView", mimeview_menu_actions,
389 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
391 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
392 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
393 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
394 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
395 "/Menus/MimeView/", "Open", "MimeView/Open",
396 GTK_UI_MANAGER_MENUITEM);
397 #if (!defined G_OS_WIN32)
398 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
399 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
400 GTK_UI_MANAGER_MENUITEM);
402 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
403 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
404 GTK_UI_MANAGER_MENUITEM);
405 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
406 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
407 GTK_UI_MANAGER_MENUITEM);
408 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
409 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
410 GTK_UI_MANAGER_MENUITEM);
411 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
412 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
413 GTK_UI_MANAGER_MENUITEM);
414 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
415 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
416 GTK_UI_MANAGER_MENUITEM);
417 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
418 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
419 GTK_UI_MANAGER_MENUITEM);
421 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
422 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
425 vbox = gtk_vbox_new(FALSE, 0);
426 gtk_widget_show(vbox);
427 siginfoview = noticeview_create(mainwin);
428 noticeview_hide(siginfoview);
429 noticeview_set_icon_clickable(siginfoview, TRUE);
430 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
431 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
433 paned = gtk_vpaned_new();
434 gtk_widget_show(paned);
435 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
436 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
438 hbox = gtk_hbox_new(FALSE, 0);
439 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
440 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
442 gtk_widget_show(hbox);
443 gtk_widget_hide(ctree_mainbox);
445 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
447 mimeview->hbox = hbox;
448 mimeview->paned = paned;
449 mimeview->scrolledwin = scrolledwin;
450 mimeview->ctree = ctree;
451 mimeview->mime_notebook = mime_notebook;
452 mimeview->popupmenu = popupmenu;
454 mimeview->ctree_mainbox = ctree_mainbox;
455 mimeview->icon_scroll = icon_scroll;
456 mimeview->icon_vbox = icon_vbox;
457 mimeview->icon_mainbox = icon_mainbox;
458 mimeview->icon_count = 0;
459 mimeview->mainwin = mainwin;
460 mimeview->mime_toggle = mime_toggle;
461 mimeview->siginfoview = siginfoview;
462 mimeview->scrollbutton = scrollbutton;
463 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
465 mimeviews = g_slist_prepend(mimeviews, mimeview);
470 void mimeview_init(MimeView *mimeview)
472 textview_init(mimeview->textview);
474 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
475 GTK_WIDGET_PTR(mimeview->textview));
478 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
481 if (privacy_mimeinfo_is_signed(mimeinfo))
483 mimeinfo = procmime_mimeinfo_next(mimeinfo);
489 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
492 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
494 mimeview_clear(mimeview);
496 cm_return_if_fail(file != NULL);
497 cm_return_if_fail(mimeinfo != NULL);
499 mimeview->mimeinfo = mimeinfo;
501 mimeview->file = g_strdup(file);
503 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
506 /* check if the mail's signed - it can change the mail structure */
508 if (any_part_is_signed(mimeinfo))
509 debug_print("signed mail\n");
511 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
512 gtk_tree_view_expand_all(ctree);
513 icon_list_clear(mimeview);
514 icon_list_create(mimeview, mimeinfo);
516 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
517 mimeview_selected, mimeview);
521 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
524 static void mimeview_free_mimeinfo(MimeView *mimeview)
526 gboolean defer = FALSE;
528 defer = (mimeview->check_data != NULL);
530 mimeview->check_data->free_after_use = TRUE;
532 if (mimeview->mimeinfo != NULL && !defer) {
533 procmime_mimeinfo_free_all(&mimeview->mimeinfo);
534 mimeview->mimeinfo = NULL;
537 debug_print("deferring free(mimeinfo) and cancelling check\n");
538 mimeview_check_sig_cancel_now(mimeview);
543 void mimeview_destroy(MimeView *mimeview)
547 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
548 MimeViewer *viewer = (MimeViewer *) cur->data;
549 viewer->destroy_viewer(viewer);
551 g_slist_free(mimeview->viewers);
552 gtk_target_list_unref(mimeview->target_list);
555 if (mimeview->check_data) {
556 mimeview->check_data->destroy_mimeview = TRUE;
557 debug_print("deferring destroy\n");
561 mimeview_free_mimeinfo(mimeview);
562 gtk_tree_path_free(mimeview->opened);
563 g_free(mimeview->file);
565 mimeviews = g_slist_remove(mimeviews, mimeview);
570 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
572 return gtkut_tree_view_get_selected_pointer(
573 GTK_TREE_VIEW(mimeview->ctree), COL_DATA,
577 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
579 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
583 gtk_tree_model_get_iter(model, &iter, path);
584 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
588 gboolean mimeview_tree_is_empty(MimeView *mimeview)
590 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
592 return !gtk_tree_model_get_iter_first(model, &iter);
595 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
597 GtkTreeIter iter, parent;
600 gtk_tree_model_get_iter(model, &iter, path);
602 if (gtk_tree_model_iter_has_child(model, &iter)) {
603 gtk_tree_path_down(path);
607 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
609 if (!gtk_tree_model_iter_next(model, &iter)) {
611 GtkTreeIter saved_parent = parent;
612 gtk_tree_path_up(path);
613 if (gtk_tree_model_iter_next(model, &parent)) {
614 gtk_tree_path_next(path);
617 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
622 gtk_tree_path_next(path);
629 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
631 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
632 GtkTreeIter iter, child, parent;
635 gtk_tree_model_get_iter(model, &iter, path);
636 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
638 if (gtk_tree_path_prev(path)) {
639 gtk_tree_model_get_iter(model, &iter, path);
641 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
642 gtk_tree_path_down(path);
644 while (gtk_tree_model_iter_next(model, &child))
645 gtk_tree_path_next(path);
652 gtk_tree_path_up(path);
659 gint mimeview_get_selected_part_num(MimeView *mimeview)
661 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
666 if (!gtk_tree_model_get_iter_first(model, &iter))
668 path = gtk_tree_model_get_path(model, &iter);
671 if (!gtk_tree_path_compare(mimeview->opened, path)) {
672 gtk_tree_path_free(path);
677 } while (mimeview_tree_next(model, path));
679 gtk_tree_path_free(path);
684 void mimeview_select_part_num(MimeView *mimeview, gint i)
686 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
689 GtkTreeSelection *selection;
695 if (!gtk_tree_model_get_iter_first(model, &iter))
697 path = gtk_tree_model_get_path(model, &iter);
700 if (!mimeview_tree_next(model, path)) {
701 gtk_tree_path_free(path);
707 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
708 gtk_tree_selection_select_path(selection, path);
709 gtk_tree_path_free(path);
712 static void mimeview_set_multipart_tree(MimeView *mimeview,
716 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
717 GTK_TREE_VIEW(mimeview->ctree)));
719 gchar *content_type, *length, *name;
721 cm_return_if_fail(mimeinfo != NULL);
723 while (mimeinfo != NULL) {
724 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
725 content_type = g_strdup_printf("%s/%s",
726 procmime_get_media_type_str(mimeinfo->type),
729 content_type = g_strdup("UNKNOWN");
731 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
733 if (prefs_common.attach_desc)
734 name = g_strdup(get_part_description(mimeinfo));
736 name = g_strdup(get_part_name(mimeinfo));
738 gtk_tree_store_append(model, &iter, parent);
739 gtk_tree_store_set(model, &iter,
740 COL_MIMETYPE, content_type,
743 COL_DATA, mimeinfo, -1);
744 g_free(content_type);
748 if (mimeinfo->node->children)
749 mimeview_set_multipart_tree(mimeview,
750 (MimeInfo *) mimeinfo->node->children->data, &iter);
751 mimeinfo = mimeinfo->node->next != NULL ?
752 (MimeInfo *) mimeinfo->node->next->data : NULL;
756 static const gchar *get_real_part_name(MimeInfo *partinfo)
758 const gchar *name = NULL;
760 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
762 name = procmime_mimeinfo_get_parameter(partinfo, "name");
767 static const gchar *get_part_name(MimeInfo *partinfo)
771 name = get_real_part_name(partinfo);
778 static const gchar *get_part_description(MimeInfo *partinfo)
780 if (partinfo->description)
781 return partinfo->description;
783 return get_part_name(partinfo);
786 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
791 if (!partinfo) return;
793 fname = mimeview->file;
796 if ((fp = claws_fopen(fname, "rb")) == NULL) {
797 FILE_OP_ERROR(fname, "claws_fopen");
801 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
802 FILE_OP_ERROR(mimeview->file, "fseek");
807 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
808 textview_show_part(mimeview->textview, partinfo, fp);
813 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
816 MimeViewerFactory *factory = NULL;
817 MimeViewer *viewer = NULL;
818 gchar *real_contenttype = NULL, *tmp;
820 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
822 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
823 MimeViewerFactory *curfactory = cur->data;
826 while (curfactory->content_types[i] != NULL) {
827 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
828 if (g_pattern_match_simple(tmp, real_contenttype)) {
829 debug_print("%s\n", curfactory->content_types[i]);
830 factory = curfactory;
840 g_free(real_contenttype);
844 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
845 MimeViewer *curviewer = cur->data;
847 if (curviewer->factory == factory)
850 viewer = factory->create_viewer();
851 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
852 GTK_WIDGET(viewer->get_widget(viewer)));
854 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
859 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
861 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
864 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
866 gchar *content_type = NULL;
867 MimeViewer *viewer = NULL;
869 if ((partinfo->type == MIMETYPE_APPLICATION) &&
870 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
871 const gchar *filename;
873 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
874 if (filename == NULL)
875 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
876 if (filename != NULL)
877 content_type = procmime_get_mime_type(filename);
879 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
882 if (content_type != NULL) {
883 viewer = get_viewer_for_content_type(mimeview, content_type);
884 g_free(content_type);
890 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
894 if (mimeview->messageview->partial_display_shown) {
895 noticeview_hide(mimeview->messageview->noticeview);
896 mimeview->messageview->partial_display_shown = FALSE;
899 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
900 if (viewer == NULL) {
901 if (mimeview->mimeviewer != NULL)
902 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
903 mimeview->mimeviewer = NULL;
907 if (mimeview->mimeviewer != NULL)
908 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
910 if (mimeview->mimeviewer != viewer)
911 mimeview->mimeviewer = viewer;
913 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
914 viewer->mimeview = mimeview;
915 viewer->show_mimepart(viewer, mimeview->file, partinfo);
920 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
922 TextView *textview = mimeview->textview;
923 GtkWidget *focused = NULL;
925 if (mainwindow_get_mainwindow())
926 focused = gtkut_get_focused_child(
927 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
929 if ((mimeview->type != MIMEVIEW_VIEWER) &&
930 (mimeview->type == type)) return;
934 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
935 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
936 GTK_WIDGET_PTR(textview)));
938 case MIMEVIEW_VIEWER:
939 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
940 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
941 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
947 gtk_widget_grab_focus(focused);
948 mimeview->type = type;
951 void mimeview_clear(MimeView *mimeview)
958 if (g_slist_find(mimeviews, mimeview) == NULL)
961 noticeview_hide(mimeview->siginfoview);
963 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
964 gtk_tree_store_clear(GTK_TREE_STORE(model));
966 textview_clear(mimeview->textview);
967 if (mimeview->mimeviewer != NULL)
968 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
970 mimeview_free_mimeinfo(mimeview);
972 mimeview->mimeinfo = NULL;
974 gtk_tree_path_free(mimeview->opened);
975 mimeview->opened = NULL;
977 g_free(mimeview->file);
978 mimeview->file = NULL;
980 icon_list_clear(mimeview);
981 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
984 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
985 GtkUIManager *ui_manager;
987 if (messageview->window != NULL)
988 ui_manager = messageview->ui_manager;
990 ui_manager = messageview->mainwin->ui_manager;
992 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
995 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
996 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
998 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
999 gboolean special, SignatureStatus code)
1001 gchar *text = NULL, *button_text = NULL;
1003 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1004 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1006 cm_return_if_fail(mimeview != NULL);
1007 cm_return_if_fail(mimeinfo != NULL);
1012 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1015 case SIGNATURE_UNCHECKED:
1016 button_text = _("Check signature");
1017 func = check_signature_cb;
1018 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1021 button_text = _("View full information");
1022 func = display_full_info_cb;
1023 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1025 case SIGNATURE_WARN:
1026 button_text = _("View full information");
1027 func = display_full_info_cb;
1028 icon = STOCK_PIXMAP_PRIVACY_WARN;
1030 case SIGNATURE_KEY_EXPIRED:
1031 button_text = _("View full information");
1032 func = display_full_info_cb;
1033 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1035 case SIGNATURE_INVALID:
1036 button_text = _("View full information");
1037 func = display_full_info_cb;
1038 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1040 case SIGNATURE_CHECK_FAILED:
1041 case SIGNATURE_CHECK_TIMEOUT:
1042 button_text = _("Check again");
1043 func = check_signature_cb;
1044 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1049 if (mycode == SIGNATURE_UNCHECKED) {
1050 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1051 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1053 if (*shortcut == '\0')
1054 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1056 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1060 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1061 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1062 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1063 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1065 if (*shortcut == '\0')
1066 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1068 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1072 noticeview_set_text(mimeview->siginfoview, text);
1073 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1076 noticeview_set_button_text(mimeview->siginfoview, NULL);
1077 noticeview_set_button_press_callback(
1078 mimeview->siginfoview,
1080 (gpointer) mimeview);
1081 noticeview_set_icon(mimeview->siginfoview, icon);
1082 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1084 icon_list_clear(mimeview);
1085 icon_list_create(mimeview, mimeview->mimeinfo);
1090 /* reset all thread stuff, and do the cleanups we've been left to do */
1091 static void mimeview_check_data_reset(MimeView *mimeview)
1094 gboolean must_destroy;
1096 if (!mimeview->check_data)
1099 must_free = mimeview->check_data->free_after_use;
1100 must_destroy = mimeview->check_data->destroy_mimeview;
1102 if (mimeview->check_data->cancel_th_init) {
1103 debug_print("killing canceller thread\n");
1104 mimeview->check_data->cancel_th_init = FALSE;
1105 pthread_cancel(mimeview->check_data->cancel_th);
1109 debug_print("freeing deferred mimeinfo\n");
1110 procmime_mimeinfo_free_all(&mimeview->check_data->siginfo);
1113 g_free(mimeview->check_data);
1114 mimeview->check_data = NULL;
1117 debug_print("freeing deferred mimeview\n");
1118 mimeview_free_mimeinfo(mimeview);
1119 gtk_tree_path_free(mimeview->opened);
1120 g_free(mimeview->file);
1122 mimeviews = g_slist_remove(mimeviews, mimeview);
1126 /* GUI update once the checker thread is done or killed */
1127 static gboolean mimeview_check_sig_thread_cb(void *data)
1129 MimeView *mimeview = (MimeView *) data;
1130 MimeInfo *mimeinfo = mimeview->siginfo;
1132 debug_print("mimeview_check_sig_thread_cb\n");
1134 if (mimeinfo == NULL) {
1135 /* message changed !? */
1136 g_warning("no more siginfo!");
1140 if (!mimeview->check_data) {
1141 g_warning("nothing to check");
1145 if (mimeview->check_data->siginfo != mimeinfo) {
1146 /* message changed !? */
1147 g_warning("different siginfo!");
1151 if (mimeview->check_data->destroy_mimeview ||
1152 mimeview->check_data->free_after_use) {
1153 debug_print("not bothering, we're changing message\n");
1158 if (mimeview->check_data->timeout)
1159 update_signature_noticeview(mimeview, mimeview->siginfo,
1160 TRUE, SIGNATURE_CHECK_TIMEOUT);
1162 update_signature_noticeview(mimeview, mimeview->siginfo,
1166 mimeview_check_data_reset(mimeview);
1170 /* sig checker thread */
1171 static void *mimeview_check_sig_worker_thread(void *data)
1173 MimeView *mimeview = (MimeView *)data;
1174 MimeInfo *mimeinfo = mimeview->siginfo;
1176 debug_print("checking...\n");
1178 if (!mimeview->check_data)
1181 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1182 privacy_mimeinfo_check_signature(mimeinfo);
1183 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1184 mimeview->check_data->cancel_th_init = FALSE;
1185 pthread_cancel(mimeview->check_data->cancel_th);
1188 /* that's strange! we changed message without
1189 * getting killed. */
1190 g_warning("different siginfo!");
1191 mimeview_check_data_reset(mimeview);
1195 /* use g_timeout so that GUI updates is done from the
1197 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1202 /* killer thread - acts when the checker didn't work fast
1204 static void *mimeview_check_sig_cancel_thread(void *data)
1206 MimeView *mimeview = (MimeView *)data;
1208 if (!mimeview->check_data)
1209 return NULL; /* nothing to kill ! */
1211 /* wait for a few seconds... */
1212 debug_print("waiting a while\n");
1214 g_usleep(5 * 1000 * 1000);
1216 if (!mimeview->check_data)
1217 return NULL; /* nothing to kill, it's done in time :) */
1219 /* too late, go away checker thread */
1220 debug_print("killing checker thread\n");
1221 if (mimeview->check_data->th_init) {
1222 mimeview->check_data->th_init = FALSE;
1223 pthread_cancel(mimeview->check_data->th);
1226 /* tell upstream it was a timeout */
1227 mimeview->check_data->timeout = TRUE;
1228 /* use g_timeout so that GUI updates is done from the
1230 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1235 /* get rid of the checker thread right now - used when changing the
1236 * displayed message for example. */
1237 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1239 if (!mimeview->check_data)
1241 debug_print("killing checker thread NOW\n");
1242 if (mimeview->check_data->th_init) {
1243 mimeview->check_data->th_init = FALSE;
1244 pthread_cancel(mimeview->check_data->th);
1247 /* tell upstream it was a timeout */
1248 mimeview->check_data->timeout = TRUE;
1249 mimeview_check_sig_thread_cb(mimeview);
1253 /* creates a thread to check the signature, and a second one
1254 * to kill the first one after a timeout */
1255 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1258 pthread_attr_t detach, detach2;
1260 if (mimeview->check_data) {
1261 g_warning("already checking it");
1265 mimeview->check_data = g_new0(SigCheckData, 1);
1266 mimeview->check_data->siginfo = mimeview->siginfo;
1267 debug_print("creating thread\n");
1269 /* init thread attributes and create the checker thread */
1270 if (pthread_attr_init(&detach) != 0 ||
1271 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1272 pthread_attr_init(&detach2) != 0 ||
1273 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1274 pthread_create(&th, &detach,
1275 mimeview_check_sig_worker_thread,
1277 /* arh. We'll do it synchronously. */
1278 g_warning("can't create thread");
1279 g_free(mimeview->check_data);
1280 mimeview->check_data = NULL;
1283 mimeview->check_data->th = th;
1284 mimeview->check_data->th_init = TRUE;
1287 /* create the killer thread */
1288 pthread_create(&th2, &detach2,
1289 mimeview_check_sig_cancel_thread,
1292 mimeview->check_data->cancel_th = th2;
1293 mimeview->check_data->cancel_th_init = TRUE;
1297 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1299 MimeView *mimeview = (MimeView *) user_data;
1300 MimeInfo *mimeinfo = mimeview->siginfo;
1302 if (mimeinfo == NULL)
1305 if (mimeview->check_data)
1308 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1311 /* let's do it non-blocking */
1312 mimeview_check_sig_in_thread(mimeview);
1313 if (!mimeview->check_data) /* let's check syncronously */
1316 debug_print("checking without thread\n");
1317 privacy_mimeinfo_check_signature(mimeinfo);
1318 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1322 void mimeview_check_signature(MimeView *mimeview)
1324 check_signature_cb(NULL, mimeview);
1327 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1329 MimeView *mimeview = (MimeView *) user_data;
1330 gtk_tree_path_free(mimeview->opened);
1331 mimeview->opened = NULL;
1332 mimeview_selected(gtk_tree_view_get_selection(
1333 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1336 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1338 MimeView *mimeview = (MimeView *) user_data;
1341 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1342 textview_set_text(mimeview->textview, siginfo);
1344 noticeview_set_button_text(mimeview->siginfoview, NULL);
1345 noticeview_set_button_press_callback(
1346 mimeview->siginfoview,
1347 G_CALLBACK(redisplay_email),
1348 (gpointer) mimeview);
1349 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1352 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1355 MimeInfo *first_text;
1357 cm_return_if_fail(mimeview != NULL);
1358 cm_return_if_fail(selected != NULL);
1360 if (selected->type == MIMETYPE_MESSAGE
1361 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1362 /* if the first text part is signed, check that */
1363 first_text = selected;
1364 while (first_text && first_text->type != MIMETYPE_TEXT) {
1365 first_text = procmime_mimeinfo_next(first_text);
1368 update_signature_info(mimeview, first_text);
1374 while (siginfo != NULL) {
1375 if (privacy_mimeinfo_is_signed(siginfo))
1377 siginfo = procmime_mimeinfo_parent(siginfo);
1379 mimeview->siginfo = siginfo;
1381 /* This shortcut boolean is there to correctly set the menu's
1382 * CheckSignature item sensitivity without killing performance
1383 * each time the menu sensitiveness is updated (a lot).
1385 mimeview->signed_part = (siginfo != NULL);
1387 if (siginfo == NULL) {
1388 noticeview_hide(mimeview->siginfoview);
1392 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1393 noticeview_show(mimeview->siginfoview);
1396 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1398 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1399 GtkTreeModel *model;
1403 MainWindow *mainwin;
1405 selection = gtk_tree_view_get_selection(ctree);
1406 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1409 path = gtk_tree_model_get_path(model, &iter);
1411 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1412 gtk_tree_path_free(path);
1416 gtk_tree_path_free(mimeview->opened);
1417 mimeview->opened = path;
1418 mimeview->spec_part = NULL;
1419 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1421 partinfo = mimeview_get_node_part(mimeview, path);
1422 if (!partinfo) return;
1424 /* ungrab the mouse event */
1425 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1426 gtk_grab_remove(GTK_WIDGET(ctree));
1427 if (gdk_pointer_is_grabbed())
1428 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1431 mimeview->textview->default_text = FALSE;
1433 update_signature_info(mimeview, partinfo);
1435 if (!mimeview_show_part(mimeview, partinfo)) {
1436 switch (partinfo->type) {
1438 case MIMETYPE_MESSAGE:
1439 case MIMETYPE_MULTIPART:
1440 mimeview_show_message_part(mimeview, partinfo);
1444 mimeview->textview->default_text = TRUE;
1445 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1446 textview_clear(mimeview->textview);
1447 textview_show_mime_part(mimeview->textview, partinfo);
1451 mainwin = mainwindow_get_mainwindow();
1453 main_window_set_menu_sensitive(mainwin);
1455 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1456 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1457 mimeview_check_signature(mimeview);
1461 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1464 GtkTreeSelection *selection;
1467 if (!event) return FALSE;
1469 if (event->button == 2 || event->button == 3) {
1470 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1471 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1474 gtk_tree_selection_select_iter(selection, &iter);
1476 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1481 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1484 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1485 if (event->direction == GDK_SCROLL_UP) {
1486 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1488 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1490 gtk_vscrollbutton_scroll(scrollbutton);
1494 /* from gdkevents.c */
1495 #define DOUBLE_CLICK_TIME 250
1497 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1500 static MimeInfo *lastinfo;
1501 static guint32 lasttime;
1503 if (event->button == 2 ||
1504 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1505 /* call external program for image, audio or html */
1506 mimeview_launch(mimeview, partinfo);
1508 } else if (event->button == 3) {
1509 MainWindow *mainwin = mainwindow_get_mainwindow();
1511 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1512 partinfo->type == MIMETYPE_IMAGE ||
1513 partinfo->type == MIMETYPE_MULTIPART))
1514 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1516 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1519 partinfo->type == MIMETYPE_APPLICATION &&
1520 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1521 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1524 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1526 main_window_set_menu_sensitive(mainwin);
1527 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1528 "pop_partinfo", partinfo);
1530 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1531 NULL, NULL, NULL, NULL,
1532 event->button, event->time);
1536 lastinfo = partinfo;
1537 lasttime = event->time;
1542 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1544 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1547 void mimeview_select_next_part(MimeView *mimeview)
1549 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1550 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1551 GtkTreeSelection *selection;
1554 MimeInfo *partinfo = NULL;
1557 if (!mimeview->opened) return;
1559 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1560 path = gtk_tree_model_get_path(model, &iter);
1562 has_next = mimeview_tree_next(model, path);
1564 has_next = gtk_tree_model_get_iter_first(model, &iter);
1565 gtk_tree_path_free(path);
1566 path = gtk_tree_model_get_path(model, &iter);
1570 partinfo = mimeview_get_node_part(mimeview, path);
1571 if (partinfo->type == MIMETYPE_MULTIPART ||
1572 (!prefs_common.show_inline_attachments && partinfo->id))
1574 selection = gtk_tree_view_get_selection(ctree);
1575 gtk_tree_selection_select_path(selection, path);
1576 icon_list_toggle_by_mime_info(mimeview, partinfo);
1579 gtk_tree_path_free(path);
1582 void mimeview_select_prev_part(MimeView *mimeview)
1584 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1585 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1586 GtkTreeSelection *selection;
1589 MimeInfo *partinfo = NULL;
1592 if (!mimeview->opened) return;
1594 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1595 path = gtk_tree_model_get_path(model, &iter);
1597 has_prev = mimeview_tree_prev(mimeview, path);
1599 while (mimeview_tree_next(model, path)) {}
1604 partinfo = mimeview_get_node_part(mimeview, path);
1605 if (partinfo->type == MIMETYPE_MULTIPART ||
1606 (!prefs_common.show_inline_attachments && partinfo->id))
1608 selection = gtk_tree_view_get_selection(ctree);
1609 gtk_tree_selection_select_path(selection, path);
1610 icon_list_toggle_by_mime_info(mimeview, partinfo);
1613 gtk_tree_path_free(path);
1616 #define BREAK_ON_MODIFIER_KEY() \
1617 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1619 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1622 SummaryView *summaryview;
1624 if (!event) return FALSE;
1625 if (!mimeview->opened) return FALSE;
1627 summaryview = mimeview->messageview->mainwin->summaryview;
1629 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1632 switch (event->keyval) {
1635 textview_scroll_max(mimeview->textview,
1636 (event->keyval == GDK_KEY_Home));
1638 case GDK_KEY_Page_Down:
1640 if (mimeview_scroll_page(mimeview,
1641 (event->state & GDK_SHIFT_MASK) != 0))
1644 if (!(event->state & GDK_SHIFT_MASK))
1645 mimeview_select_next_part(mimeview);
1647 case GDK_KEY_Page_Up:
1648 case GDK_KEY_BackSpace:
1649 mimeview_scroll_page(mimeview, TRUE);
1651 case GDK_KEY_Return:
1652 case GDK_KEY_KP_Enter:
1653 mimeview_scroll_one_line(mimeview,
1654 (event->state & GDK_MOD1_MASK) != 0);
1658 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1664 if (mimeview->messageview->new_window) return FALSE;
1666 return summary_pass_key_press_event(summaryview, event);
1669 static void mimeview_drag_data_get(GtkWidget *widget,
1670 GdkDragContext *drag_context,
1671 GtkSelectionData *selection_data,
1676 gchar *filename = NULL, *uriname, *tmp;
1681 if (!mimeview->opened) return;
1682 if (!mimeview->file) return;
1684 partinfo = mimeview_get_selected_part(mimeview);
1685 if (!partinfo) return;
1687 if (strlen(get_part_name(partinfo)) > 0) {
1688 filename = g_path_get_basename(get_part_name(partinfo));
1689 if (*filename == '\0') return;
1690 } else if (partinfo->type == MIMETYPE_MESSAGE
1691 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1693 GPtrArray *headers = NULL;
1696 fp = claws_fopen(partinfo->data.filename, "rb");
1697 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1698 headers = procheader_get_header_array_asis(fp);
1701 for (i = 0; i < headers->len; i++) {
1702 Header *header = g_ptr_array_index(headers, i);
1703 if (procheader_headername_equal(header->name, "Subject")) {
1704 unfold_line(header->body);
1705 name = g_strconcat(header->body, ".txt", NULL);
1706 subst_for_filename(name);
1709 procheader_header_array_destroy(headers);
1715 filename = g_path_get_basename(name);
1718 if (filename == NULL)
1719 filename = g_path_get_basename("Unnamed part");
1721 if (!g_utf8_validate(filename, -1, NULL))
1722 tmp = conv_codeset_strdup(filename,
1723 conv_get_locale_charset_str(),
1726 tmp = g_strdup(filename);
1729 g_warning("filename not in UTF-8");
1730 tmp = g_strdup("Unnamed part");
1732 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1736 if (is_file_exist(filename)) {
1738 gchar *prefix = NULL;
1739 gchar *new_name = NULL;
1740 if (strrchr(tmp, '.')) {
1741 prefix = g_strdup(tmp);
1742 ext = g_strdup(strrchr(tmp, '.'));
1743 *(strrchr(prefix, '.')) = '\0';
1745 prefix = g_strdup(tmp);
1749 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1753 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1756 goto check_new_file;
1761 if ((err = procmime_get_part(filename, partinfo)) < 0)
1763 (_("Couldn't save the part of multipart message: %s"),
1766 tmp = g_filename_to_uri(filename, NULL, NULL);
1767 uriname = g_strconcat(tmp, "\r\n", NULL);
1770 gtk_selection_data_set(selection_data,
1771 gtk_selection_data_get_target(selection_data), 8,
1772 (guchar *)uriname, strlen(uriname));
1779 * Returns a filename (with path) for an attachment
1780 * \param partinfo The attachment to save
1781 * \param basedir The target directory
1782 * \param number Used for dummy filename if attachment is unnamed
1784 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1785 const gchar *basedir,
1791 filename = g_strdup(get_part_name(partinfo));
1792 if (!filename || !*filename) {
1794 filename = g_strdup_printf("noname.%d", number);
1797 if (!g_utf8_validate(filename, -1, NULL)) {
1798 gchar *tmp = conv_filename_to_utf8(filename);
1803 subst_for_filename(filename);
1805 fullname = g_strconcat
1806 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1807 ? &filename[1] : filename, NULL);
1810 filename = conv_filename_from_utf8(fullname);
1816 * Write a single attachment to file
1817 * \param filename Filename with path
1818 * \param partinfo Attachment to save
1820 static gboolean mimeview_write_part(const gchar *filename,
1822 gboolean handle_error)
1827 dir= g_path_get_dirname(filename);
1828 if (!is_dir_exist(dir))
1832 if (is_file_exist(filename)) {
1837 if (!g_utf8_validate(filename, -1, NULL))
1838 tmp = conv_filename_to_utf8(filename);
1840 tmp = g_strdup(filename);
1842 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1845 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
1846 GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST);
1848 if (G_ALERTALTERNATE != aval) return FALSE;
1851 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1852 debug_print("error saving MIME part: %d\n", err);
1855 (_("Couldn't save the part of multipart message: %s"),
1863 static AlertValue mimeview_save_all_error_ask(gint n)
1865 gchar *message = g_strdup_printf(
1866 _("An error has occurred while saving message part #%d. "
1867 "Do you want to cancel operation or skip error and "
1869 AlertValue av = alertpanel_full(_("Error saving all message parts"),
1870 message, GTK_STOCK_CANCEL, _("Skip"), _("Skip all"),
1871 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1876 static void mimeview_save_all_info(gint errors, gint total)
1879 gchar *msg = g_strdup_printf(
1880 ngettext("%d file saved successfully.",
1881 "%d files saved successfully.",
1884 alertpanel_notice("%s", msg);
1887 gchar *msg1 = g_strdup_printf(
1888 ngettext("%d file saved successfully",
1889 "%d files saved successfully",
1892 gchar *msg2 = g_strdup_printf(
1893 ngettext("%s, %d file failed.",
1894 "%s, %d files failed.",
1897 alertpanel_warning("%s", msg2);
1904 * Menu callback: Save all attached files
1905 * \param mimeview Current display
1907 static void mimeview_save_all(MimeView *mimeview)
1911 gchar *startdir = NULL;
1912 gint number = 1, errors = 0;
1913 gboolean skip_errors = FALSE;
1915 if (!mimeview->opened) return;
1916 if (!mimeview->file) return;
1917 if (!mimeview->mimeinfo) return;
1919 partinfo = mimeview->mimeinfo;
1920 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1921 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1923 startdir = g_strdup(get_home_dir());
1925 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1926 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1932 if (!is_dir_exist (dirname)) {
1933 alertpanel_error(_("'%s' is not a directory."), dirname);
1939 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1940 dirname[strlen(dirname)-1] = '\0';
1942 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1943 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1944 partinfo = procmime_mimeinfo_next(partinfo);
1945 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1946 partinfo = procmime_mimeinfo_next(partinfo);
1947 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1948 partinfo = procmime_mimeinfo_next(partinfo);
1951 while (partinfo != NULL) {
1952 if (partinfo->type != MIMETYPE_MESSAGE &&
1953 partinfo->type != MIMETYPE_MULTIPART &&
1954 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1955 || get_real_part_name(partinfo) != NULL)) {
1956 gchar *filename = mimeview_get_filename_for_part(
1957 partinfo, dirname, number++);
1959 gboolean ok = mimeview_write_part(filename, partinfo, FALSE);
1964 AlertValue av = mimeview_save_all_error_ask(number - 1);
1965 skip_errors = (av == G_ALERTOTHER);
1966 if (av == G_ALERTDEFAULT) /* cancel */
1971 partinfo = procmime_mimeinfo_next(partinfo);
1974 g_free(prefs_common.attach_save_dir);
1976 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1977 -1, NULL, NULL, NULL);
1980 mimeview_save_all_info(errors, number - 1);
1983 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1985 MimeInfo *partinfo = NULL;
1986 if (mimeview->spec_part) {
1987 partinfo = mimeview->spec_part;
1988 mimeview->spec_part = NULL;
1990 partinfo = (MimeInfo *) g_object_get_data
1991 (G_OBJECT(mimeview->popupmenu),
1993 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1994 "pop_partinfo", NULL);
1996 partinfo = mimeview_get_selected_part(mimeview);
2003 * Menu callback: Save the selected attachment
2004 * \param mimeview Current display
2006 void mimeview_save_as(MimeView *mimeview)
2009 gchar *filepath = NULL;
2010 gchar *filedir = NULL;
2012 gchar *partname = NULL;
2014 if (!mimeview->opened) return;
2015 if (!mimeview->file) return;
2017 partinfo = mimeview_get_part_to_use(mimeview);
2019 cm_return_if_fail(partinfo != NULL);
2021 if (get_part_name(partinfo) == NULL) {
2024 partname = g_strdup(get_part_name(partinfo));
2026 if (!g_utf8_validate(partname, -1, NULL)) {
2027 gchar *tmp = conv_filename_to_utf8(partname);
2029 tmp = conv_codeset_strdup(partname,
2030 conv_get_locale_charset_str(),
2039 subst_for_filename(partname);
2041 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2042 filepath = g_strconcat(prefs_common.attach_save_dir,
2043 G_DIR_SEPARATOR_S, partname, NULL);
2045 filepath = g_strdup(partname);
2049 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2050 filename = filesel_select_file_save(_("Save as"), filepath);
2056 mimeview_write_part(filename, partinfo, TRUE);
2058 filedir = g_path_get_dirname(filename);
2059 if (filedir && strcmp(filedir, ".")) {
2060 g_free(prefs_common.attach_save_dir);
2061 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2068 void mimeview_display_as_text(MimeView *mimeview)
2072 if (!mimeview->opened) return;
2074 partinfo = mimeview_get_part_to_use(mimeview);
2075 mimeview_select_mimepart_icon(mimeview, partinfo);
2076 cm_return_if_fail(partinfo != NULL);
2077 mimeview_show_message_part(mimeview, partinfo);
2080 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2085 if (!mimeview->opened) return;
2086 if (!mimeview->file) return;
2089 partinfo = mimeview_get_part_to_use(mimeview);
2091 cm_return_if_fail(partinfo != NULL);
2093 filename = procmime_get_tmp_file_name(partinfo);
2095 if ((err = procmime_get_part(filename, partinfo)) < 0)
2097 (_("Couldn't save the part of multipart message: %s"),
2100 mimeview_view_file(filename, partinfo, NULL, mimeview);
2106 void mimeview_open_with(MimeView *mimeview)
2110 if (!mimeview) return;
2111 if (!mimeview->opened) return;
2112 if (!mimeview->file) return;
2114 partinfo = mimeview_get_part_to_use(mimeview);
2116 mimeview_open_part_with(mimeview, partinfo, FALSE);
2119 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2123 gchar *mime_command = NULL;
2124 gchar *content_type = NULL;
2127 cm_return_if_fail(partinfo != NULL);
2129 filename = procmime_get_tmp_file_name(partinfo);
2131 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2133 (_("Couldn't save the part of multipart message: %s"),
2139 if (!prefs_common.mime_open_cmd_history)
2140 prefs_common.mime_open_cmd_history =
2141 add_history(NULL, prefs_common.mime_open_cmd);
2143 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2144 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2145 /* guess content-type from filename */
2146 content_type = procmime_get_mime_type(filename);
2148 if (content_type == NULL) {
2149 content_type = procmime_get_content_type_str(partinfo->type,
2153 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2154 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2155 mime_command = g_strdup(prefs_common_get_uri_cmd());
2156 g_free(content_type);
2157 content_type = NULL;
2158 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2159 || !prefs_common_get_ext_editor_cmd()[0]) {
2160 mime_command = mailcap_get_command_for_type(content_type, filename);
2162 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2163 g_free(content_type);
2164 content_type = NULL;
2166 if (mime_command == NULL) {
2167 /* try with extension this time */
2168 g_free(content_type);
2169 content_type = procmime_get_mime_type(filename);
2170 mime_command = mailcap_get_command_for_type(content_type, filename);
2173 if (mime_command == NULL)
2177 gboolean remember = FALSE;
2178 if (content_type != NULL)
2179 cmd = input_dialog_combo_remember
2181 _("Enter the command-line to open file:\n"
2182 "('%s' will be replaced with file name)"),
2183 mime_command ? mime_command : prefs_common.mime_open_cmd,
2184 prefs_common.mime_open_cmd_history, &remember);
2186 cmd = input_dialog_combo
2188 _("Enter the command-line to open file:\n"
2189 "('%s' will be replaced with file name)"),
2190 mime_command ? mime_command : prefs_common.mime_open_cmd,
2191 prefs_common.mime_open_cmd_history);
2192 if (cmd && remember) {
2193 mailcap_update_default(content_type, cmd);
2195 g_free(mime_command);
2200 mimeview_view_file(filename, partinfo, cmd, mimeview);
2201 g_free(prefs_common.mime_open_cmd);
2202 prefs_common.mime_open_cmd = cmd;
2203 prefs_common.mime_open_cmd_history =
2204 add_history(prefs_common.mime_open_cmd_history, cmd);
2207 g_free(content_type);
2212 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2214 GList *attach_file = NULL;
2215 AttachInfo *ainfo = NULL;
2219 if (!mimeview->opened) return;
2220 if (!mimeview->file) return;
2222 cm_return_if_fail(partinfo != NULL);
2224 filename = procmime_get_tmp_file_name(partinfo);
2226 if (!(err = procmime_get_part(filename, partinfo))) {
2227 ainfo = g_new0(AttachInfo, 1);
2228 ainfo->file = filename;
2229 ainfo->name = g_strdup(get_part_name(partinfo));
2230 ainfo->content_type = procmime_get_content_type_str(
2231 partinfo->type, partinfo->subtype);
2232 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2233 partinfo, "charset"));
2234 attach_file = g_list_append(attach_file, ainfo);
2236 compose_new(NULL, NULL, attach_file);
2238 g_free(ainfo->name);
2239 g_free(ainfo->content_type);
2240 g_free(ainfo->charset);
2242 g_list_free(attach_file);
2245 (_("Couldn't save the part of multipart message: %s"),
2250 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2251 const gchar *cmd, MimeView *mimeview)
2255 gchar buf[BUFFSIZE];
2258 mimeview_open_part_with(mimeview, partinfo, TRUE);
2260 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2261 !strchr(p + 2, '%')) {
2262 g_snprintf(buf, sizeof(buf), cmd, filename);
2263 if (!prefs_common.save_parts_readwrite)
2264 g_chmod(filename, S_IRUSR);
2266 g_chmod(filename, S_IRUSR|S_IWUSR);
2268 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2269 mimeview_open_part_with(mimeview, partinfo, FALSE);
2271 if (execute_command_line(buf, TRUE, NULL) != 0) {
2272 if (!prefs_common.save_parts_readwrite)
2273 g_chmod(filename, S_IRUSR|S_IWUSR);
2274 mimeview_open_part_with(mimeview, partinfo, FALSE);
2278 SHFILEINFO file_info;
2279 GError *error = NULL;
2280 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2282 if (error != NULL) {
2283 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2285 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2286 g_error_free(error);
2290 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2291 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2292 _("This attachment is an executable file. Executing "
2293 "untrusted binaries is dangerous and could compromise "
2294 "your computer.\n\n"
2295 "Do you want to run this file?"), GTK_STOCK_CANCEL,
2297 NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
2298 if (val == G_ALERTALTERNATE) {
2299 debug_print("executing binary\n");
2300 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2303 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2311 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2313 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2316 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2318 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2321 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2323 GSList *mimeview_list, *viewer_list;
2325 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2326 MimeView *mimeview = (MimeView *) mimeview_list->data;
2328 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2329 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2330 mimeview->mimeviewer = NULL;
2333 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2334 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2336 mimeviewer->destroy_viewer(mimeviewer);
2337 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2341 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2344 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2349 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2350 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2352 if (event->button == 1) {
2353 icon_selected(mimeview, num, partinfo);
2354 gtk_widget_grab_focus(button);
2355 icon_list_toggle_by_mime_info(mimeview, partinfo);
2357 part_button_pressed(mimeview, event, partinfo);
2362 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2364 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2367 MimeInfo *curr = NULL;
2369 if (!gtk_tree_model_get_iter_first(model, &iter))
2371 path = gtk_tree_model_get_path(model, &iter);
2374 gtk_tree_model_get_iter(model, &iter, path);
2375 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2376 if (curr == partinfo) {
2377 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2378 GTK_TREE_VIEW(mimeview->ctree));
2379 gtk_tree_selection_select_iter(sel, &iter);
2380 gtk_tree_path_free(path);
2383 } while (mimeview_tree_next(model, path));
2385 gtk_tree_path_free(path);
2388 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2390 icon_list_toggle_by_mime_info(mimeview, partinfo);
2391 icon_selected(mimeview, -1, partinfo);
2394 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2397 SummaryView *summaryview;
2399 if (!event) return FALSE;
2401 switch (event->keyval) {
2403 if (mimeview_scroll_page(mimeview, FALSE))
2406 mimeview_select_next_part(mimeview);
2410 case GDK_KEY_BackSpace:
2411 mimeview_scroll_page(mimeview, TRUE);
2413 case GDK_KEY_Return:
2414 case GDK_KEY_KP_Enter:
2415 mimeview_scroll_one_line(mimeview,
2416 (event->state & GDK_MOD1_MASK) != 0);
2419 BREAK_ON_MODIFIER_KEY();
2420 mimeview_save_as(mimeview);
2423 BREAK_ON_MODIFIER_KEY();
2424 mimeview_display_as_text(mimeview);
2427 BREAK_ON_MODIFIER_KEY();
2428 mimeview_launch(mimeview, NULL);
2432 BREAK_ON_MODIFIER_KEY();
2433 mimeview_open_with(mimeview);
2437 BREAK_ON_MODIFIER_KEY();
2438 mimeview_check_signature(mimeview);
2441 BREAK_ON_MODIFIER_KEY();
2442 mimeview_select_next_part(mimeview);
2448 if (!mimeview->messageview->mainwin) return FALSE;
2449 summaryview = mimeview->messageview->mainwin->summaryview;
2450 return summary_pass_key_press_event(summaryview, event);
2453 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2455 MimeView *mimeview = (MimeView *)data;
2456 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2458 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2459 "pop_partinfo", partinfo);
2460 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
2461 NULL, NULL, NULL, NULL,
2462 0, gtk_get_current_event_time());
2466 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2468 GtkWidget *pixmap = NULL;
2473 const gchar *desc = NULL;
2474 gchar *sigshort = NULL;
2475 gchar *content_type;
2478 MimeInfo *siginfo = NULL;
2479 MimeInfo *encrypted = NULL;
2484 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2487 vbox = mimeview->icon_vbox;
2488 mimeview->icon_count++;
2489 button = gtk_event_box_new();
2491 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2492 G_CALLBACK(mimeview_visi_notify), mimeview);
2493 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2494 G_CALLBACK(mimeview_leave_notify), mimeview);
2495 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2496 G_CALLBACK(mimeview_enter_notify), mimeview);
2498 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2499 g_object_set_data(G_OBJECT(button), "icon_number",
2500 GINT_TO_POINTER(mimeview->icon_count));
2501 g_object_set_data(G_OBJECT(button), "partinfo",
2504 switch (mimeinfo->type) {
2507 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2508 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2509 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2510 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2511 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2512 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2513 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2514 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2515 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2517 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2519 case MIMETYPE_MESSAGE:
2520 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2522 case MIMETYPE_APPLICATION:
2523 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2524 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2525 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2526 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2527 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2528 stockp = STOCK_PIXMAP_MIME_PDF;
2529 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2530 stockp = STOCK_PIXMAP_MIME_PS;
2532 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2534 case MIMETYPE_IMAGE:
2535 stockp = STOCK_PIXMAP_MIME_IMAGE;
2537 case MIMETYPE_AUDIO:
2538 stockp = STOCK_PIXMAP_MIME_AUDIO;
2541 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2545 partinfo = mimeinfo;
2546 while (partinfo != NULL) {
2547 if (privacy_mimeinfo_is_signed(partinfo)) {
2551 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2552 encrypted = partinfo;
2555 partinfo = procmime_mimeinfo_parent(partinfo);
2558 if (siginfo != NULL) {
2559 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2560 case SIGNATURE_UNCHECKED:
2561 case SIGNATURE_CHECK_FAILED:
2562 case SIGNATURE_CHECK_TIMEOUT:
2563 pixmap = stock_pixmap_widget_with_overlay(stockp,
2564 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2567 pixmap = stock_pixmap_widget_with_overlay(stockp,
2568 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2570 case SIGNATURE_WARN:
2571 case SIGNATURE_KEY_EXPIRED:
2572 pixmap = stock_pixmap_widget_with_overlay(stockp,
2573 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2575 case SIGNATURE_INVALID:
2576 pixmap = stock_pixmap_widget_with_overlay(stockp,
2577 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2580 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2581 } else if (encrypted != NULL) {
2582 pixmap = stock_pixmap_widget_with_overlay(stockp,
2583 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2585 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2586 OVERLAY_NONE, 6, 3);
2588 gtk_container_add(GTK_CONTAINER(button), pixmap);
2590 if (prefs_common.attach_desc)
2591 desc = get_part_description(mimeinfo);
2593 desc = get_part_name(mimeinfo);
2596 content_type = procmime_get_content_type_str(mimeinfo->type,
2599 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2600 "\n<b>", _("Size:"), " </b>",
2601 to_human_readable((goffset)mimeinfo->length), NULL);
2602 g_free(content_type);
2603 if (desc && *desc) {
2604 gchar *tmp = NULL, *escaped = NULL;
2605 if (!g_utf8_validate(desc, -1, NULL)) {
2606 tmp = conv_filename_to_utf8(desc);
2608 tmp = g_strdup(desc);
2610 escaped = g_markup_escape_text(tmp,-1);
2612 tiptmp = g_strconcat(tip, "\n<b>",
2613 prefs_common.attach_desc && mimeinfo->description ?
2614 _("Description:") : _("Filename:"),
2615 " </b>", escaped, NULL);
2621 if (sigshort && *sigshort) {
2622 tiptmp = g_strjoin("\n", tip, g_markup_escape_text(sigshort, -1), NULL);
2628 gtk_widget_set_tooltip_markup(button, tip);
2630 gtk_widget_show_all(button);
2631 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2632 mimeview_mime_types, 1, GDK_ACTION_COPY);
2634 g_signal_connect(G_OBJECT(button), "popup-menu",
2635 G_CALLBACK(icon_popup_menu), mimeview);
2636 g_signal_connect(G_OBJECT(button), "button_release_event",
2637 G_CALLBACK(icon_clicked_cb), mimeview);
2638 g_signal_connect(G_OBJECT(button), "key_press_event",
2639 G_CALLBACK(icon_key_pressed), mimeview);
2640 g_signal_connect(G_OBJECT(button), "drag_data_get",
2641 G_CALLBACK(mimeview_drag_data_get), mimeview);
2642 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2644 gtk_widget_size_request(pixmap, &r);
2645 gtk_widget_set_size_request(button, -1, r.height + 4);
2650 static void icon_list_clear (MimeView *mimeview)
2652 GList *child, *orig;
2655 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2656 for (child = orig; child != NULL; child = g_list_next(child)) {
2657 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
2658 GTK_WIDGET(child->data));
2661 mimeview->icon_count = 0;
2662 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2663 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2664 gtk_adjustment_changed(adj);
2668 *\brief Used to 'click' the next or previous icon.
2670 *\return true if the icon 'number' exists and was selected.
2672 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2673 GtkAllocation *size, MimeView *mimeview)
2675 GtkAllocation vbox_size;
2676 GtkAllocation layout_size;
2681 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2683 gtk_widget_get_allocation(mimeview->icon_vbox, &vbox_size);
2684 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2686 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2687 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2689 MAX(vbox_size.height, layout_size.height));
2690 gtk_adjustment_set_step_increment(adj, 10);
2693 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2695 GtkRequisition size;
2696 GtkRequisition requisition;
2698 cm_return_if_fail(mimeinfo != NULL);
2700 while (mimeinfo != NULL) {
2701 if (mimeinfo->type != MIMETYPE_MULTIPART)
2702 icon_list_append_icon(mimeview, mimeinfo);
2703 if (mimeinfo->node->children != NULL)
2704 icon_list_create(mimeview,
2705 (MimeInfo *) mimeinfo->node->children->data);
2706 mimeinfo = mimeinfo->node->next != NULL
2707 ? (MimeInfo *) mimeinfo->node->next->data
2710 gtk_widget_size_request(mimeview->icon_vbox, &size);
2711 gtk_widget_get_requisition(mimeview->icon_mainbox, &requisition);
2712 if (size.width > requisition.width) {
2713 gtk_widget_set_size_request(mimeview->icon_mainbox,
2716 if (mimeview->opened)
2717 icon_list_toggle_by_mime_info(mimeview,
2718 mimeview_get_node_part(mimeview, mimeview->opened));
2721 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2724 GList *children, *child;
2726 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2727 for (child = children; child != NULL; child = g_list_next(child)) {
2728 gboolean *highlight = NULL;
2729 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2731 if (!GTK_IS_EVENT_BOX(child->data))
2734 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2735 *highlight = (g_object_get_data(G_OBJECT(child->data),
2736 "partinfo") == (gpointer)mimeinfo);
2738 gtk_widget_queue_draw(icon);
2740 g_list_free(children);
2743 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2746 prefs_common.mimeview_tree_height = allocation->height;
2749 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2752 g_object_ref(button);
2754 mimeview_leave_notify(button, NULL, NULL);
2756 mimeview->ctree_mode = !mimeview->ctree_mode;
2757 if (mimeview->ctree_mode) {
2758 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2760 gtk_widget_hide(mimeview->icon_mainbox);
2761 gtk_widget_show(mimeview->ctree_mainbox);
2762 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2763 prefs_common.mimeview_tree_height);
2765 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2767 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2768 button, FALSE, FALSE, 0);
2770 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2772 gtk_widget_hide(mimeview->ctree_mainbox);
2773 gtk_widget_show(mimeview->icon_mainbox);
2774 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2776 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2778 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2779 button, FALSE, FALSE, 0);
2780 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2781 if (mimeview->opened)
2782 icon_list_toggle_by_mime_info(mimeview,
2783 mimeview_get_node_part(mimeview, mimeview->opened));
2784 summary_grab_focus(mimeview->mainwin->summaryview);
2786 g_object_unref(button);
2790 void mimeview_update (MimeView *mimeview)
2792 if (mimeview && mimeview->mimeinfo) {
2793 icon_list_clear(mimeview);
2794 icon_list_create(mimeview, mimeview->mimeinfo);
2798 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2800 MessageView *msgview = NULL;
2801 MainWindow *mainwin = NULL;
2806 msgview = mimeview->messageview;
2810 mainwin = msgview->mainwin;
2814 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2815 "pop_partinfo", NULL);
2817 if (!strcmp(cmd, "sc://view_log"))
2818 log_window_show(mainwin->logwin);
2819 else if (!strcmp(cmd, "sc://save_as"))
2820 mimeview_save_as(mimeview);
2821 else if (!strcmp(cmd, "sc://display_as_text"))
2822 mimeview_display_as_text(mimeview);
2824 else if (!strcmp(cmd, "sc://open_with"))
2825 mimeview_open_with(mimeview);
2827 else if (!strcmp(cmd, "sc://open"))
2828 mimeview_launch(mimeview, NULL);
2829 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2830 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2831 icon_selected(mimeview, -1, (MimeInfo *)data);
2832 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2833 mimeview_launch(mimeview, (MimeInfo *)data);
2834 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2835 mimeview->spec_part = (MimeInfo *)data;
2836 part_button_pressed(mimeview, event, (MimeInfo *)data);
2837 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2838 const gchar *tagname = cmd + strlen("sc://search_tags:");
2839 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2840 gtk_toggle_button_set_active(
2841 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2843 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2844 ADVANCED_SEARCH_EXTENDED, buf);
2849 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2851 if (mimeview->type == MIMEVIEW_TEXT)
2852 return textview_scroll_page(mimeview->textview, up);
2853 else if (mimeview->mimeviewer) {
2854 MimeViewer *mimeviewer = mimeview->mimeviewer;
2855 if (mimeviewer->scroll_page)
2856 return mimeviewer->scroll_page(mimeviewer, up);
2861 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2863 if (mimeview->type == MIMEVIEW_TEXT)
2864 textview_scroll_one_line(mimeview->textview, up);
2865 else if (mimeview->mimeviewer) {
2866 MimeViewer *mimeviewer = mimeview->mimeviewer;
2867 if (mimeviewer->scroll_one_line)
2868 mimeviewer->scroll_one_line(mimeviewer, up);