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"
67 #define N_MIMEVIEW_COLS 4
69 static void mimeview_set_multipart_tree (MimeView *mimeview,
72 static void mimeview_show_message_part (MimeView *mimeview,
74 static void mimeview_change_view_type (MimeView *mimeview,
76 static gchar *mimeview_get_filename_for_part (MimeInfo *partinfo,
79 static gboolean mimeview_write_part (const gchar *filename,
81 gboolean handle_error);
83 static void mimeview_selected (GtkTreeSelection *selection,
85 static gint mimeview_button_pressed (GtkWidget *widget,
86 GdkEventButton *event,
88 static gint mimeview_key_pressed (GtkWidget *widget,
92 static void mimeview_drag_data_get (GtkWidget *widget,
93 GdkDragContext *drag_context,
94 GtkSelectionData *selection_data,
99 static gboolean mimeview_scrolled (GtkWidget *widget,
100 GdkEventScroll *event,
103 static void mimeview_save_all (MimeView *mimeview);
105 static void mimeview_open_part_with (MimeView *mimeview,
109 static void mimeview_send_to (MimeView *mimeview,
111 static void mimeview_view_file (const gchar *filename,
115 static gboolean icon_clicked_cb (GtkWidget *button,
116 GdkEventButton *event,
118 static void icon_selected (MimeView *mimeview,
121 static gint icon_key_pressed (GtkWidget *button,
124 static void icon_list_append_icon (MimeView *mimeview,
126 static void icon_list_create (MimeView *mimeview,
128 static void icon_list_clear (MimeView *mimeview);
129 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
131 static void ctree_size_allocate_cb (GtkWidget *widget,
132 GtkAllocation *allocation,
134 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
136 static gboolean part_button_pressed (MimeView *mimeview,
137 GdkEventButton *event,
139 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
140 GtkAllocation *layout_size,
142 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview);
143 static const gchar *get_part_name(MimeInfo *partinfo);
144 static const gchar *get_part_description(MimeInfo *partinfo);
146 static void mimeview_launch_cb(GtkAction *action, gpointer data)
148 MimeView *mimeview = (MimeView *)data;
149 mimeview_launch(mimeview, mimeview_get_part_to_use(mimeview));
153 static void mimeview_open_with_cb(GtkAction *action, gpointer data)
155 mimeview_open_with((MimeView *)data);
159 static void mimeview_send_to_cb(GtkAction *action, gpointer data)
161 MimeView *mimeview = (MimeView *)data;
162 mimeview_send_to(mimeview, mimeview_get_part_to_use(mimeview));
165 static void mimeview_display_as_text_cb(GtkAction *action, gpointer data)
167 mimeview_display_as_text((MimeView *)data);
170 static void mimeview_save_as_cb(GtkAction *action, gpointer data)
172 mimeview_save_as((MimeView *)data);
175 static void mimeview_save_all_cb(GtkAction *action, gpointer data)
177 mimeview_save_all((MimeView *)data);
180 static void mimeview_select_next_part_cb(GtkAction *action, gpointer data)
182 mimeview_select_next_part((MimeView *)data);
185 static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
187 mimeview_select_prev_part((MimeView *)data);
190 static GtkActionEntry mimeview_menu_actions[] = {
191 { "MimeView", NULL, "MimeView" },
192 { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
193 #if (!defined G_OS_WIN32)
194 { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
196 { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
197 { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
198 { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
199 { "MimeView/SaveAll", NULL, N_("Save _all..."), NULL, "Save all parts", G_CALLBACK(mimeview_save_all_cb) },
200 { "MimeView/NextPart", NULL, N_("Next part"), NULL, "Next part", G_CALLBACK(mimeview_select_next_part_cb) },
201 { "MimeView/PrevPart", NULL, N_("Previous part"), NULL, "Previous part", G_CALLBACK(mimeview_select_prev_part_cb) }
204 static GtkTargetEntry mimeview_mime_types[] =
206 {"text/uri-list", 0, 0}
209 GSList *mimeviewer_factories;
212 static GdkCursor *hand_cursor = NULL;
214 static gboolean mimeview_visi_notify(GtkWidget *widget,
215 GdkEventVisibility *event,
218 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
222 static gboolean mimeview_leave_notify(GtkWidget *widget,
223 GdkEventCrossing *event,
226 gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
230 static gboolean mimeview_enter_notify(GtkWidget *widget,
231 GdkEventCrossing *event,
234 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
238 MimeView *mimeview_create(MainWindow *mainwin)
243 GtkWidget *scrolledwin;
245 GtkWidget *mime_notebook;
246 GtkWidget *popupmenu;
247 GtkWidget *ctree_mainbox;
249 GtkWidget *mime_toggle;
250 GtkWidget *icon_mainbox;
251 GtkWidget *icon_scroll;
252 GtkWidget *icon_vbox;
254 GtkWidget *scrollbutton;
256 NoticeView *siginfoview;
259 GtkCellRenderer *renderer;
260 GtkTreeViewColumn *column;
261 GtkTreeSelection *selection;
263 gchar *titles[N_MIMEVIEW_COLS];
267 hand_cursor = gdk_cursor_new(GDK_HAND2);
269 debug_print("Creating MIME view...\n");
270 mimeview = g_new0(MimeView, 1);
272 titles[COL_MIMETYPE] = _("MIME Type");
273 titles[COL_SIZE] = _("Size");
274 titles[COL_NAME] = _("Name");
276 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
277 gtk_widget_show(scrolledwin);
278 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
279 GTK_POLICY_AUTOMATIC,
280 GTK_POLICY_AUTOMATIC);
282 model = gtk_tree_store_new(N_MIMEVIEW_COLS,
288 ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
289 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
290 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
291 prefs_common.use_stripes_everywhere);
292 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ctree),
293 prefs_common.show_col_headers);
295 renderer = gtk_cell_renderer_text_new();
296 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
297 -1, titles[COL_MIMETYPE],renderer,
298 "text", COL_MIMETYPE, NULL);
299 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
301 renderer = gtk_cell_renderer_text_new();
302 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
303 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
304 -1, titles[COL_SIZE], renderer,
305 "text", COL_SIZE, NULL);
306 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
307 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
309 renderer = gtk_cell_renderer_text_new();
310 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
311 -1, titles[COL_NAME], renderer,
312 "text", COL_NAME, NULL);
313 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
314 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
316 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
317 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
319 gtk_widget_show(ctree);
320 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
321 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
322 mimeview_mime_types, 1, GDK_ACTION_COPY);
324 g_signal_connect(G_OBJECT(selection), "changed",
325 G_CALLBACK(mimeview_selected), mimeview);
326 g_signal_connect(G_OBJECT(ctree), "button_release_event",
327 G_CALLBACK(mimeview_button_pressed), mimeview);
328 g_signal_connect(G_OBJECT(ctree), "key_press_event",
329 G_CALLBACK(mimeview_key_pressed), mimeview);
330 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
331 G_CALLBACK(mimeview_drag_data_get), mimeview);
333 mime_notebook = gtk_notebook_new();
334 gtk_widget_show(mime_notebook);
335 gtk_widget_set_can_focus(mime_notebook, FALSE);
336 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
337 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
339 icon_vbox = gtk_vbox_new(FALSE, 2);
340 gtk_widget_show(icon_vbox);
341 icon_scroll = gtk_layout_new(NULL, NULL);
342 gtk_widget_show(icon_scroll);
343 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
344 scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
345 gtk_widget_show(scrollbutton);
347 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
348 G_CALLBACK(mimeview_scrolled), mimeview);
350 mime_toggle = gtk_event_box_new();
351 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
353 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
354 G_CALLBACK(mimeview_visi_notify), mimeview);
355 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
356 G_CALLBACK(mimeview_leave_notify), mimeview);
357 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
358 G_CALLBACK(mimeview_enter_notify), mimeview);
360 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
361 gtk_widget_show(mime_toggle);
362 mimeview->ctree_mode = FALSE;
363 arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
364 gtk_widget_show(arrow);
365 gtk_widget_size_request(arrow, &r);
366 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
367 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
368 G_CALLBACK(mime_toggle_button_cb), mimeview);
370 icon_mainbox = gtk_vbox_new(FALSE, 0);
371 gtk_widget_show(icon_mainbox);
372 gtk_widget_set_size_request(icon_mainbox, 32, -1);
373 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
374 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
375 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
376 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
377 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
379 ctree_mainbox = gtk_hbox_new(FALSE, 0);
380 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
381 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
382 G_CALLBACK(ctree_size_allocate_cb), mimeview);
384 mimeview->ui_manager = gtk_ui_manager_new();
385 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
386 "MimeView", mimeview_menu_actions,
387 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
389 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
390 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
391 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
392 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
393 "/Menus/MimeView/", "Open", "MimeView/Open",
394 GTK_UI_MANAGER_MENUITEM);
395 #if (!defined G_OS_WIN32)
396 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
397 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
398 GTK_UI_MANAGER_MENUITEM);
400 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
401 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
402 GTK_UI_MANAGER_MENUITEM);
403 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
404 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
405 GTK_UI_MANAGER_MENUITEM);
406 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
407 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
408 GTK_UI_MANAGER_MENUITEM);
409 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
410 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
411 GTK_UI_MANAGER_MENUITEM);
412 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
413 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
414 GTK_UI_MANAGER_MENUITEM);
415 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
416 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
417 GTK_UI_MANAGER_MENUITEM);
419 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
420 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
423 vbox = gtk_vbox_new(FALSE, 0);
424 gtk_widget_show(vbox);
425 siginfoview = noticeview_create(mainwin);
426 noticeview_hide(siginfoview);
427 noticeview_set_icon_clickable(siginfoview, TRUE);
428 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
429 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
431 paned = gtk_vpaned_new();
432 gtk_widget_show(paned);
433 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
434 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
436 hbox = gtk_hbox_new(FALSE, 0);
437 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
438 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
440 gtk_widget_show(hbox);
441 gtk_widget_hide(ctree_mainbox);
443 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
445 mimeview->hbox = hbox;
446 mimeview->paned = paned;
447 mimeview->scrolledwin = scrolledwin;
448 mimeview->ctree = ctree;
449 mimeview->mime_notebook = mime_notebook;
450 mimeview->popupmenu = popupmenu;
452 mimeview->ctree_mainbox = ctree_mainbox;
453 mimeview->icon_scroll = icon_scroll;
454 mimeview->icon_vbox = icon_vbox;
455 mimeview->icon_mainbox = icon_mainbox;
456 mimeview->icon_count = 0;
457 mimeview->mainwin = mainwin;
458 mimeview->mime_toggle = mime_toggle;
459 mimeview->siginfoview = siginfoview;
460 mimeview->scrollbutton = scrollbutton;
461 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
463 mimeviews = g_slist_prepend(mimeviews, mimeview);
468 void mimeview_init(MimeView *mimeview)
470 textview_init(mimeview->textview);
472 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
473 GTK_WIDGET_PTR(mimeview->textview));
476 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
479 if (privacy_mimeinfo_is_signed(mimeinfo))
481 mimeinfo = procmime_mimeinfo_next(mimeinfo);
487 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
490 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
492 mimeview_clear(mimeview);
494 cm_return_if_fail(file != NULL);
495 cm_return_if_fail(mimeinfo != NULL);
497 mimeview->mimeinfo = mimeinfo;
499 mimeview->file = g_strdup(file);
501 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
504 /* check if the mail's signed - it can change the mail structure */
506 if (any_part_is_signed(mimeinfo))
507 debug_print("signed mail\n");
509 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
510 gtk_tree_view_expand_all(ctree);
511 icon_list_clear(mimeview);
512 icon_list_create(mimeview, mimeinfo);
514 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
515 mimeview_selected, mimeview);
519 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
522 static void mimeview_free_mimeinfo(MimeView *mimeview)
524 gboolean defer = FALSE;
526 defer = (mimeview->check_data != NULL);
528 mimeview->check_data->free_after_use = TRUE;
530 if (mimeview->mimeinfo != NULL && !defer) {
531 procmime_mimeinfo_free_all(&mimeview->mimeinfo);
532 mimeview->mimeinfo = NULL;
535 debug_print("deferring free(mimeinfo) and cancelling check\n");
536 mimeview_check_sig_cancel_now(mimeview);
541 void mimeview_destroy(MimeView *mimeview)
545 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
546 MimeViewer *viewer = (MimeViewer *) cur->data;
547 viewer->destroy_viewer(viewer);
549 g_slist_free(mimeview->viewers);
550 gtk_target_list_unref(mimeview->target_list);
553 if (mimeview->check_data) {
554 mimeview->check_data->destroy_mimeview = TRUE;
555 debug_print("deferring destroy\n");
559 mimeview_free_mimeinfo(mimeview);
560 gtk_tree_path_free(mimeview->opened);
561 g_free(mimeview->file);
563 mimeviews = g_slist_remove(mimeviews, mimeview);
568 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
570 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
572 GtkTreeSelection *selection;
573 MimeInfo *partinfo = NULL;
575 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
576 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
577 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
582 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
584 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
588 gtk_tree_model_get_iter(model, &iter, path);
589 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
593 gboolean mimeview_tree_is_empty(MimeView *mimeview)
595 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
597 return !gtk_tree_model_get_iter_first(model, &iter);
600 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
602 GtkTreeIter iter, parent;
605 gtk_tree_model_get_iter(model, &iter, path);
607 if (gtk_tree_model_iter_has_child(model, &iter)) {
608 gtk_tree_path_down(path);
612 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
614 if (!gtk_tree_model_iter_next(model, &iter)) {
616 GtkTreeIter saved_parent = parent;
617 gtk_tree_path_up(path);
618 if (gtk_tree_model_iter_next(model, &parent)) {
619 gtk_tree_path_next(path);
622 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
627 gtk_tree_path_next(path);
634 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
636 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
637 GtkTreeIter iter, child, parent;
640 gtk_tree_model_get_iter(model, &iter, path);
641 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
643 if (gtk_tree_path_prev(path)) {
644 gtk_tree_model_get_iter(model, &iter, path);
646 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
647 gtk_tree_path_down(path);
649 while (gtk_tree_model_iter_next(model, &child))
650 gtk_tree_path_next(path);
657 gtk_tree_path_up(path);
664 gint mimeview_get_selected_part_num(MimeView *mimeview)
666 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
671 gtk_tree_model_get_iter_first(model, &iter);
672 path = gtk_tree_model_get_path(model, &iter);
675 if (!gtk_tree_path_compare(mimeview->opened, path)) {
676 gtk_tree_path_free(path);
681 } while (mimeview_tree_next(model, path));
683 gtk_tree_path_free(path);
688 void mimeview_select_part_num(MimeView *mimeview, gint i)
690 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
693 GtkTreeSelection *selection;
699 gtk_tree_model_get_iter_first(model, &iter);
700 path = gtk_tree_model_get_path(model, &iter);
703 if (!mimeview_tree_next(model, path)) {
704 gtk_tree_path_free(path);
710 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
711 gtk_tree_selection_select_path(selection, path);
712 gtk_tree_path_free(path);
715 static void mimeview_set_multipart_tree(MimeView *mimeview,
719 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
720 GTK_TREE_VIEW(mimeview->ctree)));
722 gchar *content_type, *length, *name;
724 cm_return_if_fail(mimeinfo != NULL);
726 while (mimeinfo != NULL) {
727 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
728 content_type = g_strdup_printf("%s/%s",
729 procmime_get_media_type_str(mimeinfo->type),
732 content_type = g_strdup("UNKNOWN");
734 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
736 if (prefs_common.attach_desc)
737 name = g_strdup(get_part_description(mimeinfo));
739 name = g_strdup(get_part_name(mimeinfo));
741 gtk_tree_store_append(model, &iter, parent);
742 gtk_tree_store_set(model, &iter,
743 COL_MIMETYPE, content_type,
746 COL_DATA, mimeinfo, -1);
747 g_free(content_type);
751 if (mimeinfo->node->children)
752 mimeview_set_multipart_tree(mimeview,
753 (MimeInfo *) mimeinfo->node->children->data, &iter);
754 mimeinfo = mimeinfo->node->next != NULL ?
755 (MimeInfo *) mimeinfo->node->next->data : NULL;
759 static const gchar *get_real_part_name(MimeInfo *partinfo)
761 const gchar *name = NULL;
763 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
765 name = procmime_mimeinfo_get_parameter(partinfo, "name");
770 static const gchar *get_part_name(MimeInfo *partinfo)
774 name = get_real_part_name(partinfo);
781 static const gchar *get_part_description(MimeInfo *partinfo)
783 if (partinfo->description)
784 return partinfo->description;
786 return get_part_name(partinfo);
789 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
794 if (!partinfo) return;
796 fname = mimeview->file;
799 if ((fp = g_fopen(fname, "rb")) == NULL) {
800 FILE_OP_ERROR(fname, "fopen");
804 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
805 FILE_OP_ERROR(mimeview->file, "fseek");
810 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
811 textview_show_part(mimeview->textview, partinfo, fp);
816 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
819 MimeViewerFactory *factory = NULL;
820 MimeViewer *viewer = NULL;
821 gchar *real_contenttype = NULL, *tmp;
823 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
825 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
826 MimeViewerFactory *curfactory = cur->data;
829 while (curfactory->content_types[i] != NULL) {
830 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
831 if (g_pattern_match_simple(tmp, real_contenttype)) {
832 debug_print("%s\n", curfactory->content_types[i]);
833 factory = curfactory;
843 g_free(real_contenttype);
847 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
848 MimeViewer *curviewer = cur->data;
850 if (curviewer->factory == factory)
853 viewer = factory->create_viewer();
854 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
855 GTK_WIDGET(viewer->get_widget(viewer)));
857 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
862 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
864 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
867 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
869 gchar *content_type = NULL;
870 MimeViewer *viewer = NULL;
872 if ((partinfo->type == MIMETYPE_APPLICATION) &&
873 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
874 const gchar *filename;
876 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
877 if (filename == NULL)
878 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
879 if (filename != NULL)
880 content_type = procmime_get_mime_type(filename);
882 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
885 if (content_type != NULL) {
886 viewer = get_viewer_for_content_type(mimeview, content_type);
887 g_free(content_type);
893 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
897 if (mimeview->messageview->partial_display_shown) {
898 noticeview_hide(mimeview->messageview->noticeview);
899 mimeview->messageview->partial_display_shown = FALSE;
902 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
903 if (viewer == NULL) {
904 if (mimeview->mimeviewer != NULL)
905 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
906 mimeview->mimeviewer = NULL;
910 if (mimeview->mimeviewer != NULL)
911 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
913 if (mimeview->mimeviewer != viewer)
914 mimeview->mimeviewer = viewer;
916 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
917 viewer->mimeview = mimeview;
918 viewer->show_mimepart(viewer, mimeview->file, partinfo);
923 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
925 TextView *textview = mimeview->textview;
926 GtkWidget *focused = NULL;
928 if (mainwindow_get_mainwindow())
929 focused = gtkut_get_focused_child(
930 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
932 if ((mimeview->type != MIMEVIEW_VIEWER) &&
933 (mimeview->type == type)) return;
937 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
938 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
939 GTK_WIDGET_PTR(textview)));
941 case MIMEVIEW_VIEWER:
942 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
943 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
944 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
950 gtk_widget_grab_focus(focused);
951 mimeview->type = type;
954 void mimeview_clear(MimeView *mimeview)
961 if (g_slist_find(mimeviews, mimeview) == NULL)
964 noticeview_hide(mimeview->siginfoview);
966 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
967 gtk_tree_store_clear(GTK_TREE_STORE(model));
969 textview_clear(mimeview->textview);
970 if (mimeview->mimeviewer != NULL)
971 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
973 mimeview_free_mimeinfo(mimeview);
975 mimeview->mimeinfo = NULL;
977 gtk_tree_path_free(mimeview->opened);
978 mimeview->opened = NULL;
980 g_free(mimeview->file);
981 mimeview->file = NULL;
983 icon_list_clear(mimeview);
984 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
987 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
988 GtkUIManager *ui_manager;
990 if (messageview->window != NULL)
991 ui_manager = messageview->ui_manager;
993 ui_manager = messageview->mainwin->ui_manager;
995 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
998 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
999 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1001 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
1002 gboolean special, SignatureStatus code)
1004 gchar *text = NULL, *button_text = NULL;
1006 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1007 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1009 cm_return_if_fail(mimeview != NULL);
1010 cm_return_if_fail(mimeinfo != NULL);
1015 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1018 case SIGNATURE_UNCHECKED:
1019 button_text = _("Check signature");
1020 func = check_signature_cb;
1021 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1024 button_text = _("View full information");
1025 func = display_full_info_cb;
1026 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1028 case SIGNATURE_WARN:
1029 button_text = _("View full information");
1030 func = display_full_info_cb;
1031 icon = STOCK_PIXMAP_PRIVACY_WARN;
1033 case SIGNATURE_KEY_EXPIRED:
1034 button_text = _("View full information");
1035 func = display_full_info_cb;
1036 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1038 case SIGNATURE_INVALID:
1039 button_text = _("View full information");
1040 func = display_full_info_cb;
1041 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1043 case SIGNATURE_CHECK_FAILED:
1044 case SIGNATURE_CHECK_TIMEOUT:
1045 button_text = _("Check again");
1046 func = check_signature_cb;
1047 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1052 if (mycode == SIGNATURE_UNCHECKED) {
1053 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1054 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1056 if (*shortcut == '\0')
1057 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1059 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1063 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1064 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1065 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1066 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1068 if (*shortcut == '\0')
1069 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1071 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1075 noticeview_set_text(mimeview->siginfoview, text);
1076 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1079 noticeview_set_button_text(mimeview->siginfoview, NULL);
1080 noticeview_set_button_press_callback(
1081 mimeview->siginfoview,
1083 (gpointer) mimeview);
1084 noticeview_set_icon(mimeview->siginfoview, icon);
1085 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1087 icon_list_clear(mimeview);
1088 icon_list_create(mimeview, mimeview->mimeinfo);
1093 /* reset all thread stuff, and do the cleanups we've been left to do */
1094 static void mimeview_check_data_reset(MimeView *mimeview)
1097 gboolean must_destroy;
1099 if (!mimeview->check_data)
1102 must_free = mimeview->check_data->free_after_use;
1103 must_destroy = mimeview->check_data->destroy_mimeview;
1105 if (mimeview->check_data->cancel_th_init) {
1106 debug_print("killing canceller thread\n");
1107 mimeview->check_data->cancel_th_init = FALSE;
1108 pthread_cancel(mimeview->check_data->cancel_th);
1112 debug_print("freeing deferred mimeinfo\n");
1113 procmime_mimeinfo_free_all(&mimeview->check_data->siginfo);
1116 g_free(mimeview->check_data);
1117 mimeview->check_data = NULL;
1120 debug_print("freeing deferred mimeview\n");
1121 mimeview_free_mimeinfo(mimeview);
1122 gtk_tree_path_free(mimeview->opened);
1123 g_free(mimeview->file);
1125 mimeviews = g_slist_remove(mimeviews, mimeview);
1129 /* GUI update once the checker thread is done or killed */
1130 static gboolean mimeview_check_sig_thread_cb(void *data)
1132 MimeView *mimeview = (MimeView *) data;
1133 MimeInfo *mimeinfo = mimeview->siginfo;
1135 debug_print("mimeview_check_sig_thread_cb\n");
1137 if (mimeinfo == NULL) {
1138 /* message changed !? */
1139 g_warning("no more siginfo!");
1143 if (!mimeview->check_data) {
1144 g_warning("nothing to check");
1148 if (mimeview->check_data->siginfo != mimeinfo) {
1149 /* message changed !? */
1150 g_warning("different siginfo!");
1154 if (mimeview->check_data->destroy_mimeview ||
1155 mimeview->check_data->free_after_use) {
1156 debug_print("not bothering, we're changing message\n");
1161 if (mimeview->check_data->timeout)
1162 update_signature_noticeview(mimeview, mimeview->siginfo,
1163 TRUE, SIGNATURE_CHECK_TIMEOUT);
1165 update_signature_noticeview(mimeview, mimeview->siginfo,
1169 mimeview_check_data_reset(mimeview);
1173 /* sig checker thread */
1174 static void *mimeview_check_sig_worker_thread(void *data)
1176 MimeView *mimeview = (MimeView *)data;
1177 MimeInfo *mimeinfo = mimeview->siginfo;
1179 debug_print("checking...\n");
1181 if (!mimeview->check_data)
1184 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1185 privacy_mimeinfo_check_signature(mimeinfo);
1186 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1187 mimeview->check_data->cancel_th_init = FALSE;
1188 pthread_cancel(mimeview->check_data->cancel_th);
1191 /* that's strange! we changed message without
1192 * getting killed. */
1193 g_warning("different siginfo!");
1194 mimeview_check_data_reset(mimeview);
1198 /* use g_timeout so that GUI updates is done from the
1200 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1205 /* killer thread - acts when the checker didn't work fast
1207 static void *mimeview_check_sig_cancel_thread(void *data)
1209 MimeView *mimeview = (MimeView *)data;
1211 if (!mimeview->check_data)
1212 return NULL; /* nothing to kill ! */
1214 /* wait for a few seconds... */
1215 debug_print("waiting a while\n");
1217 g_usleep(5 * 1000 * 1000);
1219 if (!mimeview->check_data)
1220 return NULL; /* nothing to kill, it's done in time :) */
1222 /* too late, go away checker thread */
1223 debug_print("killing checker thread\n");
1224 if (mimeview->check_data->th_init) {
1225 mimeview->check_data->th_init = FALSE;
1226 pthread_cancel(mimeview->check_data->th);
1229 /* tell upstream it was a timeout */
1230 mimeview->check_data->timeout = TRUE;
1231 /* use g_timeout so that GUI updates is done from the
1233 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1238 /* get rid of the checker thread right now - used when changing the
1239 * displayed message for example. */
1240 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1242 if (!mimeview->check_data)
1244 debug_print("killing checker thread NOW\n");
1245 if (mimeview->check_data->th_init) {
1246 mimeview->check_data->th_init = FALSE;
1247 pthread_cancel(mimeview->check_data->th);
1250 /* tell upstream it was a timeout */
1251 mimeview->check_data->timeout = TRUE;
1252 mimeview_check_sig_thread_cb(mimeview);
1256 /* creates a thread to check the signature, and a second one
1257 * to kill the first one after a timeout */
1258 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1261 pthread_attr_t detach, detach2;
1263 if (mimeview->check_data) {
1264 g_warning("already checking it");
1268 mimeview->check_data = g_new0(SigCheckData, 1);
1269 mimeview->check_data->siginfo = mimeview->siginfo;
1270 debug_print("creating thread\n");
1272 /* init thread attributes and create the checker thread */
1273 if (pthread_attr_init(&detach) != 0 ||
1274 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1275 pthread_attr_init(&detach2) != 0 ||
1276 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1277 pthread_create(&th, &detach,
1278 mimeview_check_sig_worker_thread,
1280 /* arh. We'll do it synchronously. */
1281 g_warning("can't create thread");
1282 g_free(mimeview->check_data);
1283 mimeview->check_data = NULL;
1286 mimeview->check_data->th = th;
1287 mimeview->check_data->th_init = TRUE;
1290 /* create the killer thread */
1291 pthread_create(&th2, &detach2,
1292 mimeview_check_sig_cancel_thread,
1295 mimeview->check_data->cancel_th = th2;
1296 mimeview->check_data->cancel_th_init = TRUE;
1300 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1302 MimeView *mimeview = (MimeView *) user_data;
1303 MimeInfo *mimeinfo = mimeview->siginfo;
1305 if (mimeinfo == NULL)
1308 if (mimeview->check_data)
1311 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1314 /* let's do it non-blocking */
1315 mimeview_check_sig_in_thread(mimeview);
1316 if (!mimeview->check_data) /* let's check syncronously */
1319 debug_print("checking without thread\n");
1320 privacy_mimeinfo_check_signature(mimeinfo);
1321 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1325 void mimeview_check_signature(MimeView *mimeview)
1327 check_signature_cb(NULL, mimeview);
1330 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1332 MimeView *mimeview = (MimeView *) user_data;
1333 gtk_tree_path_free(mimeview->opened);
1334 mimeview->opened = NULL;
1335 mimeview_selected(gtk_tree_view_get_selection(
1336 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1339 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1341 MimeView *mimeview = (MimeView *) user_data;
1344 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1345 textview_set_text(mimeview->textview, siginfo);
1347 noticeview_set_button_text(mimeview->siginfoview, NULL);
1348 noticeview_set_button_press_callback(
1349 mimeview->siginfoview,
1350 G_CALLBACK(redisplay_email),
1351 (gpointer) mimeview);
1352 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1355 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1358 MimeInfo *first_text;
1360 cm_return_if_fail(mimeview != NULL);
1361 cm_return_if_fail(selected != NULL);
1363 if (selected->type == MIMETYPE_MESSAGE
1364 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1365 /* if the first text part is signed, check that */
1366 first_text = selected;
1367 while (first_text && first_text->type != MIMETYPE_TEXT) {
1368 first_text = procmime_mimeinfo_next(first_text);
1371 update_signature_info(mimeview, first_text);
1377 while (siginfo != NULL) {
1378 if (privacy_mimeinfo_is_signed(siginfo))
1380 siginfo = procmime_mimeinfo_parent(siginfo);
1382 mimeview->siginfo = siginfo;
1384 /* This shortcut boolean is there to correctly set the menu's
1385 * CheckSignature item sensitivity without killing performance
1386 * each time the menu sensitiveness is updated (a lot).
1388 mimeview->signed_part = (siginfo != NULL);
1390 if (siginfo == NULL) {
1391 noticeview_hide(mimeview->siginfoview);
1395 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1396 noticeview_show(mimeview->siginfoview);
1399 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1401 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1402 GtkTreeModel *model;
1407 selection = gtk_tree_view_get_selection(ctree);
1408 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1411 path = gtk_tree_model_get_path(model, &iter);
1413 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1414 gtk_tree_path_free(path);
1418 gtk_tree_path_free(mimeview->opened);
1419 mimeview->opened = path;
1420 mimeview->spec_part = NULL;
1421 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1423 partinfo = mimeview_get_node_part(mimeview, path);
1424 if (!partinfo) return;
1426 /* ungrab the mouse event */
1427 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1428 gtk_grab_remove(GTK_WIDGET(ctree));
1429 if (gdk_pointer_is_grabbed())
1430 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1433 mimeview->textview->default_text = FALSE;
1435 update_signature_info(mimeview, partinfo);
1437 if (!mimeview_show_part(mimeview, partinfo)) {
1438 switch (partinfo->type) {
1440 case MIMETYPE_MESSAGE:
1441 case MIMETYPE_MULTIPART:
1442 mimeview_show_message_part(mimeview, partinfo);
1446 mimeview->textview->default_text = TRUE;
1447 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1448 textview_clear(mimeview->textview);
1449 textview_show_mime_part(mimeview->textview, partinfo);
1454 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1455 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1456 mimeview_check_signature(mimeview);
1460 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1463 GtkTreeSelection *selection;
1466 if (!event) return FALSE;
1468 if (event->button == 2 || event->button == 3) {
1469 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1470 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1473 gtk_tree_selection_select_iter(selection, &iter);
1475 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1480 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1483 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1484 if (event->direction == GDK_SCROLL_UP) {
1485 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1487 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1489 gtk_vscrollbutton_scroll(scrollbutton);
1493 /* from gdkevents.c */
1494 #define DOUBLE_CLICK_TIME 250
1496 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1499 static MimeInfo *lastinfo;
1500 static guint32 lasttime;
1502 if (event->button == 2 ||
1503 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1504 /* call external program for image, audio or html */
1505 mimeview_launch(mimeview, partinfo);
1507 } else if (event->button == 3) {
1508 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1509 partinfo->type == MIMETYPE_IMAGE ||
1510 partinfo->type == MIMETYPE_MULTIPART))
1511 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1513 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1516 partinfo->type == MIMETYPE_APPLICATION &&
1517 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1518 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1521 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1523 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1524 "pop_partinfo", partinfo);
1526 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1527 NULL, NULL, NULL, NULL,
1528 event->button, event->time);
1532 lastinfo = partinfo;
1533 lasttime = event->time;
1538 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1540 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1543 void mimeview_select_next_part(MimeView *mimeview)
1545 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1546 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1547 GtkTreeSelection *selection;
1550 MimeInfo *partinfo = NULL;
1553 if (!mimeview->opened) return;
1555 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1556 path = gtk_tree_model_get_path(model, &iter);
1558 has_next = mimeview_tree_next(model, path);
1560 has_next = gtk_tree_model_get_iter_first(model, &iter);
1561 gtk_tree_path_free(path);
1562 path = gtk_tree_model_get_path(model, &iter);
1566 partinfo = mimeview_get_node_part(mimeview, path);
1567 if (partinfo->type == MIMETYPE_MULTIPART ||
1568 (!prefs_common.show_inline_attachments && partinfo->id))
1570 selection = gtk_tree_view_get_selection(ctree);
1571 gtk_tree_selection_select_path(selection, path);
1572 icon_list_toggle_by_mime_info(mimeview, partinfo);
1575 gtk_tree_path_free(path);
1578 void mimeview_select_prev_part(MimeView *mimeview)
1580 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1581 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1582 GtkTreeSelection *selection;
1585 MimeInfo *partinfo = NULL;
1588 if (!mimeview->opened) return;
1590 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1591 path = gtk_tree_model_get_path(model, &iter);
1593 has_prev = mimeview_tree_prev(mimeview, path);
1595 while (mimeview_tree_next(model, path)) {}
1600 partinfo = mimeview_get_node_part(mimeview, path);
1601 if (partinfo->type == MIMETYPE_MULTIPART ||
1602 (!prefs_common.show_inline_attachments && partinfo->id))
1604 selection = gtk_tree_view_get_selection(ctree);
1605 gtk_tree_selection_select_path(selection, path);
1606 icon_list_toggle_by_mime_info(mimeview, partinfo);
1609 gtk_tree_path_free(path);
1612 #define BREAK_ON_MODIFIER_KEY() \
1613 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1615 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1618 SummaryView *summaryview;
1620 if (!event) return FALSE;
1621 if (!mimeview->opened) return FALSE;
1623 summaryview = mimeview->messageview->mainwin->summaryview;
1625 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1628 switch (event->keyval) {
1631 textview_scroll_max(mimeview->textview,
1632 (event->keyval == GDK_KEY_Home));
1634 case GDK_KEY_Page_Down:
1636 if (mimeview_scroll_page(mimeview,
1637 (event->state & GDK_SHIFT_MASK) != 0))
1640 if (!(event->state & GDK_SHIFT_MASK))
1641 mimeview_select_next_part(mimeview);
1643 case GDK_KEY_Page_Up:
1644 case GDK_KEY_BackSpace:
1645 mimeview_scroll_page(mimeview, TRUE);
1647 case GDK_KEY_Return:
1648 case GDK_KEY_KP_Enter:
1649 mimeview_scroll_one_line(mimeview,
1650 (event->state & GDK_MOD1_MASK) != 0);
1654 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1660 if (mimeview->messageview->new_window) return FALSE;
1662 return summary_pass_key_press_event(summaryview, event);
1665 static void mimeview_drag_data_get(GtkWidget *widget,
1666 GdkDragContext *drag_context,
1667 GtkSelectionData *selection_data,
1672 gchar *filename = NULL, *uriname, *tmp;
1677 if (!mimeview->opened) return;
1678 if (!mimeview->file) return;
1680 partinfo = mimeview_get_selected_part(mimeview);
1681 if (!partinfo) return;
1683 if (strlen(get_part_name(partinfo)) > 0) {
1684 filename = g_path_get_basename(get_part_name(partinfo));
1685 if (*filename == '\0') return;
1686 } else if (partinfo->type == MIMETYPE_MESSAGE
1687 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1689 GPtrArray *headers = NULL;
1692 fp = g_fopen(partinfo->data.filename, "rb");
1693 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1694 headers = procheader_get_header_array_asis(fp);
1697 for (i = 0; i < headers->len; i++) {
1698 Header *header = g_ptr_array_index(headers, i);
1699 if (procheader_headername_equal(header->name, "Subject")) {
1700 unfold_line(header->body);
1701 name = g_strconcat(header->body, ".txt", NULL);
1702 subst_for_filename(name);
1705 procheader_header_array_destroy(headers);
1711 filename = g_path_get_basename(name);
1714 if (filename == NULL)
1715 filename = g_path_get_basename("Unnamed part");
1717 if (!g_utf8_validate(filename, -1, NULL))
1718 tmp = conv_codeset_strdup(filename,
1719 conv_get_locale_charset_str(),
1722 tmp = g_strdup(filename);
1725 g_warning("filename not in UTF-8");
1726 tmp = g_strdup("Unnamed part");
1728 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1732 if (is_file_exist(filename)) {
1734 gchar *prefix = NULL;
1735 gchar *new_name = NULL;
1736 if (strrchr(tmp, '.')) {
1737 prefix = g_strdup(tmp);
1738 ext = g_strdup(strrchr(tmp, '.'));
1739 *(strrchr(prefix, '.')) = '\0';
1741 prefix = g_strdup(tmp);
1745 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1749 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1752 goto check_new_file;
1757 if ((err = procmime_get_part(filename, partinfo)) < 0)
1759 (_("Couldn't save the part of multipart message: %s"),
1762 tmp = g_filename_to_uri(filename, NULL, NULL);
1763 uriname = g_strconcat(tmp, "\r\n", NULL);
1766 gtk_selection_data_set(selection_data,
1767 gtk_selection_data_get_target(selection_data), 8,
1768 (guchar *)uriname, strlen(uriname));
1775 * Returns a filename (with path) for an attachment
1776 * \param partinfo The attachment to save
1777 * \param basedir The target directory
1778 * \param number Used for dummy filename if attachment is unnamed
1780 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1781 const gchar *basedir,
1787 filename = g_strdup(get_part_name(partinfo));
1788 if (!filename || !*filename) {
1790 filename = g_strdup_printf("noname.%d", number);
1793 if (!g_utf8_validate(filename, -1, NULL)) {
1794 gchar *tmp = conv_filename_to_utf8(filename);
1799 subst_for_filename(filename);
1801 fullname = g_strconcat
1802 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1803 ? &filename[1] : filename, NULL);
1806 filename = conv_filename_from_utf8(fullname);
1812 * Write a single attachment to file
1813 * \param filename Filename with path
1814 * \param partinfo Attachment to save
1816 static gboolean mimeview_write_part(const gchar *filename,
1818 gboolean handle_error)
1823 dir= g_path_get_dirname(filename);
1824 if (!is_dir_exist(dir))
1828 if (is_file_exist(filename)) {
1833 if (!g_utf8_validate(filename, -1, NULL))
1834 tmp = conv_filename_to_utf8(filename);
1836 tmp = g_strdup(filename);
1838 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1841 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
1842 GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST);
1844 if (G_ALERTALTERNATE != aval) return FALSE;
1847 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1848 debug_print("error saving MIME part: %d\n", err);
1851 (_("Couldn't save the part of multipart message: %s"),
1859 static AlertValue mimeview_save_all_error_ask(gint n)
1861 gchar *message = g_strdup_printf(
1862 _("An error has occurred while saving message part #%d. "
1863 "Do you want to cancel operation or skip error and "
1865 AlertValue av = alertpanel_full(_("Error saving all message parts"),
1866 message, GTK_STOCK_CANCEL, _("Skip"), _("Skip all"),
1867 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1872 static void mimeview_save_all_info(gint errors, gint total)
1875 gchar *msg = g_strdup_printf(
1876 ngettext("%d file saved successfully.",
1877 "%d files saved successfully.",
1880 alertpanel_notice("%s", msg);
1883 gchar *msg1 = g_strdup_printf(
1884 ngettext("%d file saved successfully",
1885 "%d files saved successfully",
1888 gchar *msg2 = g_strdup_printf(
1889 ngettext("%s, %d file failed.",
1890 "%s, %d files failed.",
1893 alertpanel_warning("%s", msg2);
1900 * Menu callback: Save all attached files
1901 * \param mimeview Current display
1903 static void mimeview_save_all(MimeView *mimeview)
1907 gchar *startdir = NULL;
1908 gint number = 1, errors = 0;
1909 gboolean skip_errors = FALSE;
1911 if (!mimeview->opened) return;
1912 if (!mimeview->file) return;
1913 if (!mimeview->mimeinfo) return;
1915 partinfo = mimeview->mimeinfo;
1916 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1917 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1919 startdir = g_strdup(get_home_dir());
1921 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1922 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1928 if (!is_dir_exist (dirname)) {
1929 alertpanel_error(_("'%s' is not a directory."), dirname);
1935 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1936 dirname[strlen(dirname)-1] = '\0';
1938 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1939 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1940 partinfo = procmime_mimeinfo_next(partinfo);
1941 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1942 partinfo = procmime_mimeinfo_next(partinfo);
1943 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1944 partinfo = procmime_mimeinfo_next(partinfo);
1947 while (partinfo != NULL) {
1948 if (partinfo->type != MIMETYPE_MESSAGE &&
1949 partinfo->type != MIMETYPE_MULTIPART &&
1950 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1951 || get_real_part_name(partinfo) != NULL)) {
1952 gchar *filename = mimeview_get_filename_for_part(
1953 partinfo, dirname, number++);
1955 gboolean ok = mimeview_write_part(filename, partinfo, FALSE);
1960 AlertValue av = mimeview_save_all_error_ask(number - 1);
1961 skip_errors = (av == G_ALERTOTHER);
1962 if (av == G_ALERTDEFAULT) /* cancel */
1967 partinfo = procmime_mimeinfo_next(partinfo);
1970 g_free(prefs_common.attach_save_dir);
1972 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1973 -1, NULL, NULL, NULL);
1976 mimeview_save_all_info(errors, number - 1);
1979 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1981 MimeInfo *partinfo = NULL;
1982 if (mimeview->spec_part) {
1983 partinfo = mimeview->spec_part;
1984 mimeview->spec_part = NULL;
1986 partinfo = (MimeInfo *) g_object_get_data
1987 (G_OBJECT(mimeview->popupmenu),
1989 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1990 "pop_partinfo", NULL);
1992 partinfo = mimeview_get_selected_part(mimeview);
1999 * Menu callback: Save the selected attachment
2000 * \param mimeview Current display
2002 void mimeview_save_as(MimeView *mimeview)
2005 gchar *filepath = NULL;
2006 gchar *filedir = NULL;
2008 gchar *partname = NULL;
2010 if (!mimeview->opened) return;
2011 if (!mimeview->file) return;
2013 partinfo = mimeview_get_part_to_use(mimeview);
2015 cm_return_if_fail(partinfo != NULL);
2017 if (get_part_name(partinfo) == NULL) {
2020 partname = g_strdup(get_part_name(partinfo));
2022 if (!g_utf8_validate(partname, -1, NULL)) {
2023 gchar *tmp = conv_filename_to_utf8(partname);
2025 tmp = conv_codeset_strdup(partname,
2026 conv_get_locale_charset_str(),
2035 subst_for_filename(partname);
2037 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2038 filepath = g_strconcat(prefs_common.attach_save_dir,
2039 G_DIR_SEPARATOR_S, partname, NULL);
2041 filepath = g_strdup(partname);
2045 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2046 filename = filesel_select_file_save(_("Save as"), filepath);
2052 mimeview_write_part(filename, partinfo, TRUE);
2054 filedir = g_path_get_dirname(filename);
2055 if (filedir && strcmp(filedir, ".")) {
2056 g_free(prefs_common.attach_save_dir);
2057 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2064 void mimeview_display_as_text(MimeView *mimeview)
2068 if (!mimeview->opened) return;
2070 partinfo = mimeview_get_part_to_use(mimeview);
2071 mimeview_select_mimepart_icon(mimeview, partinfo);
2072 cm_return_if_fail(partinfo != NULL);
2073 mimeview_show_message_part(mimeview, partinfo);
2076 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2081 if (!mimeview->opened) return;
2082 if (!mimeview->file) return;
2085 partinfo = mimeview_get_part_to_use(mimeview);
2087 cm_return_if_fail(partinfo != NULL);
2089 filename = procmime_get_tmp_file_name(partinfo);
2091 if ((err = procmime_get_part(filename, partinfo)) < 0)
2093 (_("Couldn't save the part of multipart message: %s"),
2096 mimeview_view_file(filename, partinfo, NULL, mimeview);
2102 void mimeview_open_with(MimeView *mimeview)
2106 if (!mimeview) return;
2107 if (!mimeview->opened) return;
2108 if (!mimeview->file) return;
2110 partinfo = mimeview_get_part_to_use(mimeview);
2112 mimeview_open_part_with(mimeview, partinfo, FALSE);
2115 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2119 gchar *mime_command = NULL;
2120 gchar *content_type = NULL;
2123 cm_return_if_fail(partinfo != NULL);
2125 filename = procmime_get_tmp_file_name(partinfo);
2127 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2129 (_("Couldn't save the part of multipart message: %s"),
2135 if (!prefs_common.mime_open_cmd_history)
2136 prefs_common.mime_open_cmd_history =
2137 add_history(NULL, prefs_common.mime_open_cmd);
2139 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2140 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2141 /* guess content-type from filename */
2142 content_type = procmime_get_mime_type(filename);
2144 if (content_type == NULL) {
2145 content_type = procmime_get_content_type_str(partinfo->type,
2149 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2150 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2151 mime_command = g_strdup(prefs_common_get_uri_cmd());
2152 g_free(content_type);
2153 content_type = NULL;
2154 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2155 || !prefs_common_get_ext_editor_cmd()[0]) {
2156 mime_command = mailcap_get_command_for_type(content_type, filename);
2158 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2159 g_free(content_type);
2160 content_type = NULL;
2162 if (mime_command == NULL) {
2163 /* try with extension this time */
2164 g_free(content_type);
2165 content_type = procmime_get_mime_type(filename);
2166 mime_command = mailcap_get_command_for_type(content_type, filename);
2169 if (mime_command == NULL)
2173 gboolean remember = FALSE;
2174 if (content_type != NULL)
2175 cmd = input_dialog_combo_remember
2177 _("Enter the command-line to open file:\n"
2178 "('%s' will be replaced with file name)"),
2179 mime_command ? mime_command : prefs_common.mime_open_cmd,
2180 prefs_common.mime_open_cmd_history, &remember);
2182 cmd = input_dialog_combo
2184 _("Enter the command-line to open file:\n"
2185 "('%s' will be replaced with file name)"),
2186 mime_command ? mime_command : prefs_common.mime_open_cmd,
2187 prefs_common.mime_open_cmd_history);
2188 if (cmd && remember) {
2189 mailcap_update_default(content_type, cmd);
2191 g_free(mime_command);
2196 mimeview_view_file(filename, partinfo, cmd, mimeview);
2197 g_free(prefs_common.mime_open_cmd);
2198 prefs_common.mime_open_cmd = cmd;
2199 prefs_common.mime_open_cmd_history =
2200 add_history(prefs_common.mime_open_cmd_history, cmd);
2203 g_free(content_type);
2208 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2210 GList *attach_file = NULL;
2211 AttachInfo *ainfo = NULL;
2215 if (!mimeview->opened) return;
2216 if (!mimeview->file) return;
2218 cm_return_if_fail(partinfo != NULL);
2220 filename = procmime_get_tmp_file_name(partinfo);
2222 if (!(err = procmime_get_part(filename, partinfo))) {
2223 ainfo = g_new0(AttachInfo, 1);
2224 ainfo->file = filename;
2225 ainfo->name = g_strdup(get_part_name(partinfo));
2226 ainfo->content_type = procmime_get_content_type_str(
2227 partinfo->type, partinfo->subtype);
2228 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2229 partinfo, "charset"));
2230 attach_file = g_list_append(attach_file, ainfo);
2232 compose_new(NULL, NULL, attach_file);
2234 g_free(ainfo->name);
2235 g_free(ainfo->content_type);
2236 g_free(ainfo->charset);
2238 g_list_free(attach_file);
2241 (_("Couldn't save the part of multipart message: %s"),
2246 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2247 const gchar *cmd, MimeView *mimeview)
2251 gchar buf[BUFFSIZE];
2254 mimeview_open_part_with(mimeview, partinfo, TRUE);
2256 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2257 !strchr(p + 2, '%')) {
2258 g_snprintf(buf, sizeof(buf), cmd, filename);
2259 if (!prefs_common.save_parts_readwrite)
2260 g_chmod(filename, S_IRUSR);
2262 g_chmod(filename, S_IRUSR|S_IWUSR);
2264 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2265 mimeview_open_part_with(mimeview, partinfo, FALSE);
2267 if (execute_command_line(buf, TRUE, NULL) != 0) {
2268 if (!prefs_common.save_parts_readwrite)
2269 g_chmod(filename, S_IRUSR|S_IWUSR);
2270 mimeview_open_part_with(mimeview, partinfo, FALSE);
2274 SHFILEINFO file_info;
2275 GError *error = NULL;
2276 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2278 if (error != NULL) {
2279 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2281 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2282 g_error_free(error);
2286 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2287 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2288 _("This attachment is an executable file. Executing "
2289 "untrusted binaries is dangerous and could probably "
2290 "lead to compromission of your computer.\n\n"
2291 "Do you want to run this file?"), GTK_STOCK_CANCEL,
2293 NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
2294 if (val == G_ALERTALTERNATE) {
2295 debug_print("executing binary\n");
2296 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2299 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2307 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2309 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2312 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2314 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2317 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2319 GSList *mimeview_list, *viewer_list;
2321 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2322 MimeView *mimeview = (MimeView *) mimeview_list->data;
2324 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2325 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2326 mimeview->mimeviewer = NULL;
2329 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2330 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2332 mimeviewer->destroy_viewer(mimeviewer);
2333 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2337 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2340 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2345 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2346 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2348 if (event->button == 1) {
2349 icon_selected(mimeview, num, partinfo);
2350 gtk_widget_grab_focus(button);
2351 icon_list_toggle_by_mime_info(mimeview, partinfo);
2353 part_button_pressed(mimeview, event, partinfo);
2358 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2360 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2363 MimeInfo *curr = NULL;
2365 gtk_tree_model_get_iter_first(model, &iter);
2366 path = gtk_tree_model_get_path(model, &iter);
2369 gtk_tree_model_get_iter(model, &iter, path);
2370 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2371 if (curr == partinfo) {
2372 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2373 GTK_TREE_VIEW(mimeview->ctree));
2374 gtk_tree_selection_select_iter(sel, &iter);
2375 gtk_tree_path_free(path);
2378 } while (mimeview_tree_next(model, path));
2380 gtk_tree_path_free(path);
2383 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2385 icon_list_toggle_by_mime_info(mimeview, partinfo);
2386 icon_selected(mimeview, -1, partinfo);
2389 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2392 SummaryView *summaryview;
2394 if (!event) return FALSE;
2396 switch (event->keyval) {
2398 if (mimeview_scroll_page(mimeview, FALSE))
2401 mimeview_select_next_part(mimeview);
2405 case GDK_KEY_BackSpace:
2406 mimeview_scroll_page(mimeview, TRUE);
2408 case GDK_KEY_Return:
2409 case GDK_KEY_KP_Enter:
2410 mimeview_scroll_one_line(mimeview,
2411 (event->state & GDK_MOD1_MASK) != 0);
2414 BREAK_ON_MODIFIER_KEY();
2415 mimeview_save_as(mimeview);
2418 BREAK_ON_MODIFIER_KEY();
2419 mimeview_display_as_text(mimeview);
2422 BREAK_ON_MODIFIER_KEY();
2423 mimeview_launch(mimeview, NULL);
2427 BREAK_ON_MODIFIER_KEY();
2428 mimeview_open_with(mimeview);
2432 BREAK_ON_MODIFIER_KEY();
2433 mimeview_check_signature(mimeview);
2436 BREAK_ON_MODIFIER_KEY();
2437 mimeview_select_next_part(mimeview);
2443 if (!mimeview->messageview->mainwin) return FALSE;
2444 summaryview = mimeview->messageview->mainwin->summaryview;
2445 return summary_pass_key_press_event(summaryview, event);
2448 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2450 MimeView *mimeview = (MimeView *)data;
2451 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2453 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2454 "pop_partinfo", partinfo);
2455 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
2456 NULL, NULL, NULL, NULL,
2457 0, gtk_get_current_event_time());
2461 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2463 GtkWidget *pixmap = NULL;
2468 const gchar *desc = NULL;
2469 gchar *sigshort = NULL;
2470 gchar *content_type;
2473 MimeInfo *siginfo = NULL;
2474 MimeInfo *encrypted = NULL;
2479 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2482 vbox = mimeview->icon_vbox;
2483 mimeview->icon_count++;
2484 button = gtk_event_box_new();
2486 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2487 G_CALLBACK(mimeview_visi_notify), mimeview);
2488 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2489 G_CALLBACK(mimeview_leave_notify), mimeview);
2490 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2491 G_CALLBACK(mimeview_enter_notify), mimeview);
2493 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2494 g_object_set_data(G_OBJECT(button), "icon_number",
2495 GINT_TO_POINTER(mimeview->icon_count));
2496 g_object_set_data(G_OBJECT(button), "partinfo",
2499 switch (mimeinfo->type) {
2502 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2503 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2504 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2505 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2506 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2507 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2508 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2509 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2510 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2512 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2514 case MIMETYPE_MESSAGE:
2515 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2517 case MIMETYPE_APPLICATION:
2518 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2519 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2520 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2521 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2522 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2523 stockp = STOCK_PIXMAP_MIME_PDF;
2524 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2525 stockp = STOCK_PIXMAP_MIME_PS;
2527 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2529 case MIMETYPE_IMAGE:
2530 stockp = STOCK_PIXMAP_MIME_IMAGE;
2532 case MIMETYPE_AUDIO:
2533 stockp = STOCK_PIXMAP_MIME_AUDIO;
2536 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2540 partinfo = mimeinfo;
2541 while (partinfo != NULL) {
2542 if (privacy_mimeinfo_is_signed(partinfo)) {
2546 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2547 encrypted = partinfo;
2550 partinfo = procmime_mimeinfo_parent(partinfo);
2553 if (siginfo != NULL) {
2554 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2555 case SIGNATURE_UNCHECKED:
2556 case SIGNATURE_CHECK_FAILED:
2557 case SIGNATURE_CHECK_TIMEOUT:
2558 pixmap = stock_pixmap_widget_with_overlay(stockp,
2559 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2562 pixmap = stock_pixmap_widget_with_overlay(stockp,
2563 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2565 case SIGNATURE_WARN:
2566 case SIGNATURE_KEY_EXPIRED:
2567 pixmap = stock_pixmap_widget_with_overlay(stockp,
2568 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2570 case SIGNATURE_INVALID:
2571 pixmap = stock_pixmap_widget_with_overlay(stockp,
2572 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2575 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2576 } else if (encrypted != NULL) {
2577 pixmap = stock_pixmap_widget_with_overlay(stockp,
2578 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2580 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2581 OVERLAY_NONE, 6, 3);
2583 gtk_container_add(GTK_CONTAINER(button), pixmap);
2585 if (prefs_common.attach_desc)
2586 desc = get_part_description(mimeinfo);
2588 desc = get_part_name(mimeinfo);
2591 content_type = procmime_get_content_type_str(mimeinfo->type,
2594 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2595 "\n<b>", _("Size:"), " </b>",
2596 to_human_readable((goffset)mimeinfo->length), NULL);
2597 g_free(content_type);
2598 if (desc && *desc) {
2599 gchar *tmp = NULL, *escaped = NULL;
2600 if (!g_utf8_validate(desc, -1, NULL)) {
2601 tmp = conv_filename_to_utf8(desc);
2603 tmp = g_strdup(desc);
2605 escaped = g_markup_escape_text(tmp,-1);
2607 tiptmp = g_strconcat(tip, "\n<b>",
2608 prefs_common.attach_desc && mimeinfo->description ?
2609 _("Description:") : _("Filename:"),
2610 " </b>", escaped, NULL);
2616 if (sigshort && *sigshort) {
2617 tiptmp = g_strjoin("\n", tip, g_markup_escape_text(sigshort, -1), NULL);
2623 gtk_widget_set_tooltip_markup(button, tip);
2625 gtk_widget_show_all(button);
2626 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2627 mimeview_mime_types, 1, GDK_ACTION_COPY);
2629 g_signal_connect(G_OBJECT(button), "popup-menu",
2630 G_CALLBACK(icon_popup_menu), mimeview);
2631 g_signal_connect(G_OBJECT(button), "button_release_event",
2632 G_CALLBACK(icon_clicked_cb), mimeview);
2633 g_signal_connect(G_OBJECT(button), "key_press_event",
2634 G_CALLBACK(icon_key_pressed), mimeview);
2635 g_signal_connect(G_OBJECT(button), "drag_data_get",
2636 G_CALLBACK(mimeview_drag_data_get), mimeview);
2637 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2639 gtk_widget_size_request(pixmap, &r);
2640 gtk_widget_set_size_request(button, -1, r.height + 4);
2645 static void icon_list_clear (MimeView *mimeview)
2647 GList *child, *orig;
2650 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2651 for (child = orig; child != NULL; child = g_list_next(child)) {
2652 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
2653 GTK_WIDGET(child->data));
2656 mimeview->icon_count = 0;
2657 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2658 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2659 gtk_adjustment_changed(adj);
2663 *\brief Used to 'click' the next or previous icon.
2665 *\return true if the icon 'number' exists and was selected.
2667 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2668 GtkAllocation *size, MimeView *mimeview)
2670 GtkAllocation vbox_size;
2671 GtkAllocation layout_size;
2676 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2678 gtk_widget_get_allocation(mimeview->icon_vbox, &vbox_size);
2679 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2681 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2682 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2684 MAX(vbox_size.height, layout_size.height));
2685 gtk_adjustment_set_step_increment(adj, 10);
2688 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2690 GtkRequisition size;
2691 GtkRequisition requisition;
2693 cm_return_if_fail(mimeinfo != NULL);
2695 while (mimeinfo != NULL) {
2696 if (mimeinfo->type != MIMETYPE_MULTIPART)
2697 icon_list_append_icon(mimeview, mimeinfo);
2698 if (mimeinfo->node->children != NULL)
2699 icon_list_create(mimeview,
2700 (MimeInfo *) mimeinfo->node->children->data);
2701 mimeinfo = mimeinfo->node->next != NULL
2702 ? (MimeInfo *) mimeinfo->node->next->data
2705 gtk_widget_size_request(mimeview->icon_vbox, &size);
2706 gtk_widget_get_requisition(mimeview->icon_mainbox, &requisition);
2707 if (size.width > requisition.width) {
2708 gtk_widget_set_size_request(mimeview->icon_mainbox,
2711 if (mimeview->opened)
2712 icon_list_toggle_by_mime_info(mimeview,
2713 mimeview_get_node_part(mimeview, mimeview->opened));
2716 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2719 GList *children, *child;
2721 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2722 for (child = children; child != NULL; child = g_list_next(child)) {
2723 gboolean *highlight = NULL;
2724 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2726 if (!GTK_IS_EVENT_BOX(child->data))
2729 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2730 *highlight = (g_object_get_data(G_OBJECT(child->data),
2731 "partinfo") == (gpointer)mimeinfo);
2733 gtk_widget_queue_draw(icon);
2735 g_list_free(children);
2738 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2741 prefs_common.mimeview_tree_height = allocation->height;
2744 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2747 g_object_ref(button);
2749 mimeview_leave_notify(button, NULL, NULL);
2751 mimeview->ctree_mode = !mimeview->ctree_mode;
2752 if (mimeview->ctree_mode) {
2753 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2755 gtk_widget_hide(mimeview->icon_mainbox);
2756 gtk_widget_show(mimeview->ctree_mainbox);
2757 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2758 prefs_common.mimeview_tree_height);
2760 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2762 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2763 button, FALSE, FALSE, 0);
2765 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2767 gtk_widget_hide(mimeview->ctree_mainbox);
2768 gtk_widget_show(mimeview->icon_mainbox);
2769 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2771 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2773 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2774 button, FALSE, FALSE, 0);
2775 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2776 if (mimeview->opened)
2777 icon_list_toggle_by_mime_info(mimeview,
2778 mimeview_get_node_part(mimeview, mimeview->opened));
2779 summary_grab_focus(mimeview->mainwin->summaryview);
2781 g_object_unref(button);
2785 void mimeview_update (MimeView *mimeview)
2787 if (mimeview && mimeview->mimeinfo) {
2788 icon_list_clear(mimeview);
2789 icon_list_create(mimeview, mimeview->mimeinfo);
2793 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2795 MessageView *msgview = NULL;
2796 MainWindow *mainwin = NULL;
2801 msgview = mimeview->messageview;
2805 mainwin = msgview->mainwin;
2809 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2810 "pop_partinfo", NULL);
2812 if (!strcmp(cmd, "sc://view_log"))
2813 log_window_show(mainwin->logwin);
2814 else if (!strcmp(cmd, "sc://save_as"))
2815 mimeview_save_as(mimeview);
2816 else if (!strcmp(cmd, "sc://display_as_text"))
2817 mimeview_display_as_text(mimeview);
2819 else if (!strcmp(cmd, "sc://open_with"))
2820 mimeview_open_with(mimeview);
2822 else if (!strcmp(cmd, "sc://open"))
2823 mimeview_launch(mimeview, NULL);
2824 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2825 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2826 icon_selected(mimeview, -1, (MimeInfo *)data);
2827 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2828 mimeview_launch(mimeview, (MimeInfo *)data);
2829 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2830 mimeview->spec_part = (MimeInfo *)data;
2831 part_button_pressed(mimeview, event, (MimeInfo *)data);
2832 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2833 const gchar *tagname = cmd + strlen("sc://search_tags:");
2834 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2835 gtk_toggle_button_set_active(
2836 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2838 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2839 ADVANCED_SEARCH_EXTENDED, buf);
2844 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2846 if (mimeview->type == MIMEVIEW_TEXT)
2847 return textview_scroll_page(mimeview->textview, up);
2848 else if (mimeview->mimeviewer) {
2849 MimeViewer *mimeviewer = mimeview->mimeviewer;
2850 if (mimeviewer->scroll_page)
2851 return mimeviewer->scroll_page(mimeviewer, up);
2856 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2858 if (mimeview->type == MIMEVIEW_TEXT)
2859 textview_scroll_one_line(mimeview->textview, up);
2860 else if (mimeview->mimeviewer) {
2861 MimeViewer *mimeviewer = mimeview->mimeviewer;
2862 if (mimeviewer->scroll_one_line)
2863 mimeviewer->scroll_one_line(mimeviewer, up);