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 g_object_unref(model);
290 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
291 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
292 prefs_common.use_stripes_everywhere);
293 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ctree),
294 prefs_common.show_col_headers);
296 renderer = gtk_cell_renderer_text_new();
297 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
298 -1, titles[COL_MIMETYPE],renderer,
299 "text", COL_MIMETYPE, NULL);
300 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
302 renderer = gtk_cell_renderer_text_new();
303 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
304 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
305 -1, titles[COL_SIZE], renderer,
306 "text", COL_SIZE, NULL);
307 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
308 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
310 renderer = gtk_cell_renderer_text_new();
311 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
312 -1, titles[COL_NAME], renderer,
313 "text", COL_NAME, NULL);
314 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
315 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
317 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
318 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
320 gtk_widget_show(ctree);
321 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
322 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
323 mimeview_mime_types, 1, GDK_ACTION_COPY);
325 g_signal_connect(G_OBJECT(selection), "changed",
326 G_CALLBACK(mimeview_selected), mimeview);
327 g_signal_connect(G_OBJECT(ctree), "button_release_event",
328 G_CALLBACK(mimeview_button_pressed), mimeview);
329 g_signal_connect(G_OBJECT(ctree), "key_press_event",
330 G_CALLBACK(mimeview_key_pressed), mimeview);
331 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
332 G_CALLBACK(mimeview_drag_data_get), mimeview);
334 mime_notebook = gtk_notebook_new();
335 gtk_widget_show(mime_notebook);
336 gtk_widget_set_can_focus(mime_notebook, FALSE);
337 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
338 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
340 icon_vbox = gtk_vbox_new(FALSE, 2);
341 gtk_widget_show(icon_vbox);
342 icon_scroll = gtk_layout_new(NULL, NULL);
343 gtk_widget_show(icon_scroll);
344 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
345 scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
346 gtk_widget_show(scrollbutton);
348 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
349 G_CALLBACK(mimeview_scrolled), mimeview);
351 mime_toggle = gtk_event_box_new();
352 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
354 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
355 G_CALLBACK(mimeview_visi_notify), mimeview);
356 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
357 G_CALLBACK(mimeview_leave_notify), mimeview);
358 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
359 G_CALLBACK(mimeview_enter_notify), mimeview);
361 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
362 gtk_widget_show(mime_toggle);
363 mimeview->ctree_mode = FALSE;
364 arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
365 gtk_widget_show(arrow);
366 gtk_widget_size_request(arrow, &r);
367 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
368 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
369 G_CALLBACK(mime_toggle_button_cb), mimeview);
371 icon_mainbox = gtk_vbox_new(FALSE, 0);
372 gtk_widget_show(icon_mainbox);
373 gtk_widget_set_size_request(icon_mainbox, 32, -1);
374 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
375 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
376 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
377 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
378 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
380 ctree_mainbox = gtk_hbox_new(FALSE, 0);
381 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
382 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
383 G_CALLBACK(ctree_size_allocate_cb), mimeview);
385 mimeview->ui_manager = gtk_ui_manager_new();
386 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
387 "MimeView", mimeview_menu_actions,
388 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
390 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
391 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
392 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
393 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
394 "/Menus/MimeView/", "Open", "MimeView/Open",
395 GTK_UI_MANAGER_MENUITEM);
396 #if (!defined G_OS_WIN32)
397 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
398 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
399 GTK_UI_MANAGER_MENUITEM);
401 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
402 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
403 GTK_UI_MANAGER_MENUITEM);
404 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
405 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
406 GTK_UI_MANAGER_MENUITEM);
407 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
408 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
409 GTK_UI_MANAGER_MENUITEM);
410 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
411 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
412 GTK_UI_MANAGER_MENUITEM);
413 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
414 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
415 GTK_UI_MANAGER_MENUITEM);
416 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
417 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
418 GTK_UI_MANAGER_MENUITEM);
420 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
421 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
424 vbox = gtk_vbox_new(FALSE, 0);
425 gtk_widget_show(vbox);
426 siginfoview = noticeview_create(mainwin);
427 noticeview_hide(siginfoview);
428 noticeview_set_icon_clickable(siginfoview, TRUE);
429 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
430 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
432 paned = gtk_vpaned_new();
433 gtk_widget_show(paned);
434 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
435 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
437 hbox = gtk_hbox_new(FALSE, 0);
438 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
439 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
441 gtk_widget_show(hbox);
442 gtk_widget_hide(ctree_mainbox);
444 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
446 mimeview->hbox = hbox;
447 mimeview->paned = paned;
448 mimeview->scrolledwin = scrolledwin;
449 mimeview->ctree = ctree;
450 mimeview->mime_notebook = mime_notebook;
451 mimeview->popupmenu = popupmenu;
453 mimeview->ctree_mainbox = ctree_mainbox;
454 mimeview->icon_scroll = icon_scroll;
455 mimeview->icon_vbox = icon_vbox;
456 mimeview->icon_mainbox = icon_mainbox;
457 mimeview->icon_count = 0;
458 mimeview->mainwin = mainwin;
459 mimeview->mime_toggle = mime_toggle;
460 mimeview->siginfoview = siginfoview;
461 mimeview->scrollbutton = scrollbutton;
462 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
464 mimeviews = g_slist_prepend(mimeviews, mimeview);
469 void mimeview_init(MimeView *mimeview)
471 textview_init(mimeview->textview);
473 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
474 GTK_WIDGET_PTR(mimeview->textview));
477 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
480 if (privacy_mimeinfo_is_signed(mimeinfo))
482 mimeinfo = procmime_mimeinfo_next(mimeinfo);
488 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
491 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
493 mimeview_clear(mimeview);
495 cm_return_if_fail(file != NULL);
496 cm_return_if_fail(mimeinfo != NULL);
498 mimeview->mimeinfo = mimeinfo;
500 mimeview->file = g_strdup(file);
502 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
505 /* check if the mail's signed - it can change the mail structure */
507 if (any_part_is_signed(mimeinfo))
508 debug_print("signed mail\n");
510 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
511 gtk_tree_view_expand_all(ctree);
512 icon_list_clear(mimeview);
513 icon_list_create(mimeview, mimeinfo);
515 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
516 mimeview_selected, mimeview);
520 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
523 static void mimeview_free_mimeinfo(MimeView *mimeview)
525 gboolean defer = FALSE;
527 defer = (mimeview->check_data != NULL);
529 mimeview->check_data->free_after_use = TRUE;
531 if (mimeview->mimeinfo != NULL && !defer) {
532 procmime_mimeinfo_free_all(&mimeview->mimeinfo);
533 mimeview->mimeinfo = NULL;
536 debug_print("deferring free(mimeinfo) and cancelling check\n");
537 mimeview_check_sig_cancel_now(mimeview);
542 void mimeview_destroy(MimeView *mimeview)
546 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
547 MimeViewer *viewer = (MimeViewer *) cur->data;
548 viewer->destroy_viewer(viewer);
550 g_slist_free(mimeview->viewers);
551 gtk_target_list_unref(mimeview->target_list);
554 if (mimeview->check_data) {
555 mimeview->check_data->destroy_mimeview = TRUE;
556 debug_print("deferring destroy\n");
560 mimeview_free_mimeinfo(mimeview);
561 gtk_tree_path_free(mimeview->opened);
562 g_free(mimeview->file);
564 mimeviews = g_slist_remove(mimeviews, mimeview);
569 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
571 return gtkut_tree_view_get_selected_pointer(
572 GTK_TREE_VIEW(mimeview->ctree), COL_DATA,
576 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
578 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
582 gtk_tree_model_get_iter(model, &iter, path);
583 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
587 gboolean mimeview_tree_is_empty(MimeView *mimeview)
589 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
591 return !gtk_tree_model_get_iter_first(model, &iter);
594 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
596 GtkTreeIter iter, parent;
599 gtk_tree_model_get_iter(model, &iter, path);
601 if (gtk_tree_model_iter_has_child(model, &iter)) {
602 gtk_tree_path_down(path);
606 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
608 if (!gtk_tree_model_iter_next(model, &iter)) {
610 GtkTreeIter saved_parent = parent;
611 gtk_tree_path_up(path);
612 if (gtk_tree_model_iter_next(model, &parent)) {
613 gtk_tree_path_next(path);
616 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
621 gtk_tree_path_next(path);
628 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
630 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
631 GtkTreeIter iter, child, parent;
634 gtk_tree_model_get_iter(model, &iter, path);
635 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
637 if (gtk_tree_path_prev(path)) {
638 gtk_tree_model_get_iter(model, &iter, path);
640 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
641 gtk_tree_path_down(path);
643 while (gtk_tree_model_iter_next(model, &child))
644 gtk_tree_path_next(path);
651 gtk_tree_path_up(path);
658 gint mimeview_get_selected_part_num(MimeView *mimeview)
660 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
665 gtk_tree_model_get_iter_first(model, &iter);
666 path = gtk_tree_model_get_path(model, &iter);
669 if (!gtk_tree_path_compare(mimeview->opened, path)) {
670 gtk_tree_path_free(path);
675 } while (mimeview_tree_next(model, path));
677 gtk_tree_path_free(path);
682 void mimeview_select_part_num(MimeView *mimeview, gint i)
684 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
687 GtkTreeSelection *selection;
693 gtk_tree_model_get_iter_first(model, &iter);
694 path = gtk_tree_model_get_path(model, &iter);
697 if (!mimeview_tree_next(model, path)) {
698 gtk_tree_path_free(path);
704 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
705 gtk_tree_selection_select_path(selection, path);
706 gtk_tree_path_free(path);
709 static void mimeview_set_multipart_tree(MimeView *mimeview,
713 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
714 GTK_TREE_VIEW(mimeview->ctree)));
716 gchar *content_type, *length, *name;
718 cm_return_if_fail(mimeinfo != NULL);
720 while (mimeinfo != NULL) {
721 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
722 content_type = g_strdup_printf("%s/%s",
723 procmime_get_media_type_str(mimeinfo->type),
726 content_type = g_strdup("UNKNOWN");
728 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
730 if (prefs_common.attach_desc)
731 name = g_strdup(get_part_description(mimeinfo));
733 name = g_strdup(get_part_name(mimeinfo));
735 gtk_tree_store_append(model, &iter, parent);
736 gtk_tree_store_set(model, &iter,
737 COL_MIMETYPE, content_type,
740 COL_DATA, mimeinfo, -1);
741 g_free(content_type);
745 if (mimeinfo->node->children)
746 mimeview_set_multipart_tree(mimeview,
747 (MimeInfo *) mimeinfo->node->children->data, &iter);
748 mimeinfo = mimeinfo->node->next != NULL ?
749 (MimeInfo *) mimeinfo->node->next->data : NULL;
753 static const gchar *get_real_part_name(MimeInfo *partinfo)
755 const gchar *name = NULL;
757 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
759 name = procmime_mimeinfo_get_parameter(partinfo, "name");
764 static const gchar *get_part_name(MimeInfo *partinfo)
768 name = get_real_part_name(partinfo);
775 static const gchar *get_part_description(MimeInfo *partinfo)
777 if (partinfo->description)
778 return partinfo->description;
780 return get_part_name(partinfo);
783 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
788 if (!partinfo) return;
790 fname = mimeview->file;
793 if ((fp = g_fopen(fname, "rb")) == NULL) {
794 FILE_OP_ERROR(fname, "fopen");
798 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
799 FILE_OP_ERROR(mimeview->file, "fseek");
804 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
805 textview_show_part(mimeview->textview, partinfo, fp);
810 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
813 MimeViewerFactory *factory = NULL;
814 MimeViewer *viewer = NULL;
815 gchar *real_contenttype = NULL, *tmp;
817 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
819 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
820 MimeViewerFactory *curfactory = cur->data;
823 while (curfactory->content_types[i] != NULL) {
824 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
825 if (g_pattern_match_simple(tmp, real_contenttype)) {
826 debug_print("%s\n", curfactory->content_types[i]);
827 factory = curfactory;
837 g_free(real_contenttype);
841 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
842 MimeViewer *curviewer = cur->data;
844 if (curviewer->factory == factory)
847 viewer = factory->create_viewer();
848 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
849 GTK_WIDGET(viewer->get_widget(viewer)));
851 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
856 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
858 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
861 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
863 gchar *content_type = NULL;
864 MimeViewer *viewer = NULL;
866 if ((partinfo->type == MIMETYPE_APPLICATION) &&
867 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
868 const gchar *filename;
870 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
871 if (filename == NULL)
872 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
873 if (filename != NULL)
874 content_type = procmime_get_mime_type(filename);
876 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
879 if (content_type != NULL) {
880 viewer = get_viewer_for_content_type(mimeview, content_type);
881 g_free(content_type);
887 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
891 if (mimeview->messageview->partial_display_shown) {
892 noticeview_hide(mimeview->messageview->noticeview);
893 mimeview->messageview->partial_display_shown = FALSE;
896 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
897 if (viewer == NULL) {
898 if (mimeview->mimeviewer != NULL)
899 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
900 mimeview->mimeviewer = NULL;
904 if (mimeview->mimeviewer != NULL)
905 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
907 if (mimeview->mimeviewer != viewer)
908 mimeview->mimeviewer = viewer;
910 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
911 viewer->mimeview = mimeview;
912 viewer->show_mimepart(viewer, mimeview->file, partinfo);
917 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
919 TextView *textview = mimeview->textview;
920 GtkWidget *focused = NULL;
922 if (mainwindow_get_mainwindow())
923 focused = gtkut_get_focused_child(
924 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
926 if ((mimeview->type != MIMEVIEW_VIEWER) &&
927 (mimeview->type == type)) return;
931 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
932 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
933 GTK_WIDGET_PTR(textview)));
935 case MIMEVIEW_VIEWER:
936 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
937 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
938 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
944 gtk_widget_grab_focus(focused);
945 mimeview->type = type;
948 void mimeview_clear(MimeView *mimeview)
955 if (g_slist_find(mimeviews, mimeview) == NULL)
958 noticeview_hide(mimeview->siginfoview);
960 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
961 gtk_tree_store_clear(GTK_TREE_STORE(model));
963 textview_clear(mimeview->textview);
964 if (mimeview->mimeviewer != NULL)
965 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
967 mimeview_free_mimeinfo(mimeview);
969 mimeview->mimeinfo = NULL;
971 gtk_tree_path_free(mimeview->opened);
972 mimeview->opened = NULL;
974 g_free(mimeview->file);
975 mimeview->file = NULL;
977 icon_list_clear(mimeview);
978 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
981 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
982 GtkUIManager *ui_manager;
984 if (messageview->window != NULL)
985 ui_manager = messageview->ui_manager;
987 ui_manager = messageview->mainwin->ui_manager;
989 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
992 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
993 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
995 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
996 gboolean special, SignatureStatus code)
998 gchar *text = NULL, *button_text = NULL;
1000 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1001 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1003 cm_return_if_fail(mimeview != NULL);
1004 cm_return_if_fail(mimeinfo != NULL);
1009 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1012 case SIGNATURE_UNCHECKED:
1013 button_text = _("Check signature");
1014 func = check_signature_cb;
1015 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1018 button_text = _("View full information");
1019 func = display_full_info_cb;
1020 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1022 case SIGNATURE_WARN:
1023 button_text = _("View full information");
1024 func = display_full_info_cb;
1025 icon = STOCK_PIXMAP_PRIVACY_WARN;
1027 case SIGNATURE_KEY_EXPIRED:
1028 button_text = _("View full information");
1029 func = display_full_info_cb;
1030 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1032 case SIGNATURE_INVALID:
1033 button_text = _("View full information");
1034 func = display_full_info_cb;
1035 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1037 case SIGNATURE_CHECK_FAILED:
1038 case SIGNATURE_CHECK_TIMEOUT:
1039 button_text = _("Check again");
1040 func = check_signature_cb;
1041 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1046 if (mycode == SIGNATURE_UNCHECKED) {
1047 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1048 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1050 if (*shortcut == '\0')
1051 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1053 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1057 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1058 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1059 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1060 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1062 if (*shortcut == '\0')
1063 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1065 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1069 noticeview_set_text(mimeview->siginfoview, text);
1070 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1073 noticeview_set_button_text(mimeview->siginfoview, NULL);
1074 noticeview_set_button_press_callback(
1075 mimeview->siginfoview,
1077 (gpointer) mimeview);
1078 noticeview_set_icon(mimeview->siginfoview, icon);
1079 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1081 icon_list_clear(mimeview);
1082 icon_list_create(mimeview, mimeview->mimeinfo);
1087 /* reset all thread stuff, and do the cleanups we've been left to do */
1088 static void mimeview_check_data_reset(MimeView *mimeview)
1091 gboolean must_destroy;
1093 if (!mimeview->check_data)
1096 must_free = mimeview->check_data->free_after_use;
1097 must_destroy = mimeview->check_data->destroy_mimeview;
1099 if (mimeview->check_data->cancel_th_init) {
1100 debug_print("killing canceller thread\n");
1101 mimeview->check_data->cancel_th_init = FALSE;
1102 pthread_cancel(mimeview->check_data->cancel_th);
1106 debug_print("freeing deferred mimeinfo\n");
1107 procmime_mimeinfo_free_all(&mimeview->check_data->siginfo);
1110 g_free(mimeview->check_data);
1111 mimeview->check_data = NULL;
1114 debug_print("freeing deferred mimeview\n");
1115 mimeview_free_mimeinfo(mimeview);
1116 gtk_tree_path_free(mimeview->opened);
1117 g_free(mimeview->file);
1119 mimeviews = g_slist_remove(mimeviews, mimeview);
1123 /* GUI update once the checker thread is done or killed */
1124 static gboolean mimeview_check_sig_thread_cb(void *data)
1126 MimeView *mimeview = (MimeView *) data;
1127 MimeInfo *mimeinfo = mimeview->siginfo;
1129 debug_print("mimeview_check_sig_thread_cb\n");
1131 if (mimeinfo == NULL) {
1132 /* message changed !? */
1133 g_warning("no more siginfo!");
1137 if (!mimeview->check_data) {
1138 g_warning("nothing to check");
1142 if (mimeview->check_data->siginfo != mimeinfo) {
1143 /* message changed !? */
1144 g_warning("different siginfo!");
1148 if (mimeview->check_data->destroy_mimeview ||
1149 mimeview->check_data->free_after_use) {
1150 debug_print("not bothering, we're changing message\n");
1155 if (mimeview->check_data->timeout)
1156 update_signature_noticeview(mimeview, mimeview->siginfo,
1157 TRUE, SIGNATURE_CHECK_TIMEOUT);
1159 update_signature_noticeview(mimeview, mimeview->siginfo,
1163 mimeview_check_data_reset(mimeview);
1167 /* sig checker thread */
1168 static void *mimeview_check_sig_worker_thread(void *data)
1170 MimeView *mimeview = (MimeView *)data;
1171 MimeInfo *mimeinfo = mimeview->siginfo;
1173 debug_print("checking...\n");
1175 if (!mimeview->check_data)
1178 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1179 privacy_mimeinfo_check_signature(mimeinfo);
1180 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1181 mimeview->check_data->cancel_th_init = FALSE;
1182 pthread_cancel(mimeview->check_data->cancel_th);
1185 /* that's strange! we changed message without
1186 * getting killed. */
1187 g_warning("different siginfo!");
1188 mimeview_check_data_reset(mimeview);
1192 /* use g_timeout so that GUI updates is done from the
1194 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1199 /* killer thread - acts when the checker didn't work fast
1201 static void *mimeview_check_sig_cancel_thread(void *data)
1203 MimeView *mimeview = (MimeView *)data;
1205 if (!mimeview->check_data)
1206 return NULL; /* nothing to kill ! */
1208 /* wait for a few seconds... */
1209 debug_print("waiting a while\n");
1211 g_usleep(5 * 1000 * 1000);
1213 if (!mimeview->check_data)
1214 return NULL; /* nothing to kill, it's done in time :) */
1216 /* too late, go away checker thread */
1217 debug_print("killing checker thread\n");
1218 if (mimeview->check_data->th_init) {
1219 mimeview->check_data->th_init = FALSE;
1220 pthread_cancel(mimeview->check_data->th);
1223 /* tell upstream it was a timeout */
1224 mimeview->check_data->timeout = TRUE;
1225 /* use g_timeout so that GUI updates is done from the
1227 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1232 /* get rid of the checker thread right now - used when changing the
1233 * displayed message for example. */
1234 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1236 if (!mimeview->check_data)
1238 debug_print("killing checker thread NOW\n");
1239 if (mimeview->check_data->th_init) {
1240 mimeview->check_data->th_init = FALSE;
1241 pthread_cancel(mimeview->check_data->th);
1244 /* tell upstream it was a timeout */
1245 mimeview->check_data->timeout = TRUE;
1246 mimeview_check_sig_thread_cb(mimeview);
1250 /* creates a thread to check the signature, and a second one
1251 * to kill the first one after a timeout */
1252 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1255 pthread_attr_t detach, detach2;
1257 if (mimeview->check_data) {
1258 g_warning("already checking it");
1262 mimeview->check_data = g_new0(SigCheckData, 1);
1263 mimeview->check_data->siginfo = mimeview->siginfo;
1264 debug_print("creating thread\n");
1266 /* init thread attributes and create the checker thread */
1267 if (pthread_attr_init(&detach) != 0 ||
1268 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1269 pthread_attr_init(&detach2) != 0 ||
1270 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1271 pthread_create(&th, &detach,
1272 mimeview_check_sig_worker_thread,
1274 /* arh. We'll do it synchronously. */
1275 g_warning("can't create thread");
1276 g_free(mimeview->check_data);
1277 mimeview->check_data = NULL;
1280 mimeview->check_data->th = th;
1281 mimeview->check_data->th_init = TRUE;
1284 /* create the killer thread */
1285 pthread_create(&th2, &detach2,
1286 mimeview_check_sig_cancel_thread,
1289 mimeview->check_data->cancel_th = th2;
1290 mimeview->check_data->cancel_th_init = TRUE;
1294 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1296 MimeView *mimeview = (MimeView *) user_data;
1297 MimeInfo *mimeinfo = mimeview->siginfo;
1299 if (mimeinfo == NULL)
1302 if (mimeview->check_data)
1305 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1308 /* let's do it non-blocking */
1309 mimeview_check_sig_in_thread(mimeview);
1310 if (!mimeview->check_data) /* let's check syncronously */
1313 debug_print("checking without thread\n");
1314 privacy_mimeinfo_check_signature(mimeinfo);
1315 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1319 void mimeview_check_signature(MimeView *mimeview)
1321 check_signature_cb(NULL, mimeview);
1324 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1326 MimeView *mimeview = (MimeView *) user_data;
1327 gtk_tree_path_free(mimeview->opened);
1328 mimeview->opened = NULL;
1329 mimeview_selected(gtk_tree_view_get_selection(
1330 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1333 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1335 MimeView *mimeview = (MimeView *) user_data;
1338 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1339 textview_set_text(mimeview->textview, siginfo);
1341 noticeview_set_button_text(mimeview->siginfoview, NULL);
1342 noticeview_set_button_press_callback(
1343 mimeview->siginfoview,
1344 G_CALLBACK(redisplay_email),
1345 (gpointer) mimeview);
1346 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1349 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1352 MimeInfo *first_text;
1354 cm_return_if_fail(mimeview != NULL);
1355 cm_return_if_fail(selected != NULL);
1357 if (selected->type == MIMETYPE_MESSAGE
1358 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1359 /* if the first text part is signed, check that */
1360 first_text = selected;
1361 while (first_text && first_text->type != MIMETYPE_TEXT) {
1362 first_text = procmime_mimeinfo_next(first_text);
1365 update_signature_info(mimeview, first_text);
1371 while (siginfo != NULL) {
1372 if (privacy_mimeinfo_is_signed(siginfo))
1374 siginfo = procmime_mimeinfo_parent(siginfo);
1376 mimeview->siginfo = siginfo;
1378 /* This shortcut boolean is there to correctly set the menu's
1379 * CheckSignature item sensitivity without killing performance
1380 * each time the menu sensitiveness is updated (a lot).
1382 mimeview->signed_part = (siginfo != NULL);
1384 if (siginfo == NULL) {
1385 noticeview_hide(mimeview->siginfoview);
1389 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1390 noticeview_show(mimeview->siginfoview);
1393 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1395 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1396 GtkTreeModel *model;
1401 selection = gtk_tree_view_get_selection(ctree);
1402 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1405 path = gtk_tree_model_get_path(model, &iter);
1407 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1408 gtk_tree_path_free(path);
1412 gtk_tree_path_free(mimeview->opened);
1413 mimeview->opened = path;
1414 mimeview->spec_part = NULL;
1415 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1417 partinfo = mimeview_get_node_part(mimeview, path);
1418 if (!partinfo) return;
1420 /* ungrab the mouse event */
1421 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1422 gtk_grab_remove(GTK_WIDGET(ctree));
1423 if (gdk_pointer_is_grabbed())
1424 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1427 mimeview->textview->default_text = FALSE;
1429 update_signature_info(mimeview, partinfo);
1431 if (!mimeview_show_part(mimeview, partinfo)) {
1432 switch (partinfo->type) {
1434 case MIMETYPE_MESSAGE:
1435 case MIMETYPE_MULTIPART:
1436 mimeview_show_message_part(mimeview, partinfo);
1440 mimeview->textview->default_text = TRUE;
1441 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1442 textview_clear(mimeview->textview);
1443 textview_show_mime_part(mimeview->textview, partinfo);
1448 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1449 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1450 mimeview_check_signature(mimeview);
1454 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1457 GtkTreeSelection *selection;
1460 if (!event) return FALSE;
1462 if (event->button == 2 || event->button == 3) {
1463 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1464 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1467 gtk_tree_selection_select_iter(selection, &iter);
1469 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1474 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1477 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1478 if (event->direction == GDK_SCROLL_UP) {
1479 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1481 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1483 gtk_vscrollbutton_scroll(scrollbutton);
1487 /* from gdkevents.c */
1488 #define DOUBLE_CLICK_TIME 250
1490 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1493 static MimeInfo *lastinfo;
1494 static guint32 lasttime;
1496 if (event->button == 2 ||
1497 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1498 /* call external program for image, audio or html */
1499 mimeview_launch(mimeview, partinfo);
1501 } else if (event->button == 3) {
1502 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1503 partinfo->type == MIMETYPE_IMAGE ||
1504 partinfo->type == MIMETYPE_MULTIPART))
1505 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1507 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1510 partinfo->type == MIMETYPE_APPLICATION &&
1511 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1512 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1515 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1517 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1518 "pop_partinfo", partinfo);
1520 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1521 NULL, NULL, NULL, NULL,
1522 event->button, event->time);
1526 lastinfo = partinfo;
1527 lasttime = event->time;
1532 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1534 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1537 void mimeview_select_next_part(MimeView *mimeview)
1539 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1540 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1541 GtkTreeSelection *selection;
1544 MimeInfo *partinfo = NULL;
1547 if (!mimeview->opened) return;
1549 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1550 path = gtk_tree_model_get_path(model, &iter);
1552 has_next = mimeview_tree_next(model, path);
1554 has_next = gtk_tree_model_get_iter_first(model, &iter);
1555 gtk_tree_path_free(path);
1556 path = gtk_tree_model_get_path(model, &iter);
1560 partinfo = mimeview_get_node_part(mimeview, path);
1561 if (partinfo->type == MIMETYPE_MULTIPART ||
1562 (!prefs_common.show_inline_attachments && partinfo->id))
1564 selection = gtk_tree_view_get_selection(ctree);
1565 gtk_tree_selection_select_path(selection, path);
1566 icon_list_toggle_by_mime_info(mimeview, partinfo);
1569 gtk_tree_path_free(path);
1572 void mimeview_select_prev_part(MimeView *mimeview)
1574 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1575 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1576 GtkTreeSelection *selection;
1579 MimeInfo *partinfo = NULL;
1582 if (!mimeview->opened) return;
1584 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1585 path = gtk_tree_model_get_path(model, &iter);
1587 has_prev = mimeview_tree_prev(mimeview, path);
1589 while (mimeview_tree_next(model, path)) {}
1594 partinfo = mimeview_get_node_part(mimeview, path);
1595 if (partinfo->type == MIMETYPE_MULTIPART ||
1596 (!prefs_common.show_inline_attachments && partinfo->id))
1598 selection = gtk_tree_view_get_selection(ctree);
1599 gtk_tree_selection_select_path(selection, path);
1600 icon_list_toggle_by_mime_info(mimeview, partinfo);
1603 gtk_tree_path_free(path);
1606 #define BREAK_ON_MODIFIER_KEY() \
1607 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1609 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1612 SummaryView *summaryview;
1614 if (!event) return FALSE;
1615 if (!mimeview->opened) return FALSE;
1617 summaryview = mimeview->messageview->mainwin->summaryview;
1619 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1622 switch (event->keyval) {
1625 textview_scroll_max(mimeview->textview,
1626 (event->keyval == GDK_KEY_Home));
1628 case GDK_KEY_Page_Down:
1630 if (mimeview_scroll_page(mimeview,
1631 (event->state & GDK_SHIFT_MASK) != 0))
1634 if (!(event->state & GDK_SHIFT_MASK))
1635 mimeview_select_next_part(mimeview);
1637 case GDK_KEY_Page_Up:
1638 case GDK_KEY_BackSpace:
1639 mimeview_scroll_page(mimeview, TRUE);
1641 case GDK_KEY_Return:
1642 case GDK_KEY_KP_Enter:
1643 mimeview_scroll_one_line(mimeview,
1644 (event->state & GDK_MOD1_MASK) != 0);
1648 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1654 if (mimeview->messageview->new_window) return FALSE;
1656 return summary_pass_key_press_event(summaryview, event);
1659 static void mimeview_drag_data_get(GtkWidget *widget,
1660 GdkDragContext *drag_context,
1661 GtkSelectionData *selection_data,
1666 gchar *filename = NULL, *uriname, *tmp;
1671 if (!mimeview->opened) return;
1672 if (!mimeview->file) return;
1674 partinfo = mimeview_get_selected_part(mimeview);
1675 if (!partinfo) return;
1677 if (strlen(get_part_name(partinfo)) > 0) {
1678 filename = g_path_get_basename(get_part_name(partinfo));
1679 if (*filename == '\0') return;
1680 } else if (partinfo->type == MIMETYPE_MESSAGE
1681 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1683 GPtrArray *headers = NULL;
1686 fp = g_fopen(partinfo->data.filename, "rb");
1687 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1688 headers = procheader_get_header_array_asis(fp);
1691 for (i = 0; i < headers->len; i++) {
1692 Header *header = g_ptr_array_index(headers, i);
1693 if (procheader_headername_equal(header->name, "Subject")) {
1694 unfold_line(header->body);
1695 name = g_strconcat(header->body, ".txt", NULL);
1696 subst_for_filename(name);
1699 procheader_header_array_destroy(headers);
1705 filename = g_path_get_basename(name);
1708 if (filename == NULL)
1709 filename = g_path_get_basename("Unnamed part");
1711 if (!g_utf8_validate(filename, -1, NULL))
1712 tmp = conv_codeset_strdup(filename,
1713 conv_get_locale_charset_str(),
1716 tmp = g_strdup(filename);
1719 g_warning("filename not in UTF-8");
1720 tmp = g_strdup("Unnamed part");
1722 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1726 if (is_file_exist(filename)) {
1728 gchar *prefix = NULL;
1729 gchar *new_name = NULL;
1730 if (strrchr(tmp, '.')) {
1731 prefix = g_strdup(tmp);
1732 ext = g_strdup(strrchr(tmp, '.'));
1733 *(strrchr(prefix, '.')) = '\0';
1735 prefix = g_strdup(tmp);
1739 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1743 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1746 goto check_new_file;
1751 if ((err = procmime_get_part(filename, partinfo)) < 0)
1753 (_("Couldn't save the part of multipart message: %s"),
1756 tmp = g_filename_to_uri(filename, NULL, NULL);
1757 uriname = g_strconcat(tmp, "\r\n", NULL);
1760 gtk_selection_data_set(selection_data,
1761 gtk_selection_data_get_target(selection_data), 8,
1762 (guchar *)uriname, strlen(uriname));
1769 * Returns a filename (with path) for an attachment
1770 * \param partinfo The attachment to save
1771 * \param basedir The target directory
1772 * \param number Used for dummy filename if attachment is unnamed
1774 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1775 const gchar *basedir,
1781 filename = g_strdup(get_part_name(partinfo));
1782 if (!filename || !*filename) {
1784 filename = g_strdup_printf("noname.%d", number);
1787 if (!g_utf8_validate(filename, -1, NULL)) {
1788 gchar *tmp = conv_filename_to_utf8(filename);
1793 subst_for_filename(filename);
1795 fullname = g_strconcat
1796 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1797 ? &filename[1] : filename, NULL);
1800 filename = conv_filename_from_utf8(fullname);
1806 * Write a single attachment to file
1807 * \param filename Filename with path
1808 * \param partinfo Attachment to save
1810 static gboolean mimeview_write_part(const gchar *filename,
1812 gboolean handle_error)
1817 dir= g_path_get_dirname(filename);
1818 if (!is_dir_exist(dir))
1822 if (is_file_exist(filename)) {
1827 if (!g_utf8_validate(filename, -1, NULL))
1828 tmp = conv_filename_to_utf8(filename);
1830 tmp = g_strdup(filename);
1832 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1835 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
1836 GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST);
1838 if (G_ALERTALTERNATE != aval) return FALSE;
1841 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1842 debug_print("error saving MIME part: %d\n", err);
1845 (_("Couldn't save the part of multipart message: %s"),
1853 static AlertValue mimeview_save_all_error_ask(gint n)
1855 gchar *message = g_strdup_printf(
1856 _("An error has occurred while saving message part #%d. "
1857 "Do you want to cancel operation or skip error and "
1859 AlertValue av = alertpanel_full(_("Error saving all message parts"),
1860 message, GTK_STOCK_CANCEL, _("Skip"), _("Skip all"),
1861 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1866 static void mimeview_save_all_info(gint errors, gint total)
1869 gchar *msg = g_strdup_printf(
1870 ngettext("%d file saved successfully.",
1871 "%d files saved successfully.",
1874 alertpanel_notice("%s", msg);
1877 gchar *msg1 = g_strdup_printf(
1878 ngettext("%d file saved successfully",
1879 "%d files saved successfully",
1882 gchar *msg2 = g_strdup_printf(
1883 ngettext("%s, %d file failed.",
1884 "%s, %d files failed.",
1887 alertpanel_warning("%s", msg2);
1894 * Menu callback: Save all attached files
1895 * \param mimeview Current display
1897 static void mimeview_save_all(MimeView *mimeview)
1901 gchar *startdir = NULL;
1902 gint number = 1, errors = 0;
1903 gboolean skip_errors = FALSE;
1905 if (!mimeview->opened) return;
1906 if (!mimeview->file) return;
1907 if (!mimeview->mimeinfo) return;
1909 partinfo = mimeview->mimeinfo;
1910 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1911 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1913 startdir = g_strdup(get_home_dir());
1915 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1916 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1922 if (!is_dir_exist (dirname)) {
1923 alertpanel_error(_("'%s' is not a directory."), dirname);
1929 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1930 dirname[strlen(dirname)-1] = '\0';
1932 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1933 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1934 partinfo = procmime_mimeinfo_next(partinfo);
1935 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1936 partinfo = procmime_mimeinfo_next(partinfo);
1937 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1938 partinfo = procmime_mimeinfo_next(partinfo);
1941 while (partinfo != NULL) {
1942 if (partinfo->type != MIMETYPE_MESSAGE &&
1943 partinfo->type != MIMETYPE_MULTIPART &&
1944 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1945 || get_real_part_name(partinfo) != NULL)) {
1946 gchar *filename = mimeview_get_filename_for_part(
1947 partinfo, dirname, number++);
1949 gboolean ok = mimeview_write_part(filename, partinfo, FALSE);
1954 AlertValue av = mimeview_save_all_error_ask(number - 1);
1955 skip_errors = (av == G_ALERTOTHER);
1956 if (av == G_ALERTDEFAULT) /* cancel */
1961 partinfo = procmime_mimeinfo_next(partinfo);
1964 g_free(prefs_common.attach_save_dir);
1966 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1967 -1, NULL, NULL, NULL);
1970 mimeview_save_all_info(errors, number - 1);
1973 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1975 MimeInfo *partinfo = NULL;
1976 if (mimeview->spec_part) {
1977 partinfo = mimeview->spec_part;
1978 mimeview->spec_part = NULL;
1980 partinfo = (MimeInfo *) g_object_get_data
1981 (G_OBJECT(mimeview->popupmenu),
1983 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1984 "pop_partinfo", NULL);
1986 partinfo = mimeview_get_selected_part(mimeview);
1993 * Menu callback: Save the selected attachment
1994 * \param mimeview Current display
1996 void mimeview_save_as(MimeView *mimeview)
1999 gchar *filepath = NULL;
2000 gchar *filedir = NULL;
2002 gchar *partname = NULL;
2004 if (!mimeview->opened) return;
2005 if (!mimeview->file) return;
2007 partinfo = mimeview_get_part_to_use(mimeview);
2009 cm_return_if_fail(partinfo != NULL);
2011 if (get_part_name(partinfo) == NULL) {
2014 partname = g_strdup(get_part_name(partinfo));
2016 if (!g_utf8_validate(partname, -1, NULL)) {
2017 gchar *tmp = conv_filename_to_utf8(partname);
2019 tmp = conv_codeset_strdup(partname,
2020 conv_get_locale_charset_str(),
2029 subst_for_filename(partname);
2031 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2032 filepath = g_strconcat(prefs_common.attach_save_dir,
2033 G_DIR_SEPARATOR_S, partname, NULL);
2035 filepath = g_strdup(partname);
2039 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2040 filename = filesel_select_file_save(_("Save as"), filepath);
2046 mimeview_write_part(filename, partinfo, TRUE);
2048 filedir = g_path_get_dirname(filename);
2049 if (filedir && strcmp(filedir, ".")) {
2050 g_free(prefs_common.attach_save_dir);
2051 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2058 void mimeview_display_as_text(MimeView *mimeview)
2062 if (!mimeview->opened) return;
2064 partinfo = mimeview_get_part_to_use(mimeview);
2065 mimeview_select_mimepart_icon(mimeview, partinfo);
2066 cm_return_if_fail(partinfo != NULL);
2067 mimeview_show_message_part(mimeview, partinfo);
2070 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2075 if (!mimeview->opened) return;
2076 if (!mimeview->file) return;
2079 partinfo = mimeview_get_part_to_use(mimeview);
2081 cm_return_if_fail(partinfo != NULL);
2083 filename = procmime_get_tmp_file_name(partinfo);
2085 if ((err = procmime_get_part(filename, partinfo)) < 0)
2087 (_("Couldn't save the part of multipart message: %s"),
2090 mimeview_view_file(filename, partinfo, NULL, mimeview);
2096 void mimeview_open_with(MimeView *mimeview)
2100 if (!mimeview) return;
2101 if (!mimeview->opened) return;
2102 if (!mimeview->file) return;
2104 partinfo = mimeview_get_part_to_use(mimeview);
2106 mimeview_open_part_with(mimeview, partinfo, FALSE);
2109 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2113 gchar *mime_command = NULL;
2114 gchar *content_type = NULL;
2117 cm_return_if_fail(partinfo != NULL);
2119 filename = procmime_get_tmp_file_name(partinfo);
2121 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2123 (_("Couldn't save the part of multipart message: %s"),
2129 if (!prefs_common.mime_open_cmd_history)
2130 prefs_common.mime_open_cmd_history =
2131 add_history(NULL, prefs_common.mime_open_cmd);
2133 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2134 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2135 /* guess content-type from filename */
2136 content_type = procmime_get_mime_type(filename);
2138 if (content_type == NULL) {
2139 content_type = procmime_get_content_type_str(partinfo->type,
2143 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2144 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2145 mime_command = g_strdup(prefs_common_get_uri_cmd());
2146 g_free(content_type);
2147 content_type = NULL;
2148 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2149 || !prefs_common_get_ext_editor_cmd()[0]) {
2150 mime_command = mailcap_get_command_for_type(content_type, filename);
2152 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2153 g_free(content_type);
2154 content_type = NULL;
2156 if (mime_command == NULL) {
2157 /* try with extension this time */
2158 g_free(content_type);
2159 content_type = procmime_get_mime_type(filename);
2160 mime_command = mailcap_get_command_for_type(content_type, filename);
2163 if (mime_command == NULL)
2167 gboolean remember = FALSE;
2168 if (content_type != NULL)
2169 cmd = input_dialog_combo_remember
2171 _("Enter the command-line to open file:\n"
2172 "('%s' will be replaced with file name)"),
2173 mime_command ? mime_command : prefs_common.mime_open_cmd,
2174 prefs_common.mime_open_cmd_history, &remember);
2176 cmd = input_dialog_combo
2178 _("Enter the command-line to open file:\n"
2179 "('%s' will be replaced with file name)"),
2180 mime_command ? mime_command : prefs_common.mime_open_cmd,
2181 prefs_common.mime_open_cmd_history);
2182 if (cmd && remember) {
2183 mailcap_update_default(content_type, cmd);
2185 g_free(mime_command);
2190 mimeview_view_file(filename, partinfo, cmd, mimeview);
2191 g_free(prefs_common.mime_open_cmd);
2192 prefs_common.mime_open_cmd = cmd;
2193 prefs_common.mime_open_cmd_history =
2194 add_history(prefs_common.mime_open_cmd_history, cmd);
2197 g_free(content_type);
2202 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2204 GList *attach_file = NULL;
2205 AttachInfo *ainfo = NULL;
2209 if (!mimeview->opened) return;
2210 if (!mimeview->file) return;
2212 cm_return_if_fail(partinfo != NULL);
2214 filename = procmime_get_tmp_file_name(partinfo);
2216 if (!(err = procmime_get_part(filename, partinfo))) {
2217 ainfo = g_new0(AttachInfo, 1);
2218 ainfo->file = filename;
2219 ainfo->name = g_strdup(get_part_name(partinfo));
2220 ainfo->content_type = procmime_get_content_type_str(
2221 partinfo->type, partinfo->subtype);
2222 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2223 partinfo, "charset"));
2224 attach_file = g_list_append(attach_file, ainfo);
2226 compose_new(NULL, NULL, attach_file);
2228 g_free(ainfo->name);
2229 g_free(ainfo->content_type);
2230 g_free(ainfo->charset);
2232 g_list_free(attach_file);
2235 (_("Couldn't save the part of multipart message: %s"),
2240 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2241 const gchar *cmd, MimeView *mimeview)
2245 gchar buf[BUFFSIZE];
2248 mimeview_open_part_with(mimeview, partinfo, TRUE);
2250 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2251 !strchr(p + 2, '%')) {
2252 g_snprintf(buf, sizeof(buf), cmd, filename);
2253 if (!prefs_common.save_parts_readwrite)
2254 g_chmod(filename, S_IRUSR);
2256 g_chmod(filename, S_IRUSR|S_IWUSR);
2258 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2259 mimeview_open_part_with(mimeview, partinfo, FALSE);
2261 if (execute_command_line(buf, TRUE, NULL) != 0) {
2262 if (!prefs_common.save_parts_readwrite)
2263 g_chmod(filename, S_IRUSR|S_IWUSR);
2264 mimeview_open_part_with(mimeview, partinfo, FALSE);
2268 SHFILEINFO file_info;
2269 GError *error = NULL;
2270 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2272 if (error != NULL) {
2273 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2275 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2276 g_error_free(error);
2280 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2281 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2282 _("This attachment is an executable file. Executing "
2283 "untrusted binaries is dangerous and could probably "
2284 "lead to compromission of your computer.\n\n"
2285 "Do you want to run this file?"), GTK_STOCK_CANCEL,
2287 NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
2288 if (val == G_ALERTALTERNATE) {
2289 debug_print("executing binary\n");
2290 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2293 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2301 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2303 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2306 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2308 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2311 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2313 GSList *mimeview_list, *viewer_list;
2315 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2316 MimeView *mimeview = (MimeView *) mimeview_list->data;
2318 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2319 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2320 mimeview->mimeviewer = NULL;
2323 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2324 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2326 mimeviewer->destroy_viewer(mimeviewer);
2327 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2331 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2334 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2339 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2340 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2342 if (event->button == 1) {
2343 icon_selected(mimeview, num, partinfo);
2344 gtk_widget_grab_focus(button);
2345 icon_list_toggle_by_mime_info(mimeview, partinfo);
2347 part_button_pressed(mimeview, event, partinfo);
2352 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2354 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2357 MimeInfo *curr = NULL;
2359 gtk_tree_model_get_iter_first(model, &iter);
2360 path = gtk_tree_model_get_path(model, &iter);
2363 gtk_tree_model_get_iter(model, &iter, path);
2364 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2365 if (curr == partinfo) {
2366 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2367 GTK_TREE_VIEW(mimeview->ctree));
2368 gtk_tree_selection_select_iter(sel, &iter);
2369 gtk_tree_path_free(path);
2372 } while (mimeview_tree_next(model, path));
2374 gtk_tree_path_free(path);
2377 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2379 icon_list_toggle_by_mime_info(mimeview, partinfo);
2380 icon_selected(mimeview, -1, partinfo);
2383 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2386 SummaryView *summaryview;
2388 if (!event) return FALSE;
2390 switch (event->keyval) {
2392 if (mimeview_scroll_page(mimeview, FALSE))
2395 mimeview_select_next_part(mimeview);
2399 case GDK_KEY_BackSpace:
2400 mimeview_scroll_page(mimeview, TRUE);
2402 case GDK_KEY_Return:
2403 case GDK_KEY_KP_Enter:
2404 mimeview_scroll_one_line(mimeview,
2405 (event->state & GDK_MOD1_MASK) != 0);
2408 BREAK_ON_MODIFIER_KEY();
2409 mimeview_save_as(mimeview);
2412 BREAK_ON_MODIFIER_KEY();
2413 mimeview_display_as_text(mimeview);
2416 BREAK_ON_MODIFIER_KEY();
2417 mimeview_launch(mimeview, NULL);
2421 BREAK_ON_MODIFIER_KEY();
2422 mimeview_open_with(mimeview);
2426 BREAK_ON_MODIFIER_KEY();
2427 mimeview_check_signature(mimeview);
2430 BREAK_ON_MODIFIER_KEY();
2431 mimeview_select_next_part(mimeview);
2437 if (!mimeview->messageview->mainwin) return FALSE;
2438 summaryview = mimeview->messageview->mainwin->summaryview;
2439 return summary_pass_key_press_event(summaryview, event);
2442 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2444 MimeView *mimeview = (MimeView *)data;
2445 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2447 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2448 "pop_partinfo", partinfo);
2449 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
2450 NULL, NULL, NULL, NULL,
2451 0, gtk_get_current_event_time());
2455 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2457 GtkWidget *pixmap = NULL;
2462 const gchar *desc = NULL;
2463 gchar *sigshort = NULL;
2464 gchar *content_type;
2467 MimeInfo *siginfo = NULL;
2468 MimeInfo *encrypted = NULL;
2473 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2476 vbox = mimeview->icon_vbox;
2477 mimeview->icon_count++;
2478 button = gtk_event_box_new();
2480 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2481 G_CALLBACK(mimeview_visi_notify), mimeview);
2482 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2483 G_CALLBACK(mimeview_leave_notify), mimeview);
2484 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2485 G_CALLBACK(mimeview_enter_notify), mimeview);
2487 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2488 g_object_set_data(G_OBJECT(button), "icon_number",
2489 GINT_TO_POINTER(mimeview->icon_count));
2490 g_object_set_data(G_OBJECT(button), "partinfo",
2493 switch (mimeinfo->type) {
2496 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2497 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2498 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2499 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2500 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2501 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2502 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2503 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2504 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2506 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2508 case MIMETYPE_MESSAGE:
2509 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2511 case MIMETYPE_APPLICATION:
2512 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2513 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2514 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2515 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2516 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2517 stockp = STOCK_PIXMAP_MIME_PDF;
2518 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2519 stockp = STOCK_PIXMAP_MIME_PS;
2521 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2523 case MIMETYPE_IMAGE:
2524 stockp = STOCK_PIXMAP_MIME_IMAGE;
2526 case MIMETYPE_AUDIO:
2527 stockp = STOCK_PIXMAP_MIME_AUDIO;
2530 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2534 partinfo = mimeinfo;
2535 while (partinfo != NULL) {
2536 if (privacy_mimeinfo_is_signed(partinfo)) {
2540 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2541 encrypted = partinfo;
2544 partinfo = procmime_mimeinfo_parent(partinfo);
2547 if (siginfo != NULL) {
2548 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2549 case SIGNATURE_UNCHECKED:
2550 case SIGNATURE_CHECK_FAILED:
2551 case SIGNATURE_CHECK_TIMEOUT:
2552 pixmap = stock_pixmap_widget_with_overlay(stockp,
2553 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2556 pixmap = stock_pixmap_widget_with_overlay(stockp,
2557 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2559 case SIGNATURE_WARN:
2560 case SIGNATURE_KEY_EXPIRED:
2561 pixmap = stock_pixmap_widget_with_overlay(stockp,
2562 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2564 case SIGNATURE_INVALID:
2565 pixmap = stock_pixmap_widget_with_overlay(stockp,
2566 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2569 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2570 } else if (encrypted != NULL) {
2571 pixmap = stock_pixmap_widget_with_overlay(stockp,
2572 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2574 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2575 OVERLAY_NONE, 6, 3);
2577 gtk_container_add(GTK_CONTAINER(button), pixmap);
2579 if (prefs_common.attach_desc)
2580 desc = get_part_description(mimeinfo);
2582 desc = get_part_name(mimeinfo);
2585 content_type = procmime_get_content_type_str(mimeinfo->type,
2588 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2589 "\n<b>", _("Size:"), " </b>",
2590 to_human_readable((goffset)mimeinfo->length), NULL);
2591 g_free(content_type);
2592 if (desc && *desc) {
2593 gchar *tmp = NULL, *escaped = NULL;
2594 if (!g_utf8_validate(desc, -1, NULL)) {
2595 tmp = conv_filename_to_utf8(desc);
2597 tmp = g_strdup(desc);
2599 escaped = g_markup_escape_text(tmp,-1);
2601 tiptmp = g_strconcat(tip, "\n<b>",
2602 prefs_common.attach_desc && mimeinfo->description ?
2603 _("Description:") : _("Filename:"),
2604 " </b>", escaped, NULL);
2610 if (sigshort && *sigshort) {
2611 tiptmp = g_strjoin("\n", tip, g_markup_escape_text(sigshort, -1), NULL);
2617 gtk_widget_set_tooltip_markup(button, tip);
2619 gtk_widget_show_all(button);
2620 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2621 mimeview_mime_types, 1, GDK_ACTION_COPY);
2623 g_signal_connect(G_OBJECT(button), "popup-menu",
2624 G_CALLBACK(icon_popup_menu), mimeview);
2625 g_signal_connect(G_OBJECT(button), "button_release_event",
2626 G_CALLBACK(icon_clicked_cb), mimeview);
2627 g_signal_connect(G_OBJECT(button), "key_press_event",
2628 G_CALLBACK(icon_key_pressed), mimeview);
2629 g_signal_connect(G_OBJECT(button), "drag_data_get",
2630 G_CALLBACK(mimeview_drag_data_get), mimeview);
2631 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2633 gtk_widget_size_request(pixmap, &r);
2634 gtk_widget_set_size_request(button, -1, r.height + 4);
2639 static void icon_list_clear (MimeView *mimeview)
2641 GList *child, *orig;
2644 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2645 for (child = orig; child != NULL; child = g_list_next(child)) {
2646 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
2647 GTK_WIDGET(child->data));
2650 mimeview->icon_count = 0;
2651 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2652 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2653 gtk_adjustment_changed(adj);
2657 *\brief Used to 'click' the next or previous icon.
2659 *\return true if the icon 'number' exists and was selected.
2661 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2662 GtkAllocation *size, MimeView *mimeview)
2664 GtkAllocation vbox_size;
2665 GtkAllocation layout_size;
2670 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2672 gtk_widget_get_allocation(mimeview->icon_vbox, &vbox_size);
2673 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2675 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2676 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2678 MAX(vbox_size.height, layout_size.height));
2679 gtk_adjustment_set_step_increment(adj, 10);
2682 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2684 GtkRequisition size;
2685 GtkRequisition requisition;
2687 cm_return_if_fail(mimeinfo != NULL);
2689 while (mimeinfo != NULL) {
2690 if (mimeinfo->type != MIMETYPE_MULTIPART)
2691 icon_list_append_icon(mimeview, mimeinfo);
2692 if (mimeinfo->node->children != NULL)
2693 icon_list_create(mimeview,
2694 (MimeInfo *) mimeinfo->node->children->data);
2695 mimeinfo = mimeinfo->node->next != NULL
2696 ? (MimeInfo *) mimeinfo->node->next->data
2699 gtk_widget_size_request(mimeview->icon_vbox, &size);
2700 gtk_widget_get_requisition(mimeview->icon_mainbox, &requisition);
2701 if (size.width > requisition.width) {
2702 gtk_widget_set_size_request(mimeview->icon_mainbox,
2705 if (mimeview->opened)
2706 icon_list_toggle_by_mime_info(mimeview,
2707 mimeview_get_node_part(mimeview, mimeview->opened));
2710 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2713 GList *children, *child;
2715 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2716 for (child = children; child != NULL; child = g_list_next(child)) {
2717 gboolean *highlight = NULL;
2718 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2720 if (!GTK_IS_EVENT_BOX(child->data))
2723 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2724 *highlight = (g_object_get_data(G_OBJECT(child->data),
2725 "partinfo") == (gpointer)mimeinfo);
2727 gtk_widget_queue_draw(icon);
2729 g_list_free(children);
2732 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2735 prefs_common.mimeview_tree_height = allocation->height;
2738 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2741 g_object_ref(button);
2743 mimeview_leave_notify(button, NULL, NULL);
2745 mimeview->ctree_mode = !mimeview->ctree_mode;
2746 if (mimeview->ctree_mode) {
2747 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2749 gtk_widget_hide(mimeview->icon_mainbox);
2750 gtk_widget_show(mimeview->ctree_mainbox);
2751 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2752 prefs_common.mimeview_tree_height);
2754 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2756 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2757 button, FALSE, FALSE, 0);
2759 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2761 gtk_widget_hide(mimeview->ctree_mainbox);
2762 gtk_widget_show(mimeview->icon_mainbox);
2763 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2765 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2767 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2768 button, FALSE, FALSE, 0);
2769 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2770 if (mimeview->opened)
2771 icon_list_toggle_by_mime_info(mimeview,
2772 mimeview_get_node_part(mimeview, mimeview->opened));
2773 summary_grab_focus(mimeview->mainwin->summaryview);
2775 g_object_unref(button);
2779 void mimeview_update (MimeView *mimeview)
2781 if (mimeview && mimeview->mimeinfo) {
2782 icon_list_clear(mimeview);
2783 icon_list_create(mimeview, mimeview->mimeinfo);
2787 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2789 MessageView *msgview = NULL;
2790 MainWindow *mainwin = NULL;
2795 msgview = mimeview->messageview;
2799 mainwin = msgview->mainwin;
2803 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2804 "pop_partinfo", NULL);
2806 if (!strcmp(cmd, "sc://view_log"))
2807 log_window_show(mainwin->logwin);
2808 else if (!strcmp(cmd, "sc://save_as"))
2809 mimeview_save_as(mimeview);
2810 else if (!strcmp(cmd, "sc://display_as_text"))
2811 mimeview_display_as_text(mimeview);
2813 else if (!strcmp(cmd, "sc://open_with"))
2814 mimeview_open_with(mimeview);
2816 else if (!strcmp(cmd, "sc://open"))
2817 mimeview_launch(mimeview, NULL);
2818 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2819 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2820 icon_selected(mimeview, -1, (MimeInfo *)data);
2821 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2822 mimeview_launch(mimeview, (MimeInfo *)data);
2823 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2824 mimeview->spec_part = (MimeInfo *)data;
2825 part_button_pressed(mimeview, event, (MimeInfo *)data);
2826 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2827 const gchar *tagname = cmd + strlen("sc://search_tags:");
2828 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2829 gtk_toggle_button_set_active(
2830 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2832 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2833 ADVANCED_SEARCH_EXTENDED, buf);
2838 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2840 if (mimeview->type == MIMEVIEW_TEXT)
2841 return textview_scroll_page(mimeview->textview, up);
2842 else if (mimeview->mimeviewer) {
2843 MimeViewer *mimeviewer = mimeview->mimeviewer;
2844 if (mimeviewer->scroll_page)
2845 return mimeviewer->scroll_page(mimeviewer, up);
2850 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2852 if (mimeview->type == MIMEVIEW_TEXT)
2853 textview_scroll_one_line(mimeview->textview, up);
2854 else if (mimeview->mimeviewer) {
2855 MimeViewer *mimeviewer = mimeview->mimeviewer;
2856 if (mimeviewer->scroll_one_line)
2857 mimeviewer->scroll_one_line(mimeviewer, up);