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 mimeview->mime_toggle = mime_toggle;
462 mimeview->siginfoview = siginfoview;
463 mimeview->scrollbutton = scrollbutton;
464 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
466 mimeviews = g_slist_prepend(mimeviews, mimeview);
471 void mimeview_init(MimeView *mimeview)
473 textview_init(mimeview->textview);
475 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
476 GTK_WIDGET_PTR(mimeview->textview));
479 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
482 if (privacy_mimeinfo_is_signed(mimeinfo))
484 mimeinfo = procmime_mimeinfo_next(mimeinfo);
490 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
493 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
495 mimeview_clear(mimeview);
497 cm_return_if_fail(file != NULL);
498 cm_return_if_fail(mimeinfo != NULL);
500 mimeview->mimeinfo = mimeinfo;
502 mimeview->file = g_strdup(file);
504 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
507 /* check if the mail's signed - it can change the mail structure */
509 if (any_part_is_signed(mimeinfo))
510 debug_print("signed mail\n");
512 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
513 gtk_tree_view_expand_all(ctree);
514 icon_list_clear(mimeview);
515 icon_list_create(mimeview, mimeinfo);
517 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
518 mimeview_selected, mimeview);
522 static void mimeview_check_sig_cancel_now(MimeView *mimeview);
525 static void mimeview_free_mimeinfo(MimeView *mimeview)
527 gboolean defer = FALSE;
529 defer = (mimeview->check_data != NULL);
531 mimeview->check_data->free_after_use = TRUE;
533 if (mimeview->mimeinfo != NULL && !defer) {
534 procmime_mimeinfo_free_all(mimeview->mimeinfo);
535 mimeview->mimeinfo = NULL;
538 debug_print("deferring free(mimeinfo) and cancelling check\n");
539 mimeview_check_sig_cancel_now(mimeview);
544 void mimeview_destroy(MimeView *mimeview)
548 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
549 MimeViewer *viewer = (MimeViewer *) cur->data;
550 viewer->destroy_viewer(viewer);
552 g_slist_free(mimeview->viewers);
553 gtk_target_list_unref(mimeview->target_list);
556 if (mimeview->check_data) {
557 mimeview->check_data->destroy_mimeview = TRUE;
558 debug_print("deferring destroy\n");
562 mimeview_free_mimeinfo(mimeview);
563 gtk_tree_path_free(mimeview->opened);
564 g_free(mimeview->file);
566 mimeviews = g_slist_remove(mimeviews, mimeview);
571 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
573 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
575 GtkTreeSelection *selection;
576 MimeInfo *partinfo = NULL;
578 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
579 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
580 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
585 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
587 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
591 gtk_tree_model_get_iter(model, &iter, path);
592 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
596 gboolean mimeview_tree_is_empty(MimeView *mimeview)
598 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
600 return !gtk_tree_model_get_iter_first(model, &iter);
603 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
605 GtkTreeIter iter, parent;
608 gtk_tree_model_get_iter(model, &iter, path);
610 if (gtk_tree_model_iter_has_child(model, &iter)) {
611 gtk_tree_path_down(path);
615 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
617 if (!gtk_tree_model_iter_next(model, &iter)) {
619 GtkTreeIter saved_parent = parent;
620 gtk_tree_path_up(path);
621 if (gtk_tree_model_iter_next(model, &parent)) {
622 gtk_tree_path_next(path);
625 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
630 gtk_tree_path_next(path);
637 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
639 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
640 GtkTreeIter iter, child, parent;
643 gtk_tree_model_get_iter(model, &iter, path);
644 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
646 if (gtk_tree_path_prev(path)) {
647 gtk_tree_model_get_iter(model, &iter, path);
649 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
650 gtk_tree_path_down(path);
652 while (gtk_tree_model_iter_next(model, &child))
653 gtk_tree_path_next(path);
660 gtk_tree_path_up(path);
667 gint mimeview_get_selected_part_num(MimeView *mimeview)
669 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
674 gtk_tree_model_get_iter_first(model, &iter);
675 path = gtk_tree_model_get_path(model, &iter);
678 if (!gtk_tree_path_compare(mimeview->opened, path)) {
679 gtk_tree_path_free(path);
684 } while (mimeview_tree_next(model, path));
686 gtk_tree_path_free(path);
691 void mimeview_select_part_num(MimeView *mimeview, gint i)
693 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
696 GtkTreeSelection *selection;
702 gtk_tree_model_get_iter_first(model, &iter);
703 path = gtk_tree_model_get_path(model, &iter);
706 if (!mimeview_tree_next(model, path)) {
707 gtk_tree_path_free(path);
713 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
714 gtk_tree_selection_select_path(selection, path);
715 gtk_tree_path_free(path);
718 static void mimeview_set_multipart_tree(MimeView *mimeview,
722 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
723 GTK_TREE_VIEW(mimeview->ctree)));
725 gchar *content_type, *length, *name;
727 cm_return_if_fail(mimeinfo != NULL);
729 while (mimeinfo != NULL) {
730 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
731 content_type = g_strdup_printf("%s/%s",
732 procmime_get_media_type_str(mimeinfo->type),
735 content_type = g_strdup("UNKNOWN");
737 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
739 if (prefs_common.attach_desc)
740 name = g_strdup(get_part_description(mimeinfo));
742 name = g_strdup(get_part_name(mimeinfo));
744 gtk_tree_store_append(model, &iter, parent);
745 gtk_tree_store_set(model, &iter,
746 COL_MIMETYPE, content_type,
749 COL_DATA, mimeinfo, -1);
750 g_free(content_type);
754 if (mimeinfo->node->children)
755 mimeview_set_multipart_tree(mimeview,
756 (MimeInfo *) mimeinfo->node->children->data, &iter);
757 mimeinfo = mimeinfo->node->next != NULL ?
758 (MimeInfo *) mimeinfo->node->next->data : NULL;
762 static const gchar *get_real_part_name(MimeInfo *partinfo)
764 const gchar *name = NULL;
766 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
768 name = procmime_mimeinfo_get_parameter(partinfo, "name");
773 static const gchar *get_part_name(MimeInfo *partinfo)
777 name = get_real_part_name(partinfo);
784 static const gchar *get_part_description(MimeInfo *partinfo)
786 if (partinfo->description)
787 return partinfo->description;
789 return get_part_name(partinfo);
792 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
797 if (!partinfo) return;
799 fname = mimeview->file;
802 if ((fp = g_fopen(fname, "rb")) == NULL) {
803 FILE_OP_ERROR(fname, "fopen");
807 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
808 FILE_OP_ERROR(mimeview->file, "fseek");
813 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
814 textview_show_part(mimeview->textview, partinfo, fp);
819 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
822 MimeViewerFactory *factory = NULL;
823 MimeViewer *viewer = NULL;
824 gchar *real_contenttype = NULL;
827 * FNM_CASEFOLD is a GNU extension
830 #define FNM_CASEFOLD 0
831 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
833 real_contenttype = g_strdup(content_type);
836 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
837 MimeViewerFactory *curfactory = cur->data;
840 while (curfactory->content_types[i] != NULL) {
841 if(!fnmatch(curfactory->content_types[i], real_contenttype, FNM_CASEFOLD)) {
842 debug_print("%s\n", curfactory->content_types[i]);
843 factory = curfactory;
851 g_free(real_contenttype);
855 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
856 MimeViewer *curviewer = cur->data;
858 if (curviewer->factory == factory)
861 viewer = factory->create_viewer();
862 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
863 GTK_WIDGET(viewer->get_widget(viewer)));
865 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
870 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
872 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
875 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
877 gchar *content_type = NULL;
878 MimeViewer *viewer = NULL;
880 if ((partinfo->type == MIMETYPE_APPLICATION) &&
881 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
882 const gchar *filename;
884 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
885 if (filename == NULL)
886 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
887 if (filename != NULL)
888 content_type = procmime_get_mime_type(filename);
890 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
893 if (content_type != NULL) {
894 viewer = get_viewer_for_content_type(mimeview, content_type);
895 g_free(content_type);
901 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
905 if (mimeview->messageview->partial_display_shown) {
906 noticeview_hide(mimeview->messageview->noticeview);
907 mimeview->messageview->partial_display_shown = FALSE;
910 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
911 if (viewer == NULL) {
912 if (mimeview->mimeviewer != NULL)
913 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
914 mimeview->mimeviewer = NULL;
918 if (mimeview->mimeviewer != NULL)
919 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
921 if (mimeview->mimeviewer != viewer)
922 mimeview->mimeviewer = viewer;
924 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
925 viewer->mimeview = mimeview;
926 viewer->show_mimepart(viewer, mimeview->file, partinfo);
931 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
933 TextView *textview = mimeview->textview;
934 GtkWidget *focused = NULL;
936 if (mainwindow_get_mainwindow())
937 focused = gtkut_get_focused_child(
938 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
940 if ((mimeview->type != MIMEVIEW_VIEWER) &&
941 (mimeview->type == type)) return;
945 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
946 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
947 GTK_WIDGET_PTR(textview)));
949 case MIMEVIEW_VIEWER:
950 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
951 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
952 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
958 gtk_widget_grab_focus(focused);
959 mimeview->type = type;
962 void mimeview_clear(MimeView *mimeview)
969 if (g_slist_find(mimeviews, mimeview) == NULL)
972 noticeview_hide(mimeview->siginfoview);
974 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
975 gtk_tree_store_clear(GTK_TREE_STORE(model));
977 textview_clear(mimeview->textview);
978 if (mimeview->mimeviewer != NULL)
979 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
981 mimeview_free_mimeinfo(mimeview);
983 mimeview->mimeinfo = NULL;
985 gtk_tree_path_free(mimeview->opened);
986 mimeview->opened = NULL;
988 g_free(mimeview->file);
989 mimeview->file = NULL;
991 icon_list_clear(mimeview);
992 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
995 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
996 GtkUIManager *ui_manager;
998 if (messageview->window != NULL)
999 ui_manager = messageview->ui_manager;
1001 ui_manager = messageview->mainwin->ui_manager;
1003 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
1006 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1007 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1009 static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
1010 gboolean special, SignatureStatus code)
1012 gchar *text = NULL, *button_text = NULL;
1014 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1015 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1017 cm_return_if_fail(mimeview != NULL);
1018 cm_return_if_fail(mimeinfo != NULL);
1023 mycode = privacy_mimeinfo_get_sig_status(mimeinfo);
1026 case SIGNATURE_UNCHECKED:
1027 button_text = _("Check signature");
1028 func = check_signature_cb;
1029 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1032 button_text = _("View full information");
1033 func = display_full_info_cb;
1034 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1036 case SIGNATURE_WARN:
1037 button_text = _("View full information");
1038 func = display_full_info_cb;
1039 icon = STOCK_PIXMAP_PRIVACY_WARN;
1041 case SIGNATURE_KEY_EXPIRED:
1042 button_text = _("View full information");
1043 func = display_full_info_cb;
1044 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1046 case SIGNATURE_INVALID:
1047 button_text = _("View full information");
1048 func = display_full_info_cb;
1049 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1051 case SIGNATURE_CHECK_FAILED:
1052 case SIGNATURE_CHECK_TIMEOUT:
1053 button_text = _("Check again");
1054 func = check_signature_cb;
1055 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1060 if (mycode == SIGNATURE_UNCHECKED) {
1061 gchar *tmp = privacy_mimeinfo_sig_info_short(mimeinfo);
1062 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1064 if (*shortcut == '\0')
1065 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1067 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."),
1071 } else if (mycode != SIGNATURE_CHECK_TIMEOUT) {
1072 text = privacy_mimeinfo_sig_info_short(mimeinfo);
1073 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1074 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1076 if (*shortcut == '\0')
1077 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1079 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1083 noticeview_set_text(mimeview->siginfoview, text);
1084 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1087 noticeview_set_button_text(mimeview->siginfoview, NULL);
1088 noticeview_set_button_press_callback(
1089 mimeview->siginfoview,
1091 (gpointer) mimeview);
1092 noticeview_set_icon(mimeview->siginfoview, icon);
1093 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1095 icon_list_clear(mimeview);
1096 icon_list_create(mimeview, mimeview->mimeinfo);
1101 /* reset all thread stuff, and do the cleanups we've been left to do */
1102 static void mimeview_check_data_reset(MimeView *mimeview)
1105 gboolean must_destroy;
1107 if (!mimeview->check_data)
1110 must_free = mimeview->check_data->free_after_use;
1111 must_destroy = mimeview->check_data->destroy_mimeview;
1113 if (mimeview->check_data->cancel_th_init) {
1114 debug_print("killing canceller thread\n");
1115 mimeview->check_data->cancel_th_init = FALSE;
1116 pthread_cancel(mimeview->check_data->cancel_th);
1120 debug_print("freeing deferred mimeinfo\n");
1121 procmime_mimeinfo_free_all(mimeview->check_data->siginfo);
1124 g_free(mimeview->check_data);
1125 mimeview->check_data = NULL;
1128 debug_print("freeing deferred mimeview\n");
1129 mimeview_free_mimeinfo(mimeview);
1130 gtk_tree_path_free(mimeview->opened);
1131 g_free(mimeview->file);
1133 mimeviews = g_slist_remove(mimeviews, mimeview);
1137 /* GUI update once the checker thread is done or killed */
1138 static gboolean mimeview_check_sig_thread_cb(void *data)
1140 MimeView *mimeview = (MimeView *) data;
1141 MimeInfo *mimeinfo = mimeview->siginfo;
1143 debug_print("mimeview_check_sig_thread_cb\n");
1145 if (mimeinfo == NULL) {
1146 /* message changed !? */
1147 g_warning("no more siginfo!\n");
1151 if (!mimeview->check_data) {
1152 g_warning("nothing to check\n");
1156 if (mimeview->check_data->siginfo != mimeinfo) {
1157 /* message changed !? */
1158 g_warning("different siginfo!\n");
1162 if (mimeview->check_data->destroy_mimeview ||
1163 mimeview->check_data->free_after_use) {
1164 debug_print("not bothering, we're changing message\n");
1169 if (mimeview->check_data->timeout)
1170 update_signature_noticeview(mimeview, mimeview->siginfo,
1171 TRUE, SIGNATURE_CHECK_TIMEOUT);
1173 update_signature_noticeview(mimeview, mimeview->siginfo,
1177 mimeview_check_data_reset(mimeview);
1181 /* sig checker thread */
1182 static void *mimeview_check_sig_worker_thread(void *data)
1184 MimeView *mimeview = (MimeView *)data;
1185 MimeInfo *mimeinfo = mimeview->siginfo;
1187 debug_print("checking...\n");
1189 if (!mimeview->check_data)
1192 if (mimeinfo && mimeinfo == mimeview->check_data->siginfo) {
1193 privacy_mimeinfo_check_signature(mimeinfo);
1194 if (mimeview->check_data && mimeview->check_data->cancel_th_init) {
1195 mimeview->check_data->cancel_th_init = FALSE;
1196 pthread_cancel(mimeview->check_data->cancel_th);
1199 /* that's strange! we changed message without
1200 * getting killed. */
1201 g_warning("different siginfo!\n");
1202 mimeview_check_data_reset(mimeview);
1206 /* use g_timeout so that GUI updates is done from the
1208 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1213 /* killer thread - acts when the checker didn't work fast
1215 static void *mimeview_check_sig_cancel_thread(void *data)
1217 MimeView *mimeview = (MimeView *)data;
1219 if (!mimeview->check_data)
1220 return NULL; /* nothing to kill ! */
1222 /* wait for a few seconds... */
1223 debug_print("waiting a while\n");
1225 g_usleep(5 * 1000 * 1000);
1227 if (!mimeview->check_data)
1228 return NULL; /* nothing to kill, it's done in time :) */
1230 /* too late, go away checker thread */
1231 debug_print("killing checker thread\n");
1232 if (mimeview->check_data->th_init) {
1233 mimeview->check_data->th_init = FALSE;
1234 pthread_cancel(mimeview->check_data->th);
1237 /* tell upstream it was a timeout */
1238 mimeview->check_data->timeout = TRUE;
1239 /* use g_timeout so that GUI updates is done from the
1241 g_timeout_add(0,mimeview_check_sig_thread_cb,mimeview);
1246 /* get rid of the checker thread right now - used when changing the
1247 * displayed message for example. */
1248 static void mimeview_check_sig_cancel_now(MimeView *mimeview)
1250 if (!mimeview->check_data)
1252 debug_print("killing checker thread NOW\n");
1253 if (mimeview->check_data->th_init) {
1254 mimeview->check_data->th_init = FALSE;
1255 pthread_cancel(mimeview->check_data->th);
1258 /* tell upstream it was a timeout */
1259 mimeview->check_data->timeout = TRUE;
1260 mimeview_check_sig_thread_cb(mimeview);
1264 /* creates a thread to check the signature, and a second one
1265 * to kill the first one after a timeout */
1266 static void mimeview_check_sig_in_thread(MimeView *mimeview)
1269 pthread_attr_t detach, detach2;
1271 if (mimeview->check_data) {
1272 g_warning("already checking it");
1276 mimeview->check_data = g_new0(SigCheckData, 1);
1277 mimeview->check_data->siginfo = mimeview->siginfo;
1278 debug_print("creating thread\n");
1280 /* init thread attributes and create the checker thread */
1281 if (pthread_attr_init(&detach) != 0 ||
1282 pthread_attr_setdetachstate(&detach, PTHREAD_CREATE_DETACHED) != 0 ||
1283 pthread_attr_init(&detach2) != 0 ||
1284 pthread_attr_setdetachstate(&detach2, PTHREAD_CREATE_DETACHED) != 0 ||
1285 pthread_create(&th, &detach,
1286 mimeview_check_sig_worker_thread,
1288 /* arh. We'll do it synchronously. */
1289 g_warning("can't create thread");
1290 g_free(mimeview->check_data);
1291 mimeview->check_data = NULL;
1294 mimeview->check_data->th = th;
1295 mimeview->check_data->th_init = TRUE;
1298 /* create the killer thread */
1299 pthread_create(&th2, &detach2,
1300 mimeview_check_sig_cancel_thread,
1303 mimeview->check_data->cancel_th = th2;
1304 mimeview->check_data->cancel_th_init = TRUE;
1308 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1310 MimeView *mimeview = (MimeView *) user_data;
1311 MimeInfo *mimeinfo = mimeview->siginfo;
1313 if (mimeinfo == NULL)
1316 if (mimeview->check_data)
1319 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1322 /* let's do it non-blocking */
1323 mimeview_check_sig_in_thread(mimeview);
1324 if (!mimeview->check_data) /* let's check syncronously */
1327 debug_print("checking without thread\n");
1328 privacy_mimeinfo_check_signature(mimeinfo);
1329 update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
1333 void mimeview_check_signature(MimeView *mimeview)
1335 check_signature_cb(NULL, mimeview);
1338 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1340 MimeView *mimeview = (MimeView *) user_data;
1341 gtk_tree_path_free(mimeview->opened);
1342 mimeview->opened = NULL;
1343 mimeview_selected(gtk_tree_view_get_selection(
1344 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1347 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1349 MimeView *mimeview = (MimeView *) user_data;
1352 siginfo = privacy_mimeinfo_sig_info_full(mimeview->siginfo);
1353 textview_set_text(mimeview->textview, siginfo);
1355 noticeview_set_button_text(mimeview->siginfoview, NULL);
1356 noticeview_set_button_press_callback(
1357 mimeview->siginfoview,
1358 G_CALLBACK(redisplay_email),
1359 (gpointer) mimeview);
1360 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1363 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1366 MimeInfo *first_text;
1368 cm_return_if_fail(mimeview != NULL);
1369 cm_return_if_fail(selected != NULL);
1371 if (selected->type == MIMETYPE_MESSAGE
1372 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1373 /* if the first text part is signed, check that */
1374 first_text = selected;
1375 while (first_text && first_text->type != MIMETYPE_TEXT) {
1376 first_text = procmime_mimeinfo_next(first_text);
1379 update_signature_info(mimeview, first_text);
1385 while (siginfo != NULL) {
1386 if (privacy_mimeinfo_is_signed(siginfo))
1388 siginfo = procmime_mimeinfo_parent(siginfo);
1390 mimeview->siginfo = siginfo;
1392 /* This shortcut boolean is there to correctly set the menu's
1393 * CheckSignature item sensitivity without killing performance
1394 * each time the menu sensitiveness is updated (a lot).
1396 mimeview->signed_part = (siginfo != NULL);
1398 if (siginfo == NULL) {
1399 noticeview_hide(mimeview->siginfoview);
1403 update_signature_noticeview(mimeview, siginfo, FALSE, 0);
1404 noticeview_show(mimeview->siginfoview);
1407 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1409 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1410 GtkTreeModel *model;
1415 selection = gtk_tree_view_get_selection(ctree);
1416 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1419 path = gtk_tree_model_get_path(model, &iter);
1421 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1422 gtk_tree_path_free(path);
1426 gtk_tree_path_free(mimeview->opened);
1427 mimeview->opened = path;
1428 mimeview->spec_part = NULL;
1429 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1431 partinfo = mimeview_get_node_part(mimeview, path);
1432 if (!partinfo) return;
1434 /* ungrab the mouse event */
1435 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1436 gtk_grab_remove(GTK_WIDGET(ctree));
1437 if (gdk_pointer_is_grabbed())
1438 gdk_pointer_ungrab(GDK_CURRENT_TIME);
1441 mimeview->textview->default_text = FALSE;
1443 update_signature_info(mimeview, partinfo);
1445 if (!mimeview_show_part(mimeview, partinfo)) {
1446 switch (partinfo->type) {
1448 case MIMETYPE_MESSAGE:
1449 case MIMETYPE_MULTIPART:
1450 mimeview_show_message_part(mimeview, partinfo);
1454 mimeview->textview->default_text = TRUE;
1455 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1456 textview_clear(mimeview->textview);
1457 textview_show_mime_part(mimeview->textview, partinfo);
1462 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1463 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1464 mimeview_check_signature(mimeview);
1468 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1471 GtkTreeSelection *selection;
1474 if (!event) return FALSE;
1476 if (event->button == 2 || event->button == 3) {
1477 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1478 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1481 gtk_tree_selection_select_iter(selection, &iter);
1483 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1488 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1491 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1492 if (event->direction == GDK_SCROLL_UP) {
1493 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1495 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1497 gtk_vscrollbutton_scroll(scrollbutton);
1501 /* from gdkevents.c */
1502 #define DOUBLE_CLICK_TIME 250
1504 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1507 static MimeInfo *lastinfo;
1508 static guint32 lasttime;
1510 if (event->button == 2 ||
1511 (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
1512 /* call external program for image, audio or html */
1513 mimeview_launch(mimeview, partinfo);
1515 } else if (event->button == 3) {
1516 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1517 partinfo->type == MIMETYPE_IMAGE ||
1518 partinfo->type == MIMETYPE_MULTIPART))
1519 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1521 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1524 partinfo->type == MIMETYPE_APPLICATION &&
1525 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1526 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1529 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1531 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1532 "pop_partinfo", partinfo);
1534 gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
1535 NULL, NULL, NULL, NULL,
1536 event->button, event->time);
1540 lastinfo = partinfo;
1541 lasttime = event->time;
1546 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1548 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1551 void mimeview_select_next_part(MimeView *mimeview)
1553 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1554 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1555 GtkTreeSelection *selection;
1558 MimeInfo *partinfo = NULL;
1561 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1562 path = gtk_tree_model_get_path(model, &iter);
1564 has_next = mimeview_tree_next(model, path);
1566 has_next = gtk_tree_model_get_iter_first(model, &iter);
1567 gtk_tree_path_free(path);
1568 path = gtk_tree_model_get_path(model, &iter);
1572 partinfo = mimeview_get_node_part(mimeview, path);
1573 if (partinfo->type == MIMETYPE_MULTIPART ||
1574 (!prefs_common.show_inline_attachments && partinfo->id))
1576 selection = gtk_tree_view_get_selection(ctree);
1577 gtk_tree_selection_select_path(selection, path);
1578 icon_list_toggle_by_mime_info(mimeview, partinfo);
1581 gtk_tree_path_free(path);
1584 void mimeview_select_prev_part(MimeView *mimeview)
1586 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1587 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1588 GtkTreeSelection *selection;
1591 MimeInfo *partinfo = NULL;
1594 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1595 path = gtk_tree_model_get_path(model, &iter);
1597 has_prev = mimeview_tree_prev(mimeview, path);
1599 while (mimeview_tree_next(model, path)) {}
1604 partinfo = mimeview_get_node_part(mimeview, path);
1605 if (partinfo->type == MIMETYPE_MULTIPART ||
1606 (!prefs_common.show_inline_attachments && partinfo->id))
1608 selection = gtk_tree_view_get_selection(ctree);
1609 gtk_tree_selection_select_path(selection, path);
1610 icon_list_toggle_by_mime_info(mimeview, partinfo);
1613 gtk_tree_path_free(path);
1616 #define BREAK_ON_MODIFIER_KEY() \
1617 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1619 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1622 SummaryView *summaryview;
1624 if (!event) return FALSE;
1625 if (!mimeview->opened) return FALSE;
1627 summaryview = mimeview->messageview->mainwin->summaryview;
1629 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1632 switch (event->keyval) {
1635 textview_scroll_max(mimeview->textview,
1636 (event->keyval == GDK_KEY_Home));
1638 case GDK_KEY_Page_Down:
1640 if (mimeview_scroll_page(mimeview,
1641 (event->state & GDK_SHIFT_MASK) != 0))
1644 if (!(event->state & GDK_SHIFT_MASK))
1645 mimeview_select_next_part(mimeview);
1647 case GDK_KEY_Page_Up:
1648 case GDK_KEY_BackSpace:
1649 mimeview_scroll_page(mimeview, TRUE);
1651 case GDK_KEY_Return:
1652 case GDK_KEY_KP_Enter:
1653 mimeview_scroll_one_line(mimeview,
1654 (event->state & GDK_MOD1_MASK) != 0);
1658 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1664 if (mimeview->messageview->new_window) return FALSE;
1666 return summary_pass_key_press_event(summaryview, event);
1669 static void mimeview_drag_data_get(GtkWidget *widget,
1670 GdkDragContext *drag_context,
1671 GtkSelectionData *selection_data,
1676 gchar *filename = NULL, *uriname, *tmp;
1681 if (!mimeview->opened) return;
1682 if (!mimeview->file) return;
1684 partinfo = mimeview_get_selected_part(mimeview);
1685 if (!partinfo) return;
1687 if (strlen(get_part_name(partinfo)) > 0) {
1688 filename = g_path_get_basename(get_part_name(partinfo));
1689 if (*filename == '\0') return;
1690 } else if (partinfo->type == MIMETYPE_MESSAGE
1691 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1693 GPtrArray *headers = NULL;
1696 fp = g_fopen(partinfo->data.filename, "rb");
1697 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1698 headers = procheader_get_header_array_asis(fp);
1701 for (i = 0; i < headers->len; i++) {
1702 Header *header = g_ptr_array_index(headers, i);
1703 if (procheader_headername_equal(header->name, "Subject")) {
1704 unfold_line(header->body);
1705 name = g_strconcat(header->body, ".txt", NULL);
1706 subst_for_filename(name);
1709 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;
2415 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2418 vbox = mimeview->icon_vbox;
2419 mimeview->icon_count++;
2420 button = gtk_event_box_new();
2422 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2423 G_CALLBACK(mimeview_visi_notify), mimeview);
2424 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2425 G_CALLBACK(mimeview_leave_notify), mimeview);
2426 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2427 G_CALLBACK(mimeview_enter_notify), mimeview);
2429 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2430 g_object_set_data(G_OBJECT(button), "icon_number",
2431 GINT_TO_POINTER(mimeview->icon_count));
2432 g_object_set_data(G_OBJECT(button), "partinfo",
2435 switch (mimeinfo->type) {
2438 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2439 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2440 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2441 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2442 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2443 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2444 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2445 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2446 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2448 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2450 case MIMETYPE_MESSAGE:
2451 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2453 case MIMETYPE_APPLICATION:
2454 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2455 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2456 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2457 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2458 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2459 stockp = STOCK_PIXMAP_MIME_PDF;
2460 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2461 stockp = STOCK_PIXMAP_MIME_PS;
2463 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2465 case MIMETYPE_IMAGE:
2466 stockp = STOCK_PIXMAP_MIME_IMAGE;
2468 case MIMETYPE_AUDIO:
2469 stockp = STOCK_PIXMAP_MIME_AUDIO;
2472 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2476 partinfo = mimeinfo;
2477 while (partinfo != NULL) {
2478 if (privacy_mimeinfo_is_signed(partinfo)) {
2482 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2483 encrypted = partinfo;
2486 partinfo = procmime_mimeinfo_parent(partinfo);
2489 if (siginfo != NULL) {
2490 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2491 case SIGNATURE_UNCHECKED:
2492 case SIGNATURE_CHECK_FAILED:
2493 case SIGNATURE_CHECK_TIMEOUT:
2494 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2495 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2498 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2499 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2501 case SIGNATURE_WARN:
2502 case SIGNATURE_KEY_EXPIRED:
2503 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2504 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2506 case SIGNATURE_INVALID:
2507 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2508 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2511 sigshort = privacy_mimeinfo_sig_info_short(siginfo);
2512 } else if (encrypted != NULL) {
2513 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
2514 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2516 pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
2517 OVERLAY_NONE, 6, 3);
2519 gtk_container_add(GTK_CONTAINER(button), pixmap);
2521 if (prefs_common.attach_desc)
2522 desc = get_part_description(mimeinfo);
2524 desc = get_part_name(mimeinfo);
2527 content_type = procmime_get_content_type_str(mimeinfo->type,
2530 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2531 "\n<b>", _("Size:"), " </b>",
2532 to_human_readable((goffset)mimeinfo->length), NULL);
2533 g_free(content_type);
2534 if (desc && *desc) {
2535 gchar *tmp = NULL, *escaped = NULL;
2536 if (!g_utf8_validate(desc, -1, NULL)) {
2537 tmp = conv_filename_to_utf8(desc);
2539 tmp = g_strdup(desc);
2541 escaped = g_markup_escape_text(tmp,-1);
2543 tiptmp = g_strconcat(tip, "\n<b>",
2544 prefs_common.attach_desc && mimeinfo->description ?
2545 _("Description:") : _("Filename:"),
2546 " </b>", escaped, 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,
2647 if (mimeview->opened)
2648 icon_list_toggle_by_mime_info(mimeview,
2649 mimeview_get_node_part(mimeview, mimeview->opened));
2652 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2655 GList *children, *child;
2657 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
2658 for (child = children; child != NULL; child = g_list_next(child)) {
2659 gboolean *highlight = NULL;
2660 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2662 if (!GTK_IS_EVENT_BOX(child->data))
2665 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2666 *highlight = (g_object_get_data(G_OBJECT(child->data),
2667 "partinfo") == (gpointer)mimeinfo);
2669 gtk_widget_queue_draw(icon);
2671 g_list_free(children);
2674 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2677 prefs_common.mimeview_tree_height = allocation->height;
2680 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2683 g_object_ref(button);
2685 mimeview_leave_notify(button, NULL, NULL);
2687 mimeview->ctree_mode = !mimeview->ctree_mode;
2688 if (mimeview->ctree_mode) {
2689 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_RIGHT,
2691 gtk_widget_hide(mimeview->icon_mainbox);
2692 gtk_widget_show(mimeview->ctree_mainbox);
2693 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2694 prefs_common.mimeview_tree_height);
2696 gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2698 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2699 button, FALSE, FALSE, 0);
2701 gtk_arrow_set(GTK_ARROW(gtk_bin_get_child(GTK_BIN((button)))), GTK_ARROW_LEFT,
2703 gtk_widget_hide(mimeview->ctree_mainbox);
2704 gtk_widget_show(mimeview->icon_mainbox);
2705 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2707 gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2709 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2710 button, FALSE, FALSE, 0);
2711 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2712 if (mimeview->opened)
2713 icon_list_toggle_by_mime_info(mimeview,
2714 mimeview_get_node_part(mimeview, mimeview->opened));
2715 summary_grab_focus(mimeview->mainwin->summaryview);
2717 g_object_unref(button);
2721 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);