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 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
573 GtkTreeSelection *selection;
574 MimeInfo *partinfo = NULL;
576 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
577 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
578 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
583 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
585 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
589 gtk_tree_model_get_iter(model, &iter, path);
590 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
594 gboolean mimeview_tree_is_empty(MimeView *mimeview)
596 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
598 return !gtk_tree_model_get_iter_first(model, &iter);
601 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
603 GtkTreeIter iter, parent;
606 gtk_tree_model_get_iter(model, &iter, path);
608 if (gtk_tree_model_iter_has_child(model, &iter)) {
609 gtk_tree_path_down(path);
613 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
615 if (!gtk_tree_model_iter_next(model, &iter)) {
617 GtkTreeIter saved_parent = parent;
618 gtk_tree_path_up(path);
619 if (gtk_tree_model_iter_next(model, &parent)) {
620 gtk_tree_path_next(path);
623 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
628 gtk_tree_path_next(path);
635 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
637 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
638 GtkTreeIter iter, child, parent;
641 gtk_tree_model_get_iter(model, &iter, path);
642 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
644 if (gtk_tree_path_prev(path)) {
645 gtk_tree_model_get_iter(model, &iter, path);
647 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
648 gtk_tree_path_down(path);
650 while (gtk_tree_model_iter_next(model, &child))
651 gtk_tree_path_next(path);
658 gtk_tree_path_up(path);
665 gint mimeview_get_selected_part_num(MimeView *mimeview)
667 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
672 gtk_tree_model_get_iter_first(model, &iter);
673 path = gtk_tree_model_get_path(model, &iter);
676 if (!gtk_tree_path_compare(mimeview->opened, path)) {
677 gtk_tree_path_free(path);
682 } while (mimeview_tree_next(model, path));
684 gtk_tree_path_free(path);
689 void mimeview_select_part_num(MimeView *mimeview, gint i)
691 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
694 GtkTreeSelection *selection;
700 gtk_tree_model_get_iter_first(model, &iter);
701 path = gtk_tree_model_get_path(model, &iter);
704 if (!mimeview_tree_next(model, path)) {
705 gtk_tree_path_free(path);
711 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
712 gtk_tree_selection_select_path(selection, path);
713 gtk_tree_path_free(path);
716 static void mimeview_set_multipart_tree(MimeView *mimeview,
720 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
721 GTK_TREE_VIEW(mimeview->ctree)));
723 gchar *content_type, *length, *name;
725 cm_return_if_fail(mimeinfo != NULL);
727 while (mimeinfo != NULL) {
728 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
729 content_type = g_strdup_printf("%s/%s",
730 procmime_get_media_type_str(mimeinfo->type),
733 content_type = g_strdup("UNKNOWN");
735 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
737 if (prefs_common.attach_desc)
738 name = g_strdup(get_part_description(mimeinfo));
740 name = g_strdup(get_part_name(mimeinfo));
742 gtk_tree_store_append(model, &iter, parent);
743 gtk_tree_store_set(model, &iter,
744 COL_MIMETYPE, content_type,
747 COL_DATA, mimeinfo, -1);
748 g_free(content_type);
752 if (mimeinfo->node->children)
753 mimeview_set_multipart_tree(mimeview,
754 (MimeInfo *) mimeinfo->node->children->data, &iter);
755 mimeinfo = mimeinfo->node->next != NULL ?
756 (MimeInfo *) mimeinfo->node->next->data : NULL;
760 static const gchar *get_real_part_name(MimeInfo *partinfo)
762 const gchar *name = NULL;
764 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
766 name = procmime_mimeinfo_get_parameter(partinfo, "name");
771 static const gchar *get_part_name(MimeInfo *partinfo)
775 name = get_real_part_name(partinfo);
782 static const gchar *get_part_description(MimeInfo *partinfo)
784 if (partinfo->description)
785 return partinfo->description;
787 return get_part_name(partinfo);
790 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
795 if (!partinfo) return;
797 fname = mimeview->file;
800 if ((fp = g_fopen(fname, "rb")) == NULL) {
801 FILE_OP_ERROR(fname, "fopen");
805 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
806 FILE_OP_ERROR(mimeview->file, "fseek");
811 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
812 textview_show_part(mimeview->textview, partinfo, fp);
817 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
820 MimeViewerFactory *factory = NULL;
821 MimeViewer *viewer = NULL;
822 gchar *real_contenttype = NULL, *tmp;
824 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
826 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
827 MimeViewerFactory *curfactory = cur->data;
830 while (curfactory->content_types[i] != NULL) {
831 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
832 if (g_pattern_match_simple(tmp, real_contenttype)) {
833 debug_print("%s\n", curfactory->content_types[i]);
834 factory = curfactory;
844 g_free(real_contenttype);
848 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
849 MimeViewer *curviewer = cur->data;
851 if (curviewer->factory == factory)
854 viewer = factory->create_viewer();
855 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
856 GTK_WIDGET(viewer->get_widget(viewer)));
858 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
863 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
865 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
868 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
870 gchar *content_type = NULL;
871 MimeViewer *viewer = NULL;
873 if ((partinfo->type == MIMETYPE_APPLICATION) &&
874 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
875 const gchar *filename;
877 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
878 if (filename == NULL)
879 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
880 if (filename != NULL)
881 content_type = procmime_get_mime_type(filename);
883 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
886 if (content_type != NULL) {
887 viewer = get_viewer_for_content_type(mimeview, content_type);
888 g_free(content_type);
894 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
898 if (mimeview->messageview->partial_display_shown) {
899 noticeview_hide(mimeview->messageview->noticeview);
900 mimeview->messageview->partial_display_shown = FALSE;
903 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
904 if (viewer == NULL) {
905 if (mimeview->mimeviewer != NULL)
906 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
907 mimeview->mimeviewer = NULL;
911 if (mimeview->mimeviewer != NULL)
912 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
914 if (mimeview->mimeviewer != viewer)
915 mimeview->mimeviewer = viewer;
917 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
918 viewer->mimeview = mimeview;
919 viewer->show_mimepart(viewer, mimeview->file, partinfo);
924 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
926 TextView *textview = mimeview->textview;
927 GtkWidget *focused = NULL;
929 if (mainwindow_get_mainwindow())
930 focused = gtkut_get_focused_child(
931 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
933 if ((mimeview->type != MIMEVIEW_VIEWER) &&
934 (mimeview->type == type)) return;
938 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
939 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
940 GTK_WIDGET_PTR(textview)));
942 case MIMEVIEW_VIEWER:
943 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
944 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
945 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
951 gtk_widget_grab_focus(focused);
952 mimeview->type = type;
955 void mimeview_clear(MimeView *mimeview)
962 if (g_slist_find(mimeviews, mimeview) == NULL)
965 noticeview_hide(mimeview->siginfoview);
967 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
968 gtk_tree_store_clear(GTK_TREE_STORE(model));
970 textview_clear(mimeview->textview);
971 if (mimeview->mimeviewer != NULL)
972 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
974 mimeview_free_mimeinfo(mimeview);
976 mimeview->mimeinfo = NULL;
978 gtk_tree_path_free(mimeview->opened);
979 mimeview->opened = NULL;
981 g_free(mimeview->file);
982 mimeview->file = NULL;
984 icon_list_clear(mimeview);
985 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
988 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
989 GtkUIManager *ui_manager;
991 if (messageview->window != NULL)
992 ui_manager = messageview->ui_manager;
994 ui_manager = messageview->mainwin->ui_manager;
996 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
999 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1000 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1002 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
1003 gboolean special, SignatureStatus code)
1005 gchar *text = NULL, *button_text = NULL;
1007 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1008 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1010 cm_return_if_fail(mimeview != NULL);
1011 cm_return_if_fail(mimeinfo != NULL);
1016 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1019 case SIGNATURE_UNCHECKED:
1020 button_text = _("Check signature");
1021 func = check_signature_cb;
1022 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1025 button_text = _("View full information");
1026 func = display_full_info_cb;
1027 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1029 case SIGNATURE_WARN:
1030 button_text = _("View full information");
1031 func = display_full_info_cb;
1032 icon = STOCK_PIXMAP_PRIVACY_WARN;
1034 case SIGNATURE_KEY_EXPIRED:
1035 button_text = _("View full information");
1036 func = display_full_info_cb;
1037 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1039 case SIGNATURE_INVALID:
1040 button_text = _("View full information");
1041 func = display_full_info_cb;
1042 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1044 case SIGNATURE_CHECK_FAILED:
1045 case SIGNATURE_CHECK_TIMEOUT:
1046 button_text = _("Check again");
1047 func = check_signature_cb;
1048 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1053 if (mycode == SIGNATURE_UNCHECKED) {
1054 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1055 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1057 if (*shortcut == '\0')
1058 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1060 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1064 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1065 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1066 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1067 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1069 if (*shortcut == '\0')
1070 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1072 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1076 noticeview_set_text(mimeview->siginfoview, text);
1077 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1080 noticeview_set_button_text(mimeview->siginfoview, NULL);
1081 noticeview_set_button_press_callback(
1082 mimeview->siginfoview,
1084 (gpointer) mimeview);
1085 noticeview_set_icon(mimeview->siginfoview, icon);
1086 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1088 icon_list_clear(mimeview);
1089 icon_list_create(mimeview, mimeview->mimeinfo);
1094 /* reset all thread stuff, and do the cleanups we've been left to do */
1095 static void mimeview_check_data_reset(MimeView *mimeview)
1098 gboolean must_destroy;
1100 if (!mimeview->check_data)
1103 must_free = mimeview->check_data->free_after_use;
1104 must_destroy = mimeview->check_data->destroy_mimeview;
1106 if (mimeview->check_data->cancel_th_init) {
1107 debug_print("killing canceller thread\n");
1108 mimeview->check_data->cancel_th_init = FALSE;
1109 pthread_cancel(mimeview->check_data->cancel_th);
1113 debug_print("freeing deferred mimeinfo\n");
1114 procmime_mimeinfo_free_all(&mimeview->check_data->siginfo);
1117 g_free(mimeview->check_data);
1118 mimeview->check_data = NULL;
1121 debug_print("freeing deferred mimeview\n");
1122 mimeview_free_mimeinfo(mimeview);
1123 gtk_tree_path_free(mimeview->opened);
1124 g_free(mimeview->file);
1126 mimeviews = g_slist_remove(mimeviews, mimeview);
1130 /* GUI update once the checker thread is done or killed */
1131 static gboolean mimeview_check_sig_thread_cb(void *data)
1133 MimeView *mimeview = (MimeView *) data;
1134 MimeInfo *mimeinfo = mimeview->siginfo;
1136 debug_print("mimeview_check_sig_thread_cb\n");
1138 if (mimeinfo == NULL) {
1139 /* message changed !? */
1140 g_warning("no more siginfo!");
1144 if (!mimeview->check_data) {
1145 g_warning("nothing to check");
1149 if (mimeview->check_data->siginfo != mimeinfo) {
1150 /* message changed !? */
1151 g_warning("different siginfo!");
1155 if (mimeview->check_data->destroy_mimeview ||
1156 mimeview->check_data->free_after_use) {
1157 debug_print("not bothering, we're changing message\n");
1162 if (mimeview->check_data->timeout)
1163 update_signature_noticeview(mimeview, mimeview->siginfo,
1164 TRUE, SIGNATURE_CHECK_TIMEOUT);
1166 update_signature_noticeview(mimeview, mimeview->siginfo,
1170 mimeview_check_data_reset(mimeview);
1174 /* sig checker thread */
1175 static void *mimeview_check_sig_worker_thread(void *data)
1177 MimeView *mimeview = (MimeView *)data;
1178 MimeInfo *mimeinfo = mimeview->siginfo;
1180 debug_print("checking...\n");
1182 if (!mimeview->check_data)
1185 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1186 privacy_mimeinfo_check_signature(mimeinfo);
1187 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1188 mimeview->check_data->cancel_th_init = FALSE;
1189 pthread_cancel(mimeview->check_data->cancel_th);
1192 /* that's strange! we changed message without
1193 * getting killed. */
1194 g_warning("different siginfo!");
1195 mimeview_check_data_reset(mimeview);
1199 /* use g_timeout so that GUI updates is done from the
1201 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1206 /* killer thread - acts when the checker didn't work fast
1208 static void *mimeview_check_sig_cancel_thread(void *data)
1210 MimeView *mimeview = (MimeView *)data;
1212 if (!mimeview->check_data)
1213 return NULL; /* nothing to kill ! */
1215 /* wait for a few seconds... */
1216 debug_print("waiting a while\n");
1218 g_usleep(5 * 1000 * 1000);
1220 if (!mimeview->check_data)
1221 return NULL; /* nothing to kill, it's done in time :) */
1223 /* too late, go away checker thread */
1224 debug_print("killing checker thread\n");
1225 if (mimeview->check_data->th_init) {
1226 mimeview->check_data->th_init = FALSE;
1227 pthread_cancel(mimeview->check_data->th);
1230 /* tell upstream it was a timeout */
1231 mimeview->check_data->timeout = TRUE;
1232 /* use g_timeout so that GUI updates is done from the
1234 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1239 /* get rid of the checker thread right now - used when changing the
1240 * displayed message for example. */
1241 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1243 if (!mimeview->check_data)
1245 debug_print("killing checker thread NOW\n");
1246 if (mimeview->check_data->th_init) {
1247 mimeview->check_data->th_init = FALSE;
1248 pthread_cancel(mimeview->check_data->th);
1251 /* tell upstream it was a timeout */
1252 mimeview->check_data->timeout = TRUE;
1253 mimeview_check_sig_thread_cb(mimeview);
1257 /* creates a thread to check the signature, and a second one
1258 * to kill the first one after a timeout */
1259 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1262 pthread_attr_t detach, detach2;
1264 if (mimeview->check_data) {
1265 g_warning("already checking it");
1269 mimeview->check_data = g_new0(SigCheckData, 1);
1270 mimeview->check_data->siginfo = mimeview->siginfo;
1271 debug_print("creating thread\n");
1273 /* init thread attributes and create the checker thread */
1274 if (pthread_attr_init(&detach) != 0 ||
1275 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1276 pthread_attr_init(&detach2) != 0 ||
1277 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1278 pthread_create(&th, &detach,
1279 mimeview_check_sig_worker_thread,
1281 /* arh. We'll do it synchronously. */
1282 g_warning("can't create thread");
1283 g_free(mimeview->check_data);
1284 mimeview->check_data = NULL;
1287 mimeview->check_data->th = th;
1288 mimeview->check_data->th_init = TRUE;
1291 /* create the killer thread */
1292 pthread_create(&th2, &detach2,
1293 mimeview_check_sig_cancel_thread,
1296 mimeview->check_data->cancel_th = th2;
1297 mimeview->check_data->cancel_th_init = TRUE;
1301 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1303 MimeView *mimeview = (MimeView *) user_data;
1304 MimeInfo *mimeinfo = mimeview->siginfo;
1306 if (mimeinfo == NULL)
1309 if (mimeview->check_data)
1312 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1315 /* let's do it non-blocking */
1316 mimeview_check_sig_in_thread(mimeview);
1317 if (!mimeview->check_data) /* let's check syncronously */
1320 debug_print("checking without thread\n");
1321 privacy_mimeinfo_check_signature(mimeinfo);
1322 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1326 void mimeview_check_signature(MimeView *mimeview)
1328 check_signature_cb(NULL, mimeview);
1331 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1333 MimeView *mimeview = (MimeView *) user_data;
1334 gtk_tree_path_free(mimeview->opened);
1335 mimeview->opened = NULL;
1336 mimeview_selected(gtk_tree_view_get_selection(
1337 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1340 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1342 MimeView *mimeview = (MimeView *) user_data;
1345 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1346 textview_set_text(mimeview->textview, siginfo);
1348 noticeview_set_button_text(mimeview->siginfoview, NULL);
1349 noticeview_set_button_press_callback(
1350 mimeview->siginfoview,
1351 G_CALLBACK(redisplay_email),
1352 (gpointer) mimeview);
1353 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1356 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1359 MimeInfo *first_text;
1361 cm_return_if_fail(mimeview != NULL);
1362 cm_return_if_fail(selected != NULL);
1364 if (selected->type == MIMETYPE_MESSAGE
1365 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1366 /* if the first text part is signed, check that */
1367 first_text = selected;
1368 while (first_text && first_text->type != MIMETYPE_TEXT) {
1369 first_text = procmime_mimeinfo_next(first_text);
1372 update_signature_info(mimeview, first_text);
1378 while (siginfo != NULL) {
1379 if (privacy_mimeinfo_is_signed(siginfo))
1381 siginfo = procmime_mimeinfo_parent(siginfo);
1383 mimeview->siginfo = siginfo;
1385 /* This shortcut boolean is there to correctly set the menu's
1386 * CheckSignature item sensitivity without killing performance
1387 * each time the menu sensitiveness is updated (a lot).
1389 mimeview->signed_part = (siginfo != NULL);
1391 if (siginfo == NULL) {
1392 noticeview_hide(mimeview->siginfoview);
1396 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1397 noticeview_show(mimeview->siginfoview);
1400 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1402 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1403 GtkTreeModel *model;
1408 selection = gtk_tree_view_get_selection(ctree);
1409 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1412 path = gtk_tree_model_get_path(model, &iter);
1414 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1415 gtk_tree_path_free(path);
1419 gtk_tree_path_free(mimeview->opened);
1420 mimeview->opened = path;
1421 mimeview->spec_part = NULL;
1422 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1424 partinfo = mimeview_get_node_part(mimeview, path);
1425 if (!partinfo) return;
1427 /* ungrab the mouse event */
1428 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1429 gtk_grab_remove(GTK_WIDGET(ctree));
1430 if (gdk_pointer_is_grabbed())
1431 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1434 mimeview->textview->default_text = FALSE;
1436 update_signature_info(mimeview, partinfo);
1438 if (!mimeview_show_part(mimeview, partinfo)) {
1439 switch (partinfo->type) {
1441 case MIMETYPE_MESSAGE:
1442 case MIMETYPE_MULTIPART:
1443 mimeview_show_message_part(mimeview, partinfo);
1447 mimeview->textview->default_text = TRUE;
1448 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1449 textview_clear(mimeview->textview);
1450 textview_show_mime_part(mimeview->textview, partinfo);
1455 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1456 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1457 mimeview_check_signature(mimeview);
1461 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1464 GtkTreeSelection *selection;
1467 if (!event) return FALSE;
1469 if (event->button == 2 || event->button == 3) {
1470 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1471 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1474 gtk_tree_selection_select_iter(selection, &iter);
1476 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1481 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1484 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1485 if (event->direction == GDK_SCROLL_UP) {
1486 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1488 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1490 gtk_vscrollbutton_scroll(scrollbutton);
1494 /* from gdkevents.c */
1495 #define DOUBLE_CLICK_TIME 250
1497 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1500 static MimeInfo *lastinfo;
1501 static guint32 lasttime;
1503 if (event->button == 2 ||
1504 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1505 /* call external program for image, audio or html */
1506 mimeview_launch(mimeview, partinfo);
1508 } else if (event->button == 3) {
1509 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1510 partinfo->type == MIMETYPE_IMAGE ||
1511 partinfo->type == MIMETYPE_MULTIPART))
1512 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1514 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1517 partinfo->type == MIMETYPE_APPLICATION &&
1518 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1519 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1522 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1524 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1525 "pop_partinfo", partinfo);
1527 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1528 NULL, NULL, NULL, NULL,
1529 event->button, event->time);
1533 lastinfo = partinfo;
1534 lasttime = event->time;
1539 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1541 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1544 void mimeview_select_next_part(MimeView *mimeview)
1546 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1547 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1548 GtkTreeSelection *selection;
1551 MimeInfo *partinfo = NULL;
1554 if (!mimeview->opened) return;
1556 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1557 path = gtk_tree_model_get_path(model, &iter);
1559 has_next = mimeview_tree_next(model, path);
1561 has_next = gtk_tree_model_get_iter_first(model, &iter);
1562 gtk_tree_path_free(path);
1563 path = gtk_tree_model_get_path(model, &iter);
1567 partinfo = mimeview_get_node_part(mimeview, path);
1568 if (partinfo->type == MIMETYPE_MULTIPART ||
1569 (!prefs_common.show_inline_attachments && partinfo->id))
1571 selection = gtk_tree_view_get_selection(ctree);
1572 gtk_tree_selection_select_path(selection, path);
1573 icon_list_toggle_by_mime_info(mimeview, partinfo);
1576 gtk_tree_path_free(path);
1579 void mimeview_select_prev_part(MimeView *mimeview)
1581 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1582 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1583 GtkTreeSelection *selection;
1586 MimeInfo *partinfo = NULL;
1589 if (!mimeview->opened) return;
1591 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1592 path = gtk_tree_model_get_path(model, &iter);
1594 has_prev = mimeview_tree_prev(mimeview, path);
1596 while (mimeview_tree_next(model, path)) {}
1601 partinfo = mimeview_get_node_part(mimeview, path);
1602 if (partinfo->type == MIMETYPE_MULTIPART ||
1603 (!prefs_common.show_inline_attachments && partinfo->id))
1605 selection = gtk_tree_view_get_selection(ctree);
1606 gtk_tree_selection_select_path(selection, path);
1607 icon_list_toggle_by_mime_info(mimeview, partinfo);
1610 gtk_tree_path_free(path);
1613 #define BREAK_ON_MODIFIER_KEY() \
1614 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1616 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1619 SummaryView *summaryview;
1621 if (!event) return FALSE;
1622 if (!mimeview->opened) return FALSE;
1624 summaryview = mimeview->messageview->mainwin->summaryview;
1626 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1629 switch (event->keyval) {
1632 textview_scroll_max(mimeview->textview,
1633 (event->keyval == GDK_KEY_Home));
1635 case GDK_KEY_Page_Down:
1637 if (mimeview_scroll_page(mimeview,
1638 (event->state & GDK_SHIFT_MASK) != 0))
1641 if (!(event->state & GDK_SHIFT_MASK))
1642 mimeview_select_next_part(mimeview);
1644 case GDK_KEY_Page_Up:
1645 case GDK_KEY_BackSpace:
1646 mimeview_scroll_page(mimeview, TRUE);
1648 case GDK_KEY_Return:
1649 case GDK_KEY_KP_Enter:
1650 mimeview_scroll_one_line(mimeview,
1651 (event->state & GDK_MOD1_MASK) != 0);
1655 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1661 if (mimeview->messageview->new_window) return FALSE;
1663 return summary_pass_key_press_event(summaryview, event);
1666 static void mimeview_drag_data_get(GtkWidget *widget,
1667 GdkDragContext *drag_context,
1668 GtkSelectionData *selection_data,
1673 gchar *filename = NULL, *uriname, *tmp;
1678 if (!mimeview->opened) return;
1679 if (!mimeview->file) return;
1681 partinfo = mimeview_get_selected_part(mimeview);
1682 if (!partinfo) return;
1684 if (strlen(get_part_name(partinfo)) > 0) {
1685 filename = g_path_get_basename(get_part_name(partinfo));
1686 if (*filename == '\0') return;
1687 } else if (partinfo->type == MIMETYPE_MESSAGE
1688 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1690 GPtrArray *headers = NULL;
1693 fp = g_fopen(partinfo->data.filename, "rb");
1694 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1695 headers = procheader_get_header_array_asis(fp);
1698 for (i = 0; i < headers->len; i++) {
1699 Header *header = g_ptr_array_index(headers, i);
1700 if (procheader_headername_equal(header->name, "Subject")) {
1701 unfold_line(header->body);
1702 name = g_strconcat(header->body, ".txt", NULL);
1703 subst_for_filename(name);
1706 procheader_header_array_destroy(headers);
1712 filename = g_path_get_basename(name);
1715 if (filename == NULL)
1716 filename = g_path_get_basename("Unnamed part");
1718 if (!g_utf8_validate(filename, -1, NULL))
1719 tmp = conv_codeset_strdup(filename,
1720 conv_get_locale_charset_str(),
1723 tmp = g_strdup(filename);
1726 g_warning("filename not in UTF-8");
1727 tmp = g_strdup("Unnamed part");
1729 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1733 if (is_file_exist(filename)) {
1735 gchar *prefix = NULL;
1736 gchar *new_name = NULL;
1737 if (strrchr(tmp, '.')) {
1738 prefix = g_strdup(tmp);
1739 ext = g_strdup(strrchr(tmp, '.'));
1740 *(strrchr(prefix, '.')) = '\0';
1742 prefix = g_strdup(tmp);
1746 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1750 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1753 goto check_new_file;
1758 if ((err = procmime_get_part(filename, partinfo)) < 0)
1760 (_("Couldn't save the part of multipart message: %s"),
1763 tmp = g_filename_to_uri(filename, NULL, NULL);
1764 uriname = g_strconcat(tmp, "\r\n", NULL);
1767 gtk_selection_data_set(selection_data,
1768 gtk_selection_data_get_target(selection_data), 8,
1769 (guchar *)uriname, strlen(uriname));
1776 * Returns a filename (with path) for an attachment
1777 * \param partinfo The attachment to save
1778 * \param basedir The target directory
1779 * \param number Used for dummy filename if attachment is unnamed
1781 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1782 const gchar *basedir,
1788 filename = g_strdup(get_part_name(partinfo));
1789 if (!filename || !*filename) {
1791 filename = g_strdup_printf("noname.%d", number);
1794 if (!g_utf8_validate(filename, -1, NULL)) {
1795 gchar *tmp = conv_filename_to_utf8(filename);
1800 subst_for_filename(filename);
1802 fullname = g_strconcat
1803 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1804 ? &filename[1] : filename, NULL);
1807 filename = conv_filename_from_utf8(fullname);
1813 * Write a single attachment to file
1814 * \param filename Filename with path
1815 * \param partinfo Attachment to save
1817 static gboolean mimeview_write_part(const gchar *filename,
1819 gboolean handle_error)
1824 dir= g_path_get_dirname(filename);
1825 if (!is_dir_exist(dir))
1829 if (is_file_exist(filename)) {
1834 if (!g_utf8_validate(filename, -1, NULL))
1835 tmp = conv_filename_to_utf8(filename);
1837 tmp = g_strdup(filename);
1839 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1842 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
1843 GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST);
1845 if (G_ALERTALTERNATE != aval) return FALSE;
1848 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1849 debug_print("error saving MIME part: %d\n", err);
1852 (_("Couldn't save the part of multipart message: %s"),
1860 static AlertValue mimeview_save_all_error_ask(gint n)
1862 gchar *message = g_strdup_printf(
1863 _("An error has occurred while saving message part #%d. "
1864 "Do you want to cancel operation or skip error and "
1866 AlertValue av = alertpanel_full(_("Error saving all message parts"),
1867 message, GTK_STOCK_CANCEL, _("Skip"), _("Skip all"),
1868 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1873 static void mimeview_save_all_info(gint errors, gint total)
1876 gchar *msg = g_strdup_printf(
1877 ngettext("%d file saved successfully.",
1878 "%d files saved successfully.",
1881 alertpanel_notice("%s", msg);
1884 gchar *msg1 = g_strdup_printf(
1885 ngettext("%d file saved successfully",
1886 "%d files saved successfully",
1889 gchar *msg2 = g_strdup_printf(
1890 ngettext("%s, %d file failed.",
1891 "%s, %d files failed.",
1894 alertpanel_warning("%s", msg2);
1901 * Menu callback: Save all attached files
1902 * \param mimeview Current display
1904 static void mimeview_save_all(MimeView *mimeview)
1908 gchar *startdir = NULL;
1909 gint number = 1, errors = 0;
1910 gboolean skip_errors = FALSE;
1912 if (!mimeview->opened) return;
1913 if (!mimeview->file) return;
1914 if (!mimeview->mimeinfo) return;
1916 partinfo = mimeview->mimeinfo;
1917 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1918 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1920 startdir = g_strdup(get_home_dir());
1922 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1923 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1929 if (!is_dir_exist (dirname)) {
1930 alertpanel_error(_("'%s' is not a directory."), dirname);
1936 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1937 dirname[strlen(dirname)-1] = '\0';
1939 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1940 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1941 partinfo = procmime_mimeinfo_next(partinfo);
1942 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1943 partinfo = procmime_mimeinfo_next(partinfo);
1944 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1945 partinfo = procmime_mimeinfo_next(partinfo);
1948 while (partinfo != NULL) {
1949 if (partinfo->type != MIMETYPE_MESSAGE &&
1950 partinfo->type != MIMETYPE_MULTIPART &&
1951 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1952 || get_real_part_name(partinfo) != NULL)) {
1953 gchar *filename = mimeview_get_filename_for_part(
1954 partinfo, dirname, number++);
1956 gboolean ok = mimeview_write_part(filename, partinfo, FALSE);
1961 AlertValue av = mimeview_save_all_error_ask(number - 1);
1962 skip_errors = (av == G_ALERTOTHER);
1963 if (av == G_ALERTDEFAULT) /* cancel */
1968 partinfo = procmime_mimeinfo_next(partinfo);
1971 g_free(prefs_common.attach_save_dir);
1973 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1974 -1, NULL, NULL, NULL);
1977 mimeview_save_all_info(errors, number - 1);
1980 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1982 MimeInfo *partinfo = NULL;
1983 if (mimeview->spec_part) {
1984 partinfo = mimeview->spec_part;
1985 mimeview->spec_part = NULL;
1987 partinfo = (MimeInfo *) g_object_get_data
1988 (G_OBJECT(mimeview->popupmenu),
1990 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1991 "pop_partinfo", NULL);
1993 partinfo = mimeview_get_selected_part(mimeview);
2000 * Menu callback: Save the selected attachment
2001 * \param mimeview Current display
2003 void mimeview_save_as(MimeView *mimeview)
2006 gchar *filepath = NULL;
2007 gchar *filedir = NULL;
2009 gchar *partname = NULL;
2011 if (!mimeview->opened) return;
2012 if (!mimeview->file) return;
2014 partinfo = mimeview_get_part_to_use(mimeview);
2016 cm_return_if_fail(partinfo != NULL);
2018 if (get_part_name(partinfo) == NULL) {
2021 partname = g_strdup(get_part_name(partinfo));
2023 if (!g_utf8_validate(partname, -1, NULL)) {
2024 gchar *tmp = conv_filename_to_utf8(partname);
2026 tmp = conv_codeset_strdup(partname,
2027 conv_get_locale_charset_str(),
2036 subst_for_filename(partname);
2038 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2039 filepath = g_strconcat(prefs_common.attach_save_dir,
2040 G_DIR_SEPARATOR_S, partname, NULL);
2042 filepath = g_strdup(partname);
2046 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2047 filename = filesel_select_file_save(_("Save as"), filepath);
2053 mimeview_write_part(filename, partinfo, TRUE);
2055 filedir = g_path_get_dirname(filename);
2056 if (filedir && strcmp(filedir, ".")) {
2057 g_free(prefs_common.attach_save_dir);
2058 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2065 void mimeview_display_as_text(MimeView *mimeview)
2069 if (!mimeview->opened) return;
2071 partinfo = mimeview_get_part_to_use(mimeview);
2072 mimeview_select_mimepart_icon(mimeview, partinfo);
2073 cm_return_if_fail(partinfo != NULL);
2074 mimeview_show_message_part(mimeview, partinfo);
2077 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2082 if (!mimeview->opened) return;
2083 if (!mimeview->file) return;
2086 partinfo = mimeview_get_part_to_use(mimeview);
2088 cm_return_if_fail(partinfo != NULL);
2090 filename = procmime_get_tmp_file_name(partinfo);
2092 if ((err = procmime_get_part(filename, partinfo)) < 0)
2094 (_("Couldn't save the part of multipart message: %s"),
2097 mimeview_view_file(filename, partinfo, NULL, mimeview);
2103 void mimeview_open_with(MimeView *mimeview)
2107 if (!mimeview) return;
2108 if (!mimeview->opened) return;
2109 if (!mimeview->file) return;
2111 partinfo = mimeview_get_part_to_use(mimeview);
2113 mimeview_open_part_with(mimeview, partinfo, FALSE);
2116 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2120 gchar *mime_command = NULL;
2121 gchar *content_type = NULL;
2124 cm_return_if_fail(partinfo != NULL);
2126 filename = procmime_get_tmp_file_name(partinfo);
2128 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2130 (_("Couldn't save the part of multipart message: %s"),
2136 if (!prefs_common.mime_open_cmd_history)
2137 prefs_common.mime_open_cmd_history =
2138 add_history(NULL, prefs_common.mime_open_cmd);
2140 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2141 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2142 /* guess content-type from filename */
2143 content_type = procmime_get_mime_type(filename);
2145 if (content_type == NULL) {
2146 content_type = procmime_get_content_type_str(partinfo->type,
2150 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2151 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2152 mime_command = g_strdup(prefs_common_get_uri_cmd());
2153 g_free(content_type);
2154 content_type = NULL;
2155 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2156 || !prefs_common_get_ext_editor_cmd()[0]) {
2157 mime_command = mailcap_get_command_for_type(content_type, filename);
2159 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2160 g_free(content_type);
2161 content_type = NULL;
2163 if (mime_command == NULL) {
2164 /* try with extension this time */
2165 g_free(content_type);
2166 content_type = procmime_get_mime_type(filename);
2167 mime_command = mailcap_get_command_for_type(content_type, filename);
2170 if (mime_command == NULL)
2174 gboolean remember = FALSE;
2175 if (content_type != NULL)
2176 cmd = input_dialog_combo_remember
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, &remember);
2183 cmd = input_dialog_combo
2185 _("Enter the command-line to open file:\n"
2186 "('%s' will be replaced with file name)"),
2187 mime_command ? mime_command : prefs_common.mime_open_cmd,
2188 prefs_common.mime_open_cmd_history);
2189 if (cmd && remember) {
2190 mailcap_update_default(content_type, cmd);
2192 g_free(mime_command);
2197 mimeview_view_file(filename, partinfo, cmd, mimeview);
2198 g_free(prefs_common.mime_open_cmd);
2199 prefs_common.mime_open_cmd = cmd;
2200 prefs_common.mime_open_cmd_history =
2201 add_history(prefs_common.mime_open_cmd_history, cmd);
2204 g_free(content_type);
2209 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2211 GList *attach_file = NULL;
2212 AttachInfo *ainfo = NULL;
2216 if (!mimeview->opened) return;
2217 if (!mimeview->file) return;
2219 cm_return_if_fail(partinfo != NULL);
2221 filename = procmime_get_tmp_file_name(partinfo);
2223 if (!(err = procmime_get_part(filename, partinfo))) {
2224 ainfo = g_new0(AttachInfo, 1);
2225 ainfo->file = filename;
2226 ainfo->name = g_strdup(get_part_name(partinfo));
2227 ainfo->content_type = procmime_get_content_type_str(
2228 partinfo->type, partinfo->subtype);
2229 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2230 partinfo, "charset"));
2231 attach_file = g_list_append(attach_file, ainfo);
2233 compose_new(NULL, NULL, attach_file);
2235 g_free(ainfo->name);
2236 g_free(ainfo->content_type);
2237 g_free(ainfo->charset);
2239 g_list_free(attach_file);
2242 (_("Couldn't save the part of multipart message: %s"),
2247 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2248 const gchar *cmd, MimeView *mimeview)
2252 gchar buf[BUFFSIZE];
2255 mimeview_open_part_with(mimeview, partinfo, TRUE);
2257 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2258 !strchr(p + 2, '%')) {
2259 g_snprintf(buf, sizeof(buf), cmd, filename);
2260 if (!prefs_common.save_parts_readwrite)
2261 g_chmod(filename, S_IRUSR);
2263 g_chmod(filename, S_IRUSR|S_IWUSR);
2265 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2266 mimeview_open_part_with(mimeview, partinfo, FALSE);
2268 if (execute_command_line(buf, TRUE, NULL) != 0) {
2269 if (!prefs_common.save_parts_readwrite)
2270 g_chmod(filename, S_IRUSR|S_IWUSR);
2271 mimeview_open_part_with(mimeview, partinfo, FALSE);
2275 SHFILEINFO file_info;
2276 GError *error = NULL;
2277 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2279 if (error != NULL) {
2280 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2282 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2283 g_error_free(error);
2287 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2288 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2289 _("This attachment is an executable file. Executing "
2290 "untrusted binaries is dangerous and could probably "
2291 "lead to compromission of your computer.\n\n"
2292 "Do you want to run this file?"), GTK_STOCK_CANCEL,
2294 NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
2295 if (val == G_ALERTALTERNATE) {
2296 debug_print("executing binary\n");
2297 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2300 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2308 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2310 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2313 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2315 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2318 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2320 GSList *mimeview_list, *viewer_list;
2322 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2323 MimeView *mimeview = (MimeView *) mimeview_list->data;
2325 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2326 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2327 mimeview->mimeviewer = NULL;
2330 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2331 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2333 mimeviewer->destroy_viewer(mimeviewer);
2334 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2338 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2341 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2346 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2347 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2349 if (event->button == 1) {
2350 icon_selected(mimeview, num, partinfo);
2351 gtk_widget_grab_focus(button);
2352 icon_list_toggle_by_mime_info(mimeview, partinfo);
2354 part_button_pressed(mimeview, event, partinfo);
2359 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2361 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2364 MimeInfo *curr = NULL;
2366 gtk_tree_model_get_iter_first(model, &iter);
2367 path = gtk_tree_model_get_path(model, &iter);
2370 gtk_tree_model_get_iter(model, &iter, path);
2371 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2372 if (curr == partinfo) {
2373 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2374 GTK_TREE_VIEW(mimeview->ctree));
2375 gtk_tree_selection_select_iter(sel, &iter);
2376 gtk_tree_path_free(path);
2379 } while (mimeview_tree_next(model, path));
2381 gtk_tree_path_free(path);
2384 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2386 icon_list_toggle_by_mime_info(mimeview, partinfo);
2387 icon_selected(mimeview, -1, partinfo);
2390 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2393 SummaryView *summaryview;
2395 if (!event) return FALSE;
2397 switch (event->keyval) {
2399 if (mimeview_scroll_page(mimeview, FALSE))
2402 mimeview_select_next_part(mimeview);
2406 case GDK_KEY_BackSpace:
2407 mimeview_scroll_page(mimeview, TRUE);
2409 case GDK_KEY_Return:
2410 case GDK_KEY_KP_Enter:
2411 mimeview_scroll_one_line(mimeview,
2412 (event->state & GDK_MOD1_MASK) != 0);
2415 BREAK_ON_MODIFIER_KEY();
2416 mimeview_save_as(mimeview);
2419 BREAK_ON_MODIFIER_KEY();
2420 mimeview_display_as_text(mimeview);
2423 BREAK_ON_MODIFIER_KEY();
2424 mimeview_launch(mimeview, NULL);
2428 BREAK_ON_MODIFIER_KEY();
2429 mimeview_open_with(mimeview);
2433 BREAK_ON_MODIFIER_KEY();
2434 mimeview_check_signature(mimeview);
2437 BREAK_ON_MODIFIER_KEY();
2438 mimeview_select_next_part(mimeview);
2444 if (!mimeview->messageview->mainwin) return FALSE;
2445 summaryview = mimeview->messageview->mainwin->summaryview;
2446 return summary_pass_key_press_event(summaryview, event);
2449 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2451 MimeView *mimeview = (MimeView *)data;
2452 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2454 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2455 "pop_partinfo", partinfo);
2456 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
2457 NULL, NULL, NULL, NULL,
2458 0, gtk_get_current_event_time());
2462 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2464 GtkWidget *pixmap = NULL;
2469 const gchar *desc = NULL;
2470 gchar *sigshort = NULL;
2471 gchar *content_type;
2474 MimeInfo *siginfo = NULL;
2475 MimeInfo *encrypted = NULL;
2480 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2483 vbox = mimeview->icon_vbox;
2484 mimeview->icon_count++;
2485 button = gtk_event_box_new();
2487 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2488 G_CALLBACK(mimeview_visi_notify), mimeview);
2489 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2490 G_CALLBACK(mimeview_leave_notify), mimeview);
2491 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2492 G_CALLBACK(mimeview_enter_notify), mimeview);
2494 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2495 g_object_set_data(G_OBJECT(button), "icon_number",
2496 GINT_TO_POINTER(mimeview->icon_count));
2497 g_object_set_data(G_OBJECT(button), "partinfo",
2500 switch (mimeinfo->type) {
2503 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2504 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2505 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2506 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2507 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2508 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2509 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2510 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2511 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2513 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2515 case MIMETYPE_MESSAGE:
2516 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2518 case MIMETYPE_APPLICATION:
2519 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2520 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2521 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2522 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2523 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2524 stockp = STOCK_PIXMAP_MIME_PDF;
2525 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2526 stockp = STOCK_PIXMAP_MIME_PS;
2528 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2530 case MIMETYPE_IMAGE:
2531 stockp = STOCK_PIXMAP_MIME_IMAGE;
2533 case MIMETYPE_AUDIO:
2534 stockp = STOCK_PIXMAP_MIME_AUDIO;
2537 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2541 partinfo = mimeinfo;
2542 while (partinfo != NULL) {
2543 if (privacy_mimeinfo_is_signed(partinfo)) {
2547 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2548 encrypted = partinfo;
2551 partinfo = procmime_mimeinfo_parent(partinfo);
2554 if (siginfo != NULL) {
2555 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2556 case SIGNATURE_UNCHECKED:
2557 case SIGNATURE_CHECK_FAILED:
2558 case SIGNATURE_CHECK_TIMEOUT:
2559 pixmap = stock_pixmap_widget_with_overlay(stockp,
2560 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2563 pixmap = stock_pixmap_widget_with_overlay(stockp,
2564 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2566 case SIGNATURE_WARN:
2567 case SIGNATURE_KEY_EXPIRED:
2568 pixmap = stock_pixmap_widget_with_overlay(stockp,
2569 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2571 case SIGNATURE_INVALID:
2572 pixmap = stock_pixmap_widget_with_overlay(stockp,
2573 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2576 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2577 } else if (encrypted != NULL) {
2578 pixmap = stock_pixmap_widget_with_overlay(stockp,
2579 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2581 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2582 OVERLAY_NONE, 6, 3);
2584 gtk_container_add(GTK_CONTAINER(button), pixmap);
2586 if (prefs_common.attach_desc)
2587 desc = get_part_description(mimeinfo);
2589 desc = get_part_name(mimeinfo);
2592 content_type = procmime_get_content_type_str(mimeinfo->type,
2595 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2596 "\n<b>", _("Size:"), " </b>",
2597 to_human_readable((goffset)mimeinfo->length), NULL);
2598 g_free(content_type);
2599 if (desc && *desc) {
2600 gchar *tmp = NULL, *escaped = NULL;
2601 if (!g_utf8_validate(desc, -1, NULL)) {
2602 tmp = conv_filename_to_utf8(desc);
2604 tmp = g_strdup(desc);
2606 escaped = g_markup_escape_text(tmp,-1);
2608 tiptmp = g_strconcat(tip, "\n<b>",
2609 prefs_common.attach_desc && mimeinfo->description ?
2610 _("Description:") : _("Filename:"),
2611 " </b>", escaped, NULL);
2617 if (sigshort && *sigshort) {
2618 tiptmp = g_strjoin("\n", tip, g_markup_escape_text(sigshort, -1), NULL);
2624 gtk_widget_set_tooltip_markup(button, tip);
2626 gtk_widget_show_all(button);
2627 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2628 mimeview_mime_types, 1, GDK_ACTION_COPY);
2630 g_signal_connect(G_OBJECT(button), "popup-menu",
2631 G_CALLBACK(icon_popup_menu), mimeview);
2632 g_signal_connect(G_OBJECT(button), "button_release_event",
2633 G_CALLBACK(icon_clicked_cb), mimeview);
2634 g_signal_connect(G_OBJECT(button), "key_press_event",
2635 G_CALLBACK(icon_key_pressed), mimeview);
2636 g_signal_connect(G_OBJECT(button), "drag_data_get",
2637 G_CALLBACK(mimeview_drag_data_get), mimeview);
2638 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2640 gtk_widget_size_request(pixmap, &r);
2641 gtk_widget_set_size_request(button, -1, r.height + 4);
2646 static void icon_list_clear (MimeView *mimeview)
2648 GList *child, *orig;
2651 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2652 for (child = orig; child != NULL; child = g_list_next(child)) {
2653 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
2654 GTK_WIDGET(child->data));
2657 mimeview->icon_count = 0;
2658 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2659 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2660 gtk_adjustment_changed(adj);
2664 *\brief Used to 'click' the next or previous icon.
2666 *\return true if the icon 'number' exists and was selected.
2668 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2669 GtkAllocation *size, MimeView *mimeview)
2671 GtkAllocation vbox_size;
2672 GtkAllocation layout_size;
2677 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2679 gtk_widget_get_allocation(mimeview->icon_vbox, &vbox_size);
2680 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2682 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2683 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2685 MAX(vbox_size.height, layout_size.height));
2686 gtk_adjustment_set_step_increment(adj, 10);
2689 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2691 GtkRequisition size;
2692 GtkRequisition requisition;
2694 cm_return_if_fail(mimeinfo != NULL);
2696 while (mimeinfo != NULL) {
2697 if (mimeinfo->type != MIMETYPE_MULTIPART)
2698 icon_list_append_icon(mimeview, mimeinfo);
2699 if (mimeinfo->node->children != NULL)
2700 icon_list_create(mimeview,
2701 (MimeInfo *) mimeinfo->node->children->data);
2702 mimeinfo = mimeinfo->node->next != NULL
2703 ? (MimeInfo *) mimeinfo->node->next->data
2706 gtk_widget_size_request(mimeview->icon_vbox, &size);
2707 gtk_widget_get_requisition(mimeview->icon_mainbox, &requisition);
2708 if (size.width > requisition.width) {
2709 gtk_widget_set_size_request(mimeview->icon_mainbox,
2712 if (mimeview->opened)
2713 icon_list_toggle_by_mime_info(mimeview,
2714 mimeview_get_node_part(mimeview, mimeview->opened));
2717 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2720 GList *children, *child;
2722 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2723 for (child = children; child != NULL; child = g_list_next(child)) {
2724 gboolean *highlight = NULL;
2725 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2727 if (!GTK_IS_EVENT_BOX(child->data))
2730 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2731 *highlight = (g_object_get_data(G_OBJECT(child->data),
2732 "partinfo") == (gpointer)mimeinfo);
2734 gtk_widget_queue_draw(icon);
2736 g_list_free(children);
2739 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2742 prefs_common.mimeview_tree_height = allocation->height;
2745 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2748 g_object_ref(button);
2750 mimeview_leave_notify(button, NULL, NULL);
2752 mimeview->ctree_mode = !mimeview->ctree_mode;
2753 if (mimeview->ctree_mode) {
2754 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2756 gtk_widget_hide(mimeview->icon_mainbox);
2757 gtk_widget_show(mimeview->ctree_mainbox);
2758 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2759 prefs_common.mimeview_tree_height);
2761 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2763 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2764 button, FALSE, FALSE, 0);
2766 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2768 gtk_widget_hide(mimeview->ctree_mainbox);
2769 gtk_widget_show(mimeview->icon_mainbox);
2770 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2772 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2774 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2775 button, FALSE, FALSE, 0);
2776 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2777 if (mimeview->opened)
2778 icon_list_toggle_by_mime_info(mimeview,
2779 mimeview_get_node_part(mimeview, mimeview->opened));
2780 summary_grab_focus(mimeview->mainwin->summaryview);
2782 g_object_unref(button);
2786 void mimeview_update (MimeView *mimeview)
2788 if (mimeview && mimeview->mimeinfo) {
2789 icon_list_clear(mimeview);
2790 icon_list_create(mimeview, mimeview->mimeinfo);
2794 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2796 MessageView *msgview = NULL;
2797 MainWindow *mainwin = NULL;
2802 msgview = mimeview->messageview;
2806 mainwin = msgview->mainwin;
2810 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2811 "pop_partinfo", NULL);
2813 if (!strcmp(cmd, "sc://view_log"))
2814 log_window_show(mainwin->logwin);
2815 else if (!strcmp(cmd, "sc://save_as"))
2816 mimeview_save_as(mimeview);
2817 else if (!strcmp(cmd, "sc://display_as_text"))
2818 mimeview_display_as_text(mimeview);
2820 else if (!strcmp(cmd, "sc://open_with"))
2821 mimeview_open_with(mimeview);
2823 else if (!strcmp(cmd, "sc://open"))
2824 mimeview_launch(mimeview, NULL);
2825 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2826 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2827 icon_selected(mimeview, -1, (MimeInfo *)data);
2828 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2829 mimeview_launch(mimeview, (MimeInfo *)data);
2830 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2831 mimeview->spec_part = (MimeInfo *)data;
2832 part_button_pressed(mimeview, event, (MimeInfo *)data);
2833 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2834 const gchar *tagname = cmd + strlen("sc://search_tags:");
2835 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2836 gtk_toggle_button_set_active(
2837 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2839 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2840 ADVANCED_SEARCH_EXTENDED, buf);
2845 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2847 if (mimeview->type == MIMEVIEW_TEXT)
2848 return textview_scroll_page(mimeview->textview, up);
2849 else if (mimeview->mimeviewer) {
2850 MimeViewer *mimeviewer = mimeview->mimeviewer;
2851 if (mimeviewer->scroll_page)
2852 return mimeviewer->scroll_page(mimeviewer, up);
2857 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2859 if (mimeview->type == MIMEVIEW_TEXT)
2860 textview_scroll_one_line(mimeview->textview, up);
2861 else if (mimeview->mimeviewer) {
2862 MimeViewer *mimeviewer = mimeview->mimeviewer;
2863 if (mimeviewer->scroll_one_line)
2864 mimeviewer->scroll_one_line(mimeviewer, up);