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/>.
22 #include "claws-features.h"
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
37 #include <sys/types.h>
40 #ifndef HAVE_APACHE_FNMATCH
41 /* kludge: apache's fnmatch clashes with <regex.h>, don't include
50 #include "summaryview.h"
53 #include "alertpanel.h"
54 #include "inputdialog.h"
57 #include "prefs_common.h"
58 #include "procheader.h"
59 #include "stock_pixmap.h"
60 #include "gtk/gtkvscrollbutton.h"
61 #include "gtk/logwindow.h"
63 #include "manage_window.h"
74 #define N_MIMEVIEW_COLS 4
76 static void mimeview_set_multipart_tree (MimeView *mimeview,
79 static void mimeview_show_message_part (MimeView *mimeview,
81 static void mimeview_change_view_type (MimeView *mimeview,
83 static gchar *mimeview_get_filename_for_part (MimeInfo *partinfo,
86 static gboolean mimeview_write_part (const gchar *filename,
89 static void mimeview_selected (GtkTreeSelection *selection,
91 static gint mimeview_button_pressed (GtkWidget *widget,
92 GdkEventButton *event,
94 static gint mimeview_key_pressed (GtkWidget *widget,
98 static void mimeview_drag_data_get (GtkWidget *widget,
99 GdkDragContext *drag_context,
100 GtkSelectionData *selection_data,
105 static gboolean mimeview_scrolled (GtkWidget *widget,
106 GdkEventScroll *event,
109 static void mimeview_save_all (MimeView *mimeview);
111 static void mimeview_open_part_with (MimeView *mimeview,
115 static void mimeview_send_to (MimeView *mimeview,
117 static void mimeview_view_file (const gchar *filename,
121 static gboolean icon_clicked_cb (GtkWidget *button,
122 GdkEventButton *event,
124 static void icon_selected (MimeView *mimeview,
127 static gint icon_key_pressed (GtkWidget *button,
130 static void icon_list_append_icon (MimeView *mimeview,
132 static void icon_list_create (MimeView *mimeview,
134 static void icon_list_clear (MimeView *mimeview);
135 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
137 static void ctree_size_allocate_cb (GtkWidget *widget,
138 GtkAllocation *allocation,
140 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
142 static gboolean part_button_pressed (MimeView *mimeview,
143 GdkEventButton *event,
145 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
146 GtkAllocation *layout_size,
148 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview);
149 static const gchar *get_part_name(MimeInfo *partinfo);
150 static const gchar *get_part_description(MimeInfo *partinfo);
152 static void mimeview_launch_cb(GtkAction *action, gpointer data)
154 MimeView *mimeview = (MimeView *)data;
155 mimeview_launch(mimeview, mimeview_get_part_to_use(mimeview));
159 static void mimeview_open_with_cb(GtkAction *action, gpointer data)
161 mimeview_open_with((MimeView *)data);
165 static void mimeview_send_to_cb(GtkAction *action, gpointer data)
167 MimeView *mimeview = (MimeView *)data;
168 mimeview_send_to(mimeview, mimeview_get_part_to_use(mimeview));
171 static void mimeview_display_as_text_cb(GtkAction *action, gpointer data)
173 mimeview_display_as_text((MimeView *)data);
176 static void mimeview_save_as_cb(GtkAction *action, gpointer data)
178 mimeview_save_as((MimeView *)data);
181 static void mimeview_save_all_cb(GtkAction *action, gpointer data)
183 mimeview_save_all((MimeView *)data);
186 static void mimeview_select_next_part_cb(GtkAction *action, gpointer data)
188 mimeview_select_next_part((MimeView *)data);
191 static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
193 mimeview_select_prev_part((MimeView *)data);
196 static GtkActionEntry mimeview_menu_actions[] = {
197 { "MimeView", NULL, "MimeView" },
198 { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
199 #if (!defined G_OS_WIN32)
200 { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
202 { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
203 { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
204 { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
205 { "MimeView/SaveAll", NULL, N_("Save _all..."), NULL, "Save all parts", G_CALLBACK(mimeview_save_all_cb) },
206 { "MimeView/NextPart", NULL, N_("Next part"), NULL, "Next part", G_CALLBACK(mimeview_select_next_part_cb) },
207 { "MimeView/PrevPart", NULL, N_("Previous part"), NULL, "Previous part", G_CALLBACK(mimeview_select_prev_part_cb) }
210 static GtkTargetEntry mimeview_mime_types[] =
212 {"text/uri-list", 0, 0}
215 GSList *mimeviewer_factories;
218 static GdkCursor *hand_cursor = NULL;
220 static gboolean mimeview_visi_notify(GtkWidget *widget,
221 GdkEventVisibility *event,
224 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
228 static gboolean mimeview_leave_notify(GtkWidget *widget,
229 GdkEventCrossing *event,
232 gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
236 static gboolean mimeview_enter_notify(GtkWidget *widget,
237 GdkEventCrossing *event,
240 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
244 MimeView *mimeview_create(MainWindow *mainwin)
249 GtkWidget *scrolledwin;
251 GtkWidget *mime_notebook;
252 GtkWidget *popupmenu;
253 GtkWidget *ctree_mainbox;
255 GtkWidget *mime_toggle;
256 GtkWidget *icon_mainbox;
257 GtkWidget *icon_scroll;
258 GtkWidget *icon_vbox;
260 GtkWidget *scrollbutton;
262 NoticeView *siginfoview;
265 GtkCellRenderer *renderer;
266 GtkTreeViewColumn *column;
267 GtkTreeSelection *selection;
269 gchar *titles[N_MIMEVIEW_COLS];
273 hand_cursor = gdk_cursor_new(GDK_HAND2);
275 debug_print("Creating MIME view...\n");
276 mimeview = g_new0(MimeView, 1);
278 titles[COL_MIMETYPE] = _("MIME Type");
279 titles[COL_SIZE] = _("Size");
280 titles[COL_NAME] = _("Name");
282 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
283 gtk_widget_show(scrolledwin);
284 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
285 GTK_POLICY_AUTOMATIC,
286 GTK_POLICY_AUTOMATIC);
288 model = gtk_tree_store_new(N_MIMEVIEW_COLS,
294 ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
295 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
296 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
297 prefs_common.use_stripes_everywhere);
298 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ctree),
299 prefs_common.show_col_headers);
301 renderer = gtk_cell_renderer_text_new();
302 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
303 -1, titles[COL_MIMETYPE],renderer,
304 "text", COL_MIMETYPE, NULL);
305 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
307 renderer = gtk_cell_renderer_text_new();
308 #if(GTK_CHECK_VERSION(2,18,0))
309 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
311 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
312 -1, titles[COL_SIZE], renderer,
313 "text", COL_SIZE, NULL);
314 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
315 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
317 renderer = gtk_cell_renderer_text_new();
318 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
319 -1, titles[COL_NAME], renderer,
320 "text", COL_NAME, NULL);
321 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
322 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
324 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
325 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
327 gtk_widget_show(ctree);
328 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
329 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
330 mimeview_mime_types, 1, GDK_ACTION_COPY);
332 g_signal_connect(G_OBJECT(selection), "changed",
333 G_CALLBACK(mimeview_selected), mimeview);
334 g_signal_connect(G_OBJECT(ctree), "button_release_event",
335 G_CALLBACK(mimeview_button_pressed), mimeview);
336 g_signal_connect(G_OBJECT(ctree), "key_press_event",
337 G_CALLBACK(mimeview_key_pressed), mimeview);
338 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
339 G_CALLBACK(mimeview_drag_data_get), mimeview);
341 mime_notebook = gtk_notebook_new();
342 gtk_widget_show(mime_notebook);
343 gtkut_widget_set_can_focus(mime_notebook, FALSE);
344 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
345 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
347 icon_vbox = gtk_vbox_new(FALSE, 2);
348 gtk_widget_show(icon_vbox);
349 icon_scroll = gtk_layout_new(NULL, NULL);
350 gtk_widget_show(icon_scroll);
351 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
352 scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
353 gtk_widget_show(scrollbutton);
355 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
356 G_CALLBACK(mimeview_scrolled), mimeview);
358 mime_toggle = gtk_event_box_new();
359 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
361 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
362 G_CALLBACK(mimeview_visi_notify), mimeview);
363 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
364 G_CALLBACK(mimeview_leave_notify), mimeview);
365 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
366 G_CALLBACK(mimeview_enter_notify), mimeview);
368 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
369 gtk_widget_show(mime_toggle);
370 mimeview->ctree_mode = FALSE;
371 arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
372 gtk_widget_show(arrow);
373 gtk_widget_size_request(arrow, &r);
374 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
375 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
376 G_CALLBACK(mime_toggle_button_cb), mimeview);
378 icon_mainbox = gtk_vbox_new(FALSE, 0);
379 gtk_widget_show(icon_mainbox);
380 gtk_widget_set_size_request(icon_mainbox, 32, -1);
381 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
382 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
383 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
384 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
385 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
387 ctree_mainbox = gtk_hbox_new(FALSE, 0);
388 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
389 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
390 G_CALLBACK(ctree_size_allocate_cb), mimeview);
392 mimeview->ui_manager = gtk_ui_manager_new();
393 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
394 "MimeView", mimeview_menu_actions,
395 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
397 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
398 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
399 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
400 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
401 "/Menus/MimeView/", "Open", "MimeView/Open",
402 GTK_UI_MANAGER_MENUITEM);
403 #if (!defined G_OS_WIN32)
404 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
405 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
406 GTK_UI_MANAGER_MENUITEM);
408 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
409 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
410 GTK_UI_MANAGER_MENUITEM);
411 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
412 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
413 GTK_UI_MANAGER_MENUITEM);
414 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
415 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
416 GTK_UI_MANAGER_MENUITEM);
417 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
418 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
419 GTK_UI_MANAGER_MENUITEM);
420 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
421 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
422 GTK_UI_MANAGER_MENUITEM);
423 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
424 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
425 GTK_UI_MANAGER_MENUITEM);
427 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
428 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
431 vbox = gtk_vbox_new(FALSE, 0);
432 gtk_widget_show(vbox);
433 siginfoview = noticeview_create(mainwin);
434 noticeview_hide(siginfoview);
435 noticeview_set_icon_clickable(siginfoview, TRUE);
436 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
437 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
439 paned = gtk_vpaned_new();
440 gtk_widget_show(paned);
441 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
442 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
444 hbox = gtk_hbox_new(FALSE, 0);
445 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
446 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
448 gtk_widget_show(hbox);
449 gtk_widget_hide(ctree_mainbox);
451 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
453 mimeview->hbox = hbox;
454 mimeview->paned = paned;
455 mimeview->scrolledwin = scrolledwin;
456 mimeview->ctree = ctree;
457 mimeview->mime_notebook = mime_notebook;
458 mimeview->popupmenu = popupmenu;
460 mimeview->ctree_mainbox = ctree_mainbox;
461 mimeview->icon_scroll = icon_scroll;
462 mimeview->icon_vbox = icon_vbox;
463 mimeview->icon_mainbox = icon_mainbox;
464 mimeview->icon_count = 0;
465 mimeview->mainwin = mainwin;
466 mimeview->mime_toggle = mime_toggle;
467 mimeview->siginfoview = siginfoview;
468 mimeview->scrollbutton = scrollbutton;
469 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
471 mimeviews = g_slist_prepend(mimeviews, mimeview);
476 void mimeview_init(MimeView *mimeview)
478 textview_init(mimeview->textview);
480 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
481 GTK_WIDGET_PTR(mimeview->textview));
484 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
487 if (privacy_mimeinfo_is_signed(mimeinfo))
489 mimeinfo = procmime_mimeinfo_next(mimeinfo);
495 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
498 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
500 mimeview_clear(mimeview);
502 cm_return_if_fail(file != NULL);
503 cm_return_if_fail(mimeinfo != NULL);
505 mimeview->mimeinfo = mimeinfo;
507 mimeview->file = g_strdup(file);
509 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
512 /* check if the mail's signed - it can change the mail structure */
514 if (any_part_is_signed(mimeinfo))
515 debug_print("signed mail\n");
517 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
518 gtk_tree_view_expand_all(ctree);
519 icon_list_clear(mimeview);
520 icon_list_create(mimeview, mimeinfo);
522 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
523 mimeview_selected, mimeview);
527 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
530 static void mimeview_free_mimeinfo(MimeView *mimeview)
532 gboolean defer = FALSE;
534 defer = (mimeview->check_data != NULL);
536 mimeview->check_data->free_after_use = TRUE;
538 if (mimeview->mimeinfo != NULL && !defer) {
539 procmime_mimeinfo_free_all(mimeview->mimeinfo);
540 mimeview->mimeinfo = NULL;
543 debug_print("deferring free(mimeinfo) and cancelling check\n");
544 mimeview_check_sig_cancel_now(mimeview);
549 void mimeview_destroy(MimeView *mimeview)
553 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
554 MimeViewer *viewer = (MimeViewer *) cur->data;
555 viewer->destroy_viewer(viewer);
557 g_slist_free(mimeview->viewers);
558 gtk_target_list_unref(mimeview->target_list);
561 if (mimeview->check_data) {
562 mimeview->check_data->destroy_mimeview = TRUE;
563 debug_print("deferring destroy\n");
567 mimeview_free_mimeinfo(mimeview);
568 gtk_tree_path_free(mimeview->opened);
569 g_free(mimeview->file);
571 mimeviews = g_slist_remove(mimeviews, mimeview);
576 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
578 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
580 GtkTreeSelection *selection;
581 MimeInfo *partinfo = NULL;
583 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
584 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
585 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
590 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
592 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
596 gtk_tree_model_get_iter(model, &iter, path);
597 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
601 gboolean mimeview_tree_is_empty(MimeView *mimeview)
603 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
605 return !gtk_tree_model_get_iter_first(model, &iter);
608 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
610 GtkTreeIter iter, parent;
613 gtk_tree_model_get_iter(model, &iter, path);
615 if (gtk_tree_model_iter_has_child(model, &iter)) {
616 gtk_tree_path_down(path);
620 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
622 if (!gtk_tree_model_iter_next(model, &iter)) {
624 GtkTreeIter saved_parent = parent;
625 gtk_tree_path_up(path);
626 if (gtk_tree_model_iter_next(model, &parent)) {
627 gtk_tree_path_next(path);
630 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
635 gtk_tree_path_next(path);
642 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
644 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
645 GtkTreeIter iter, child, parent;
648 gtk_tree_model_get_iter(model, &iter, path);
649 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
651 if (gtk_tree_path_prev(path)) {
652 gtk_tree_model_get_iter(model, &iter, path);
654 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
655 gtk_tree_path_down(path);
657 while (gtk_tree_model_iter_next(model, &child))
658 gtk_tree_path_next(path);
665 gtk_tree_path_up(path);
672 gint mimeview_get_selected_part_num(MimeView *mimeview)
674 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
679 gtk_tree_model_get_iter_first(model, &iter);
680 path = gtk_tree_model_get_path(model, &iter);
683 if (!gtk_tree_path_compare(mimeview->opened, path)) {
684 gtk_tree_path_free(path);
689 } while (mimeview_tree_next(model, path));
691 gtk_tree_path_free(path);
696 void mimeview_select_part_num(MimeView *mimeview, gint i)
698 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
701 GtkTreeSelection *selection;
707 gtk_tree_model_get_iter_first(model, &iter);
708 path = gtk_tree_model_get_path(model, &iter);
711 if (!mimeview_tree_next(model, path)) {
712 gtk_tree_path_free(path);
718 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
719 gtk_tree_selection_select_path(selection, path);
720 gtk_tree_path_free(path);
723 static void mimeview_set_multipart_tree(MimeView *mimeview,
727 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
728 GTK_TREE_VIEW(mimeview->ctree)));
730 gchar *content_type, *length, *name;
732 cm_return_if_fail(mimeinfo != NULL);
734 while (mimeinfo != NULL) {
735 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
736 content_type = g_strdup_printf("%s/%s",
737 procmime_get_media_type_str(mimeinfo->type),
740 content_type = g_strdup("UNKNOWN");
742 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
744 if (prefs_common.attach_desc)
745 name = g_strdup(get_part_description(mimeinfo));
747 name = g_strdup(get_part_name(mimeinfo));
749 gtk_tree_store_append(model, &iter, parent);
750 gtk_tree_store_set(model, &iter,
751 COL_MIMETYPE, content_type,
754 COL_DATA, mimeinfo, -1);
755 g_free(content_type);
759 if (mimeinfo->node->children)
760 mimeview_set_multipart_tree(mimeview,
761 (MimeInfo *) mimeinfo->node->children->data, &iter);
762 mimeinfo = mimeinfo->node->next != NULL ?
763 (MimeInfo *) mimeinfo->node->next->data : NULL;
767 static const gchar *get_real_part_name(MimeInfo *partinfo)
769 const gchar *name = NULL;
771 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
773 name = procmime_mimeinfo_get_parameter(partinfo, "name");
778 static const gchar *get_part_name(MimeInfo *partinfo)
782 name = get_real_part_name(partinfo);
789 static const gchar *get_part_description(MimeInfo *partinfo)
791 if (partinfo->description)
792 return partinfo->description;
794 return get_part_name(partinfo);
797 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
802 if (!partinfo) return;
804 fname = mimeview->file;
807 if ((fp = g_fopen(fname, "rb")) == NULL) {
808 FILE_OP_ERROR(fname, "fopen");
812 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
813 FILE_OP_ERROR(mimeview->file, "fseek");
818 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
819 textview_show_part(mimeview->textview, partinfo, fp);
824 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
827 MimeViewerFactory *factory = NULL;
828 MimeViewer *viewer = NULL;
829 gchar *real_contenttype = NULL;
832 * FNM_CASEFOLD is a GNU extension
835 #define FNM_CASEFOLD 0
836 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
838 real_contenttype = g_strdup(content_type);
841 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
842 MimeViewerFactory *curfactory = cur->data;
845 while (curfactory->content_types[i] != NULL) {
846 if(!fnmatch(curfactory->content_types[i], real_contenttype, FNM_CASEFOLD)) {
847 debug_print("%s\n", curfactory->content_types[i]);
848 factory = curfactory;
856 g_free(real_contenttype);
860 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
861 MimeViewer *curviewer = cur->data;
863 if (curviewer->factory == factory)
866 viewer = factory->create_viewer();
867 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
868 GTK_WIDGET(viewer->get_widget(viewer)));
870 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
875 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
877 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
880 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
882 gchar *content_type = NULL;
883 MimeViewer *viewer = NULL;
885 if ((partinfo->type == MIMETYPE_APPLICATION) &&
886 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
887 const gchar *filename;
889 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
890 if (filename == NULL)
891 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
892 if (filename != NULL)
893 content_type = procmime_get_mime_type(filename);
895 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
898 if (content_type != NULL) {
899 viewer = get_viewer_for_content_type(mimeview, content_type);
900 g_free(content_type);
906 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
910 if (mimeview->messageview->partial_display_shown) {
911 noticeview_hide(mimeview->messageview->noticeview);
912 mimeview->messageview->partial_display_shown = FALSE;
915 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
916 if (viewer == NULL) {
917 if (mimeview->mimeviewer != NULL)
918 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
919 mimeview->mimeviewer = NULL;
923 if (mimeview->mimeviewer != NULL)
924 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
926 if (mimeview->mimeviewer != viewer)
927 mimeview->mimeviewer = viewer;
929 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
930 viewer->mimeview = mimeview;
931 viewer->show_mimepart(viewer, mimeview->file, partinfo);
936 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
938 TextView *textview = mimeview->textview;
939 GtkWidget *focused = NULL;
941 if (mainwindow_get_mainwindow())
942 focused = gtkut_get_focused_child(
943 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
945 if ((mimeview->type != MIMEVIEW_VIEWER) &&
946 (mimeview->type == type)) return;
950 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
951 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
952 GTK_WIDGET_PTR(textview)));
954 case MIMEVIEW_VIEWER:
955 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
956 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
957 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
963 gtk_widget_grab_focus(focused);
964 mimeview->type = type;
967 void mimeview_clear(MimeView *mimeview)
974 if (g_slist_find(mimeviews, mimeview) == NULL)
977 noticeview_hide(mimeview->siginfoview);
979 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
980 gtk_tree_store_clear(GTK_TREE_STORE(model));
982 textview_clear(mimeview->textview);
983 if (mimeview->mimeviewer != NULL)
984 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
986 mimeview_free_mimeinfo(mimeview);
988 mimeview->mimeinfo = NULL;
990 gtk_tree_path_free(mimeview->opened);
991 mimeview->opened = NULL;
993 g_free(mimeview->file);
994 mimeview->file = NULL;
996 icon_list_clear(mimeview);
997 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1000 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
1001 GtkUIManager *ui_manager;
1003 if (messageview->window != NULL)
1004 ui_manager = messageview->ui_manager;
1006 ui_manager = messageview->mainwin->ui_manager;
1008 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
1011 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1012 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1014 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
1015 gboolean special, SignatureStatus code)
1017 gchar *text = NULL, *button_text = NULL;
1019 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1020 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1022 cm_return_if_fail(mimeview != NULL);
1023 cm_return_if_fail(mimeinfo != NULL);
1028 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1031 case SIGNATURE_UNCHECKED:
1032 button_text = _("Check signature");
1033 func = check_signature_cb;
1034 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1037 button_text = _("View full information");
1038 func = display_full_info_cb;
1039 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1041 case SIGNATURE_WARN:
1042 button_text = _("View full information");
1043 func = display_full_info_cb;
1044 icon = STOCK_PIXMAP_PRIVACY_WARN;
1046 case SIGNATURE_KEY_EXPIRED:
1047 button_text = _("View full information");
1048 func = display_full_info_cb;
1049 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1051 case SIGNATURE_INVALID:
1052 button_text = _("View full information");
1053 func = display_full_info_cb;
1054 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1056 case SIGNATURE_CHECK_FAILED:
1057 case SIGNATURE_CHECK_TIMEOUT:
1058 button_text = _("Check again");
1059 func = check_signature_cb;
1060 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1065 if (mycode == SIGNATURE_UNCHECKED) {
1066 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1067 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1069 if (*shortcut == '\0')
1070 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1072 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1076 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1077 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1078 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1079 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1081 if (*shortcut == '\0')
1082 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1084 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1088 noticeview_set_text(mimeview->siginfoview, text);
1089 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1092 noticeview_set_button_text(mimeview->siginfoview, NULL);
1093 noticeview_set_button_press_callback(
1094 mimeview->siginfoview,
1096 (gpointer) mimeview);
1097 noticeview_set_icon(mimeview->siginfoview, icon);
1098 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1100 icon_list_clear(mimeview);
1101 icon_list_create(mimeview, mimeview->mimeinfo);
1106 /* reset all thread stuff, and do the cleanups we've been left to do */
1107 static void mimeview_check_data_reset(MimeView *mimeview)
1110 gboolean must_destroy;
1112 if (!mimeview->check_data)
1115 must_free = mimeview->check_data->free_after_use;
1116 must_destroy = mimeview->check_data->destroy_mimeview;
1118 if (mimeview->check_data->cancel_th_init) {
1119 debug_print("killing canceller thread\n");
1120 mimeview->check_data->cancel_th_init = FALSE;
1121 pthread_cancel(mimeview->check_data->cancel_th);
1125 debug_print("freeing deferred mimeinfo\n");
1126 procmime_mimeinfo_free_all(mimeview->check_data->siginfo);
1129 g_free(mimeview->check_data);
1130 mimeview->check_data = NULL;
1133 debug_print("freeing deferred mimeview\n");
1134 mimeview_free_mimeinfo(mimeview);
1135 gtk_tree_path_free(mimeview->opened);
1136 g_free(mimeview->file);
1138 mimeviews = g_slist_remove(mimeviews, mimeview);
1142 /* GUI update once the checker thread is done or killed */
1143 static gboolean mimeview_check_sig_thread_cb(void *data)
1145 MimeView *mimeview = (MimeView *) data;
1146 MimeInfo *mimeinfo = mimeview->siginfo;
1148 debug_print("mimeview_check_sig_thread_cb\n");
1150 if (mimeinfo == NULL) {
1151 /* message changed !? */
1152 g_warning("no more siginfo!");
1156 if (!mimeview->check_data) {
1157 g_warning("nothing to check");
1161 if (mimeview->check_data->siginfo != mimeinfo) {
1162 /* message changed !? */
1163 g_warning("different siginfo!");
1167 if (mimeview->check_data->destroy_mimeview ||
1168 mimeview->check_data->free_after_use) {
1169 debug_print("not bothering, we're changing message\n");
1174 if (mimeview->check_data->timeout)
1175 update_signature_noticeview(mimeview, mimeview->siginfo,
1176 TRUE, SIGNATURE_CHECK_TIMEOUT);
1178 update_signature_noticeview(mimeview, mimeview->siginfo,
1182 mimeview_check_data_reset(mimeview);
1186 /* sig checker thread */
1187 static void *mimeview_check_sig_worker_thread(void *data)
1189 MimeView *mimeview = (MimeView *)data;
1190 MimeInfo *mimeinfo = mimeview->siginfo;
1192 debug_print("checking...\n");
1194 if (!mimeview->check_data)
1197 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1198 privacy_mimeinfo_check_signature(mimeinfo);
1199 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1200 mimeview->check_data->cancel_th_init = FALSE;
1201 pthread_cancel(mimeview->check_data->cancel_th);
1204 /* that's strange! we changed message without
1205 * getting killed. */
1206 g_warning("different siginfo!");
1207 mimeview_check_data_reset(mimeview);
1211 /* use g_timeout so that GUI updates is done from the
1213 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1218 /* killer thread - acts when the checker didn't work fast
1220 static void *mimeview_check_sig_cancel_thread(void *data)
1222 MimeView *mimeview = (MimeView *)data;
1224 if (!mimeview->check_data)
1225 return NULL; /* nothing to kill ! */
1227 /* wait for a few seconds... */
1228 debug_print("waiting a while\n");
1230 g_usleep(5 * 1000 * 1000);
1232 if (!mimeview->check_data)
1233 return NULL; /* nothing to kill, it's done in time :) */
1235 /* too late, go away checker thread */
1236 debug_print("killing checker thread\n");
1237 if (mimeview->check_data->th_init) {
1238 mimeview->check_data->th_init = FALSE;
1239 pthread_cancel(mimeview->check_data->th);
1242 /* tell upstream it was a timeout */
1243 mimeview->check_data->timeout = TRUE;
1244 /* use g_timeout so that GUI updates is done from the
1246 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1251 /* get rid of the checker thread right now - used when changing the
1252 * displayed message for example. */
1253 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1255 if (!mimeview->check_data)
1257 debug_print("killing checker thread NOW\n");
1258 if (mimeview->check_data->th_init) {
1259 mimeview->check_data->th_init = FALSE;
1260 pthread_cancel(mimeview->check_data->th);
1263 /* tell upstream it was a timeout */
1264 mimeview->check_data->timeout = TRUE;
1265 mimeview_check_sig_thread_cb(mimeview);
1269 /* creates a thread to check the signature, and a second one
1270 * to kill the first one after a timeout */
1271 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1274 pthread_attr_t detach, detach2;
1276 if (mimeview->check_data) {
1277 g_warning("already checking it");
1281 mimeview->check_data = g_new0(SigCheckData, 1);
1282 mimeview->check_data->siginfo = mimeview->siginfo;
1283 debug_print("creating thread\n");
1285 /* init thread attributes and create the checker thread */
1286 if (pthread_attr_init(&detach) != 0 ||
1287 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1288 pthread_attr_init(&detach2) != 0 ||
1289 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1290 pthread_create(&th, &detach,
1291 mimeview_check_sig_worker_thread,
1293 /* arh. We'll do it synchronously. */
1294 g_warning("can't create thread");
1295 g_free(mimeview->check_data);
1296 mimeview->check_data = NULL;
1299 mimeview->check_data->th = th;
1300 mimeview->check_data->th_init = TRUE;
1303 /* create the killer thread */
1304 pthread_create(&th2, &detach2,
1305 mimeview_check_sig_cancel_thread,
1308 mimeview->check_data->cancel_th = th2;
1309 mimeview->check_data->cancel_th_init = TRUE;
1313 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1315 MimeView *mimeview = (MimeView *) user_data;
1316 MimeInfo *mimeinfo = mimeview->siginfo;
1318 if (mimeinfo == NULL)
1321 if (mimeview->check_data)
1324 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1327 /* let's do it non-blocking */
1328 mimeview_check_sig_in_thread(mimeview);
1329 if (!mimeview->check_data) /* let's check syncronously */
1332 debug_print("checking without thread\n");
1333 privacy_mimeinfo_check_signature(mimeinfo);
1334 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1338 void mimeview_check_signature(MimeView *mimeview)
1340 check_signature_cb(NULL, mimeview);
1343 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1345 MimeView *mimeview = (MimeView *) user_data;
1346 gtk_tree_path_free(mimeview->opened);
1347 mimeview->opened = NULL;
1348 mimeview_selected(gtk_tree_view_get_selection(
1349 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1352 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1354 MimeView *mimeview = (MimeView *) user_data;
1357 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1358 textview_set_text(mimeview->textview, siginfo);
1360 noticeview_set_button_text(mimeview->siginfoview, NULL);
1361 noticeview_set_button_press_callback(
1362 mimeview->siginfoview,
1363 G_CALLBACK(redisplay_email),
1364 (gpointer) mimeview);
1365 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1368 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1371 MimeInfo *first_text;
1373 cm_return_if_fail(mimeview != NULL);
1374 cm_return_if_fail(selected != NULL);
1376 if (selected->type == MIMETYPE_MESSAGE
1377 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1378 /* if the first text part is signed, check that */
1379 first_text = selected;
1380 while (first_text && first_text->type != MIMETYPE_TEXT) {
1381 first_text = procmime_mimeinfo_next(first_text);
1384 update_signature_info(mimeview, first_text);
1390 while (siginfo != NULL) {
1391 if (privacy_mimeinfo_is_signed(siginfo))
1393 siginfo = procmime_mimeinfo_parent(siginfo);
1395 mimeview->siginfo = siginfo;
1397 /* This shortcut boolean is there to correctly set the menu's
1398 * CheckSignature item sensitivity without killing performance
1399 * each time the menu sensitiveness is updated (a lot).
1401 mimeview->signed_part = (siginfo != NULL);
1403 if (siginfo == NULL) {
1404 noticeview_hide(mimeview->siginfoview);
1408 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1409 noticeview_show(mimeview->siginfoview);
1412 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1414 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1415 GtkTreeModel *model;
1420 selection = gtk_tree_view_get_selection(ctree);
1421 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1424 path = gtk_tree_model_get_path(model, &iter);
1426 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1427 gtk_tree_path_free(path);
1431 gtk_tree_path_free(mimeview->opened);
1432 mimeview->opened = path;
1433 mimeview->spec_part = NULL;
1434 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1436 partinfo = mimeview_get_node_part(mimeview, path);
1437 if (!partinfo) return;
1439 /* ungrab the mouse event */
1440 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1441 gtk_grab_remove(GTK_WIDGET(ctree));
1442 if (gdk_pointer_is_grabbed())
1443 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1446 mimeview->textview->default_text = FALSE;
1448 update_signature_info(mimeview, partinfo);
1450 if (!mimeview_show_part(mimeview, partinfo)) {
1451 switch (partinfo->type) {
1453 case MIMETYPE_MESSAGE:
1454 case MIMETYPE_MULTIPART:
1455 mimeview_show_message_part(mimeview, partinfo);
1459 mimeview->textview->default_text = TRUE;
1460 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1461 textview_clear(mimeview->textview);
1462 textview_show_mime_part(mimeview->textview, partinfo);
1467 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1468 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1469 mimeview_check_signature(mimeview);
1473 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1476 GtkTreeSelection *selection;
1479 if (!event) return FALSE;
1481 if (event->button == 2 || event->button == 3) {
1482 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1483 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1486 gtk_tree_selection_select_iter(selection, &iter);
1488 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1493 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1496 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1497 if (event->direction == GDK_SCROLL_UP) {
1498 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1500 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1502 gtk_vscrollbutton_scroll(scrollbutton);
1506 /* from gdkevents.c */
1507 #define DOUBLE_CLICK_TIME 250
1509 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1512 static MimeInfo *lastinfo;
1513 static guint32 lasttime;
1515 if (event->button == 2 ||
1516 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1517 /* call external program for image, audio or html */
1518 mimeview_launch(mimeview, partinfo);
1520 } else if (event->button == 3) {
1521 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1522 partinfo->type == MIMETYPE_IMAGE ||
1523 partinfo->type == MIMETYPE_MULTIPART))
1524 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1526 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1529 partinfo->type == MIMETYPE_APPLICATION &&
1530 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1531 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1534 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1536 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1537 "pop_partinfo", partinfo);
1539 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1540 NULL, NULL, NULL, NULL,
1541 event->button, event->time);
1545 lastinfo = partinfo;
1546 lasttime = event->time;
1551 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1553 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1556 void mimeview_select_next_part(MimeView *mimeview)
1558 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1559 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1560 GtkTreeSelection *selection;
1563 MimeInfo *partinfo = NULL;
1566 if (!mimeview->opened) return;
1568 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1569 path = gtk_tree_model_get_path(model, &iter);
1571 has_next = mimeview_tree_next(model, path);
1573 has_next = gtk_tree_model_get_iter_first(model, &iter);
1574 gtk_tree_path_free(path);
1575 path = gtk_tree_model_get_path(model, &iter);
1579 partinfo = mimeview_get_node_part(mimeview, path);
1580 if (partinfo->type == MIMETYPE_MULTIPART ||
1581 (!prefs_common.show_inline_attachments && partinfo->id))
1583 selection = gtk_tree_view_get_selection(ctree);
1584 gtk_tree_selection_select_path(selection, path);
1585 icon_list_toggle_by_mime_info(mimeview, partinfo);
1588 gtk_tree_path_free(path);
1591 void mimeview_select_prev_part(MimeView *mimeview)
1593 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1594 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1595 GtkTreeSelection *selection;
1598 MimeInfo *partinfo = NULL;
1601 if (!mimeview->opened) return;
1603 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1604 path = gtk_tree_model_get_path(model, &iter);
1606 has_prev = mimeview_tree_prev(mimeview, path);
1608 while (mimeview_tree_next(model, path)) {}
1613 partinfo = mimeview_get_node_part(mimeview, path);
1614 if (partinfo->type == MIMETYPE_MULTIPART ||
1615 (!prefs_common.show_inline_attachments && partinfo->id))
1617 selection = gtk_tree_view_get_selection(ctree);
1618 gtk_tree_selection_select_path(selection, path);
1619 icon_list_toggle_by_mime_info(mimeview, partinfo);
1622 gtk_tree_path_free(path);
1625 #define BREAK_ON_MODIFIER_KEY() \
1626 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1628 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1631 SummaryView *summaryview;
1633 if (!event) return FALSE;
1634 if (!mimeview->opened) return FALSE;
1636 summaryview = mimeview->messageview->mainwin->summaryview;
1638 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1641 switch (event->keyval) {
1644 textview_scroll_max(mimeview->textview,
1645 (event->keyval == GDK_KEY_Home));
1647 case GDK_KEY_Page_Down:
1649 if (mimeview_scroll_page(mimeview,
1650 (event->state & GDK_SHIFT_MASK) != 0))
1653 if (!(event->state & GDK_SHIFT_MASK))
1654 mimeview_select_next_part(mimeview);
1656 case GDK_KEY_Page_Up:
1657 case GDK_KEY_BackSpace:
1658 mimeview_scroll_page(mimeview, TRUE);
1660 case GDK_KEY_Return:
1661 case GDK_KEY_KP_Enter:
1662 mimeview_scroll_one_line(mimeview,
1663 (event->state & GDK_MOD1_MASK) != 0);
1667 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1673 if (mimeview->messageview->new_window) return FALSE;
1675 return summary_pass_key_press_event(summaryview, event);
1678 static void mimeview_drag_data_get(GtkWidget *widget,
1679 GdkDragContext *drag_context,
1680 GtkSelectionData *selection_data,
1685 gchar *filename = NULL, *uriname, *tmp;
1690 if (!mimeview->opened) return;
1691 if (!mimeview->file) return;
1693 partinfo = mimeview_get_selected_part(mimeview);
1694 if (!partinfo) return;
1696 if (strlen(get_part_name(partinfo)) > 0) {
1697 filename = g_path_get_basename(get_part_name(partinfo));
1698 if (*filename == '\0') return;
1699 } else if (partinfo->type == MIMETYPE_MESSAGE
1700 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1702 GPtrArray *headers = NULL;
1705 fp = g_fopen(partinfo->data.filename, "rb");
1706 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1707 headers = procheader_get_header_array_asis(fp);
1710 for (i = 0; i < headers->len; i++) {
1711 Header *header = g_ptr_array_index(headers, i);
1712 if (procheader_headername_equal(header->name, "Subject")) {
1713 unfold_line(header->body);
1714 name = g_strconcat(header->body, ".txt", NULL);
1715 subst_for_filename(name);
1718 procheader_header_array_destroy(headers);
1724 filename = g_path_get_basename(name);
1727 if (filename == NULL)
1728 filename = g_path_get_basename("Unnamed part");
1730 if (!g_utf8_validate(filename, -1, NULL))
1731 tmp = conv_codeset_strdup(filename,
1732 conv_get_locale_charset_str(),
1735 tmp = g_strdup(filename);
1738 g_warning("filename not in UTF-8");
1739 tmp = g_strdup("Unnamed part");
1741 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1745 if (is_file_exist(filename)) {
1747 gchar *prefix = NULL;
1748 gchar *new_name = NULL;
1749 if (strrchr(tmp, '.')) {
1750 prefix = g_strdup(tmp);
1751 ext = g_strdup(strrchr(tmp, '.'));
1752 *(strrchr(prefix, '.')) = '\0';
1754 prefix = g_strdup(tmp);
1758 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1762 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1765 goto check_new_file;
1770 if ((err = procmime_get_part(filename, partinfo)) < 0)
1772 (_("Couldn't save the part of multipart message: %s"),
1775 tmp = g_filename_to_uri(filename, NULL, NULL);
1776 uriname = g_strconcat(tmp, "\r\n", NULL);
1779 gtk_selection_data_set(selection_data,
1780 gtk_selection_data_get_target(selection_data), 8,
1781 (guchar *)uriname, strlen(uriname));
1788 * Returns a filename (with path) for an attachment
1789 * \param partinfo The attachment to save
1790 * \param basedir The target directory
1791 * \param number Used for dummy filename if attachment is unnamed
1793 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1794 const gchar *basedir,
1800 filename = g_strdup(get_part_name(partinfo));
1801 if (!filename || !*filename) {
1803 filename = g_strdup_printf("noname.%d", number);
1806 if (!g_utf8_validate(filename, -1, NULL)) {
1807 gchar *tmp = conv_filename_to_utf8(filename);
1812 subst_for_filename(filename);
1814 fullname = g_strconcat
1815 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1816 ? &filename[1] : filename, NULL);
1819 filename = conv_filename_from_utf8(fullname);
1825 * Write a single attachment to file
1826 * \param filename Filename with path
1827 * \param partinfo Attachment to save
1829 static gboolean mimeview_write_part(const gchar *filename,
1835 dir= g_path_get_dirname(filename);
1836 if (!is_dir_exist(dir))
1840 if (is_file_exist(filename)) {
1845 if (!g_utf8_validate(filename, -1, NULL))
1846 tmp = conv_filename_to_utf8(filename);
1848 tmp = g_strdup(filename);
1850 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1853 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
1854 GTK_STOCK_OK, NULL);
1856 if (G_ALERTALTERNATE != aval) return FALSE;
1859 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1861 (_("Couldn't save the part of multipart message: %s"),
1870 * Menu callback: Save all attached files
1871 * \param mimeview Current display
1873 static void mimeview_save_all(MimeView *mimeview)
1877 gchar *startdir = NULL;
1880 if (!mimeview->opened) return;
1881 if (!mimeview->file) return;
1882 if (!mimeview->mimeinfo) return;
1884 partinfo = mimeview->mimeinfo;
1885 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1886 startdir = g_strconcat(prefs_common.attach_save_dir,
1887 G_DIR_SEPARATOR_S, NULL);
1889 startdir = g_strdup(get_home_dir());
1891 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1892 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1898 if (!is_dir_exist (dirname)) {
1899 alertpanel_error(_("'%s' is not a directory."),
1906 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1907 dirname[strlen(dirname)-1] = '\0';
1909 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1910 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1911 partinfo = procmime_mimeinfo_next(partinfo);
1912 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1913 partinfo = procmime_mimeinfo_next(partinfo);
1914 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1915 partinfo = procmime_mimeinfo_next(partinfo);
1918 while (partinfo != NULL) {
1919 if (partinfo->type != MIMETYPE_MESSAGE &&
1920 partinfo->type != MIMETYPE_MULTIPART &&
1921 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1922 || get_real_part_name(partinfo) != NULL)) {
1923 gchar *filename = mimeview_get_filename_for_part
1924 (partinfo, dirname, number++);
1926 mimeview_write_part(filename, partinfo);
1929 partinfo = procmime_mimeinfo_next(partinfo);
1932 g_free(prefs_common.attach_save_dir);
1934 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1935 -1, NULL, NULL, NULL);
1939 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1941 MimeInfo *partinfo = NULL;
1942 if (mimeview->spec_part) {
1943 partinfo = mimeview->spec_part;
1944 mimeview->spec_part = NULL;
1946 partinfo = (MimeInfo *) g_object_get_data
1947 (G_OBJECT(mimeview->popupmenu),
1949 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1950 "pop_partinfo", NULL);
1952 partinfo = mimeview_get_selected_part(mimeview);
1959 * Menu callback: Save the selected attachment
1960 * \param mimeview Current display
1962 void mimeview_save_as(MimeView *mimeview)
1965 gchar *filepath = NULL;
1966 gchar *filedir = NULL;
1968 gchar *partname = NULL;
1970 if (!mimeview->opened) return;
1971 if (!mimeview->file) return;
1973 partinfo = mimeview_get_part_to_use(mimeview);
1975 cm_return_if_fail(partinfo != NULL);
1977 if (get_part_name(partinfo) == NULL) {
1980 partname = g_strdup(get_part_name(partinfo));
1982 if (!g_utf8_validate(partname, -1, NULL)) {
1983 gchar *tmp = conv_filename_to_utf8(partname);
1985 tmp = conv_codeset_strdup(partname,
1986 conv_get_locale_charset_str(),
1995 subst_for_filename(partname);
1997 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1998 filepath = g_strconcat(prefs_common.attach_save_dir,
1999 G_DIR_SEPARATOR_S, partname, NULL);
2001 filepath = g_strdup(partname);
2005 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2006 filename = filesel_select_file_save(_("Save as"), filepath);
2012 mimeview_write_part(filename, partinfo);
2014 filedir = g_path_get_dirname(filename);
2015 if (filedir && strcmp(filedir, ".")) {
2016 g_free(prefs_common.attach_save_dir);
2017 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2024 void mimeview_display_as_text(MimeView *mimeview)
2028 if (!mimeview->opened) return;
2030 partinfo = mimeview_get_part_to_use(mimeview);
2031 mimeview_select_mimepart_icon(mimeview, partinfo);
2032 cm_return_if_fail(partinfo != NULL);
2033 mimeview_show_message_part(mimeview, partinfo);
2036 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2041 if (!mimeview->opened) return;
2042 if (!mimeview->file) return;
2045 partinfo = mimeview_get_part_to_use(mimeview);
2047 cm_return_if_fail(partinfo != NULL);
2049 filename = procmime_get_tmp_file_name(partinfo);
2051 if ((err = procmime_get_part(filename, partinfo)) < 0)
2053 (_("Couldn't save the part of multipart message: %s"),
2056 mimeview_view_file(filename, partinfo, NULL, mimeview);
2062 void mimeview_open_with(MimeView *mimeview)
2066 if (!mimeview) return;
2067 if (!mimeview->opened) return;
2068 if (!mimeview->file) return;
2070 partinfo = mimeview_get_part_to_use(mimeview);
2072 mimeview_open_part_with(mimeview, partinfo, FALSE);
2075 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2079 gchar *mime_command = NULL;
2080 gchar *content_type = NULL;
2083 cm_return_if_fail(partinfo != NULL);
2085 filename = procmime_get_tmp_file_name(partinfo);
2087 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2089 (_("Couldn't save the part of multipart message: %s"),
2095 if (!prefs_common.mime_open_cmd_history)
2096 prefs_common.mime_open_cmd_history =
2097 add_history(NULL, prefs_common.mime_open_cmd);
2099 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2100 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2101 /* guess content-type from filename */
2102 content_type = procmime_get_mime_type(filename);
2104 if (content_type == NULL) {
2105 content_type = procmime_get_content_type_str(partinfo->type,
2109 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2110 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2111 mime_command = g_strdup(prefs_common_get_uri_cmd());
2112 g_free(content_type);
2113 content_type = NULL;
2114 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2115 || !prefs_common_get_ext_editor_cmd()[0]) {
2116 mime_command = mailcap_get_command_for_type(content_type, filename);
2118 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2119 g_free(content_type);
2120 content_type = NULL;
2122 if (mime_command == NULL) {
2123 /* try with extension this time */
2124 g_free(content_type);
2125 content_type = procmime_get_mime_type(filename);
2126 mime_command = mailcap_get_command_for_type(content_type, filename);
2129 if (mime_command == NULL)
2133 gboolean remember = FALSE;
2134 if (content_type != NULL)
2135 cmd = input_dialog_combo_remember
2137 _("Enter the command-line to open file:\n"
2138 "('%s' will be replaced with file name)"),
2139 mime_command ? mime_command : prefs_common.mime_open_cmd,
2140 prefs_common.mime_open_cmd_history, &remember);
2142 cmd = input_dialog_combo
2144 _("Enter the command-line to open file:\n"
2145 "('%s' will be replaced with file name)"),
2146 mime_command ? mime_command : prefs_common.mime_open_cmd,
2147 prefs_common.mime_open_cmd_history);
2148 if (cmd && remember) {
2149 mailcap_update_default(content_type, cmd);
2151 g_free(mime_command);
2156 mimeview_view_file(filename, partinfo, cmd, mimeview);
2157 g_free(prefs_common.mime_open_cmd);
2158 prefs_common.mime_open_cmd = cmd;
2159 prefs_common.mime_open_cmd_history =
2160 add_history(prefs_common.mime_open_cmd_history, cmd);
2163 g_free(content_type);
2168 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2170 GList *attach_file = NULL;
2171 AttachInfo *ainfo = NULL;
2175 if (!mimeview->opened) return;
2176 if (!mimeview->file) return;
2178 cm_return_if_fail(partinfo != NULL);
2180 filename = procmime_get_tmp_file_name(partinfo);
2182 if (!(err = procmime_get_part(filename, partinfo))) {
2183 ainfo = g_new0(AttachInfo, 1);
2184 ainfo->file = filename;
2185 ainfo->name = g_strdup(get_part_name(partinfo));
2186 ainfo->content_type = procmime_get_content_type_str(
2187 partinfo->type, partinfo->subtype);
2188 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2189 partinfo, "charset"));
2190 attach_file = g_list_append(attach_file, ainfo);
2192 compose_new(NULL, NULL, attach_file);
2194 g_free(ainfo->name);
2195 g_free(ainfo->content_type);
2196 g_free(ainfo->charset);
2198 g_list_free(attach_file);
2201 (_("Couldn't save the part of multipart message: %s"),
2206 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2207 const gchar *cmd, MimeView *mimeview)
2211 gchar buf[BUFFSIZE];
2214 mimeview_open_part_with(mimeview, partinfo, TRUE);
2216 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2217 !strchr(p + 2, '%')) {
2218 g_snprintf(buf, sizeof(buf), cmd, filename);
2219 if (!prefs_common.save_parts_readwrite)
2220 g_chmod(filename, S_IRUSR);
2222 g_chmod(filename, S_IRUSR|S_IWUSR);
2224 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2225 mimeview_open_part_with(mimeview, partinfo, FALSE);
2227 if (execute_command_line(buf, TRUE) != 0) {
2228 if (!prefs_common.save_parts_readwrite)
2229 g_chmod(filename, S_IRUSR|S_IWUSR);
2230 mimeview_open_part_with(mimeview, partinfo, FALSE);
2234 SHFILEINFO file_info;
2235 GError *error = NULL;
2236 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2238 if (error != NULL) {
2239 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2241 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2242 g_error_free(error);
2246 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2247 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2248 _("This attachment is an executable file. Executing "
2249 "untrusted binaries is dangerous and could probably "
2250 "lead to compromission of your computer.\n\n"
2251 "Do you want to run this file?"), GTK_STOCK_CANCEL,
2253 NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
2254 if (val == G_ALERTALTERNATE) {
2255 debug_print("executing binary\n");
2256 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2259 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2267 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2269 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2272 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2274 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2277 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2279 GSList *mimeview_list, *viewer_list;
2281 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2282 MimeView *mimeview = (MimeView *) mimeview_list->data;
2284 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2285 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2286 mimeview->mimeviewer = NULL;
2289 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2290 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2292 mimeviewer->destroy_viewer(mimeviewer);
2293 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2297 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2300 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2305 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2306 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2308 if (event->button == 1) {
2309 icon_selected(mimeview, num, partinfo);
2310 gtk_widget_grab_focus(button);
2311 icon_list_toggle_by_mime_info(mimeview, partinfo);
2313 part_button_pressed(mimeview, event, partinfo);
2318 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2320 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2323 MimeInfo *curr = NULL;
2325 gtk_tree_model_get_iter_first(model, &iter);
2326 path = gtk_tree_model_get_path(model, &iter);
2329 gtk_tree_model_get_iter(model, &iter, path);
2330 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2331 if (curr == partinfo) {
2332 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2333 GTK_TREE_VIEW(mimeview->ctree));
2334 gtk_tree_selection_select_iter(sel, &iter);
2335 gtk_tree_path_free(path);
2338 } while (mimeview_tree_next(model, path));
2340 gtk_tree_path_free(path);
2343 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2345 icon_list_toggle_by_mime_info(mimeview, partinfo);
2346 icon_selected(mimeview, -1, partinfo);
2349 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2352 SummaryView *summaryview;
2354 if (!event) return FALSE;
2356 switch (event->keyval) {
2358 if (mimeview_scroll_page(mimeview, FALSE))
2361 mimeview_select_next_part(mimeview);
2365 case GDK_KEY_BackSpace:
2366 mimeview_scroll_page(mimeview, TRUE);
2368 case GDK_KEY_Return:
2369 case GDK_KEY_KP_Enter:
2370 mimeview_scroll_one_line(mimeview,
2371 (event->state & GDK_MOD1_MASK) != 0);
2374 BREAK_ON_MODIFIER_KEY();
2375 mimeview_save_as(mimeview);
2378 BREAK_ON_MODIFIER_KEY();
2379 mimeview_display_as_text(mimeview);
2382 BREAK_ON_MODIFIER_KEY();
2383 mimeview_launch(mimeview, NULL);
2387 BREAK_ON_MODIFIER_KEY();
2388 mimeview_open_with(mimeview);
2392 BREAK_ON_MODIFIER_KEY();
2393 mimeview_check_signature(mimeview);
2396 BREAK_ON_MODIFIER_KEY();
2397 mimeview_select_next_part(mimeview);
2403 if (!mimeview->messageview->mainwin) return FALSE;
2404 summaryview = mimeview->messageview->mainwin->summaryview;
2405 return summary_pass_key_press_event(summaryview, event);
2408 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2410 MimeView *mimeview = (MimeView *)data;
2411 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2413 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2414 "pop_partinfo", partinfo);
2415 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
2416 NULL, NULL, NULL, NULL,
2417 0, gtk_get_current_event_time());
2421 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2423 GtkWidget *pixmap = NULL;
2428 const gchar *desc = NULL;
2429 gchar *sigshort = NULL;
2430 gchar *content_type;
2433 MimeInfo *siginfo = NULL;
2434 MimeInfo *encrypted = NULL;
2439 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2442 vbox = mimeview->icon_vbox;
2443 mimeview->icon_count++;
2444 button = gtk_event_box_new();
2446 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2447 G_CALLBACK(mimeview_visi_notify), mimeview);
2448 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2449 G_CALLBACK(mimeview_leave_notify), mimeview);
2450 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2451 G_CALLBACK(mimeview_enter_notify), mimeview);
2453 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2454 g_object_set_data(G_OBJECT(button), "icon_number",
2455 GINT_TO_POINTER(mimeview->icon_count));
2456 g_object_set_data(G_OBJECT(button), "partinfo",
2459 switch (mimeinfo->type) {
2462 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2463 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2464 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2465 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2466 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2467 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2468 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2469 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2470 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2472 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2474 case MIMETYPE_MESSAGE:
2475 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2477 case MIMETYPE_APPLICATION:
2478 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2479 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2480 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2481 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2482 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2483 stockp = STOCK_PIXMAP_MIME_PDF;
2484 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2485 stockp = STOCK_PIXMAP_MIME_PS;
2487 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2489 case MIMETYPE_IMAGE:
2490 stockp = STOCK_PIXMAP_MIME_IMAGE;
2492 case MIMETYPE_AUDIO:
2493 stockp = STOCK_PIXMAP_MIME_AUDIO;
2496 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2500 partinfo = mimeinfo;
2501 while (partinfo != NULL) {
2502 if (privacy_mimeinfo_is_signed(partinfo)) {
2506 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2507 encrypted = partinfo;
2510 partinfo = procmime_mimeinfo_parent(partinfo);
2513 if (siginfo != NULL) {
2514 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2515 case SIGNATURE_UNCHECKED:
2516 case SIGNATURE_CHECK_FAILED:
2517 case SIGNATURE_CHECK_TIMEOUT:
2518 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2519 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2522 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2523 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2525 case SIGNATURE_WARN:
2526 case SIGNATURE_KEY_EXPIRED:
2527 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2528 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2530 case SIGNATURE_INVALID:
2531 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2532 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2535 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2536 } else if (encrypted != NULL) {
2537 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2538 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2540 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
2541 OVERLAY_NONE, 6, 3);
2543 gtk_container_add(GTK_CONTAINER(button), pixmap);
2545 if (prefs_common.attach_desc)
2546 desc = get_part_description(mimeinfo);
2548 desc = get_part_name(mimeinfo);
2551 content_type = procmime_get_content_type_str(mimeinfo->type,
2554 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2555 "\n<b>", _("Size:"), " </b>",
2556 to_human_readable((goffset)mimeinfo->length), NULL);
2557 g_free(content_type);
2558 if (desc && *desc) {
2559 gchar *tmp = NULL, *escaped = NULL;
2560 if (!g_utf8_validate(desc, -1, NULL)) {
2561 tmp = conv_filename_to_utf8(desc);
2563 tmp = g_strdup(desc);
2565 escaped = g_markup_escape_text(tmp,-1);
2567 tiptmp = g_strconcat(tip, "\n<b>",
2568 prefs_common.attach_desc && mimeinfo->description ?
2569 _("Description:") : _("Filename:"),
2570 " </b>", escaped, NULL);
2576 if (sigshort && *sigshort) {
2577 tiptmp = g_strjoin("\n", tip, g_markup_escape_text(sigshort, -1), NULL);
2583 gtk_widget_set_tooltip_markup(button, tip);
2585 gtk_widget_show_all(button);
2586 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2587 mimeview_mime_types, 1, GDK_ACTION_COPY);
2589 g_signal_connect(G_OBJECT(button), "popup-menu",
2590 G_CALLBACK(icon_popup_menu), mimeview);
2591 g_signal_connect(G_OBJECT(button), "button_release_event",
2592 G_CALLBACK(icon_clicked_cb), mimeview);
2593 g_signal_connect(G_OBJECT(button), "key_press_event",
2594 G_CALLBACK(icon_key_pressed), mimeview);
2595 g_signal_connect(G_OBJECT(button), "drag_data_get",
2596 G_CALLBACK(mimeview_drag_data_get), mimeview);
2597 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
2599 gtk_widget_size_request(pixmap, &r);
2600 gtk_widget_set_size_request(button, -1, r.height + 4);
2605 static void icon_list_clear (MimeView *mimeview)
2607 GList *child, *orig;
2610 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2611 for (child = orig; child != NULL; child = g_list_next(child)) {
2612 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
2613 GTK_WIDGET(child->data));
2616 mimeview->icon_count = 0;
2617 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2618 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2619 gtk_adjustment_changed(adj);
2623 *\brief Used to 'click' the next or previous icon.
2625 *\return true if the icon 'number' exists and was selected.
2627 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2628 GtkAllocation *size, MimeView *mimeview)
2630 GtkAllocation vbox_size;
2631 GtkAllocation layout_size;
2636 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
2638 gtk_widget_get_allocation(mimeview->icon_vbox, &vbox_size);
2639 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2641 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2642 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2644 MAX(vbox_size.height, layout_size.height));
2645 gtk_adjustment_set_step_increment(adj, 10);
2648 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2650 GtkRequisition size;
2651 GtkRequisition requisition;
2653 cm_return_if_fail(mimeinfo != NULL);
2655 while (mimeinfo != NULL) {
2656 if (mimeinfo->type != MIMETYPE_MULTIPART)
2657 icon_list_append_icon(mimeview, mimeinfo);
2658 if (mimeinfo->node->children != NULL)
2659 icon_list_create(mimeview,
2660 (MimeInfo *) mimeinfo->node->children->data);
2661 mimeinfo = mimeinfo->node->next != NULL
2662 ? (MimeInfo *) mimeinfo->node->next->data
2665 gtk_widget_size_request(mimeview->icon_vbox, &size);
2666 gtk_widget_get_requisition(mimeview->icon_mainbox, &requisition);
2667 if (size.width > requisition.width) {
2668 gtk_widget_set_size_request(mimeview->icon_mainbox,
2671 if (mimeview->opened)
2672 icon_list_toggle_by_mime_info(mimeview,
2673 mimeview_get_node_part(mimeview, mimeview->opened));
2676 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2679 GList *children, *child;
2681 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2682 for (child = children; child != NULL; child = g_list_next(child)) {
2683 gboolean *highlight = NULL;
2684 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2686 if (!GTK_IS_EVENT_BOX(child->data))
2689 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2690 *highlight = (g_object_get_data(G_OBJECT(child->data),
2691 "partinfo") == (gpointer)mimeinfo);
2693 gtk_widget_queue_draw(icon);
2695 g_list_free(children);
2698 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2701 prefs_common.mimeview_tree_height = allocation->height;
2704 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2707 g_object_ref(button);
2709 mimeview_leave_notify(button, NULL, NULL);
2711 mimeview->ctree_mode = !mimeview->ctree_mode;
2712 if (mimeview->ctree_mode) {
2713 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2715 gtk_widget_hide(mimeview->icon_mainbox);
2716 gtk_widget_show(mimeview->ctree_mainbox);
2717 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2718 prefs_common.mimeview_tree_height);
2720 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2722 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2723 button, FALSE, FALSE, 0);
2725 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2727 gtk_widget_hide(mimeview->ctree_mainbox);
2728 gtk_widget_show(mimeview->icon_mainbox);
2729 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2731 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2733 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2734 button, FALSE, FALSE, 0);
2735 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2736 if (mimeview->opened)
2737 icon_list_toggle_by_mime_info(mimeview,
2738 mimeview_get_node_part(mimeview, mimeview->opened));
2739 summary_grab_focus(mimeview->mainwin->summaryview);
2741 g_object_unref(button);
2745 void mimeview_update (MimeView *mimeview)
2747 if (mimeview && mimeview->mimeinfo) {
2748 icon_list_clear(mimeview);
2749 icon_list_create(mimeview, mimeview->mimeinfo);
2753 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2755 MessageView *msgview = NULL;
2756 MainWindow *mainwin = NULL;
2761 msgview = mimeview->messageview;
2765 mainwin = msgview->mainwin;
2769 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2770 "pop_partinfo", NULL);
2772 if (!strcmp(cmd, "sc://view_log"))
2773 log_window_show(mainwin->logwin);
2774 else if (!strcmp(cmd, "sc://save_as"))
2775 mimeview_save_as(mimeview);
2776 else if (!strcmp(cmd, "sc://display_as_text"))
2777 mimeview_display_as_text(mimeview);
2779 else if (!strcmp(cmd, "sc://open_with"))
2780 mimeview_open_with(mimeview);
2782 else if (!strcmp(cmd, "sc://open"))
2783 mimeview_launch(mimeview, NULL);
2784 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2785 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2786 icon_selected(mimeview, -1, (MimeInfo *)data);
2787 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2788 mimeview_launch(mimeview, (MimeInfo *)data);
2789 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2790 mimeview->spec_part = (MimeInfo *)data;
2791 part_button_pressed(mimeview, event, (MimeInfo *)data);
2792 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2793 const gchar *tagname = cmd + strlen("sc://search_tags:");
2794 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2795 gtk_toggle_button_set_active(
2796 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2798 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2799 ADVANCED_SEARCH_EXTENDED, buf);
2804 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2806 if (mimeview->type == MIMEVIEW_TEXT)
2807 return textview_scroll_page(mimeview->textview, up);
2808 else if (mimeview->mimeviewer) {
2809 MimeViewer *mimeviewer = mimeview->mimeviewer;
2810 if (mimeviewer->scroll_page)
2811 return mimeviewer->scroll_page(mimeviewer, up);
2816 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2818 if (mimeview->type == MIMEVIEW_TEXT)
2819 textview_scroll_one_line(mimeview->textview, up);
2820 else if (mimeview->mimeviewer) {
2821 MimeViewer *mimeviewer = mimeview->mimeviewer;
2822 if (mimeviewer->scroll_one_line)
2823 mimeviewer->scroll_one_line(mimeviewer, up);