2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
32 #include <sys/types.h>
35 #ifndef HAVE_APACHE_FNMATCH
36 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
45 #include "summaryview.h"
48 #include "alertpanel.h"
49 #include "inputdialog.h"
52 #include "prefs_common.h"
53 #include "procheader.h"
54 #include "stock_pixmap.h"
55 #include "gtk/gtkvscrollbutton.h"
56 #include "gtk/logwindow.h"
58 #include "manage_window.h"
69 #define N_MIMEVIEW_COLS 4
71 static void mimeview_set_multipart_tree (MimeView *mimeview,
74 static void mimeview_show_message_part (MimeView *mimeview,
76 static void mimeview_change_view_type (MimeView *mimeview,
78 static gchar *mimeview_get_filename_for_part (MimeInfo *partinfo,
81 static gboolean mimeview_write_part (const gchar *filename,
84 static void mimeview_selected (GtkTreeSelection *selection,
86 static gint mimeview_button_pressed (GtkWidget *widget,
87 GdkEventButton *event,
89 static gint mimeview_key_pressed (GtkWidget *widget,
93 static void mimeview_drag_data_get (GtkWidget *widget,
94 GdkDragContext *drag_context,
95 GtkSelectionData *selection_data,
100 static gboolean mimeview_scrolled (GtkWidget *widget,
101 GdkEventScroll *event,
104 static void mimeview_save_all (MimeView *mimeview);
106 static void mimeview_open_part_with (MimeView *mimeview,
110 static void mimeview_send_to (MimeView *mimeview,
112 static void mimeview_view_file (const gchar *filename,
116 static gboolean icon_clicked_cb (GtkWidget *button,
117 GdkEventButton *event,
119 static void icon_selected (MimeView *mimeview,
122 static gint icon_key_pressed (GtkWidget *button,
125 static void icon_list_append_icon (MimeView *mimeview,
127 static void icon_list_create (MimeView *mimeview,
129 static void icon_list_clear (MimeView *mimeview);
130 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
132 static void ctree_size_allocate_cb (GtkWidget *widget,
133 GtkAllocation *allocation,
135 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
137 static gboolean part_button_pressed (MimeView *mimeview,
138 GdkEventButton *event,
140 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
141 GtkAllocation *layout_size,
143 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview);
144 static const gchar *get_part_name(MimeInfo *partinfo);
145 static const gchar *get_part_description(MimeInfo *partinfo);
147 static void mimeview_launch_cb(GtkAction *action, gpointer data)
149 MimeView *mimeview = (MimeView *)data;
150 mimeview_launch(mimeview, mimeview_get_part_to_use(mimeview));
154 static void mimeview_open_with_cb(GtkAction *action, gpointer data)
156 mimeview_open_with((MimeView *)data);
160 static void mimeview_send_to_cb(GtkAction *action, gpointer data)
162 MimeView *mimeview = (MimeView *)data;
163 mimeview_send_to(mimeview, mimeview_get_part_to_use(mimeview));
166 static void mimeview_display_as_text_cb(GtkAction *action, gpointer data)
168 mimeview_display_as_text((MimeView *)data);
171 static void mimeview_save_as_cb(GtkAction *action, gpointer data)
173 mimeview_save_as((MimeView *)data);
176 static void mimeview_save_all_cb(GtkAction *action, gpointer data)
178 mimeview_save_all((MimeView *)data);
181 static void mimeview_select_next_part_cb(GtkAction *action, gpointer data)
183 mimeview_select_next_part((MimeView *)data);
186 static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
188 mimeview_select_prev_part((MimeView *)data);
191 static GtkActionEntry mimeview_menu_actions[] = {
192 { "MimeView", NULL, "MimeView" },
193 { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
194 #if (!defined G_OS_WIN32)
195 { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
197 { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
198 { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
199 { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
200 { "MimeView/SaveAll", NULL, N_("Save _all..."), NULL, "Save all parts", G_CALLBACK(mimeview_save_all_cb) },
201 { "MimeView/NextPart", NULL, N_("Next part"), NULL, "Next part", G_CALLBACK(mimeview_select_next_part_cb) },
202 { "MimeView/PrevPart", NULL, N_("Previous part"), NULL, "Previous part", G_CALLBACK(mimeview_select_prev_part_cb) }
205 static GtkTargetEntry mimeview_mime_types[] =
207 {"text/uri-list", 0, 0}
210 GSList *mimeviewer_factories;
213 static GdkCursor *hand_cursor = NULL;
215 static gboolean mimeview_visi_notify(GtkWidget *widget,
216 GdkEventVisibility *event,
219 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
223 static gboolean mimeview_leave_notify(GtkWidget *widget,
224 GdkEventCrossing *event,
227 gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
231 static gboolean mimeview_enter_notify(GtkWidget *widget,
232 GdkEventCrossing *event,
235 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
239 MimeView *mimeview_create(MainWindow *mainwin)
244 GtkWidget *scrolledwin;
246 GtkWidget *mime_notebook;
247 GtkWidget *popupmenu;
248 GtkWidget *ctree_mainbox;
250 GtkWidget *mime_toggle;
251 GtkWidget *icon_mainbox;
252 GtkWidget *icon_scroll;
253 GtkWidget *icon_vbox;
255 GtkWidget *scrollbutton;
257 NoticeView *siginfoview;
260 GtkCellRenderer *renderer;
261 GtkTreeViewColumn *column;
262 GtkTreeSelection *selection;
264 gchar *titles[N_MIMEVIEW_COLS];
268 hand_cursor = gdk_cursor_new(GDK_HAND2);
270 debug_print("Creating MIME view...\n");
271 mimeview = g_new0(MimeView, 1);
273 titles[COL_MIMETYPE] = _("MIME Type");
274 titles[COL_SIZE] = _("Size");
275 titles[COL_NAME] = _("Name");
277 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
278 gtk_widget_show(scrolledwin);
279 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
280 GTK_POLICY_AUTOMATIC,
281 GTK_POLICY_AUTOMATIC);
283 model = gtk_tree_store_new(N_MIMEVIEW_COLS,
289 ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
290 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 #if(GTK_CHECK_VERSION(2,18,0))
304 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
306 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
307 -1, titles[COL_SIZE], renderer,
308 "text", COL_SIZE, NULL);
309 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
310 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
312 renderer = gtk_cell_renderer_text_new();
313 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
314 -1, titles[COL_NAME], renderer,
315 "text", COL_NAME, NULL);
316 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
317 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
319 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
320 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
322 gtk_widget_show(ctree);
323 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
324 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
325 mimeview_mime_types, 1, GDK_ACTION_COPY);
327 g_signal_connect(G_OBJECT(selection), "changed",
328 G_CALLBACK(mimeview_selected), mimeview);
329 g_signal_connect(G_OBJECT(ctree), "button_release_event",
330 G_CALLBACK(mimeview_button_pressed), mimeview);
331 g_signal_connect(G_OBJECT(ctree), "key_press_event",
332 G_CALLBACK(mimeview_key_pressed), mimeview);
333 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
334 G_CALLBACK(mimeview_drag_data_get), mimeview);
336 mime_notebook = gtk_notebook_new();
337 gtk_widget_show(mime_notebook);
338 gtkut_widget_set_can_focus(mime_notebook, FALSE);
339 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
340 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
342 icon_vbox = gtk_vbox_new(FALSE, 2);
343 gtk_widget_show(icon_vbox);
344 icon_scroll = gtk_layout_new(NULL, NULL);
345 gtk_widget_show(icon_scroll);
346 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
347 scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
348 gtk_widget_show(scrollbutton);
350 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
351 G_CALLBACK(mimeview_scrolled), mimeview);
353 mime_toggle = gtk_event_box_new();
354 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
356 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
357 G_CALLBACK(mimeview_visi_notify), mimeview);
358 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
359 G_CALLBACK(mimeview_leave_notify), mimeview);
360 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
361 G_CALLBACK(mimeview_enter_notify), mimeview);
363 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
364 gtk_widget_show(mime_toggle);
365 mimeview->ctree_mode = FALSE;
366 arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
367 gtk_widget_show(arrow);
368 gtk_widget_size_request(arrow, &r);
369 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
370 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
371 G_CALLBACK(mime_toggle_button_cb), mimeview);
373 icon_mainbox = gtk_vbox_new(FALSE, 0);
374 gtk_widget_show(icon_mainbox);
375 gtk_widget_set_size_request(icon_mainbox, 32, -1);
376 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
377 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
378 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
379 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
380 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
382 ctree_mainbox = gtk_hbox_new(FALSE, 0);
383 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
384 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
385 G_CALLBACK(ctree_size_allocate_cb), mimeview);
387 mimeview->ui_manager = gtk_ui_manager_new();
388 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
389 "MimeView", mimeview_menu_actions,
390 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
392 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
393 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
394 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
395 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
396 "/Menus/MimeView/", "Open", "MimeView/Open",
397 GTK_UI_MANAGER_MENUITEM);
398 #if (!defined G_OS_WIN32)
399 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
400 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
401 GTK_UI_MANAGER_MENUITEM);
403 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
404 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
405 GTK_UI_MANAGER_MENUITEM);
406 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
407 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
408 GTK_UI_MANAGER_MENUITEM);
409 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
410 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
411 GTK_UI_MANAGER_MENUITEM);
412 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
413 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
414 GTK_UI_MANAGER_MENUITEM);
415 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
416 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
417 GTK_UI_MANAGER_MENUITEM);
418 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
419 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
420 GTK_UI_MANAGER_MENUITEM);
422 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
423 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
426 vbox = gtk_vbox_new(FALSE, 0);
427 gtk_widget_show(vbox);
428 siginfoview = noticeview_create(mainwin);
429 noticeview_hide(siginfoview);
430 noticeview_set_icon_clickable(siginfoview, TRUE);
431 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
432 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
434 paned = gtk_vpaned_new();
435 gtk_widget_show(paned);
436 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
437 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
439 hbox = gtk_hbox_new(FALSE, 0);
440 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
441 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
443 gtk_widget_show(hbox);
444 gtk_widget_hide(ctree_mainbox);
446 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
448 mimeview->hbox = hbox;
449 mimeview->paned = paned;
450 mimeview->scrolledwin = scrolledwin;
451 mimeview->ctree = ctree;
452 mimeview->mime_notebook = mime_notebook;
453 mimeview->popupmenu = popupmenu;
455 mimeview->ctree_mainbox = ctree_mainbox;
456 mimeview->icon_scroll = icon_scroll;
457 mimeview->icon_vbox = icon_vbox;
458 mimeview->icon_mainbox = icon_mainbox;
459 mimeview->icon_count = 0;
460 mimeview->mainwin = mainwin;
461 #if !(GTK_CHECK_VERSION(2,12,0))
462 mimeview->tooltips = tips;
464 mimeview->mime_toggle = mime_toggle;
465 mimeview->siginfoview = siginfoview;
466 mimeview->scrollbutton = scrollbutton;
467 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
469 mimeviews = g_slist_prepend(mimeviews, mimeview);
474 void mimeview_init(MimeView *mimeview)
476 textview_init(mimeview->textview);
478 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
479 GTK_WIDGET_PTR(mimeview->textview));
482 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
485 if (privacy_mimeinfo_is_signed(mimeinfo))
487 mimeinfo = procmime_mimeinfo_next(mimeinfo);
493 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
496 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
498 mimeview_clear(mimeview);
500 cm_return_if_fail(file != NULL);
501 cm_return_if_fail(mimeinfo != NULL);
503 mimeview->mimeinfo = mimeinfo;
505 mimeview->file = g_strdup(file);
507 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
510 /* check if the mail's signed - it can change the mail structure */
512 if (any_part_is_signed(mimeinfo))
513 debug_print("signed mail\n");
515 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
516 gtk_tree_view_expand_all(ctree);
517 icon_list_clear(mimeview);
518 icon_list_create(mimeview, mimeinfo);
520 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
521 mimeview_selected, mimeview);
525 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
528 static void mimeview_free_mimeinfo(MimeView *mimeview)
530 gboolean defer = FALSE;
532 defer = (mimeview->check_data != NULL);
534 mimeview->check_data->free_after_use = TRUE;
536 if (mimeview->mimeinfo != NULL && !defer) {
537 procmime_mimeinfo_free_all(mimeview->mimeinfo);
538 mimeview->mimeinfo = NULL;
541 debug_print("deferring free(mimeinfo) and cancelling check\n");
542 mimeview_check_sig_cancel_now(mimeview);
547 void mimeview_destroy(MimeView *mimeview)
551 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
552 MimeViewer *viewer = (MimeViewer *) cur->data;
553 viewer->destroy_viewer(viewer);
555 g_slist_free(mimeview->viewers);
556 gtk_target_list_unref(mimeview->target_list);
559 if (mimeview->check_data) {
560 mimeview->check_data->destroy_mimeview = TRUE;
561 debug_print("deferring destroy\n");
565 mimeview_free_mimeinfo(mimeview);
566 gtk_tree_path_free(mimeview->opened);
567 g_free(mimeview->file);
569 mimeviews = g_slist_remove(mimeviews, mimeview);
574 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
576 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
578 GtkTreeSelection *selection;
579 MimeInfo *partinfo = NULL;
581 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
582 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
583 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
588 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
590 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
594 gtk_tree_model_get_iter(model, &iter, path);
595 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
599 gboolean mimeview_tree_is_empty(MimeView *mimeview)
601 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
603 return !gtk_tree_model_get_iter_first(model, &iter);
606 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
608 GtkTreeIter iter, parent;
611 gtk_tree_model_get_iter(model, &iter, path);
613 if (gtk_tree_model_iter_has_child(model, &iter)) {
614 gtk_tree_path_down(path);
618 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
620 if (!gtk_tree_model_iter_next(model, &iter)) {
622 GtkTreeIter saved_parent = parent;
623 gtk_tree_path_up(path);
624 if (gtk_tree_model_iter_next(model, &parent)) {
625 gtk_tree_path_next(path);
628 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
633 gtk_tree_path_next(path);
640 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
642 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
643 GtkTreeIter iter, child, parent;
646 gtk_tree_model_get_iter(model, &iter, path);
647 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
649 if (gtk_tree_path_prev(path)) {
650 gtk_tree_model_get_iter(model, &iter, path);
652 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
653 gtk_tree_path_down(path);
655 while (gtk_tree_model_iter_next(model, &child))
656 gtk_tree_path_next(path);
663 gtk_tree_path_up(path);
670 gint mimeview_get_selected_part_num(MimeView *mimeview)
672 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
677 gtk_tree_model_get_iter_first(model, &iter);
678 path = gtk_tree_model_get_path(model, &iter);
681 if (!gtk_tree_path_compare(mimeview->opened, path)) {
682 gtk_tree_path_free(path);
687 } while (mimeview_tree_next(model, path));
689 gtk_tree_path_free(path);
694 void mimeview_select_part_num(MimeView *mimeview, gint i)
696 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
699 GtkTreeSelection *selection;
705 gtk_tree_model_get_iter_first(model, &iter);
706 path = gtk_tree_model_get_path(model, &iter);
709 if (!mimeview_tree_next(model, path)) {
710 gtk_tree_path_free(path);
716 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
717 gtk_tree_selection_select_path(selection, path);
718 gtk_tree_path_free(path);
721 static void mimeview_set_multipart_tree(MimeView *mimeview,
725 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
726 GTK_TREE_VIEW(mimeview->ctree)));
728 gchar *content_type, *length, *name;
730 cm_return_if_fail(mimeinfo != NULL);
732 while (mimeinfo != NULL) {
733 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
734 content_type = g_strdup_printf("%s/%s",
735 procmime_get_media_type_str(mimeinfo->type),
738 content_type = g_strdup("UNKNOWN");
740 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
742 if (prefs_common.attach_desc)
743 name = g_strdup(get_part_description(mimeinfo));
745 name = g_strdup(get_part_name(mimeinfo));
747 gtk_tree_store_append(model, &iter, parent);
748 gtk_tree_store_set(model, &iter,
749 COL_MIMETYPE, content_type,
752 COL_DATA, mimeinfo, -1);
753 g_free(content_type);
757 if (mimeinfo->node->children)
758 mimeview_set_multipart_tree(mimeview,
759 (MimeInfo *) mimeinfo->node->children->data, &iter);
760 mimeinfo = mimeinfo->node->next != NULL ?
761 (MimeInfo *) mimeinfo->node->next->data : NULL;
765 static const gchar *get_real_part_name(MimeInfo *partinfo)
767 const gchar *name = NULL;
769 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
771 name = procmime_mimeinfo_get_parameter(partinfo, "name");
776 static const gchar *get_part_name(MimeInfo *partinfo)
780 name = get_real_part_name(partinfo);
787 static const gchar *get_part_description(MimeInfo *partinfo)
789 if (partinfo->description)
790 return partinfo->description;
792 return get_part_name(partinfo);
795 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
800 if (!partinfo) return;
802 fname = mimeview->file;
805 if ((fp = g_fopen(fname, "rb")) == NULL) {
806 FILE_OP_ERROR(fname, "fopen");
810 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
811 FILE_OP_ERROR(mimeview->file, "fseek");
816 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
817 textview_show_part(mimeview->textview, partinfo, fp);
822 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
825 MimeViewerFactory *factory = NULL;
826 MimeViewer *viewer = NULL;
827 gchar *real_contenttype = NULL;
830 * FNM_CASEFOLD is a GNU extension
833 #define FNM_CASEFOLD 0
834 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
836 real_contenttype = g_strdup(content_type);
839 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
840 MimeViewerFactory *curfactory = cur->data;
843 while (curfactory->content_types[i] != NULL) {
844 if(!fnmatch(curfactory->content_types[i], real_contenttype, FNM_CASEFOLD)) {
845 debug_print("%s\n", curfactory->content_types[i]);
846 factory = curfactory;
854 g_free(real_contenttype);
858 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
859 MimeViewer *curviewer = cur->data;
861 if (curviewer->factory == factory)
864 viewer = factory->create_viewer();
865 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
866 GTK_WIDGET(viewer->get_widget(viewer)));
868 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
873 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
875 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
878 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
880 gchar *content_type = NULL;
881 MimeViewer *viewer = NULL;
883 if ((partinfo->type == MIMETYPE_APPLICATION) &&
884 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
885 const gchar *filename;
887 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
888 if (filename == NULL)
889 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
890 if (filename != NULL)
891 content_type = procmime_get_mime_type(filename);
893 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
896 if (content_type != NULL) {
897 viewer = get_viewer_for_content_type(mimeview, content_type);
898 g_free(content_type);
904 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
908 if (mimeview->messageview->partial_display_shown) {
909 noticeview_hide(mimeview->messageview->noticeview);
910 mimeview->messageview->partial_display_shown = FALSE;
913 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
914 if (viewer == NULL) {
915 if (mimeview->mimeviewer != NULL)
916 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
917 mimeview->mimeviewer = NULL;
921 if (mimeview->mimeviewer != NULL)
922 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
924 if (mimeview->mimeviewer != viewer)
925 mimeview->mimeviewer = viewer;
927 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
928 viewer->mimeview = mimeview;
929 viewer->show_mimepart(viewer, mimeview->file, partinfo);
934 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
936 TextView *textview = mimeview->textview;
937 GtkWidget *focused = NULL;
939 if (mainwindow_get_mainwindow())
940 focused = gtkut_get_focused_child(
941 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
943 if ((mimeview->type != MIMEVIEW_VIEWER) &&
944 (mimeview->type == type)) return;
948 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
949 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
950 GTK_WIDGET_PTR(textview)));
952 case MIMEVIEW_VIEWER:
953 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
954 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
955 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
961 gtk_widget_grab_focus(focused);
962 mimeview->type = type;
965 void mimeview_clear(MimeView *mimeview)
972 if (g_slist_find(mimeviews, mimeview) == NULL)
975 noticeview_hide(mimeview->siginfoview);
977 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
978 gtk_tree_store_clear(GTK_TREE_STORE(model));
980 textview_clear(mimeview->textview);
981 if (mimeview->mimeviewer != NULL)
982 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
984 mimeview_free_mimeinfo(mimeview);
986 mimeview->mimeinfo = NULL;
988 gtk_tree_path_free(mimeview->opened);
989 mimeview->opened = NULL;
991 g_free(mimeview->file);
992 mimeview->file = NULL;
994 icon_list_clear(mimeview);
995 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
998 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
999 GtkUIManager *ui_manager;
1001 if (messageview->window != NULL)
1002 ui_manager = messageview->ui_manager;
1004 ui_manager = messageview->mainwin->ui_manager;
1006 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
1009 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1010 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1012 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
1013 gboolean special, SignatureStatus code)
1015 gchar *text = NULL, *button_text = NULL;
1017 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1018 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1020 cm_return_if_fail(mimeview != NULL);
1021 cm_return_if_fail(mimeinfo != NULL);
1026 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1029 case SIGNATURE_UNCHECKED:
1030 button_text = _("Check signature");
1031 func = check_signature_cb;
1032 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1035 button_text = _("View full information");
1036 func = display_full_info_cb;
1037 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1039 case SIGNATURE_WARN:
1040 button_text = _("View full information");
1041 func = display_full_info_cb;
1042 icon = STOCK_PIXMAP_PRIVACY_WARN;
1044 case SIGNATURE_KEY_EXPIRED:
1045 button_text = _("View full information");
1046 func = display_full_info_cb;
1047 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1049 case SIGNATURE_INVALID:
1050 button_text = _("View full information");
1051 func = display_full_info_cb;
1052 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1054 case SIGNATURE_CHECK_FAILED:
1055 case SIGNATURE_CHECK_TIMEOUT:
1056 button_text = _("Check again");
1057 func = check_signature_cb;
1058 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1063 if (mycode == SIGNATURE_UNCHECKED) {
1064 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1065 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1067 if (*shortcut == '\0')
1068 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1070 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1074 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1075 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1076 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1077 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1079 if (*shortcut == '\0')
1080 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1082 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1086 noticeview_set_text(mimeview->siginfoview, text);
1087 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1090 noticeview_set_button_text(mimeview->siginfoview, NULL);
1091 noticeview_set_button_press_callback(
1092 mimeview->siginfoview,
1094 (gpointer) mimeview);
1095 noticeview_set_icon(mimeview->siginfoview, icon);
1096 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1098 icon_list_clear(mimeview);
1099 icon_list_create(mimeview, mimeview->mimeinfo);
1104 /* reset all thread stuff, and do the cleanups we've been left to do */
1105 static void mimeview_check_data_reset(MimeView *mimeview)
1108 gboolean must_destroy;
1110 if (!mimeview->check_data)
1113 must_free = mimeview->check_data->free_after_use;
1114 must_destroy = mimeview->check_data->destroy_mimeview;
1116 if (mimeview->check_data->cancel_th_init) {
1117 debug_print("killing canceller thread\n");
1118 mimeview->check_data->cancel_th_init = FALSE;
1119 pthread_cancel(mimeview->check_data->cancel_th);
1123 debug_print("freeing deferred mimeinfo\n");
1124 procmime_mimeinfo_free_all(mimeview->check_data->siginfo);
1127 g_free(mimeview->check_data);
1128 mimeview->check_data = NULL;
1131 debug_print("freeing deferred mimeview\n");
1132 mimeview_free_mimeinfo(mimeview);
1133 gtk_tree_path_free(mimeview->opened);
1134 g_free(mimeview->file);
1136 mimeviews = g_slist_remove(mimeviews, mimeview);
1140 /* GUI update once the checker thread is done or killed */
1141 static gboolean mimeview_check_sig_thread_cb(void *data)
1143 MimeView *mimeview = (MimeView *) data;
1144 MimeInfo *mimeinfo = mimeview->siginfo;
1146 debug_print("mimeview_check_sig_thread_cb\n");
1148 if (mimeinfo == NULL) {
1149 /* message changed !? */
1150 g_warning("no more siginfo!\n");
1154 if (!mimeview->check_data) {
1155 g_warning("nothing to check\n");
1159 if (mimeview->check_data->siginfo != mimeinfo) {
1160 /* message changed !? */
1161 g_warning("different siginfo!\n");
1165 if (mimeview->check_data->destroy_mimeview ||
1166 mimeview->check_data->free_after_use) {
1167 debug_print("not bothering, we're changing message\n");
1172 if (mimeview->check_data->timeout)
1173 update_signature_noticeview(mimeview, mimeview->siginfo,
1174 TRUE, SIGNATURE_CHECK_TIMEOUT);
1176 update_signature_noticeview(mimeview, mimeview->siginfo,
1180 mimeview_check_data_reset(mimeview);
1184 /* sig checker thread */
1185 static void *mimeview_check_sig_worker_thread(void *data)
1187 MimeView *mimeview = (MimeView *)data;
1188 MimeInfo *mimeinfo = mimeview->siginfo;
1190 debug_print("checking...\n");
1192 if (!mimeview->check_data)
1195 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1196 privacy_mimeinfo_check_signature(mimeinfo);
1197 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1198 mimeview->check_data->cancel_th_init = FALSE;
1199 pthread_cancel(mimeview->check_data->cancel_th);
1202 /* that's strange! we changed message without
1203 * getting killed. */
1204 g_warning("different siginfo!\n");
1205 mimeview_check_data_reset(mimeview);
1209 /* use g_timeout so that GUI updates is done from the
1211 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1216 /* killer thread - acts when the checker didn't work fast
1218 static void *mimeview_check_sig_cancel_thread(void *data)
1220 MimeView *mimeview = (MimeView *)data;
1222 if (!mimeview->check_data)
1223 return NULL; /* nothing to kill ! */
1225 /* wait for a few seconds... */
1226 debug_print("waiting a while\n");
1228 g_usleep(5 * 1000 * 1000);
1230 if (!mimeview->check_data)
1231 return NULL; /* nothing to kill, it's done in time :) */
1233 /* too late, go away checker thread */
1234 debug_print("killing checker thread\n");
1235 if (mimeview->check_data->th_init) {
1236 mimeview->check_data->th_init = FALSE;
1237 pthread_cancel(mimeview->check_data->th);
1240 /* tell upstream it was a timeout */
1241 mimeview->check_data->timeout = TRUE;
1242 /* use g_timeout so that GUI updates is done from the
1244 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1249 /* get rid of the checker thread right now - used when changing the
1250 * displayed message for example. */
1251 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1253 if (!mimeview->check_data)
1255 debug_print("killing checker thread NOW\n");
1256 if (mimeview->check_data->th_init) {
1257 mimeview->check_data->th_init = FALSE;
1258 pthread_cancel(mimeview->check_data->th);
1261 /* tell upstream it was a timeout */
1262 mimeview->check_data->timeout = TRUE;
1263 mimeview_check_sig_thread_cb(mimeview);
1267 /* creates a thread to check the signature, and a second one
1268 * to kill the first one after a timeout */
1269 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1272 pthread_attr_t detach, detach2;
1274 if (mimeview->check_data) {
1275 g_warning("already checking it");
1279 mimeview->check_data = g_new0(SigCheckData, 1);
1280 mimeview->check_data->siginfo = mimeview->siginfo;
1281 debug_print("creating thread\n");
1283 /* init thread attributes and create the checker thread */
1284 if (pthread_attr_init(&detach) != 0 ||
1285 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1286 pthread_attr_init(&detach2) != 0 ||
1287 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1288 pthread_create(&th, &detach,
1289 mimeview_check_sig_worker_thread,
1291 /* arh. We'll do it synchronously. */
1292 g_warning("can't create thread");
1293 g_free(mimeview->check_data);
1294 mimeview->check_data = NULL;
1297 mimeview->check_data->th = th;
1298 mimeview->check_data->th_init = TRUE;
1301 /* create the killer thread */
1302 pthread_create(&th2, &detach2,
1303 mimeview_check_sig_cancel_thread,
1306 mimeview->check_data->cancel_th = th2;
1307 mimeview->check_data->cancel_th_init = TRUE;
1311 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1313 MimeView *mimeview = (MimeView *) user_data;
1314 MimeInfo *mimeinfo = mimeview->siginfo;
1316 if (mimeinfo == NULL)
1319 if (mimeview->check_data)
1322 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1325 /* let's do it non-blocking */
1326 mimeview_check_sig_in_thread(mimeview);
1327 if (!mimeview->check_data) /* let's check syncronously */
1330 debug_print("checking without thread\n");
1331 privacy_mimeinfo_check_signature(mimeinfo);
1332 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1336 void mimeview_check_signature(MimeView *mimeview)
1338 check_signature_cb(NULL, mimeview);
1341 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1343 MimeView *mimeview = (MimeView *) user_data;
1344 gtk_tree_path_free(mimeview->opened);
1345 mimeview->opened = NULL;
1346 mimeview_selected(gtk_tree_view_get_selection(
1347 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1350 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1352 MimeView *mimeview = (MimeView *) user_data;
1355 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1356 textview_set_text(mimeview->textview, siginfo);
1358 noticeview_set_button_text(mimeview->siginfoview, NULL);
1359 noticeview_set_button_press_callback(
1360 mimeview->siginfoview,
1361 G_CALLBACK(redisplay_email),
1362 (gpointer) mimeview);
1363 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1366 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1369 MimeInfo *first_text;
1371 cm_return_if_fail(mimeview != NULL);
1372 cm_return_if_fail(selected != NULL);
1374 if (selected->type == MIMETYPE_MESSAGE
1375 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1376 /* if the first text part is signed, check that */
1377 first_text = selected;
1378 while (first_text && first_text->type != MIMETYPE_TEXT) {
1379 first_text = procmime_mimeinfo_next(first_text);
1382 update_signature_info(mimeview, first_text);
1388 while (siginfo != NULL) {
1389 if (privacy_mimeinfo_is_signed(siginfo))
1391 siginfo = procmime_mimeinfo_parent(siginfo);
1393 mimeview->siginfo = siginfo;
1395 /* This shortcut boolean is there to correctly set the menu's
1396 * CheckSignature item sensitivity without killing performance
1397 * each time the menu sensitiveness is updated (a lot).
1399 mimeview->signed_part = (siginfo != NULL);
1401 if (siginfo == NULL) {
1402 noticeview_hide(mimeview->siginfoview);
1406 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1407 noticeview_show(mimeview->siginfoview);
1410 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1412 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1413 GtkTreeModel *model;
1418 selection = gtk_tree_view_get_selection(ctree);
1419 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1422 path = gtk_tree_model_get_path(model, &iter);
1424 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1425 gtk_tree_path_free(path);
1429 gtk_tree_path_free(mimeview->opened);
1430 mimeview->opened = path;
1431 mimeview->spec_part = NULL;
1432 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1434 partinfo = mimeview_get_node_part(mimeview, path);
1435 if (!partinfo) return;
1437 /* ungrab the mouse event */
1438 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1439 gtk_grab_remove(GTK_WIDGET(ctree));
1440 if (gdk_pointer_is_grabbed())
1441 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1444 mimeview->textview->default_text = FALSE;
1446 update_signature_info(mimeview, partinfo);
1448 if (!mimeview_show_part(mimeview, partinfo)) {
1449 switch (partinfo->type) {
1451 case MIMETYPE_MESSAGE:
1452 case MIMETYPE_MULTIPART:
1453 mimeview_show_message_part(mimeview, partinfo);
1457 mimeview->textview->default_text = TRUE;
1458 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1459 textview_clear(mimeview->textview);
1460 textview_show_mime_part(mimeview->textview, partinfo);
1465 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)) {
1466 mimeview_check_signature(mimeview);
1470 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1473 GtkTreeSelection *selection;
1476 if (!event) return FALSE;
1478 if (event->button == 2 || event->button == 3) {
1479 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1480 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1483 gtk_tree_selection_select_iter(selection, &iter);
1485 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1490 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1493 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1494 if (event->direction == GDK_SCROLL_UP) {
1495 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1497 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1499 gtk_vscrollbutton_scroll(scrollbutton);
1503 /* from gdkevents.c */
1504 #define DOUBLE_CLICK_TIME 250
1506 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1509 static MimeInfo *lastinfo;
1510 static guint32 lasttime;
1512 if (event->button == 2 ||
1513 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1514 /* call external program for image, audio or html */
1515 mimeview_launch(mimeview, partinfo);
1517 } else if (event->button == 3) {
1518 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1519 partinfo->type == MIMETYPE_IMAGE ||
1520 partinfo->type == MIMETYPE_MULTIPART))
1521 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1523 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1526 partinfo->type == MIMETYPE_APPLICATION &&
1527 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1528 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1531 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1533 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1534 "pop_partinfo", partinfo);
1536 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1537 NULL, NULL, NULL, NULL,
1538 event->button, event->time);
1542 lastinfo = partinfo;
1543 lasttime = event->time;
1548 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1550 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1553 void mimeview_select_next_part(MimeView *mimeview)
1555 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1556 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1557 GtkTreeSelection *selection;
1560 MimeInfo *partinfo = NULL;
1563 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1564 path = gtk_tree_model_get_path(model, &iter);
1566 has_next = mimeview_tree_next(model, path);
1568 has_next = gtk_tree_model_get_iter_first(model, &iter);
1569 gtk_tree_path_free(path);
1570 path = gtk_tree_model_get_path(model, &iter);
1574 partinfo = mimeview_get_node_part(mimeview, path);
1575 if (partinfo->type == MIMETYPE_MULTIPART ||
1576 (!prefs_common.show_inline_attachments && partinfo->id))
1578 selection = gtk_tree_view_get_selection(ctree);
1579 gtk_tree_selection_select_path(selection, path);
1580 icon_list_toggle_by_mime_info(mimeview, partinfo);
1583 gtk_tree_path_free(path);
1586 void mimeview_select_prev_part(MimeView *mimeview)
1588 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1589 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1590 GtkTreeSelection *selection;
1593 MimeInfo *partinfo = NULL;
1596 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1597 path = gtk_tree_model_get_path(model, &iter);
1599 has_prev = mimeview_tree_prev(mimeview, path);
1601 while (mimeview_tree_next(model, path)) {}
1606 partinfo = mimeview_get_node_part(mimeview, path);
1607 if (partinfo->type == MIMETYPE_MULTIPART ||
1608 (!prefs_common.show_inline_attachments && partinfo->id))
1610 selection = gtk_tree_view_get_selection(ctree);
1611 gtk_tree_selection_select_path(selection, path);
1612 icon_list_toggle_by_mime_info(mimeview, partinfo);
1615 gtk_tree_path_free(path);
1618 #define BREAK_ON_MODIFIER_KEY() \
1619 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1621 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1624 SummaryView *summaryview;
1626 if (!event) return FALSE;
1627 if (!mimeview->opened) return FALSE;
1629 summaryview = mimeview->messageview->mainwin->summaryview;
1631 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1634 switch (event->keyval) {
1637 textview_scroll_max(mimeview->textview,
1638 (event->keyval == GDK_KEY_Home));
1640 case GDK_KEY_Page_Down:
1642 if (mimeview_scroll_page(mimeview,
1643 (event->state & GDK_SHIFT_MASK) != 0))
1646 if (!(event->state & GDK_SHIFT_MASK))
1647 mimeview_select_next_part(mimeview);
1649 case GDK_KEY_Page_Up:
1650 case GDK_KEY_BackSpace:
1651 mimeview_scroll_page(mimeview, TRUE);
1653 case GDK_KEY_Return:
1654 case GDK_KEY_KP_Enter:
1655 mimeview_scroll_one_line(mimeview,
1656 (event->state & GDK_MOD1_MASK) != 0);
1660 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1666 if (mimeview->messageview->new_window) return FALSE;
1668 return summary_pass_key_press_event(summaryview, event);
1671 static void mimeview_drag_data_get(GtkWidget *widget,
1672 GdkDragContext *drag_context,
1673 GtkSelectionData *selection_data,
1678 gchar *filename = NULL, *uriname, *tmp;
1683 if (!mimeview->opened) return;
1684 if (!mimeview->file) return;
1686 partinfo = mimeview_get_selected_part(mimeview);
1687 if (!partinfo) return;
1689 if (strlen(get_part_name(partinfo)) > 0) {
1690 filename = g_path_get_basename(get_part_name(partinfo));
1691 if (*filename == '\0') return;
1692 } else if (partinfo->type == MIMETYPE_MESSAGE
1693 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1695 GPtrArray *headers = NULL;
1698 fp = g_fopen(partinfo->data.filename, "rb");
1699 fseek(fp, partinfo->offset, SEEK_SET);
1700 headers = procheader_get_header_array_asis(fp);
1703 for (i = 0; i < headers->len; i++) {
1704 Header *header = g_ptr_array_index(headers, i);
1705 if (procheader_headername_equal(header->name, "Subject")) {
1706 unfold_line(header->body);
1707 name = g_strconcat(header->body, ".txt", NULL);
1708 subst_for_filename(name);
1711 procheader_header_array_destroy(headers);
1715 filename = g_path_get_basename(name);
1718 if (filename == NULL)
1719 filename = g_path_get_basename("Unnamed part");
1721 if (!g_utf8_validate(filename, -1, NULL))
1722 tmp = conv_codeset_strdup(filename,
1723 conv_get_locale_charset_str(),
1726 tmp = g_strdup(filename);
1729 g_warning("filename not in UTF-8");
1730 tmp = g_strdup("Unnamed part");
1732 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1736 if (is_file_exist(filename)) {
1738 gchar *prefix = NULL;
1739 gchar *new_name = NULL;
1740 if (strrchr(tmp, '.')) {
1741 prefix = g_strdup(tmp);
1742 ext = g_strdup(strrchr(tmp, '.'));
1743 *(strrchr(prefix, '.')) = '\0';
1745 prefix = g_strdup(tmp);
1749 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1753 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1756 goto check_new_file;
1761 if ((err = procmime_get_part(filename, partinfo)) < 0)
1763 (_("Couldn't save the part of multipart message: %s"),
1766 tmp = g_filename_to_uri(filename, NULL, NULL);
1767 uriname = g_strconcat(tmp, "\r\n", NULL);
1770 gtk_selection_data_set(selection_data,
1771 gtk_selection_data_get_target(selection_data), 8,
1772 (guchar *)uriname, strlen(uriname));
1779 * Returns a filename (with path) for an attachment
1780 * \param partinfo The attachment to save
1781 * \param basedir The target directory
1782 * \param number Used for dummy filename if attachment is unnamed
1784 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1785 const gchar *basedir,
1791 filename = g_strdup(get_part_name(partinfo));
1792 if (!filename || !*filename) {
1794 filename = g_strdup_printf("noname.%d", number);
1797 if (!g_utf8_validate(filename, -1, NULL)) {
1798 gchar *tmp = conv_filename_to_utf8(filename);
1803 subst_for_filename(filename);
1805 fullname = g_strconcat
1806 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1807 ? &filename[1] : filename, NULL);
1810 filename = conv_filename_from_utf8(fullname);
1816 * Write a single attachment to file
1817 * \param filename Filename with path
1818 * \param partinfo Attachment to save
1820 static gboolean mimeview_write_part(const gchar *filename,
1826 dir= g_path_get_dirname(filename);
1827 if (!is_dir_exist(dir))
1831 if (is_file_exist(filename)) {
1836 if (!g_utf8_validate(filename, -1, NULL))
1837 tmp = conv_filename_to_utf8(filename);
1839 tmp = g_strdup(filename);
1841 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1844 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
1845 GTK_STOCK_OK, NULL);
1847 if (G_ALERTALTERNATE != aval) return FALSE;
1850 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1852 (_("Couldn't save the part of multipart message: %s"),
1861 * Menu callback: Save all attached files
1862 * \param mimeview Current display
1864 static void mimeview_save_all(MimeView *mimeview)
1868 gchar *startdir = NULL;
1871 if (!mimeview->opened) return;
1872 if (!mimeview->file) return;
1873 if (!mimeview->mimeinfo) return;
1875 partinfo = mimeview->mimeinfo;
1876 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1877 startdir = g_strconcat(prefs_common.attach_save_dir,
1878 G_DIR_SEPARATOR_S, NULL);
1880 startdir = g_strdup(get_home_dir());
1882 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1883 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1889 if (!is_dir_exist (dirname)) {
1890 alertpanel_error(_("'%s' is not a directory."),
1897 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1898 dirname[strlen(dirname)-1] = '\0';
1900 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1901 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1902 partinfo = procmime_mimeinfo_next(partinfo);
1903 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1904 partinfo = procmime_mimeinfo_next(partinfo);
1905 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1906 partinfo = procmime_mimeinfo_next(partinfo);
1909 while (partinfo != NULL) {
1910 if (partinfo->type != MIMETYPE_MESSAGE &&
1911 partinfo->type != MIMETYPE_MULTIPART &&
1912 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1913 || get_real_part_name(partinfo) != NULL)) {
1914 gchar *filename = mimeview_get_filename_for_part
1915 (partinfo, dirname, number++);
1917 mimeview_write_part(filename, partinfo);
1920 partinfo = procmime_mimeinfo_next(partinfo);
1923 g_free(prefs_common.attach_save_dir);
1925 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1926 -1, NULL, NULL, NULL);
1930 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1932 MimeInfo *partinfo = NULL;
1933 if (mimeview->spec_part) {
1934 partinfo = mimeview->spec_part;
1935 mimeview->spec_part = NULL;
1937 partinfo = (MimeInfo *) g_object_get_data
1938 (G_OBJECT(mimeview->popupmenu),
1940 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1941 "pop_partinfo", NULL);
1943 partinfo = mimeview_get_selected_part(mimeview);
1950 * Menu callback: Save the selected attachment
1951 * \param mimeview Current display
1953 void mimeview_save_as(MimeView *mimeview)
1956 gchar *filepath = NULL;
1957 gchar *filedir = NULL;
1959 gchar *partname = NULL;
1961 if (!mimeview->opened) return;
1962 if (!mimeview->file) return;
1964 partinfo = mimeview_get_part_to_use(mimeview);
1966 cm_return_if_fail(partinfo != NULL);
1968 if (get_part_name(partinfo) == NULL) {
1971 partname = g_strdup(get_part_name(partinfo));
1973 if (!g_utf8_validate(partname, -1, NULL)) {
1974 gchar *tmp = conv_filename_to_utf8(partname);
1976 tmp = conv_codeset_strdup(partname,
1977 conv_get_locale_charset_str(),
1986 subst_for_filename(partname);
1988 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1989 filepath = g_strconcat(prefs_common.attach_save_dir,
1990 G_DIR_SEPARATOR_S, partname, NULL);
1992 filepath = g_strdup(partname);
1996 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1997 filename = filesel_select_file_save(_("Save as"), filepath);
2003 mimeview_write_part(filename, partinfo);
2005 filedir = g_path_get_dirname(filename);
2006 if (filedir && strcmp(filedir, ".")) {
2007 g_free(prefs_common.attach_save_dir);
2008 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2015 void mimeview_display_as_text(MimeView *mimeview)
2019 if (!mimeview->opened) return;
2021 partinfo = mimeview_get_part_to_use(mimeview);
2022 mimeview_select_mimepart_icon(mimeview, partinfo);
2023 cm_return_if_fail(partinfo != NULL);
2024 mimeview_show_message_part(mimeview, partinfo);
2027 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2032 if (!mimeview->opened) return;
2033 if (!mimeview->file) return;
2036 partinfo = mimeview_get_part_to_use(mimeview);
2038 cm_return_if_fail(partinfo != NULL);
2040 filename = procmime_get_tmp_file_name(partinfo);
2042 if ((err = procmime_get_part(filename, partinfo)) < 0)
2044 (_("Couldn't save the part of multipart message: %s"),
2047 mimeview_view_file(filename, partinfo, NULL, mimeview);
2053 void mimeview_open_with(MimeView *mimeview)
2057 if (!mimeview) return;
2058 if (!mimeview->opened) return;
2059 if (!mimeview->file) return;
2061 partinfo = mimeview_get_part_to_use(mimeview);
2063 mimeview_open_part_with(mimeview, partinfo, FALSE);
2066 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2070 gchar *mime_command = NULL;
2071 gchar *content_type = NULL;
2074 cm_return_if_fail(partinfo != NULL);
2076 filename = procmime_get_tmp_file_name(partinfo);
2078 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2080 (_("Couldn't save the part of multipart message: %s"),
2086 if (!prefs_common.mime_open_cmd_history)
2087 prefs_common.mime_open_cmd_history =
2088 add_history(NULL, prefs_common.mime_open_cmd);
2090 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2091 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2092 /* guess content-type from filename */
2093 content_type = procmime_get_mime_type(filename);
2095 if (content_type == NULL) {
2096 content_type = procmime_get_content_type_str(partinfo->type,
2100 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2101 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2102 mime_command = g_strdup(prefs_common_get_uri_cmd());
2103 g_free(content_type);
2104 content_type = NULL;
2105 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2106 || !prefs_common_get_ext_editor_cmd()[0]) {
2107 mime_command = mailcap_get_command_for_type(content_type, filename);
2109 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2110 g_free(content_type);
2111 content_type = NULL;
2113 if (mime_command == NULL) {
2114 /* try with extension this time */
2115 g_free(content_type);
2116 content_type = procmime_get_mime_type(filename);
2117 mime_command = mailcap_get_command_for_type(content_type, filename);
2120 if (mime_command == NULL)
2124 gboolean remember = FALSE;
2125 if (content_type != NULL)
2126 cmd = input_dialog_combo_remember
2128 _("Enter the command-line to open file:\n"
2129 "('%s' will be replaced with file name)"),
2130 mime_command ? mime_command : prefs_common.mime_open_cmd,
2131 prefs_common.mime_open_cmd_history, &remember);
2133 cmd = input_dialog_combo
2135 _("Enter the command-line to open file:\n"
2136 "('%s' will be replaced with file name)"),
2137 mime_command ? mime_command : prefs_common.mime_open_cmd,
2138 prefs_common.mime_open_cmd_history);
2139 if (cmd && remember) {
2140 mailcap_update_default(content_type, cmd);
2142 g_free(mime_command);
2147 mimeview_view_file(filename, partinfo, cmd, mimeview);
2148 g_free(prefs_common.mime_open_cmd);
2149 prefs_common.mime_open_cmd = cmd;
2150 prefs_common.mime_open_cmd_history =
2151 add_history(prefs_common.mime_open_cmd_history, cmd);
2154 g_free(content_type);
2159 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2161 GList *attach_file = NULL;
2162 AttachInfo *ainfo = NULL;
2166 if (!mimeview->opened) return;
2167 if (!mimeview->file) return;
2169 cm_return_if_fail(partinfo != NULL);
2171 filename = procmime_get_tmp_file_name(partinfo);
2173 if (!(err = procmime_get_part(filename, partinfo))) {
2174 ainfo = g_new0(AttachInfo, 1);
2175 ainfo->file = filename;
2176 ainfo->name = g_strdup(get_part_name(partinfo));
2177 ainfo->content_type = procmime_get_content_type_str(
2178 partinfo->type, partinfo->subtype);
2179 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2180 partinfo, "charset"));
2181 attach_file = g_list_append(attach_file, ainfo);
2183 compose_new(NULL, NULL, attach_file);
2185 g_free(ainfo->name);
2186 g_free(ainfo->content_type);
2187 g_free(ainfo->charset);
2189 g_list_free(attach_file);
2192 (_("Couldn't save the part of multipart message: %s"),
2197 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2198 const gchar *cmd, MimeView *mimeview)
2202 gchar buf[BUFFSIZE];
2204 mimeview_open_part_with(mimeview, partinfo, TRUE);
2206 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2207 !strchr(p + 2, '%')) {
2208 g_snprintf(buf, sizeof(buf), cmd, filename);
2209 if (!prefs_common.save_parts_readwrite)
2210 g_chmod(filename, S_IRUSR);
2212 g_chmod(filename, S_IRUSR|S_IWUSR);
2214 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2215 mimeview_open_part_with(mimeview, partinfo, FALSE);
2217 if (execute_command_line(buf, TRUE) != 0) {
2218 if (!prefs_common.save_parts_readwrite)
2219 g_chmod(filename, S_IRUSR|S_IWUSR);
2220 mimeview_open_part_with(mimeview, partinfo, FALSE);
2224 SHFILEINFO file_info;
2225 if ((SHGetFileInfo(filename, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2226 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2227 _("This attachment is an executable file. Executing "
2228 "untrusted binaries is dangerous and could probably "
2229 "lead to compromission of your computer.\n\n"
2230 "Do you want to run this file?"), GTK_STOCK_CANCEL,
2232 NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
2233 if (val == G_ALERTALTERNATE) {
2234 debug_print("executing binary\n");
2235 ShellExecute(NULL, "open", filename, NULL, NULL, SW_SHOW);
2238 ShellExecute(NULL, "open", filename, NULL, NULL, SW_SHOW);
2243 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2245 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2248 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2250 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2253 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2255 GSList *mimeview_list, *viewer_list;
2257 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2258 MimeView *mimeview = (MimeView *) mimeview_list->data;
2260 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2261 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2262 mimeview->mimeviewer = NULL;
2265 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2266 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2268 mimeviewer->destroy_viewer(mimeviewer);
2269 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2273 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2276 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2281 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2282 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2284 if (event->button == 1) {
2285 icon_selected(mimeview, num, partinfo);
2286 gtk_widget_grab_focus(button);
2287 icon_list_toggle_by_mime_info(mimeview, partinfo);
2289 part_button_pressed(mimeview, event, partinfo);
2294 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2296 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2299 MimeInfo *curr = NULL;
2301 gtk_tree_model_get_iter_first(model, &iter);
2302 path = gtk_tree_model_get_path(model, &iter);
2305 gtk_tree_model_get_iter(model, &iter, path);
2306 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2307 if (curr == partinfo) {
2308 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2309 GTK_TREE_VIEW(mimeview->ctree));
2310 gtk_tree_selection_select_iter(sel, &iter);
2311 gtk_tree_path_free(path);
2314 } while (mimeview_tree_next(model, path));
2316 gtk_tree_path_free(path);
2319 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2321 icon_list_toggle_by_mime_info(mimeview, partinfo);
2322 icon_selected(mimeview, -1, partinfo);
2325 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2328 SummaryView *summaryview;
2330 if (!event) return FALSE;
2332 switch (event->keyval) {
2334 if (mimeview_scroll_page(mimeview, FALSE))
2337 mimeview_select_next_part(mimeview);
2341 case GDK_KEY_BackSpace:
2342 mimeview_scroll_page(mimeview, TRUE);
2344 case GDK_KEY_Return:
2345 case GDK_KEY_KP_Enter:
2346 mimeview_scroll_one_line(mimeview,
2347 (event->state & GDK_MOD1_MASK) != 0);
2350 BREAK_ON_MODIFIER_KEY();
2351 mimeview_save_as(mimeview);
2354 BREAK_ON_MODIFIER_KEY();
2355 mimeview_display_as_text(mimeview);
2358 BREAK_ON_MODIFIER_KEY();
2359 mimeview_launch(mimeview, NULL);
2363 BREAK_ON_MODIFIER_KEY();
2364 mimeview_open_with(mimeview);
2368 BREAK_ON_MODIFIER_KEY();
2369 mimeview_check_signature(mimeview);
2372 BREAK_ON_MODIFIER_KEY();
2373 mimeview_select_next_part(mimeview);
2379 if (!mimeview->messageview->mainwin) return FALSE;
2380 summaryview = mimeview->messageview->mainwin->summaryview;
2381 return summary_pass_key_press_event(summaryview, event);
2384 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2386 MimeView *mimeview = (MimeView *)data;
2387 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2389 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2390 "pop_partinfo", partinfo);
2391 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
2392 NULL, NULL, NULL, NULL,
2393 0, gtk_get_current_event_time());
2397 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2399 GtkWidget *pixmap = NULL;
2404 const gchar *desc = NULL;
2405 gchar *sigshort = NULL;
2406 gchar *content_type;
2409 MimeInfo *siginfo = NULL;
2410 MimeInfo *encrypted = NULL;
2414 #if !(GTK_CHECK_VERSION(2,12,0))
2415 GtkTooltips *tips = mimeview->tooltips;
2418 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2421 vbox = mimeview->icon_vbox;
2422 mimeview->icon_count++;
2423 button = gtk_event_box_new();
2425 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2426 G_CALLBACK(mimeview_visi_notify), mimeview);
2427 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2428 G_CALLBACK(mimeview_leave_notify), mimeview);
2429 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2430 G_CALLBACK(mimeview_enter_notify), mimeview);
2432 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2433 g_object_set_data(G_OBJECT(button), "icon_number",
2434 GINT_TO_POINTER(mimeview->icon_count));
2435 g_object_set_data(G_OBJECT(button), "partinfo",
2438 switch (mimeinfo->type) {
2441 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2442 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2443 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2444 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2445 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2446 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2447 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2448 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2449 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2451 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2453 case MIMETYPE_MESSAGE:
2454 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2456 case MIMETYPE_APPLICATION:
2457 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2458 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2459 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2460 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2461 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2462 stockp = STOCK_PIXMAP_MIME_PDF;
2463 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2464 stockp = STOCK_PIXMAP_MIME_PS;
2466 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2468 case MIMETYPE_IMAGE:
2469 stockp = STOCK_PIXMAP_MIME_IMAGE;
2471 case MIMETYPE_AUDIO:
2472 stockp = STOCK_PIXMAP_MIME_AUDIO;
2475 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2479 partinfo = mimeinfo;
2480 while (partinfo != NULL) {
2481 if (privacy_mimeinfo_is_signed(partinfo)) {
2485 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2486 encrypted = partinfo;
2489 partinfo = procmime_mimeinfo_parent(partinfo);
2492 if (siginfo != NULL) {
2493 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2494 case SIGNATURE_UNCHECKED:
2495 case SIGNATURE_CHECK_FAILED:
2496 case SIGNATURE_CHECK_TIMEOUT:
2497 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2498 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2501 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2502 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2504 case SIGNATURE_WARN:
2505 case SIGNATURE_KEY_EXPIRED:
2506 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2507 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2509 case SIGNATURE_INVALID:
2510 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2511 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2514 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2515 } else if (encrypted != NULL) {
2516 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2517 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2519 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
2520 OVERLAY_NONE, 6, 3);
2522 gtk_container_add(GTK_CONTAINER(button), pixmap);
2524 if (prefs_common.attach_desc)
2525 desc = get_part_description(mimeinfo);
2527 desc = get_part_name(mimeinfo);
2530 content_type = procmime_get_content_type_str(mimeinfo->type,
2533 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2534 "\n<b>", _("Size:"), " </b>",
2535 to_human_readable((goffset)mimeinfo->length), NULL);
2536 g_free(content_type);
2537 if (desc && *desc) {
2539 if (!g_utf8_validate(desc, -1, NULL)) {
2540 tmp = conv_filename_to_utf8(desc);
2542 tmp = g_strdup(desc);
2544 tiptmp = g_strconcat(tip, "\n<b>",
2545 prefs_common.attach_desc && mimeinfo->description ?
2546 _("Description:") : _("Filename:"),
2547 " </b>", g_markup_escape_text(tmp,-1), NULL);
2552 if (sigshort && *sigshort) {
2553 tiptmp = g_strjoin("\n", tip, g_markup_escape_text(sigshort, -1), NULL);
2559 gtk_widget_set_tooltip_markup(button, tip);
2561 gtk_widget_show_all(button);
2562 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2563 mimeview_mime_types, 1, GDK_ACTION_COPY);
2565 g_signal_connect(G_OBJECT(button), "popup-menu",
2566 G_CALLBACK(icon_popup_menu), mimeview);
2567 g_signal_connect(G_OBJECT(button), "button_release_event",
2568 G_CALLBACK(icon_clicked_cb), mimeview);
2569 g_signal_connect(G_OBJECT(button), "key_press_event",
2570 G_CALLBACK(icon_key_pressed), mimeview);
2571 g_signal_connect(G_OBJECT(button), "drag_data_get",
2572 G_CALLBACK(mimeview_drag_data_get), mimeview);
2573 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2575 gtk_widget_size_request(pixmap, &r);
2576 gtk_widget_set_size_request(button, -1, r.height + 4);
2581 static void icon_list_clear (MimeView *mimeview)
2583 GList *child, *orig;
2586 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2587 for (child = orig; child != NULL; child = g_list_next(child)) {
2588 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
2589 GTK_WIDGET(child->data));
2592 mimeview->icon_count = 0;
2593 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2594 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2595 gtk_adjustment_changed(adj);
2599 *\brief Used to 'click' the next or previous icon.
2601 *\return true if the icon 'number' exists and was selected.
2603 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2604 GtkAllocation *size, MimeView *mimeview)
2606 GtkAllocation vbox_size;
2607 GtkAllocation layout_size;
2612 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2614 gtk_widget_get_allocation(mimeview->icon_vbox, &vbox_size);
2615 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2617 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2618 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2620 MAX(vbox_size.height, layout_size.height));
2621 gtk_adjustment_set_step_increment(adj, 10);
2624 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2626 GtkRequisition size;
2627 GtkRequisition requisition;
2629 cm_return_if_fail(mimeinfo != NULL);
2631 while (mimeinfo != NULL) {
2632 if (mimeinfo->type != MIMETYPE_MULTIPART)
2633 icon_list_append_icon(mimeview, mimeinfo);
2634 if (mimeinfo->node->children != NULL)
2635 icon_list_create(mimeview,
2636 (MimeInfo *) mimeinfo->node->children->data);
2637 mimeinfo = mimeinfo->node->next != NULL
2638 ? (MimeInfo *) mimeinfo->node->next->data
2641 gtk_widget_size_request(mimeview->icon_vbox, &size);
2642 gtk_widget_get_requisition(mimeview->icon_mainbox, &requisition);
2643 if (size.width > requisition.width) {
2644 gtk_widget_set_size_request(mimeview->icon_mainbox,
2649 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2654 child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2655 for (; child != NULL; child = g_list_next(child)) {
2656 if (!GTK_IS_EVENT_BOX(child->data))
2658 if(g_object_get_data(G_OBJECT(child->data),
2659 "partinfo") == (gpointer)mimeinfo) {
2660 gboolean *highlight = NULL;
2661 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2662 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2664 gtk_widget_queue_draw(icon);
2666 gint *highlight = NULL;
2667 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2668 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2670 gtk_widget_queue_draw(icon);
2675 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2678 prefs_common.mimeview_tree_height = allocation->height;
2681 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2684 g_object_ref(button);
2686 mimeview_leave_notify(button, NULL, NULL);
2688 mimeview->ctree_mode = !mimeview->ctree_mode;
2689 if (mimeview->ctree_mode) {
2690 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2692 gtk_widget_hide(mimeview->icon_mainbox);
2693 gtk_widget_show(mimeview->ctree_mainbox);
2694 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2695 prefs_common.mimeview_tree_height);
2697 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2699 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2700 button, FALSE, FALSE, 0);
2702 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2704 gtk_widget_hide(mimeview->ctree_mainbox);
2705 gtk_widget_show(mimeview->icon_mainbox);
2706 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2708 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2710 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2711 button, FALSE, FALSE, 0);
2712 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2713 if (mimeview->opened)
2714 icon_list_toggle_by_mime_info(mimeview,
2715 mimeview_get_node_part(mimeview, mimeview->opened));
2716 summary_grab_focus(mimeview->mainwin->summaryview);
2718 g_object_unref(button);
2722 void mimeview_update (MimeView *mimeview) {
2723 if (mimeview && mimeview->mimeinfo) {
2724 icon_list_clear(mimeview);
2725 icon_list_create(mimeview, mimeview->mimeinfo);
2729 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2731 MessageView *msgview = NULL;
2732 MainWindow *mainwin = NULL;
2737 msgview = mimeview->messageview;
2741 mainwin = msgview->mainwin;
2745 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2746 "pop_partinfo", NULL);
2748 if (!strcmp(cmd, "sc://view_log"))
2749 log_window_show(mainwin->logwin);
2750 else if (!strcmp(cmd, "sc://save_as"))
2751 mimeview_save_as(mimeview);
2752 else if (!strcmp(cmd, "sc://display_as_text"))
2753 mimeview_display_as_text(mimeview);
2755 else if (!strcmp(cmd, "sc://open_with"))
2756 mimeview_open_with(mimeview);
2758 else if (!strcmp(cmd, "sc://open"))
2759 mimeview_launch(mimeview, NULL);
2760 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2761 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2762 icon_selected(mimeview, -1, (MimeInfo *)data);
2763 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2764 mimeview_launch(mimeview, (MimeInfo *)data);
2765 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2766 mimeview->spec_part = (MimeInfo *)data;
2767 part_button_pressed(mimeview, event, (MimeInfo *)data);
2768 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2769 const gchar *tagname = cmd + strlen("sc://search_tags:");
2770 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2771 gtk_toggle_button_set_active(
2772 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2774 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2775 ADVANCED_SEARCH_EXTENDED, buf);
2780 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2782 if (mimeview->type == MIMEVIEW_TEXT)
2783 return textview_scroll_page(mimeview->textview, up);
2784 else if (mimeview->mimeviewer) {
2785 MimeViewer *mimeviewer = mimeview->mimeviewer;
2786 if (mimeviewer->scroll_page)
2787 return mimeviewer->scroll_page(mimeviewer, up);
2792 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2794 if (mimeview->type == MIMEVIEW_TEXT)
2795 textview_scroll_one_line(mimeview->textview, up);
2796 else if (mimeview->mimeviewer) {
2797 MimeViewer *mimeviewer = mimeview->mimeviewer;
2798 if (mimeviewer->scroll_one_line)
2799 mimeviewer->scroll_one_line(mimeviewer, up);