Fix error creation and warning
[claws.git] / src / mimeview.c
blobef12ed8f53e3a7df599d77e2cdc4c368674683c3
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2023 the Claws Mail team and Hiroyuki Yamamoto
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #ifdef G_OS_WIN32
27 #define UNICODE
28 #define _UNICODE
29 #endif
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtk.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
39 #include "main.h"
40 #include "mimeview.h"
41 #include "textview.h"
42 #include "procmime.h"
43 #include "summaryview.h"
44 #include "menu.h"
45 #include "filesel.h"
46 #include "alertpanel.h"
47 #include "inputdialog.h"
48 #include "utils.h"
49 #include "gtkutils.h"
50 #include "prefs_common.h"
51 #include "procheader.h"
52 #include "stock_pixmap.h"
53 #include "gtk/gtkvscrollbutton.h"
54 #include "gtk/logwindow.h"
55 #include "timing.h"
56 #include "manage_window.h"
57 #include "privacy.h"
58 #include "file-utils.h"
60 typedef enum
62 COL_MIMETYPE = 0,
63 COL_SIZE = 1,
64 COL_NAME = 2,
65 COL_DATA = 3,
66 N_MIMEVIEW_COLUMNS
67 } MimeViewColumnPos;
69 #define N_MIMEVIEW_COLS (N_MIMEVIEW_COLUMNS)
71 static void mimeview_set_multipart_tree (MimeView *mimeview,
72 MimeInfo *mimeinfo,
73 GtkTreeIter *parent);
74 static void mimeview_show_message_part (MimeView *mimeview,
75 MimeInfo *partinfo);
76 static void mimeview_change_view_type (MimeView *mimeview,
77 MimeViewType type);
78 static gchar *mimeview_get_filename_for_part (MimeInfo *partinfo,
79 const gchar *basedir,
80 gint number);
81 static gint mimeview_write_part (const gchar *filename,
82 MimeInfo *partinfo,
83 gboolean handle_error);
85 static void mimeview_selected (GtkTreeSelection *selection,
86 MimeView *mimeview);
87 static gint mimeview_button_pressed (GtkWidget *widget,
88 GdkEventButton *event,
89 MimeView *mimeview);
90 static gint mimeview_key_pressed (GtkWidget *widget,
91 GdkEventKey *event,
92 MimeView *mimeview);
94 static void mimeview_drag_data_get (GtkWidget *widget,
95 GdkDragContext *drag_context,
96 GtkSelectionData *selection_data,
97 guint info,
98 guint time,
99 MimeView *mimeview);
101 static gboolean mimeview_scrolled (GtkWidget *widget,
102 GdkEventScroll *event,
103 MimeView *mimeview);
105 static void mimeview_save_all (MimeView *mimeview);
106 #ifndef G_OS_WIN32
107 static void mimeview_open_part_with (MimeView *mimeview,
108 MimeInfo *partinfo,
109 gboolean automatic);
110 #endif
111 static void mimeview_send_to (MimeView *mimeview,
112 MimeInfo *partinfo);
113 static void mimeview_view_file (const gchar *filename,
114 MimeInfo *partinfo,
115 const gchar *cmd,
116 MimeView *mimeview);
117 static gboolean icon_clicked_cb (GtkWidget *button,
118 GdkEventButton *event,
119 MimeView *mimeview);
120 static void icon_selected (MimeView *mimeview,
121 gint num,
122 MimeInfo *partinfo);
123 static gint icon_key_pressed (GtkWidget *button,
124 GdkEventKey *event,
125 MimeView *mimeview);
126 static void icon_list_append_icon (MimeView *mimeview,
127 MimeInfo *mimeinfo);
128 static void icon_list_create (MimeView *mimeview,
129 MimeInfo *mimeinfo);
130 static void icon_list_clear (MimeView *mimeview);
131 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
132 MimeInfo *mimeinfo);
133 static void ctree_size_allocate_cb (GtkWidget *widget,
134 GtkAllocation *allocation,
135 MimeView *mimeview);
136 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
137 MimeView *mimeview);
138 static gboolean part_button_pressed (MimeView *mimeview,
139 GdkEventButton *event,
140 MimeInfo *partinfo);
141 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
142 GtkAllocation *layout_size,
143 MimeView *mimeview);
144 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview);
145 static const gchar *get_part_name(MimeInfo *partinfo);
146 static const gchar *get_part_description(MimeInfo *partinfo);
148 static void mimeview_launch_cb(GtkAction *action, gpointer data)
150 MimeView *mimeview = (MimeView *)data;
151 mimeview_launch(mimeview, mimeview_get_part_to_use(mimeview));
154 #ifndef G_OS_WIN32
155 static void mimeview_open_with_cb(GtkAction *action, gpointer data)
157 mimeview_open_with((MimeView *)data);
159 #endif
161 static void mimeview_copy_cb(GtkAction *action, gpointer data)
163 MimeView *mimeview = (MimeView *)data;
164 MimeInfo *mimeinfo = mimeview_get_part_to_use(mimeview);
166 if (mimeinfo == NULL)
167 return;
169 if (mimeinfo->type == MIMETYPE_IMAGE) {
170 GError *error = NULL;
171 GdkPixbuf *pixbuf = procmime_get_part_as_pixbuf(mimeinfo, &error);
172 if (error != NULL) {
173 g_warning("could not copy: %s", error->message);
174 g_error_free(error);
175 } else {
176 gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
177 pixbuf);
179 g_object_unref(pixbuf);
180 } else {
181 void *data = procmime_get_part_as_string(mimeinfo, FALSE);
182 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
183 data, mimeinfo->length);
184 g_free(data);
188 static void mimeview_send_to_cb(GtkAction *action, gpointer data)
190 MimeView *mimeview = (MimeView *)data;
191 mimeview_send_to(mimeview, mimeview_get_part_to_use(mimeview));
194 static void mimeview_display_as_text_cb(GtkAction *action, gpointer data)
196 mimeview_display_as_text((MimeView *)data);
199 static void mimeview_save_as_cb(GtkAction *action, gpointer data)
201 mimeview_save_as((MimeView *)data);
204 static void mimeview_save_all_cb(GtkAction *action, gpointer data)
206 mimeview_save_all((MimeView *)data);
209 static void mimeview_select_next_part_cb(GtkAction *action, gpointer data)
211 mimeview_select_next_part((MimeView *)data);
214 static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
216 mimeview_select_prev_part((MimeView *)data);
219 static GtkActionEntry mimeview_menu_actions[] = {
220 { "MimeView", NULL, "MimeView", NULL, NULL, NULL },
221 { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
222 #if (!defined G_OS_WIN32)
223 { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
224 #endif
225 { "MimeView/Copy", NULL, N_("Copy"), NULL, "Copy", G_CALLBACK(mimeview_copy_cb) },
226 { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
227 { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
228 { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
229 { "MimeView/SaveAll", NULL, N_("Save _all..."), NULL, "Save all parts", G_CALLBACK(mimeview_save_all_cb) },
230 { "MimeView/NextPart", NULL, N_("Next part"), NULL, "Next part", G_CALLBACK(mimeview_select_next_part_cb) },
231 { "MimeView/PrevPart", NULL, N_("Previous part"), NULL, "Previous part", G_CALLBACK(mimeview_select_prev_part_cb) }
234 static GtkTargetEntry mimeview_mime_types[] =
236 {"text/uri-list", 0, 0}
239 GSList *mimeviewer_factories;
240 GSList *mimeviews;
242 static GdkCursor *hand_cursor = NULL;
244 static gboolean mimeview_visi_notify(GtkWidget *widget,
245 GdkEventVisibility *event,
246 MimeView *mimeview)
248 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
249 return FALSE;
252 static gboolean mimeview_leave_notify(GtkWidget *widget,
253 GdkEventCrossing *event,
254 MimeView *mimeview)
256 gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
257 return FALSE;
260 static gboolean mimeview_enter_notify(GtkWidget *widget,
261 GdkEventCrossing *event,
262 MimeView *mimeview)
264 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
265 return FALSE;
268 MimeView *mimeview_create(MainWindow *mainwin)
270 MimeView *mimeview;
272 GtkWidget *paned;
273 GtkWidget *scrolledwin;
274 GtkWidget *ctree;
275 GtkWidget *mime_notebook;
276 GtkWidget *popupmenu;
277 GtkWidget *ctree_mainbox;
278 GtkWidget *vbox;
279 GtkWidget *mime_toggle;
280 GtkWidget *icon_mainbox;
281 GtkWidget *icon_scroll;
282 GtkWidget *icon_grid;
283 GtkWidget *arrow;
284 GtkWidget *scrollbutton;
285 GtkWidget *hbox;
286 NoticeView *siginfoview;
287 GtkTreeStore *model;
288 GtkCellRenderer *renderer;
289 GtkTreeViewColumn *column;
290 GtkTreeSelection *selection;
292 gchar *titles[N_MIMEVIEW_COLS];
293 gint cols;
295 if (!hand_cursor)
296 hand_cursor = gdk_cursor_new_for_display(
297 gtk_widget_get_display(mainwin->window), GDK_HAND2);
299 debug_print("Creating MIME view...\n");
300 mimeview = g_new0(MimeView, 1);
302 titles[COL_MIMETYPE] = _("MIME Type");
303 titles[COL_SIZE] = _("Size");
304 titles[COL_NAME] = _("Name");
306 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
307 gtk_widget_show(scrolledwin);
308 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
309 GTK_POLICY_AUTOMATIC,
310 GTK_POLICY_AUTOMATIC);
312 model = gtk_tree_store_new(N_MIMEVIEW_COLS,
313 G_TYPE_STRING,
314 G_TYPE_STRING,
315 G_TYPE_STRING,
316 G_TYPE_POINTER);
318 ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
319 g_object_unref(model);
320 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
321 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
322 prefs_common.use_stripes_everywhere);
323 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ctree),
324 prefs_common.show_col_headers);
326 renderer = gtk_cell_renderer_text_new();
327 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
328 -1, titles[COL_MIMETYPE],renderer,
329 "text", COL_MIMETYPE, NULL);
330 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
332 renderer = gtk_cell_renderer_text_new();
333 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
334 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
335 -1, titles[COL_SIZE], renderer,
336 "text", COL_SIZE, NULL);
337 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
338 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
340 renderer = gtk_cell_renderer_text_new();
341 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
342 -1, titles[COL_NAME], renderer,
343 "text", COL_NAME, NULL);
344 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
345 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
347 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
348 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
350 gtk_widget_show(ctree);
351 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
352 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
353 mimeview_mime_types, 1, GDK_ACTION_COPY);
355 g_signal_connect(G_OBJECT(selection), "changed",
356 G_CALLBACK(mimeview_selected), mimeview);
357 g_signal_connect(G_OBJECT(ctree), "button_release_event",
358 G_CALLBACK(mimeview_button_pressed), mimeview);
359 g_signal_connect(G_OBJECT(ctree), "key_press_event",
360 G_CALLBACK(mimeview_key_pressed), mimeview);
361 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
362 G_CALLBACK(mimeview_drag_data_get), mimeview);
364 mime_notebook = gtk_notebook_new();
365 gtk_widget_set_name(GTK_WIDGET(mime_notebook), "mime_notebook");
366 gtk_widget_show(mime_notebook);
367 gtk_widget_set_can_focus(mime_notebook, FALSE);
368 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
369 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
371 icon_grid = gtk_grid_new();
372 gtk_orientable_set_orientation(GTK_ORIENTABLE(icon_grid),
373 GTK_ORIENTATION_VERTICAL);
374 gtk_grid_set_row_spacing(GTK_GRID(icon_grid), 0);
375 gtk_widget_show(icon_grid);
376 icon_scroll = gtk_layout_new(NULL, NULL);
377 gtk_widget_show(icon_scroll);
378 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_grid, 0, 0);
379 scrollbutton = gtk_vscrollbutton_new(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(icon_scroll)));
380 gtk_widget_show(scrollbutton);
382 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
383 G_CALLBACK(mimeview_scrolled), mimeview);
385 mime_toggle = gtk_event_box_new();
386 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
388 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
389 G_CALLBACK(mimeview_visi_notify), mimeview);
390 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
391 G_CALLBACK(mimeview_leave_notify), mimeview);
392 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
393 G_CALLBACK(mimeview_enter_notify), mimeview);
395 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
396 gtk_widget_show(mime_toggle);
397 mimeview->ctree_mode = FALSE;
398 arrow = gtk_image_new_from_icon_name("pan-start-symbolic", GTK_ICON_SIZE_MENU);
399 gtk_widget_show(arrow);
400 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
401 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
402 G_CALLBACK(mime_toggle_button_cb), mimeview);
404 icon_mainbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
405 gtk_widget_set_name(GTK_WIDGET(icon_mainbox), "mimeview_icon_mainbox");
406 gtk_widget_show(icon_mainbox);
407 gtk_widget_set_size_request(icon_mainbox, 32, -1);
408 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
409 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
410 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
411 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
412 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
414 ctree_mainbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
415 gtk_widget_set_name(GTK_WIDGET(ctree_mainbox), "mimeview_ctree_mainbox");
416 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
417 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
418 G_CALLBACK(ctree_size_allocate_cb), mimeview);
420 mimeview->ui_manager = gtk_ui_manager_new();
421 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
422 "MimeView", mimeview_menu_actions,
423 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
425 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
426 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
427 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
428 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
429 "/Menus/MimeView/", "Open", "MimeView/Open",
430 GTK_UI_MANAGER_MENUITEM);
431 #if (!defined G_OS_WIN32)
432 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
433 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
434 GTK_UI_MANAGER_MENUITEM);
435 #endif
436 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
437 "/Menus/MimeView/", "Copy", "MimeView/Copy",
438 GTK_UI_MANAGER_MENUITEM);
439 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
440 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
441 GTK_UI_MANAGER_MENUITEM);
442 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
443 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
444 GTK_UI_MANAGER_MENUITEM);
445 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
446 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
447 GTK_UI_MANAGER_MENUITEM);
448 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
449 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
450 GTK_UI_MANAGER_MENUITEM);
451 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
452 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
453 GTK_UI_MANAGER_MENUITEM);
454 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
455 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
456 GTK_UI_MANAGER_MENUITEM);
458 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
459 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
462 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
463 gtk_widget_show(vbox);
464 siginfoview = noticeview_create(mainwin);
465 gtk_widget_set_name(GTK_WIDGET(siginfoview->vgrid), "siginfoview");
466 noticeview_hide(siginfoview);
467 noticeview_set_icon_clickable(siginfoview, TRUE);
468 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
469 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
471 paned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
472 gtk_widget_show(paned);
473 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
474 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
476 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
477 gtk_widget_set_name(GTK_WIDGET(hbox), "mimeview");
478 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
479 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
481 gtk_widget_show(hbox);
482 gtk_widget_hide(ctree_mainbox);
483 #ifdef GENERIC_UMPC
484 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
485 #endif
486 mimeview->hbox = hbox;
487 mimeview->paned = paned;
488 mimeview->scrolledwin = scrolledwin;
489 mimeview->ctree = ctree;
490 mimeview->mime_notebook = mime_notebook;
491 mimeview->popupmenu = popupmenu;
492 mimeview->type = -1;
493 mimeview->ctree_mainbox = ctree_mainbox;
494 mimeview->icon_scroll = icon_scroll;
495 mimeview->icon_grid = icon_grid;
496 mimeview->icon_mainbox = icon_mainbox;
497 mimeview->icon_count = 0;
498 mimeview->mainwin = mainwin;
499 mimeview->mime_toggle = mime_toggle;
500 mimeview->siginfoview = siginfoview;
501 mimeview->scrollbutton = scrollbutton;
502 mimeview->arrow = arrow;
503 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
505 mimeviews = g_slist_prepend(mimeviews, mimeview);
507 return mimeview;
510 void mimeview_init(MimeView *mimeview)
512 textview_init(mimeview->textview);
514 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
515 GTK_WIDGET_PTR(mimeview->textview));
518 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
520 while (mimeinfo) {
521 if (privacy_mimeinfo_is_signed(mimeinfo))
522 return TRUE;
523 mimeinfo = procmime_mimeinfo_next(mimeinfo);
526 return FALSE;
529 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
530 const gchar *file)
532 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
534 mimeview_clear(mimeview);
536 cm_return_if_fail(file != NULL);
537 cm_return_if_fail(mimeinfo != NULL);
539 mimeview->mimeinfo = mimeinfo;
541 mimeview->file = g_strdup(file);
543 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
544 mimeview);
546 /* check if the mail's signed - it can change the mail structure */
548 if (any_part_is_signed(mimeinfo))
549 debug_print("signed mail\n");
551 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
552 gtk_tree_view_expand_all(ctree);
553 icon_list_clear(mimeview);
554 icon_list_create(mimeview, mimeinfo);
556 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
557 mimeview_selected, mimeview);
560 static void mimeview_free_mimeinfo(MimeView *mimeview)
562 if (mimeview->mimeinfo != NULL) {
563 procmime_mimeinfo_free_all(&mimeview->mimeinfo);
564 mimeview->mimeinfo = NULL;
568 void mimeview_destroy(MimeView *mimeview)
570 GSList *cur;
572 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
573 MimeViewer *viewer = (MimeViewer *) cur->data;
574 viewer->destroy_viewer(viewer);
576 g_slist_free(mimeview->viewers);
577 gtk_target_list_unref(mimeview->target_list);
579 if (mimeview->sig_check_timeout_tag != 0)
580 g_source_remove(mimeview->sig_check_timeout_tag);
581 if (mimeview->sig_check_cancellable != NULL) {
582 /* Set last_sig_check_task to NULL to discard results in async_cb */
583 mimeview->siginfo->last_sig_check_task = NULL;
584 g_cancellable_cancel(mimeview->sig_check_cancellable);
585 g_object_unref(mimeview->sig_check_cancellable);
587 mimeview_free_mimeinfo(mimeview);
588 gtk_tree_path_free(mimeview->opened);
589 g_free(mimeview->file);
590 g_free(mimeview);
591 mimeviews = g_slist_remove(mimeviews, mimeview);
594 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
596 return gtkut_tree_view_get_selected_pointer(
597 GTK_TREE_VIEW(mimeview->ctree), COL_DATA,
598 NULL, NULL, NULL);
601 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
603 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
604 GtkTreeIter iter;
605 MimeInfo *partinfo;
607 gtk_tree_model_get_iter(model, &iter, path);
608 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
609 return partinfo;
612 gboolean mimeview_tree_is_empty(MimeView *mimeview)
614 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
615 GtkTreeIter iter;
616 return !gtk_tree_model_get_iter_first(model, &iter);
619 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
621 GtkTreeIter iter, parent;
622 gboolean has_parent;
624 gtk_tree_model_get_iter(model, &iter, path);
626 if (gtk_tree_model_iter_has_child(model, &iter)) {
627 gtk_tree_path_down(path);
628 return TRUE;
631 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
633 if (!gtk_tree_model_iter_next(model, &iter)) {
634 while (has_parent) {
635 GtkTreeIter saved_parent = parent;
636 gtk_tree_path_up(path);
637 if (gtk_tree_model_iter_next(model, &parent)) {
638 gtk_tree_path_next(path);
639 return TRUE;
640 } else {
641 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
645 } else {
646 gtk_tree_path_next(path);
647 return TRUE;
650 return FALSE;
653 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
655 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
656 GtkTreeIter iter, child, parent;
657 gboolean has_parent;
659 gtk_tree_model_get_iter(model, &iter, path);
660 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
662 if (gtk_tree_path_prev(path)) {
663 gtk_tree_model_get_iter(model, &iter, path);
665 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
666 gtk_tree_path_down(path);
668 while (gtk_tree_model_iter_next(model, &child))
669 gtk_tree_path_next(path);
672 return TRUE;
675 if (has_parent) {
676 gtk_tree_path_up(path);
677 return TRUE;
680 return FALSE;
683 gint mimeview_get_selected_part_num(MimeView *mimeview)
685 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
686 GtkTreeIter iter;
687 GtkTreePath *path;
688 gint i = 0;
690 if (!gtk_tree_model_get_iter_first(model, &iter))
691 return -1;
692 path = gtk_tree_model_get_path(model, &iter);
694 do {
695 if (!gtk_tree_path_compare(mimeview->opened, path)) {
696 gtk_tree_path_free(path);
697 return i;
700 i++;
701 } while (mimeview_tree_next(model, path));
703 gtk_tree_path_free(path);
705 return -1;
708 void mimeview_select_part_num(MimeView *mimeview, gint i)
710 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
711 GtkTreeIter iter;
712 GtkTreePath *path;
713 GtkTreeSelection *selection;
714 gint x = 0;
716 if (i < 0)
717 return;
719 if (!gtk_tree_model_get_iter_first(model, &iter))
720 return;
721 path = gtk_tree_model_get_path(model, &iter);
723 while (x != i) {
724 if (!mimeview_tree_next(model, path)) {
725 gtk_tree_path_free(path);
726 return;
728 x++;
731 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
732 gtk_tree_selection_select_path(selection, path);
733 gtk_tree_path_free(path);
736 static void mimeview_set_multipart_tree(MimeView *mimeview,
737 MimeInfo *mimeinfo,
738 GtkTreeIter *parent)
740 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
741 GTK_TREE_VIEW(mimeview->ctree)));
742 GtkTreeIter iter;
743 gchar *content_type, *length, *name;
745 cm_return_if_fail(mimeinfo != NULL);
747 while (mimeinfo != NULL) {
748 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
749 content_type = g_strdup_printf("%s/%s",
750 procmime_get_media_type_str(mimeinfo->type),
751 mimeinfo->subtype);
752 else
753 content_type = g_strdup("UNKNOWN");
755 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
757 if (prefs_common.attach_desc)
758 name = g_strdup(get_part_description(mimeinfo));
759 else
760 name = g_strdup(get_part_name(mimeinfo));
762 gtk_tree_store_append(model, &iter, parent);
763 gtk_tree_store_set(model, &iter,
764 COL_MIMETYPE, content_type,
765 COL_SIZE, length,
766 COL_NAME, name,
767 COL_DATA, mimeinfo, -1);
768 g_free(content_type);
769 g_free(length);
770 g_free(name);
772 if (mimeinfo->node->children)
773 mimeview_set_multipart_tree(mimeview,
774 (MimeInfo *) mimeinfo->node->children->data, &iter);
775 mimeinfo = mimeinfo->node->next != NULL ?
776 (MimeInfo *) mimeinfo->node->next->data : NULL;
780 static const gchar *get_real_part_name(MimeInfo *partinfo)
782 const gchar *name = NULL;
784 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
785 if (name == NULL)
786 name = procmime_mimeinfo_get_parameter(partinfo, "name");
788 return name;
791 static const gchar *get_part_name(MimeInfo *partinfo)
793 const gchar *name;
795 name = get_real_part_name(partinfo);
796 if (name == NULL)
797 name = "";
799 return name;
802 static const gchar *get_part_description(MimeInfo *partinfo)
804 if (partinfo->description)
805 return partinfo->description;
806 else
807 return get_part_name(partinfo);
810 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
812 FILE *fp;
813 const gchar *fname;
815 if (!partinfo) return;
817 fname = mimeview->file;
818 if (!fname) return;
820 if ((fp = claws_fopen(fname, "rb")) == NULL) {
821 FILE_OP_ERROR(fname, "claws_fopen");
822 return;
825 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
826 FILE_OP_ERROR(mimeview->file, "fseek");
827 claws_fclose(fp);
828 return;
831 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
832 textview_show_part(mimeview->textview, partinfo, fp);
834 claws_fclose(fp);
837 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
839 GSList *cur;
840 MimeViewerFactory *factory = NULL;
841 MimeViewer *viewer = NULL;
842 gchar *real_contenttype = NULL, *tmp;
844 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
846 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
847 MimeViewerFactory *curfactory = cur->data;
848 gint i = 0;
850 while (curfactory->content_types[i] != NULL) {
851 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
852 if (g_pattern_match_simple(tmp, real_contenttype)) {
853 debug_print("%s\n", curfactory->content_types[i]);
854 factory = curfactory;
855 g_free(tmp);
856 break;
858 g_free(tmp);
859 i++;
861 if (factory != NULL)
862 break;
864 g_free(real_contenttype);
865 if (factory == NULL)
866 return NULL;
868 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
869 MimeViewer *curviewer = cur->data;
871 if (curviewer->factory == factory)
872 return curviewer;
874 viewer = factory->create_viewer();
875 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
876 GTK_WIDGET(viewer->get_widget(viewer)));
878 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
880 return viewer;
883 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
885 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
888 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
890 gchar *content_type = NULL;
891 MimeViewer *viewer = NULL;
893 if ((partinfo->type == MIMETYPE_APPLICATION) &&
894 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
895 const gchar *filename;
897 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
898 if (filename == NULL)
899 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
900 if (filename != NULL)
901 content_type = procmime_get_mime_type(filename);
902 } else {
903 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
906 if (content_type != NULL) {
907 viewer = get_viewer_for_content_type(mimeview, content_type);
908 g_free(content_type);
911 return viewer;
914 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
916 MimeViewer *viewer;
918 if (mimeview->messageview->partial_display_shown) {
919 noticeview_hide(mimeview->messageview->noticeview);
920 mimeview->messageview->partial_display_shown = FALSE;
923 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
924 if (viewer == NULL) {
925 if (mimeview->mimeviewer != NULL)
926 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
927 mimeview->mimeviewer = NULL;
928 return FALSE;
931 if (mimeview->mimeviewer != NULL)
932 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
934 if (mimeview->mimeviewer != viewer)
935 mimeview->mimeviewer = viewer;
937 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
938 viewer->mimeview = mimeview;
939 viewer->show_mimepart(viewer, mimeview->file, partinfo);
941 return TRUE;
944 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
946 TextView *textview = mimeview->textview;
947 GtkWidget *focused = NULL;
949 if (mainwindow_get_mainwindow())
950 focused = gtkut_get_focused_child(
951 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
953 if ((mimeview->type != MIMEVIEW_VIEWER) &&
954 (mimeview->type == type)) return;
956 switch (type) {
957 case MIMEVIEW_TEXT:
958 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
959 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
960 GTK_WIDGET_PTR(textview)));
961 break;
962 case MIMEVIEW_VIEWER:
963 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
964 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
965 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
966 break;
967 default:
968 return;
970 if (focused)
971 gtk_widget_grab_focus(focused);
972 mimeview->type = type;
975 void mimeview_clear(MimeView *mimeview)
977 GtkTreeModel *model;
979 if (!mimeview)
980 return;
982 if (g_slist_find(mimeviews, mimeview) == NULL)
983 return;
985 if (mimeview->sig_check_timeout_tag != 0) {
986 g_source_remove(mimeview->sig_check_timeout_tag);
987 mimeview->sig_check_timeout_tag = 0;
990 if (mimeview->sig_check_cancellable != NULL) {
991 /* Set last_sig_check_task to NULL to discard results in async_cb */
992 mimeview->siginfo->last_sig_check_task = NULL;
993 g_cancellable_cancel(mimeview->sig_check_cancellable);
994 g_object_unref(mimeview->sig_check_cancellable);
995 mimeview->sig_check_cancellable = NULL;
998 noticeview_hide(mimeview->siginfoview);
1000 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
1001 gtk_tree_store_clear(GTK_TREE_STORE(model));
1003 textview_clear(mimeview->textview);
1004 if (mimeview->mimeviewer != NULL)
1005 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
1007 mimeview_free_mimeinfo(mimeview);
1009 mimeview->mimeinfo = NULL;
1011 gtk_tree_path_free(mimeview->opened);
1012 mimeview->opened = NULL;
1014 g_free(mimeview->file);
1015 mimeview->file = NULL;
1017 icon_list_clear(mimeview);
1018 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1021 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
1022 GtkUIManager *ui_manager;
1024 if (messageview->window != NULL)
1025 ui_manager = messageview->ui_manager;
1026 else
1027 ui_manager = messageview->mainwin->ui_manager;
1029 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
1032 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1033 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1035 static void update_signature_noticeview(MimeView *mimeview, gboolean special, SignatureStatus code)
1037 gchar *text = NULL, *button_text = NULL;
1038 void *func = NULL;
1039 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1040 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1042 if (mimeview == NULL || mimeview->siginfo == NULL)
1043 g_error("bad call to update noticeview");
1045 if (special)
1046 mycode = code;
1047 else
1048 mycode = privacy_mimeinfo_get_sig_status(mimeview->siginfo);
1050 switch (mycode) {
1051 case SIGNATURE_UNCHECKED:
1052 button_text = _("Check signature");
1053 func = check_signature_cb;
1054 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1055 break;
1056 case SIGNATURE_OK:
1057 button_text = _("View full information");
1058 func = display_full_info_cb;
1059 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1060 break;
1061 case SIGNATURE_WARN:
1062 button_text = _("View full information");
1063 func = display_full_info_cb;
1064 icon = STOCK_PIXMAP_PRIVACY_WARN;
1065 break;
1066 case SIGNATURE_KEY_EXPIRED:
1067 button_text = _("View full information");
1068 func = display_full_info_cb;
1069 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1070 break;
1071 case SIGNATURE_INVALID:
1072 button_text = _("View full information");
1073 func = display_full_info_cb;
1074 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1075 break;
1076 case SIGNATURE_CHECK_ERROR:
1077 case SIGNATURE_CHECK_FAILED:
1078 case SIGNATURE_CHECK_TIMEOUT:
1079 button_text = _("Check again");
1080 func = check_signature_cb;
1081 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1082 break;
1083 default:
1084 break;
1087 if (mycode == SIGNATURE_UNCHECKED) {
1088 gchar *tmp = privacy_mimeinfo_get_sig_info(mimeview->siginfo, FALSE);
1089 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1091 if (*shortcut == '\0')
1092 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1093 else
1094 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."), tmp, shortcut);
1096 g_free(shortcut);
1097 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1098 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1100 if (*shortcut == '\0')
1101 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1102 else
1103 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1105 g_free(shortcut);
1106 } else if (mycode == SIGNATURE_CHECK_ERROR) {
1107 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1109 if (*shortcut == '\0')
1110 text = g_strdup(_("Error checking the signature. Click the icon to try again."));
1111 else
1112 text = g_strdup_printf(_("Error checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1114 g_free(shortcut);
1115 } else {
1116 text = g_strdup(privacy_mimeinfo_get_sig_info(mimeview->siginfo, FALSE));
1119 noticeview_set_text(mimeview->siginfoview, text);
1120 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1121 g_free(text);
1123 noticeview_set_button_text(mimeview->siginfoview, NULL);
1124 noticeview_set_button_press_callback(
1125 mimeview->siginfoview,
1126 G_CALLBACK(func),
1127 (gpointer) mimeview);
1128 noticeview_set_icon(mimeview->siginfoview, icon);
1129 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1131 icon_list_clear(mimeview);
1132 icon_list_create(mimeview, mimeview->mimeinfo);
1135 static void check_signature_async_cb(GObject *source_object,
1136 GAsyncResult *async_result,
1137 gpointer user_data)
1139 GTask *task = G_TASK(async_result);
1140 GCancellable *cancellable;
1141 MimeView *mimeview = (MimeView *)user_data;
1142 gboolean cancelled;
1143 SigCheckTaskResult *result;
1144 GError *error = NULL;
1146 if (mimeview->siginfo == NULL) {
1147 debug_print("discarding stale sig check task result task:%p\n", task);
1148 return;
1149 } else if (task != mimeview->siginfo->last_sig_check_task) {
1150 debug_print("discarding stale sig check task result last_task:%p task:%p\n",
1151 mimeview->siginfo->last_sig_check_task, task);
1152 return;
1153 } else {
1154 debug_print("using sig check task result task:%p\n", task);
1155 mimeview->siginfo->last_sig_check_task = NULL;
1158 cancellable = g_task_get_cancellable(task);
1159 cancelled = g_cancellable_set_error_if_cancelled(cancellable, &error);
1160 if (cancelled) {
1161 debug_print("sig check task was cancelled: task:%p GError: domain:%s code:%d message:\"%s\"\n",
1162 task, g_quark_to_string(error->domain), error->code, error->message);
1163 g_error_free(error);
1164 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_TIMEOUT);
1165 return;
1166 } else {
1167 if (mimeview->sig_check_cancellable == NULL)
1168 g_error("bad cancellable");
1169 if (mimeview->sig_check_timeout_tag == 0)
1170 g_error("bad cancel source tag");
1172 g_source_remove(mimeview->sig_check_timeout_tag);
1173 mimeview->sig_check_timeout_tag = 0;
1174 g_object_unref(mimeview->sig_check_cancellable);
1175 mimeview->sig_check_cancellable = NULL;
1178 result = g_task_propagate_pointer(task, &error);
1180 if (mimeview->siginfo->sig_data) {
1181 privacy_free_signature_data(mimeview->siginfo->sig_data);
1182 mimeview->siginfo->sig_data = NULL;
1185 if (result == NULL) {
1186 debug_print("sig check task propagated NULL task:%p GError: domain:%s code:%d message:\"%s\"\n",
1187 task, g_quark_to_string(error->domain), error->code, error->message);
1188 g_error_free(error);
1189 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_ERROR);
1190 return;
1193 mimeview->siginfo->sig_data = result->sig_data;
1194 update_signature_noticeview(mimeview, FALSE, 0);
1196 if (result->newinfo) {
1197 g_warning("Check sig task returned an unexpected new MimeInfo");
1198 procmime_mimeinfo_free_all(&result->newinfo);
1201 g_free(result);
1204 gboolean mimeview_check_sig_timeout(gpointer user_data)
1206 MimeView *mimeview = (MimeView *)user_data;
1207 GCancellable *cancellable = mimeview->sig_check_cancellable;
1209 mimeview->sig_check_timeout_tag = 0;
1211 if (cancellable == NULL) {
1212 return G_SOURCE_REMOVE;
1215 mimeview->sig_check_cancellable = NULL;
1216 g_cancellable_cancel(cancellable);
1217 g_object_unref(cancellable);
1219 return G_SOURCE_REMOVE;
1222 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1224 MimeView *mimeview = (MimeView *) user_data;
1225 MimeInfo *mimeinfo = mimeview->siginfo;
1226 gint ret;
1228 if (mimeinfo == NULL || !noticeview_is_visible(mimeview->siginfoview))
1229 return;
1231 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1232 GTK_EVENTS_FLUSH();
1234 if (mimeview->sig_check_cancellable != NULL) {
1235 if (mimeview->sig_check_timeout_tag == 0)
1236 g_error("bad cancel source tag");
1237 g_source_remove(mimeview->sig_check_timeout_tag);
1238 g_cancellable_cancel(mimeview->sig_check_cancellable);
1239 g_object_unref(mimeview->sig_check_cancellable);
1242 mimeview->sig_check_cancellable = g_cancellable_new();
1244 ret = privacy_mimeinfo_check_signature(mimeview->siginfo,
1245 mimeview->sig_check_cancellable,
1246 check_signature_async_cb,
1247 mimeview);
1248 if (ret == 0) {
1249 mimeview->sig_check_timeout_tag = g_timeout_add_seconds(prefs_common.io_timeout_secs,
1250 mimeview_check_sig_timeout, mimeview);
1251 } else if (ret < 0) {
1252 g_object_unref(mimeview->sig_check_cancellable);
1253 mimeview->sig_check_cancellable = NULL;
1254 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_ERROR);
1255 } else {
1256 g_object_unref(mimeview->sig_check_cancellable);
1257 mimeview->sig_check_cancellable = NULL;
1258 update_signature_noticeview(mimeview, FALSE, 0);
1262 void mimeview_check_signature(MimeView *mimeview)
1264 check_signature_cb(NULL, mimeview);
1267 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1269 MimeView *mimeview = (MimeView *) user_data;
1270 gtk_tree_path_free(mimeview->opened);
1271 mimeview->opened = NULL;
1272 mimeview_selected(gtk_tree_view_get_selection(
1273 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1276 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1278 MimeView *mimeview = (MimeView *) user_data;
1280 textview_set_text(mimeview->textview, privacy_mimeinfo_get_sig_info(mimeview->siginfo, TRUE));
1281 noticeview_set_button_text(mimeview->siginfoview, NULL);
1282 noticeview_set_button_press_callback(
1283 mimeview->siginfoview,
1284 G_CALLBACK(redisplay_email),
1285 (gpointer) mimeview);
1286 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1289 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1291 MimeInfo *siginfo;
1292 MimeInfo *first_text;
1294 cm_return_if_fail(mimeview != NULL);
1295 cm_return_if_fail(selected != NULL);
1297 if (selected->type == MIMETYPE_MESSAGE
1298 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1299 /* if the first text part is signed, check that */
1300 first_text = selected;
1301 while (first_text && first_text->type != MIMETYPE_TEXT) {
1302 first_text = procmime_mimeinfo_next(first_text);
1304 if (first_text) {
1305 update_signature_info(mimeview, first_text);
1306 return;
1310 siginfo = selected;
1311 while (siginfo != NULL) {
1312 if (privacy_mimeinfo_is_signed(siginfo))
1313 break;
1314 siginfo = procmime_mimeinfo_parent(siginfo);
1316 mimeview->siginfo = siginfo;
1318 /* This shortcut boolean is there to correctly set the menu's
1319 * CheckSignature item sensitivity without killing performance
1320 * each time the menu sensitiveness is updated (a lot).
1322 mimeview->signed_part = (siginfo != NULL);
1324 if (siginfo == NULL) {
1325 noticeview_hide(mimeview->siginfoview);
1326 return;
1329 update_signature_noticeview(mimeview, FALSE, 0);
1330 noticeview_show(mimeview->siginfoview);
1333 void mimeview_show_part_as_text(MimeView *mimeview, MimeInfo *partinfo)
1335 cm_return_if_fail(mimeview != NULL);
1336 cm_return_if_fail(partinfo != NULL);
1338 mimeview_show_message_part(mimeview, partinfo);
1341 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1343 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1344 GtkTreeModel *model;
1345 GtkTreeIter iter;
1346 GtkTreePath *path;
1347 MimeInfo *partinfo;
1348 MainWindow *mainwin;
1349 GdkDisplay *display;
1350 GdkSeat *seat;
1351 GdkDevice *device;
1353 selection = gtk_tree_view_get_selection(ctree);
1354 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1355 return;
1357 path = gtk_tree_model_get_path(model, &iter);
1359 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1360 gtk_tree_path_free(path);
1361 return;
1364 gtk_tree_path_free(mimeview->opened);
1365 mimeview->opened = path;
1366 mimeview->spec_part = NULL;
1367 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1369 partinfo = mimeview_get_node_part(mimeview, path);
1370 if (!partinfo) return;
1372 /* ungrab the mouse event */
1373 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1374 gtk_grab_remove(GTK_WIDGET(ctree));
1375 display = gdk_window_get_display(gtk_widget_get_window(GTK_WIDGET(ctree)));
1376 seat = gdk_display_get_default_seat(display);
1377 device = gdk_seat_get_pointer(seat);
1378 if (gdk_display_device_is_grabbed(display, device))
1379 gdk_seat_ungrab(seat);
1382 mimeview->textview->default_text = FALSE;
1384 update_signature_info(mimeview, partinfo);
1386 if (!mimeview_show_part(mimeview, partinfo)) {
1387 switch (partinfo->type) {
1388 case MIMETYPE_TEXT:
1389 case MIMETYPE_MESSAGE:
1390 case MIMETYPE_MULTIPART:
1391 mimeview_show_message_part(mimeview, partinfo);
1393 break;
1394 default:
1395 mimeview->textview->default_text = TRUE;
1396 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1397 textview_clear(mimeview->textview);
1398 textview_show_mime_part(mimeview->textview, partinfo);
1399 break;
1402 mainwin = mainwindow_get_mainwindow();
1403 if (mainwin)
1404 main_window_set_menu_sensitive(mainwin);
1406 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1407 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1408 mimeview_check_signature(mimeview);
1412 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1413 MimeView *mimeview)
1415 GtkTreeSelection *selection;
1416 GtkTreeIter iter;
1418 if (!event) return FALSE;
1420 if (event->button == 2 || event->button == 3) {
1421 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1422 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1423 return FALSE;
1425 gtk_tree_selection_select_iter(selection, &iter);
1427 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1429 return FALSE;
1432 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1433 MimeView *mimeview)
1435 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1436 if (event->direction == GDK_SCROLL_UP) {
1437 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1438 } else if (event->direction == GDK_SCROLL_DOWN) {
1439 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1440 } else {
1441 gdouble x, y;
1443 if ((event->direction == GDK_SCROLL_SMOOTH) &&
1444 gdk_event_get_scroll_deltas((GdkEvent*)event, &x, &y)) {
1445 if (y < 0)
1446 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1447 else
1448 if (y >0)
1449 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1450 } else
1451 return FALSE; /* Scrolling left or right */
1453 gtk_vscrollbutton_scroll(scrollbutton);
1454 return TRUE;
1457 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1458 MimeInfo *partinfo)
1460 static MimeInfo *lastinfo;
1461 static guint32 lasttime;
1463 gint double_click_time;
1464 g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &double_click_time, NULL);
1466 if (event->button == 2 ||
1467 (event->button == 1 && (event->time - lasttime) < double_click_time && lastinfo == partinfo)) {
1468 /* call external program for image, audio or html */
1469 mimeview_launch(mimeview, partinfo);
1470 return TRUE;
1471 } else if (event->button == 3) {
1472 MainWindow *mainwin = mainwindow_get_mainwindow();
1474 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1475 partinfo->type == MIMETYPE_IMAGE ||
1476 partinfo->type == MIMETYPE_MULTIPART))
1477 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1478 else
1479 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1481 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1482 partinfo->type == MIMETYPE_IMAGE ||
1483 partinfo->type == MIMETYPE_TEXT))
1484 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", TRUE);
1485 else
1486 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", FALSE);
1487 #ifndef G_OS_WIN32
1488 if (partinfo &&
1489 partinfo->type == MIMETYPE_APPLICATION &&
1490 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1491 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1492 else
1493 #endif
1494 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1495 if (mainwin)
1496 main_window_set_menu_sensitive(mainwin);
1497 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1498 "pop_partinfo", partinfo);
1500 gtk_menu_popup_at_pointer(GTK_MENU(mimeview->popupmenu),
1501 (GdkEvent *)event);
1502 return TRUE;
1505 lastinfo = partinfo;
1506 lasttime = event->time;
1507 return FALSE;
1511 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1513 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1516 void mimeview_select_next_part(MimeView *mimeview)
1518 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1519 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1520 GtkTreeSelection *selection;
1521 GtkTreeIter iter;
1522 GtkTreePath *path;
1523 MimeInfo *partinfo = NULL;
1524 gboolean has_next;
1526 if (!mimeview->opened) return;
1528 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1529 path = gtk_tree_model_get_path(model, &iter);
1530 skip:
1531 has_next = mimeview_tree_next(model, path);
1532 if (!has_next) {
1533 has_next = gtk_tree_model_get_iter_first(model, &iter);
1534 gtk_tree_path_free(path);
1535 path = gtk_tree_model_get_path(model, &iter);
1538 if (has_next) {
1539 partinfo = mimeview_get_node_part(mimeview, path);
1540 if (partinfo->type == MIMETYPE_MULTIPART ||
1541 (!prefs_common.show_inline_attachments && partinfo->id))
1542 goto skip;
1543 selection = gtk_tree_view_get_selection(ctree);
1544 gtk_tree_selection_select_path(selection, path);
1545 icon_list_toggle_by_mime_info(mimeview, partinfo);
1548 gtk_tree_path_free(path);
1551 void mimeview_select_prev_part(MimeView *mimeview)
1553 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1554 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1555 GtkTreeSelection *selection;
1556 GtkTreeIter iter;
1557 GtkTreePath *path;
1558 MimeInfo *partinfo = NULL;
1559 gboolean has_prev;
1561 if (!mimeview->opened) return;
1563 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1564 path = gtk_tree_model_get_path(model, &iter);
1565 skip:
1566 has_prev = mimeview_tree_prev(mimeview, path);
1567 if (!has_prev) {
1568 while (mimeview_tree_next(model, path)) {}
1569 has_prev = TRUE;
1572 if (has_prev) {
1573 partinfo = mimeview_get_node_part(mimeview, path);
1574 if (partinfo->type == MIMETYPE_MULTIPART ||
1575 (!prefs_common.show_inline_attachments && partinfo->id))
1576 goto skip;
1577 selection = gtk_tree_view_get_selection(ctree);
1578 gtk_tree_selection_select_path(selection, path);
1579 icon_list_toggle_by_mime_info(mimeview, partinfo);
1582 gtk_tree_path_free(path);
1585 #define BREAK_ON_MODIFIER_KEY() \
1586 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1588 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1589 MimeView *mimeview)
1591 SummaryView *summaryview;
1593 if (!event) return FALSE;
1594 if (!mimeview->opened) return FALSE;
1596 summaryview = mimeview->messageview->mainwin->summaryview;
1598 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1599 return FALSE;
1601 switch (event->keyval) {
1602 case GDK_KEY_Home:
1603 case GDK_KEY_End:
1604 textview_scroll_max(mimeview->textview,
1605 (event->keyval == GDK_KEY_Home));
1606 return TRUE;
1607 case GDK_KEY_Page_Down:
1608 case GDK_KEY_space:
1609 if (mimeview_scroll_page(mimeview,
1610 (event->state & GDK_SHIFT_MASK) != 0))
1611 return TRUE;
1613 if (!(event->state & GDK_SHIFT_MASK))
1614 mimeview_select_next_part(mimeview);
1615 return TRUE;
1616 case GDK_KEY_Page_Up:
1617 case GDK_KEY_BackSpace:
1618 mimeview_scroll_page(mimeview, TRUE);
1619 return TRUE;
1620 case GDK_KEY_Return:
1621 case GDK_KEY_KP_Enter:
1622 mimeview_scroll_one_line(mimeview,
1623 (event->state & GDK_MOD1_MASK) != 0);
1624 return TRUE;
1625 case GDK_KEY_Up:
1626 case GDK_KEY_Down:
1627 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1628 return TRUE;
1629 default:
1630 break;
1633 if (mimeview->messageview->new_window) return FALSE;
1635 return summary_pass_key_press_event(summaryview, event);
1638 static void mimeview_drag_data_get(GtkWidget *widget,
1639 GdkDragContext *drag_context,
1640 GtkSelectionData *selection_data,
1641 guint info,
1642 guint time,
1643 MimeView *mimeview)
1645 gchar *filename = NULL, *uriname, *tmp = NULL;
1646 MimeInfo *partinfo;
1647 gint err;
1648 gint count = 0;
1650 if (!mimeview->opened) return;
1651 if (!mimeview->file) return;
1653 partinfo = mimeview_get_selected_part(mimeview);
1654 if (!partinfo) return;
1656 if (strlen(get_part_name(partinfo)) > 0) {
1657 filename = g_path_get_basename(get_part_name(partinfo));
1658 if (filename) {
1659 if (*filename == '\0') {
1660 g_free(filename);
1661 return;
1664 } else if (partinfo->type == MIMETYPE_MESSAGE
1665 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1666 gchar *name = NULL;
1667 GPtrArray *headers = NULL;
1668 FILE *fp;
1670 fp = claws_fopen(partinfo->data.filename, "rb");
1671 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1672 headers = procheader_get_header_array(fp);
1673 if (headers) {
1674 gint i;
1675 for (i = 0; i < headers->len; i++) {
1676 Header *header = g_ptr_array_index(headers, i);
1677 if (procheader_headername_equal(header->name, "Subject")) {
1678 unfold_line(header->body);
1679 name = g_strconcat(header->body, ".txt", NULL);
1680 subst_for_filename(name);
1683 procheader_header_array_destroy(headers);
1686 if (fp != NULL)
1687 claws_fclose(fp);
1688 if (name)
1689 filename = g_path_get_basename(name);
1690 g_free(name);
1692 if (filename == NULL)
1693 filename = g_path_get_basename("Unnamed part");
1695 if (!g_utf8_validate(filename, -1, NULL))
1696 tmp = conv_codeset_strdup(filename,
1697 conv_get_locale_charset_str(),
1698 CS_UTF_8);
1700 if (tmp == NULL) {
1701 g_warning("filename not in UTF-8");
1702 tmp = g_strdup(filename);
1704 g_free(filename);
1705 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1706 tmp, NULL);
1708 check_new_file:
1709 if (is_file_exist(filename)) {
1710 gchar *ext = NULL;
1711 gchar *prefix = NULL;
1712 gchar *new_name = NULL;
1713 if (strrchr(tmp, '.')) {
1714 prefix = g_strdup(tmp);
1715 ext = g_strdup(strrchr(tmp, '.'));
1716 *(strrchr(prefix, '.')) = '\0';
1717 } else {
1718 prefix = g_strdup(tmp);
1719 ext = g_strdup("");
1721 count++;
1722 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1723 g_free(prefix);
1724 g_free(ext);
1725 g_free(filename);
1726 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1727 new_name, NULL);
1728 g_free(new_name);
1729 goto check_new_file;
1732 g_free(tmp);
1734 if ((err = procmime_get_part(filename, partinfo)) < 0)
1735 alertpanel_error
1736 (_("Couldn't save the part of multipart message: %s"),
1737 g_strerror(-err));
1739 tmp = g_filename_to_uri(filename, NULL, NULL);
1740 uriname = g_strconcat(tmp, "\r\n", NULL);
1741 g_free(tmp);
1743 gtk_selection_data_set(selection_data,
1744 gtk_selection_data_get_target(selection_data), 8,
1745 (guchar *)uriname, strlen(uriname));
1747 g_free(uriname);
1748 g_free(filename);
1752 * Returns a filename (with path) for an attachment
1753 * \param partinfo The attachment to save
1754 * \param basedir The target directory
1755 * \param number Used for dummy filename if attachment is unnamed
1757 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1758 const gchar *basedir,
1759 gint number)
1761 gchar *fullname;
1762 gchar *filename;
1764 filename = g_strdup(get_part_name(partinfo));
1765 if (!filename || !*filename) {
1766 g_free(filename);
1767 filename = g_strdup_printf("noname.%d", number);
1770 if (!g_utf8_validate(filename, -1, NULL)) {
1771 gchar *tmp = conv_filename_to_utf8(filename);
1772 g_free(filename);
1773 filename = tmp;
1776 subst_for_filename(filename);
1778 fullname = g_strconcat
1779 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1780 ? &filename[1] : filename, NULL);
1782 g_free(filename);
1783 filename = conv_filename_from_utf8(fullname);
1784 g_free(fullname);
1785 return filename;
1789 * Write a single attachment to file
1790 * \param filename Filename with path
1791 * \param partinfo Attachment to save
1793 static gint mimeview_write_part(const gchar *filename,
1794 MimeInfo *partinfo,
1795 gboolean handle_error)
1797 gchar *dir;
1798 gint err;
1800 dir= g_path_get_dirname(filename);
1801 if (!is_dir_exist(dir))
1802 make_dir_hier(dir);
1803 g_free(dir);
1805 if (is_file_exist(filename)) {
1806 AlertValue aval;
1807 gchar *res;
1808 gchar *tmp;
1810 if (!g_utf8_validate(filename, -1, NULL))
1811 tmp = conv_filename_to_utf8(filename);
1812 else
1813 tmp = g_strdup(filename);
1815 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1816 tmp);
1817 g_free(tmp);
1818 aval = alertpanel(_("Overwrite"), res, NULL, _("_No"),
1819 NULL, _("_Yes"), NULL, NULL, ALERTFOCUS_FIRST);
1820 g_free(res);
1821 if (G_ALERTALTERNATE != aval)
1822 return 2;
1825 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1826 debug_print("error saving MIME part: %d\n", err);
1827 if (handle_error)
1828 alertpanel_error
1829 (_("Couldn't save the part of multipart message: %s"),
1830 g_strerror(-err));
1831 return 0;
1834 return 1;
1837 static AlertValue mimeview_save_all_error_ask(gint n)
1839 gchar *message = g_strdup_printf(
1840 _("An error has occurred while saving message part %d. "
1841 "Do you want to cancel saving or ignore error and "
1842 "continue?"), n);
1843 AlertValue av = alertpanel_full(_("Error saving message part"),
1844 message, NULL, _("_Cancel"), NULL, _("Ignore"), NULL, _("Ignore all"),
1845 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1846 g_free(message);
1847 return av;
1850 static void mimeview_save_all_info(gint errors, gint total)
1852 AlertValue aval;
1854 if (!errors && prefs_common.show_save_all_success) {
1855 gchar *msg = g_strdup_printf(
1856 ngettext("%d file saved successfully.",
1857 "%d files saved successfully.",
1858 total),
1859 total);
1860 aval = alertpanel_full(_("Notice"), msg, NULL, _("_Close"),
1861 NULL, NULL, NULL, NULL, ALERTFOCUS_FIRST,
1862 TRUE, NULL, ALERT_NOTICE);
1863 g_free(msg);
1864 if (aval & G_ALERTDISABLE)
1865 prefs_common.show_save_all_success = FALSE;
1866 } else if (prefs_common.show_save_all_failure) {
1867 gchar *msg1 = g_strdup_printf(
1868 ngettext("%d file saved successfully",
1869 "%d files saved successfully",
1870 total - errors),
1871 total - errors);
1872 gchar *msg2 = g_strdup_printf(
1873 ngettext("%s, %d file failed.",
1874 "%s, %d files failed.",
1875 errors),
1876 msg1, errors);
1877 aval = alertpanel_full(_("Warning"), msg2, NULL, _("_Close"),
1878 NULL, NULL, NULL, NULL, ALERTFOCUS_FIRST,
1879 TRUE, NULL, ALERT_WARNING);
1880 g_free(msg2);
1881 g_free(msg1);
1882 if (aval & G_ALERTDISABLE)
1883 prefs_common.show_save_all_failure = FALSE;
1888 * Menu callback: Save all attached files
1889 * \param mimeview Current display
1891 static void mimeview_save_all(MimeView *mimeview)
1893 MimeInfo *partinfo;
1894 gchar *dirname;
1895 gchar *startdir = NULL;
1896 gint number = 0, errors = 0;
1897 gboolean skip_errors = FALSE;
1899 if (!mimeview->opened) return;
1900 if (!mimeview->file) return;
1901 if (!mimeview->mimeinfo) return;
1903 partinfo = mimeview->mimeinfo;
1904 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1905 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1906 else
1907 startdir = g_strdup(get_home_dir());
1909 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1910 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1911 if (!dirname) {
1912 g_free(startdir);
1913 return;
1916 if (!is_dir_exist (dirname)) {
1917 alertpanel_error(_("'%s' is not a directory."), dirname);
1918 g_free(startdir);
1919 g_free(dirname);
1920 return;
1923 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1924 dirname[strlen(dirname)-1] = '\0';
1926 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1927 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1928 partinfo = procmime_mimeinfo_next(partinfo);
1929 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1930 partinfo = procmime_mimeinfo_next(partinfo);
1931 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1932 partinfo = procmime_mimeinfo_next(partinfo);
1935 while (partinfo != NULL) {
1936 if (partinfo->type != MIMETYPE_MESSAGE &&
1937 partinfo->type != MIMETYPE_MULTIPART &&
1938 (partinfo->disposition != DISPOSITIONTYPE_INLINE
1939 || get_real_part_name(partinfo) != NULL)) {
1940 gchar *filename = mimeview_get_filename_for_part(
1941 partinfo, dirname, number++);
1943 gint ok = mimeview_write_part(filename, partinfo, FALSE);
1944 g_free(filename);
1945 if (ok < 1) {
1946 ++errors;
1947 if (!skip_errors) {
1948 AlertValue av = mimeview_save_all_error_ask(number);
1949 skip_errors = (av == G_ALERTOTHER);
1950 if (av == G_ALERTDEFAULT) /* cancel */
1951 break;
1953 } else if (ok == 2)
1954 number--;
1956 partinfo = procmime_mimeinfo_next(partinfo);
1959 g_free(prefs_common.attach_save_dir);
1960 g_free(startdir);
1961 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1962 -1, NULL, NULL, NULL);
1963 g_free(dirname);
1965 mimeview_save_all_info(errors, number);
1968 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1970 MimeInfo *partinfo = NULL;
1971 if (mimeview->spec_part) {
1972 partinfo = mimeview->spec_part;
1973 mimeview->spec_part = NULL;
1974 } else {
1975 partinfo = (MimeInfo *) g_object_get_data
1976 (G_OBJECT(mimeview->popupmenu),
1977 "pop_partinfo");
1978 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1979 "pop_partinfo", NULL);
1980 if (!partinfo) {
1981 partinfo = mimeview_get_selected_part(mimeview);
1985 return partinfo;
1988 * Menu callback: Save the selected attachment
1989 * \param mimeview Current display
1991 void mimeview_save_as(MimeView *mimeview)
1993 gchar *filename;
1994 gchar *filepath = NULL;
1995 gchar *filedir = NULL;
1996 MimeInfo *partinfo;
1997 gchar *partname = NULL;
1999 if (!mimeview->opened) return;
2000 if (!mimeview->file) return;
2002 partinfo = mimeview_get_part_to_use(mimeview);
2004 cm_return_if_fail(partinfo != NULL);
2006 if (get_part_name(partinfo) == NULL) {
2007 return;
2009 partname = g_strdup(get_part_name(partinfo));
2011 if (!g_utf8_validate(partname, -1, NULL)) {
2012 gchar *tmp = conv_filename_to_utf8(partname);
2013 if (!tmp) {
2014 tmp = conv_codeset_strdup(partname,
2015 conv_get_locale_charset_str(),
2016 CS_UTF_8);
2018 if (tmp) {
2019 g_free(partname);
2020 partname = tmp;
2024 subst_for_filename(partname);
2026 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2027 filepath = g_strconcat(prefs_common.attach_save_dir,
2028 G_DIR_SEPARATOR_S, partname, NULL);
2029 else
2030 filepath = g_strdup(partname);
2032 g_free(partname);
2034 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2035 filename = filesel_select_file_save(_("Save as"), filepath);
2036 if (!filename) {
2037 g_free(filepath);
2038 return;
2041 mimeview_write_part(filename, partinfo, TRUE);
2043 filedir = g_path_get_dirname(filename);
2044 if (filedir && strcmp(filedir, ".")) {
2045 g_free(prefs_common.attach_save_dir);
2046 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2049 g_free(filedir);
2050 g_free(filepath);
2053 void mimeview_display_as_text(MimeView *mimeview)
2055 MimeInfo *partinfo;
2057 if (!mimeview->opened) return;
2059 partinfo = mimeview_get_part_to_use(mimeview);
2060 mimeview_select_mimepart_icon(mimeview, partinfo);
2061 cm_return_if_fail(partinfo != NULL);
2062 mimeview_show_message_part(mimeview, partinfo);
2065 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2067 gchar *filename;
2068 gint err;
2070 if (!mimeview->opened) return;
2071 if (!mimeview->file) return;
2073 if (!partinfo)
2074 partinfo = mimeview_get_part_to_use(mimeview);
2076 cm_return_if_fail(partinfo != NULL);
2078 filename = procmime_get_tmp_file_name(partinfo);
2080 if ((err = procmime_get_part(filename, partinfo)) < 0)
2081 alertpanel_error
2082 (_("Couldn't save the part of multipart message: %s"),
2083 g_strerror(-err));
2084 else
2085 mimeview_view_file(filename, partinfo, NULL, mimeview);
2087 g_free(filename);
2090 #ifndef G_OS_WIN32
2091 void mimeview_open_with(MimeView *mimeview)
2093 MimeInfo *partinfo;
2095 if (!mimeview) return;
2096 if (!mimeview->opened) return;
2097 if (!mimeview->file) return;
2099 partinfo = mimeview_get_part_to_use(mimeview);
2101 mimeview_open_part_with(mimeview, partinfo, FALSE);
2104 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2106 gchar *filename;
2107 gchar *cmd;
2108 gchar *mime_command = NULL;
2109 gchar *content_type = NULL;
2110 gint err;
2112 cm_return_if_fail(partinfo != NULL);
2114 filename = procmime_get_tmp_file_name(partinfo);
2116 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2117 alertpanel_error
2118 (_("Couldn't save the part of multipart message: %s"),
2119 g_strerror(-err));
2120 g_free(filename);
2121 return;
2124 if (!prefs_common.mime_open_cmd_history)
2125 prefs_common.mime_open_cmd_history =
2126 add_history(NULL, prefs_common.mime_open_cmd);
2128 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2129 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2130 /* guess content-type from filename */
2131 content_type = procmime_get_mime_type(filename);
2133 if (content_type == NULL) {
2134 content_type = procmime_get_content_type_str(partinfo->type,
2135 partinfo->subtype);
2138 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2139 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2140 mime_command = g_strdup(prefs_common_get_uri_cmd());
2141 g_free(content_type);
2142 content_type = NULL;
2143 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2144 || !prefs_common_get_ext_editor_cmd()[0]) {
2145 mime_command = mailcap_get_command_for_type(content_type, filename);
2146 } else {
2147 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2148 g_free(content_type);
2149 content_type = NULL;
2151 if (mime_command == NULL) {
2152 /* try with extension this time */
2153 g_free(content_type);
2154 content_type = procmime_get_mime_type(filename);
2155 mime_command = mailcap_get_command_for_type(content_type, filename);
2158 if (mime_command == NULL)
2159 automatic = FALSE;
2161 if (!automatic) {
2162 gboolean remember = FALSE;
2163 if (content_type != NULL)
2164 cmd = input_dialog_combo_remember
2165 (_("Open with"),
2166 _("Enter the command-line to open file:\n"
2167 "('%s' will be replaced with file name)"),
2168 mime_command ? mime_command : prefs_common.mime_open_cmd,
2169 prefs_common.mime_open_cmd_history, &remember);
2170 else
2171 cmd = input_dialog_combo
2172 (_("Open with"),
2173 _("Enter the command-line to open file:\n"
2174 "('%s' will be replaced with file name)"),
2175 mime_command ? mime_command : prefs_common.mime_open_cmd,
2176 prefs_common.mime_open_cmd_history);
2177 if (cmd && remember) {
2178 mailcap_update_default(content_type, cmd);
2180 g_free(mime_command);
2181 } else {
2182 cmd = mime_command;
2184 if (cmd) {
2185 mimeview_view_file(filename, partinfo, cmd, mimeview);
2186 g_free(prefs_common.mime_open_cmd);
2187 prefs_common.mime_open_cmd = cmd;
2188 prefs_common.mime_open_cmd_history =
2189 add_history(prefs_common.mime_open_cmd_history, cmd);
2192 g_free(content_type);
2193 g_free(filename);
2195 #endif
2197 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2199 GList *attach_file = NULL;
2200 AttachInfo *ainfo = NULL;
2201 gchar *filename;
2202 gint err;
2204 if (!mimeview->opened) return;
2205 if (!mimeview->file) return;
2207 cm_return_if_fail(partinfo != NULL);
2209 filename = procmime_get_tmp_file_name(partinfo);
2211 if (!(err = procmime_get_part(filename, partinfo))) {
2212 ainfo = g_new0(AttachInfo, 1);
2213 ainfo->file = filename;
2214 ainfo->name = g_strdup(get_part_name(partinfo));
2215 ainfo->content_type = procmime_get_content_type_str(
2216 partinfo->type, partinfo->subtype);
2217 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2218 partinfo, "charset"));
2219 attach_file = g_list_append(attach_file, ainfo);
2221 compose_new(NULL, NULL, attach_file);
2223 g_free(ainfo->name);
2224 g_free(ainfo->content_type);
2225 g_free(ainfo->charset);
2226 g_free(ainfo);
2227 g_list_free(attach_file);
2228 } else
2229 alertpanel_error
2230 (_("Couldn't save the part of multipart message: %s"),
2231 g_strerror(-err));
2232 g_free(filename);
2235 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2236 const gchar *cmd, MimeView *mimeview)
2238 #ifndef G_OS_WIN32
2239 gchar *p;
2240 gchar buf[BUFFSIZE];
2242 if (cmd == NULL)
2243 mimeview_open_part_with(mimeview, partinfo, TRUE);
2244 else {
2245 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2246 !strchr(p + 2, '%')) {
2247 g_snprintf(buf, sizeof(buf), cmd, filename);
2248 if (!prefs_common.save_parts_readwrite)
2249 g_chmod(filename, S_IRUSR);
2250 else
2251 g_chmod(filename, S_IRUSR|S_IWUSR);
2252 } else {
2253 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2254 mimeview_open_part_with(mimeview, partinfo, FALSE);
2256 if (execute_command_line(buf, TRUE, NULL) != 0) {
2257 if (!prefs_common.save_parts_readwrite)
2258 g_chmod(filename, S_IRUSR|S_IWUSR);
2259 mimeview_open_part_with(mimeview, partinfo, FALSE);
2262 #else
2263 SHFILEINFO file_info;
2264 GError *error = NULL;
2265 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2267 if (error != NULL) {
2268 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2269 error->message);
2270 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2271 g_error_free(error);
2272 return;
2275 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2276 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2277 _("This attachment is an executable file. Executing "
2278 "untrusted binaries is dangerous and could compromise "
2279 "your computer.\n\n"
2280 "Do you want to run this file?"), NULL, _("_Cancel"),
2281 NULL, _("Run binary"), NULL, NULL, ALERTFOCUS_FIRST,
2282 FALSE, NULL, ALERT_WARNING);
2283 if (val == G_ALERTALTERNATE) {
2284 debug_print("executing binary\n");
2285 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2287 } else {
2288 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2291 g_free(fn16);
2293 #endif
2296 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2298 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2301 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2303 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2306 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2308 GSList *mimeview_list, *viewer_list;
2310 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2311 MimeView *mimeview = (MimeView *) mimeview_list->data;
2313 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2314 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2315 mimeview->mimeviewer = NULL;
2318 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2319 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2321 mimeviewer->destroy_viewer(mimeviewer);
2322 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2326 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2329 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2331 gint num;
2332 MimeInfo *partinfo;
2334 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2335 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2337 if (event->button == 1) {
2338 icon_selected(mimeview, num, partinfo);
2339 gtk_widget_grab_focus(button);
2340 icon_list_toggle_by_mime_info(mimeview, partinfo);
2342 part_button_pressed(mimeview, event, partinfo);
2344 return FALSE;
2347 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2349 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2350 GtkTreeIter iter;
2351 GtkTreePath *path;
2352 MimeInfo *curr = NULL;
2354 if (!gtk_tree_model_get_iter_first(model, &iter))
2355 return;
2356 path = gtk_tree_model_get_path(model, &iter);
2358 do {
2359 gtk_tree_model_get_iter(model, &iter, path);
2360 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2361 if (curr == partinfo) {
2362 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2363 GTK_TREE_VIEW(mimeview->ctree));
2364 gtk_tree_selection_select_iter(sel, &iter);
2365 gtk_tree_path_free(path);
2366 return;
2368 } while (mimeview_tree_next(model, path));
2370 gtk_tree_path_free(path);
2373 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2375 icon_list_toggle_by_mime_info(mimeview, partinfo);
2376 icon_selected(mimeview, -1, partinfo);
2379 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2380 MimeView *mimeview)
2382 SummaryView *summaryview;
2384 if (!event) return FALSE;
2386 switch (event->keyval) {
2387 case GDK_KEY_space:
2388 if (mimeview_scroll_page(mimeview, FALSE))
2389 return TRUE;
2391 mimeview_select_next_part(mimeview);
2392 return TRUE;
2394 break;
2395 case GDK_KEY_BackSpace:
2396 mimeview_scroll_page(mimeview, TRUE);
2397 return TRUE;
2398 case GDK_KEY_Return:
2399 case GDK_KEY_KP_Enter:
2400 mimeview_scroll_one_line(mimeview,
2401 (event->state & GDK_MOD1_MASK) != 0);
2402 return TRUE;
2403 case GDK_KEY_y:
2404 BREAK_ON_MODIFIER_KEY();
2405 mimeview_save_as(mimeview);
2406 return TRUE;
2407 case GDK_KEY_t:
2408 BREAK_ON_MODIFIER_KEY();
2409 mimeview_display_as_text(mimeview);
2410 return TRUE;
2411 case GDK_KEY_l:
2412 BREAK_ON_MODIFIER_KEY();
2413 mimeview_launch(mimeview, NULL);
2414 return TRUE;
2415 #ifndef G_OS_WIN32
2416 case GDK_KEY_o:
2417 BREAK_ON_MODIFIER_KEY();
2418 mimeview_open_with(mimeview);
2419 return TRUE;
2420 #endif
2421 case GDK_KEY_c:
2422 BREAK_ON_MODIFIER_KEY();
2423 mimeview_check_signature(mimeview);
2424 return TRUE;
2425 case GDK_KEY_a:
2426 BREAK_ON_MODIFIER_KEY();
2427 mimeview_select_next_part(mimeview);
2428 return TRUE;
2429 default:
2430 break;
2433 if (!mimeview->messageview->mainwin) return FALSE;
2434 summaryview = mimeview->messageview->mainwin->summaryview;
2435 return summary_pass_key_press_event(summaryview, event);
2438 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2440 MimeView *mimeview = (MimeView *)data;
2441 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2443 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2444 "pop_partinfo", partinfo);
2445 gtk_menu_popup_at_pointer(GTK_MENU(mimeview->popupmenu), NULL);
2446 return TRUE;
2449 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2451 GtkWidget *pixmap = NULL;
2452 GtkWidget *grid;
2453 GtkWidget *button;
2454 gchar *tip;
2455 gchar *tiptmp;
2456 const gchar *desc = NULL;
2457 gchar *sigshort = NULL;
2458 gchar *content_type;
2459 StockPixmap stockp;
2460 MimeInfo *partinfo;
2461 MimeInfo *siginfo = NULL;
2462 MimeInfo *encrypted = NULL;
2463 #ifdef GENERIC_UMPC
2464 GtkRequisition r;
2465 #endif
2467 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2468 return;
2470 grid = mimeview->icon_grid;
2471 mimeview->icon_count++;
2472 button = gtk_event_box_new();
2474 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2475 G_CALLBACK(mimeview_visi_notify), mimeview);
2476 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2477 G_CALLBACK(mimeview_leave_notify), mimeview);
2478 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2479 G_CALLBACK(mimeview_enter_notify), mimeview);
2481 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2482 g_object_set_data(G_OBJECT(button), "icon_number",
2483 GINT_TO_POINTER(mimeview->icon_count));
2484 g_object_set_data(G_OBJECT(button), "partinfo",
2485 mimeinfo);
2487 switch (mimeinfo->type) {
2489 case MIMETYPE_TEXT:
2490 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2491 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2492 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2493 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2494 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2495 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2496 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2497 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2498 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2499 else
2500 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2501 break;
2502 case MIMETYPE_MESSAGE:
2503 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2504 break;
2505 case MIMETYPE_APPLICATION:
2506 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2507 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2508 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2509 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2510 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2511 stockp = STOCK_PIXMAP_MIME_PDF;
2512 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2513 stockp = STOCK_PIXMAP_MIME_PS;
2514 else
2515 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2516 break;
2517 case MIMETYPE_IMAGE:
2518 stockp = STOCK_PIXMAP_MIME_IMAGE;
2519 break;
2520 case MIMETYPE_AUDIO:
2521 stockp = STOCK_PIXMAP_MIME_AUDIO;
2522 break;
2523 default:
2524 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2525 break;
2528 partinfo = mimeinfo;
2529 while (partinfo != NULL) {
2530 if (privacy_mimeinfo_is_signed(partinfo)) {
2531 siginfo = partinfo;
2532 break;
2534 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2535 encrypted = partinfo;
2536 break;
2538 partinfo = procmime_mimeinfo_parent(partinfo);
2541 if (siginfo != NULL) {
2542 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2543 case SIGNATURE_UNCHECKED:
2544 case SIGNATURE_CHECK_ERROR:
2545 case SIGNATURE_CHECK_FAILED:
2546 case SIGNATURE_CHECK_TIMEOUT:
2547 pixmap = stock_pixmap_widget_with_overlay(stockp,
2548 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2549 break;
2550 case SIGNATURE_OK:
2551 pixmap = stock_pixmap_widget_with_overlay(stockp,
2552 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2553 break;
2554 case SIGNATURE_WARN:
2555 case SIGNATURE_KEY_EXPIRED:
2556 pixmap = stock_pixmap_widget_with_overlay(stockp,
2557 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2558 break;
2559 case SIGNATURE_INVALID:
2560 pixmap = stock_pixmap_widget_with_overlay(stockp,
2561 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2562 break;
2564 sigshort = privacy_mimeinfo_get_sig_info(siginfo, FALSE);
2565 } else if (encrypted != NULL) {
2566 pixmap = stock_pixmap_widget_with_overlay(stockp,
2567 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2568 } else {
2569 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2570 OVERLAY_NONE, 6, 3);
2572 gtk_container_add(GTK_CONTAINER(button), pixmap);
2573 if (!desc) {
2574 if (prefs_common.attach_desc)
2575 desc = get_part_description(mimeinfo);
2576 else
2577 desc = get_part_name(mimeinfo);
2580 content_type = procmime_get_content_type_str(mimeinfo->type,
2581 mimeinfo->subtype);
2583 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2584 "\n<b>", _("Size:"), " </b>",
2585 to_human_readable((goffset)mimeinfo->length), NULL);
2586 g_free(content_type);
2587 if (desc && *desc) {
2588 gchar *tmp = NULL, *escaped = NULL;
2589 if (!g_utf8_validate(desc, -1, NULL)) {
2590 tmp = conv_filename_to_utf8(desc);
2591 } else {
2592 tmp = g_strdup(desc);
2594 escaped = g_markup_escape_text(tmp,-1);
2596 tiptmp = g_strconcat(tip, "\n<b>",
2597 prefs_common.attach_desc && mimeinfo->description ?
2598 _("Description:") : _("Filename:"),
2599 " </b>", escaped, NULL);
2600 g_free(tip);
2601 tip = tiptmp;
2602 g_free(tmp);
2603 g_free(escaped);
2605 if (sigshort && *sigshort) {
2606 gchar *sigshort_escaped =
2607 g_markup_escape_text(sigshort, -1);
2609 tiptmp = g_strjoin("\n", tip, sigshort_escaped, NULL);
2610 g_free(tip);
2611 g_free(sigshort_escaped);
2612 tip = tiptmp;
2615 gtk_widget_set_tooltip_markup(button, tip);
2616 g_free(tip);
2617 gtk_widget_show_all(button);
2618 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2619 mimeview_mime_types, 1, GDK_ACTION_COPY);
2621 g_signal_connect(G_OBJECT(button), "popup-menu",
2622 G_CALLBACK(icon_popup_menu), mimeview);
2623 g_signal_connect(G_OBJECT(button), "button_release_event",
2624 G_CALLBACK(icon_clicked_cb), mimeview);
2625 g_signal_connect(G_OBJECT(button), "key_press_event",
2626 G_CALLBACK(icon_key_pressed), mimeview);
2627 g_signal_connect(G_OBJECT(button), "drag_data_get",
2628 G_CALLBACK(mimeview_drag_data_get), mimeview);
2629 gtk_container_add(GTK_CONTAINER(grid), button);
2630 #ifdef GENERIC_UMPC
2631 gtk_widget_get_preferred_size(pixmap, &r, NULL);
2632 gtk_widget_set_size_request(button, -1, r.height + 4);
2633 #endif
2637 static void icon_list_clear (MimeView *mimeview)
2639 GList *child, *orig;
2640 GtkAdjustment *adj;
2642 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_grid));
2643 for (child = orig; child != NULL; child = g_list_next(child)) {
2644 gtk_container_remove(GTK_CONTAINER(mimeview->icon_grid),
2645 GTK_WIDGET(child->data));
2647 g_list_free(orig);
2648 mimeview->icon_count = 0;
2649 adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(mimeview->icon_scroll));
2650 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2654 *\brief Used to 'click' the next or previous icon.
2656 *\return true if the icon 'number' exists and was selected.
2658 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2659 GtkAllocation *size, MimeView *mimeview)
2661 GtkAllocation grid_size;
2662 GtkAllocation layout_size;
2663 GtkAdjustment *adj;
2664 guint width;
2665 guint height;
2667 adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(mimeview->icon_scroll));
2669 gtk_widget_get_allocation(mimeview->icon_grid, &grid_size);
2670 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2672 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2673 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2674 MIN(grid_size.width, layout_size.width),
2675 MAX(grid_size.height, layout_size.height));
2676 gtk_adjustment_set_step_increment(adj, 10);
2679 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2681 gint min_width, width;
2683 cm_return_if_fail(mimeinfo != NULL);
2685 while (mimeinfo != NULL) {
2686 if (mimeinfo->type != MIMETYPE_MULTIPART)
2687 icon_list_append_icon(mimeview, mimeinfo);
2688 if (mimeinfo->node->children != NULL)
2689 icon_list_create(mimeview,
2690 (MimeInfo *) mimeinfo->node->children->data);
2691 mimeinfo = mimeinfo->node->next != NULL
2692 ? (MimeInfo *) mimeinfo->node->next->data
2693 : NULL;
2695 gtk_widget_get_preferred_width(mimeview->icon_mainbox, &min_width, &width);
2696 if (min_width < width) {
2697 gtk_widget_set_size_request(mimeview->icon_mainbox,
2698 min_width, -1);
2700 if (mimeview->opened)
2701 icon_list_toggle_by_mime_info(mimeview,
2702 mimeview_get_node_part(mimeview, mimeview->opened));
2705 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2706 MimeInfo *mimeinfo)
2708 GList *children, *child;
2710 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_grid));
2711 for (child = children; child != NULL; child = g_list_next(child)) {
2712 gboolean *highlight = NULL;
2713 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2715 if (!GTK_IS_EVENT_BOX(child->data))
2716 continue;
2718 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2719 *highlight = (g_object_get_data(G_OBJECT(child->data),
2720 "partinfo") == (gpointer)mimeinfo);
2722 gtk_widget_queue_draw(icon);
2724 g_list_free(children);
2727 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2728 MimeView *mimeview)
2730 prefs_common.mimeview_tree_height = allocation->height;
2733 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2734 MimeView *mimeview)
2736 g_object_ref(button);
2738 mimeview_leave_notify(button, NULL, NULL);
2740 mimeview->ctree_mode = !mimeview->ctree_mode;
2741 if (mimeview->ctree_mode) {
2742 gtk_image_set_from_icon_name(GTK_IMAGE(mimeview->arrow),
2743 "pan-end-symbolic", GTK_ICON_SIZE_MENU);
2744 gtk_widget_hide(mimeview->icon_mainbox);
2745 gtk_widget_show(mimeview->ctree_mainbox);
2746 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2747 prefs_common.mimeview_tree_height);
2749 gtk_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2750 button);
2751 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2752 button, FALSE, FALSE, 0);
2753 } else {
2754 gtk_image_set_from_icon_name(GTK_IMAGE(mimeview->arrow),
2755 "pan-start-symbolic", GTK_ICON_SIZE_MENU);
2756 gtk_widget_hide(mimeview->ctree_mainbox);
2757 gtk_widget_show(mimeview->icon_mainbox);
2758 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2760 gtk_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2761 button);
2762 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2763 button, FALSE, FALSE, 0);
2764 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2765 if (mimeview->opened)
2766 icon_list_toggle_by_mime_info(mimeview,
2767 mimeview_get_node_part(mimeview, mimeview->opened));
2768 summary_grab_focus(mimeview->mainwin->summaryview);
2770 g_object_unref(button);
2771 return TRUE;
2774 void mimeview_update (MimeView *mimeview)
2776 if (mimeview && mimeview->mimeinfo) {
2777 icon_list_clear(mimeview);
2778 icon_list_create(mimeview, mimeview->mimeinfo);
2782 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2784 MessageView *msgview = NULL;
2785 MainWindow *mainwin = NULL;
2787 if (!cmd)
2788 return;
2790 msgview = mimeview->messageview;
2791 if (!msgview)
2792 return;
2794 mainwin = msgview->mainwin;
2795 if (!mainwin)
2796 return;
2798 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2799 "pop_partinfo", NULL);
2801 if (!strcmp(cmd, "sc://view_log"))
2802 log_window_show(mainwin->logwin);
2803 else if (!strcmp(cmd, "sc://save_as"))
2804 mimeview_save_as(mimeview);
2805 else if (!strcmp(cmd, "sc://display_as_text"))
2806 mimeview_display_as_text(mimeview);
2807 #ifndef G_OS_WIN32
2808 else if (!strcmp(cmd, "sc://open_with"))
2809 mimeview_open_with(mimeview);
2810 #endif
2811 else if (!strcmp(cmd, "sc://open"))
2812 mimeview_launch(mimeview, NULL);
2813 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2814 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2815 icon_selected(mimeview, -1, (MimeInfo *)data);
2816 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2817 mimeview_launch(mimeview, (MimeInfo *)data);
2818 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2819 mimeview->spec_part = (MimeInfo *)data;
2820 part_button_pressed(mimeview, event, (MimeInfo *)data);
2821 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2822 const gchar *tagname = cmd + strlen("sc://search_tags:");
2823 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2824 gtk_toggle_button_set_active(
2825 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2826 TRUE);
2827 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2828 ADVANCED_SEARCH_EXTENDED, buf);
2829 g_free(buf);
2833 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2835 if (mimeview->type == MIMEVIEW_TEXT)
2836 return textview_scroll_page(mimeview->textview, up);
2837 else if (mimeview->mimeviewer) {
2838 MimeViewer *mimeviewer = mimeview->mimeviewer;
2839 if (mimeviewer->scroll_page)
2840 return mimeviewer->scroll_page(mimeviewer, up);
2842 return TRUE;
2845 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2847 if (mimeview->type == MIMEVIEW_TEXT)
2848 textview_scroll_one_line(mimeview->textview, up);
2849 else if (mimeview->mimeviewer) {
2850 MimeViewer *mimeviewer = mimeview->mimeviewer;
2851 if (mimeviewer->scroll_one_line)
2852 mimeviewer->scroll_one_line(mimeviewer, up);