allow saving attachments only, without other msg parts
[claws.git] / src / mimeview.c
blob80402611ce3f8a55d6ce724e26108e0f58371bd6
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 gboolean attachments_only);
107 #ifndef G_OS_WIN32
108 static void mimeview_open_part_with (MimeView *mimeview,
109 MimeInfo *partinfo,
110 gboolean automatic);
111 #endif
112 static void mimeview_send_to (MimeView *mimeview,
113 MimeInfo *partinfo);
114 static void mimeview_view_file (const gchar *filename,
115 MimeInfo *partinfo,
116 const gchar *cmd,
117 MimeView *mimeview);
118 static gboolean icon_clicked_cb (GtkWidget *button,
119 GdkEventButton *event,
120 MimeView *mimeview);
121 static void icon_selected (MimeView *mimeview,
122 gint num,
123 MimeInfo *partinfo);
124 static gint icon_key_pressed (GtkWidget *button,
125 GdkEventKey *event,
126 MimeView *mimeview);
127 static void icon_list_append_icon (MimeView *mimeview,
128 MimeInfo *mimeinfo);
129 static void icon_list_create (MimeView *mimeview,
130 MimeInfo *mimeinfo);
131 static void icon_list_clear (MimeView *mimeview);
132 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
133 MimeInfo *mimeinfo);
134 static void ctree_size_allocate_cb (GtkWidget *widget,
135 GtkAllocation *allocation,
136 MimeView *mimeview);
137 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
138 MimeView *mimeview);
139 static gboolean part_button_pressed (MimeView *mimeview,
140 GdkEventButton *event,
141 MimeInfo *partinfo);
142 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
143 GtkAllocation *layout_size,
144 MimeView *mimeview);
145 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview);
146 static const gchar *get_part_name(MimeInfo *partinfo);
147 static const gchar *get_part_description(MimeInfo *partinfo);
149 static void mimeview_launch_cb(GtkAction *action, gpointer data)
151 MimeView *mimeview = (MimeView *)data;
152 mimeview_launch(mimeview, mimeview_get_part_to_use(mimeview));
155 #ifndef G_OS_WIN32
156 static void mimeview_open_with_cb(GtkAction *action, gpointer data)
158 mimeview_open_with((MimeView *)data);
160 #endif
162 static void mimeview_copy_cb(GtkAction *action, gpointer data)
164 MimeView *mimeview = (MimeView *)data;
165 MimeInfo *mimeinfo = mimeview_get_part_to_use(mimeview);
167 if (mimeinfo == NULL)
168 return;
170 if (mimeinfo->type == MIMETYPE_IMAGE) {
171 GError *error = NULL;
172 GdkPixbuf *pixbuf = procmime_get_part_as_pixbuf(mimeinfo, &error);
173 if (error != NULL) {
174 g_warning("could not copy: %s", error->message);
175 g_error_free(error);
176 } else {
177 gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
178 pixbuf);
180 g_object_unref(pixbuf);
181 } else {
182 void *data = procmime_get_part_as_string(mimeinfo, FALSE);
183 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
184 data, mimeinfo->length);
185 g_free(data);
189 static void mimeview_send_to_cb(GtkAction *action, gpointer data)
191 MimeView *mimeview = (MimeView *)data;
192 mimeview_send_to(mimeview, mimeview_get_part_to_use(mimeview));
195 static void mimeview_display_as_text_cb(GtkAction *action, gpointer data)
197 mimeview_display_as_text((MimeView *)data);
200 static void mimeview_save_as_cb(GtkAction *action, gpointer data)
202 mimeview_save_as((MimeView *)data);
205 static void mimeview_save_all_cb(GtkAction *action, gpointer data)
207 mimeview_save_all((MimeView *)data, FALSE);
210 static void mimeview_save_all_attachments_cb(GtkAction *action, gpointer data)
212 mimeview_save_all((MimeView *)data, TRUE);
215 static void mimeview_select_next_part_cb(GtkAction *action, gpointer data)
217 mimeview_select_next_part((MimeView *)data);
220 static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
222 mimeview_select_prev_part((MimeView *)data);
225 static GtkActionEntry mimeview_menu_actions[] = {
226 { "MimeView", NULL, "MimeView", NULL, NULL, NULL },
227 { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
228 #if (!defined G_OS_WIN32)
229 { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
230 #endif
231 { "MimeView/Copy", NULL, N_("Copy"), NULL, "Copy", G_CALLBACK(mimeview_copy_cb) },
232 { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
233 { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
234 { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
235 { "MimeView/SaveAll", NULL, N_("Save _all..."), NULL, "Save all parts", G_CALLBACK(mimeview_save_all_cb) },
236 { "MimeView/SaveAllAttachments", NULL, N_("Save all attachments..."), NULL, "Save all attachments", G_CALLBACK(mimeview_save_all_attachments_cb) },
237 { "MimeView/NextPart", NULL, N_("Next part"), NULL, "Next part", G_CALLBACK(mimeview_select_next_part_cb) },
238 { "MimeView/PrevPart", NULL, N_("Previous part"), NULL, "Previous part", G_CALLBACK(mimeview_select_prev_part_cb) }
241 static GtkTargetEntry mimeview_mime_types[] =
243 {"text/uri-list", 0, 0}
246 GSList *mimeviewer_factories;
247 GSList *mimeviews;
249 static GdkCursor *hand_cursor = NULL;
251 static gboolean mimeview_visi_notify(GtkWidget *widget,
252 GdkEventVisibility *event,
253 MimeView *mimeview)
255 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
256 return FALSE;
259 static gboolean mimeview_leave_notify(GtkWidget *widget,
260 GdkEventCrossing *event,
261 MimeView *mimeview)
263 gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
264 return FALSE;
267 static gboolean mimeview_enter_notify(GtkWidget *widget,
268 GdkEventCrossing *event,
269 MimeView *mimeview)
271 gdk_window_set_cursor(gtk_widget_get_window(widget), hand_cursor);
272 return FALSE;
275 MimeView *mimeview_create(MainWindow *mainwin)
277 MimeView *mimeview;
279 GtkWidget *paned;
280 GtkWidget *scrolledwin;
281 GtkWidget *ctree;
282 GtkWidget *mime_notebook;
283 GtkWidget *popupmenu;
284 GtkWidget *ctree_mainbox;
285 GtkWidget *vbox;
286 GtkWidget *mime_toggle;
287 GtkWidget *icon_mainbox;
288 GtkWidget *icon_scroll;
289 GtkWidget *icon_grid;
290 GtkWidget *arrow;
291 GtkWidget *scrollbutton;
292 GtkWidget *hbox;
293 NoticeView *siginfoview;
294 GtkTreeStore *model;
295 GtkCellRenderer *renderer;
296 GtkTreeViewColumn *column;
297 GtkTreeSelection *selection;
299 gchar *titles[N_MIMEVIEW_COLS];
300 gint cols;
302 if (!hand_cursor)
303 hand_cursor = gdk_cursor_new_for_display(
304 gtk_widget_get_display(mainwin->window), GDK_HAND2);
306 debug_print("Creating MIME view...\n");
307 mimeview = g_new0(MimeView, 1);
309 titles[COL_MIMETYPE] = _("MIME Type");
310 titles[COL_SIZE] = _("Size");
311 titles[COL_NAME] = _("Name");
313 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
314 gtk_widget_show(scrolledwin);
315 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
316 GTK_POLICY_AUTOMATIC,
317 GTK_POLICY_AUTOMATIC);
319 model = gtk_tree_store_new(N_MIMEVIEW_COLS,
320 G_TYPE_STRING,
321 G_TYPE_STRING,
322 G_TYPE_STRING,
323 G_TYPE_POINTER);
325 ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
326 g_object_unref(model);
327 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
328 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
329 prefs_common.use_stripes_everywhere);
330 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ctree),
331 prefs_common.show_col_headers);
333 renderer = gtk_cell_renderer_text_new();
334 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
335 -1, titles[COL_MIMETYPE],renderer,
336 "text", COL_MIMETYPE, NULL);
337 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
339 renderer = gtk_cell_renderer_text_new();
340 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
341 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
342 -1, titles[COL_SIZE], renderer,
343 "text", COL_SIZE, NULL);
344 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
345 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
347 renderer = gtk_cell_renderer_text_new();
348 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
349 -1, titles[COL_NAME], renderer,
350 "text", COL_NAME, NULL);
351 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
352 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
354 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
355 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
357 gtk_widget_show(ctree);
358 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
359 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
360 mimeview_mime_types, 1, GDK_ACTION_COPY);
362 g_signal_connect(G_OBJECT(selection), "changed",
363 G_CALLBACK(mimeview_selected), mimeview);
364 g_signal_connect(G_OBJECT(ctree), "button_release_event",
365 G_CALLBACK(mimeview_button_pressed), mimeview);
366 g_signal_connect(G_OBJECT(ctree), "key_press_event",
367 G_CALLBACK(mimeview_key_pressed), mimeview);
368 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
369 G_CALLBACK(mimeview_drag_data_get), mimeview);
371 mime_notebook = gtk_notebook_new();
372 gtk_widget_set_name(GTK_WIDGET(mime_notebook), "mime_notebook");
373 gtk_widget_show(mime_notebook);
374 gtk_widget_set_can_focus(mime_notebook, FALSE);
375 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
376 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
378 icon_grid = gtk_grid_new();
379 gtk_orientable_set_orientation(GTK_ORIENTABLE(icon_grid),
380 GTK_ORIENTATION_VERTICAL);
381 gtk_grid_set_row_spacing(GTK_GRID(icon_grid), 0);
382 gtk_widget_show(icon_grid);
383 icon_scroll = gtk_layout_new(NULL, NULL);
384 gtk_widget_show(icon_scroll);
385 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_grid, 0, 0);
386 scrollbutton = gtk_vscrollbutton_new(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(icon_scroll)));
387 gtk_widget_show(scrollbutton);
389 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
390 G_CALLBACK(mimeview_scrolled), mimeview);
392 mime_toggle = gtk_event_box_new();
393 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
395 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
396 G_CALLBACK(mimeview_visi_notify), mimeview);
397 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
398 G_CALLBACK(mimeview_leave_notify), mimeview);
399 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
400 G_CALLBACK(mimeview_enter_notify), mimeview);
402 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
403 gtk_widget_show(mime_toggle);
404 mimeview->ctree_mode = FALSE;
405 arrow = gtk_image_new_from_icon_name("pan-start-symbolic", GTK_ICON_SIZE_MENU);
406 gtk_widget_show(arrow);
407 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
408 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
409 G_CALLBACK(mime_toggle_button_cb), mimeview);
411 icon_mainbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
412 gtk_widget_set_name(GTK_WIDGET(icon_mainbox), "mimeview_icon_mainbox");
413 gtk_widget_show(icon_mainbox);
414 gtk_widget_set_size_request(icon_mainbox, 32, -1);
415 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
416 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
417 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
418 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
419 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
421 ctree_mainbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
422 gtk_widget_set_name(GTK_WIDGET(ctree_mainbox), "mimeview_ctree_mainbox");
423 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
424 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
425 G_CALLBACK(ctree_size_allocate_cb), mimeview);
427 mimeview->ui_manager = gtk_ui_manager_new();
428 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
429 "MimeView", mimeview_menu_actions,
430 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
432 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
433 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
434 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
435 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
436 "/Menus/MimeView/", "Open", "MimeView/Open",
437 GTK_UI_MANAGER_MENUITEM);
438 #if (!defined G_OS_WIN32)
439 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
440 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
441 GTK_UI_MANAGER_MENUITEM);
442 #endif
443 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
444 "/Menus/MimeView/", "Copy", "MimeView/Copy",
445 GTK_UI_MANAGER_MENUITEM);
446 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
447 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
448 GTK_UI_MANAGER_MENUITEM);
449 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
450 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
451 GTK_UI_MANAGER_MENUITEM);
452 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
453 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
454 GTK_UI_MANAGER_MENUITEM);
455 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
456 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
457 GTK_UI_MANAGER_MENUITEM);
458 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
459 "/Menus/MimeView/", "SaveAllAttachments", "MimeView/SaveAllAttachments",
460 GTK_UI_MANAGER_MENUITEM);
461 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
462 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
463 GTK_UI_MANAGER_MENUITEM);
464 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
465 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
466 GTK_UI_MANAGER_MENUITEM);
468 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
469 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
472 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
473 gtk_widget_show(vbox);
474 siginfoview = noticeview_create(mainwin);
475 gtk_widget_set_name(GTK_WIDGET(siginfoview->vgrid), "siginfoview");
476 noticeview_hide(siginfoview);
477 noticeview_set_icon_clickable(siginfoview, TRUE);
478 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
479 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
481 paned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
482 gtk_widget_show(paned);
483 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
484 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
486 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
487 gtk_widget_set_name(GTK_WIDGET(hbox), "mimeview");
488 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
489 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
491 gtk_widget_show(hbox);
492 gtk_widget_hide(ctree_mainbox);
493 #ifdef GENERIC_UMPC
494 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
495 #endif
496 mimeview->hbox = hbox;
497 mimeview->paned = paned;
498 mimeview->scrolledwin = scrolledwin;
499 mimeview->ctree = ctree;
500 mimeview->mime_notebook = mime_notebook;
501 mimeview->popupmenu = popupmenu;
502 mimeview->type = -1;
503 mimeview->ctree_mainbox = ctree_mainbox;
504 mimeview->icon_scroll = icon_scroll;
505 mimeview->icon_grid = icon_grid;
506 mimeview->icon_mainbox = icon_mainbox;
507 mimeview->icon_count = 0;
508 mimeview->mainwin = mainwin;
509 mimeview->mime_toggle = mime_toggle;
510 mimeview->siginfoview = siginfoview;
511 mimeview->scrollbutton = scrollbutton;
512 mimeview->arrow = arrow;
513 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
515 mimeviews = g_slist_prepend(mimeviews, mimeview);
517 return mimeview;
520 void mimeview_init(MimeView *mimeview)
522 textview_init(mimeview->textview);
524 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
525 GTK_WIDGET_PTR(mimeview->textview));
528 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
530 while (mimeinfo) {
531 if (privacy_mimeinfo_is_signed(mimeinfo))
532 return TRUE;
533 mimeinfo = procmime_mimeinfo_next(mimeinfo);
536 return FALSE;
539 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
540 const gchar *file)
542 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
544 mimeview_clear(mimeview);
546 cm_return_if_fail(file != NULL);
547 cm_return_if_fail(mimeinfo != NULL);
549 mimeview->mimeinfo = mimeinfo;
551 mimeview->file = g_strdup(file);
553 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
554 mimeview);
556 /* check if the mail's signed - it can change the mail structure */
558 if (any_part_is_signed(mimeinfo))
559 debug_print("signed mail\n");
561 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
562 gtk_tree_view_expand_all(ctree);
563 icon_list_clear(mimeview);
564 icon_list_create(mimeview, mimeinfo);
566 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
567 mimeview_selected, mimeview);
570 static void mimeview_free_mimeinfo(MimeView *mimeview)
572 if (mimeview->mimeinfo != NULL) {
573 procmime_mimeinfo_free_all(&mimeview->mimeinfo);
574 mimeview->mimeinfo = NULL;
578 void mimeview_destroy(MimeView *mimeview)
580 GSList *cur;
582 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
583 MimeViewer *viewer = (MimeViewer *) cur->data;
584 viewer->destroy_viewer(viewer);
586 g_slist_free(mimeview->viewers);
587 gtk_target_list_unref(mimeview->target_list);
589 if (mimeview->sig_check_timeout_tag != 0)
590 g_source_remove(mimeview->sig_check_timeout_tag);
591 if (mimeview->sig_check_cancellable != NULL) {
592 /* Set last_sig_check_task to NULL to discard results in async_cb */
593 mimeview->siginfo->last_sig_check_task = NULL;
594 g_cancellable_cancel(mimeview->sig_check_cancellable);
595 g_object_unref(mimeview->sig_check_cancellable);
597 mimeview_free_mimeinfo(mimeview);
598 gtk_tree_path_free(mimeview->opened);
599 g_free(mimeview->file);
600 g_free(mimeview);
601 mimeviews = g_slist_remove(mimeviews, mimeview);
604 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
606 return gtkut_tree_view_get_selected_pointer(
607 GTK_TREE_VIEW(mimeview->ctree), COL_DATA,
608 NULL, NULL, NULL);
611 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
613 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
614 GtkTreeIter iter;
615 MimeInfo *partinfo;
617 gtk_tree_model_get_iter(model, &iter, path);
618 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
619 return partinfo;
622 gboolean mimeview_tree_is_empty(MimeView *mimeview)
624 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
625 GtkTreeIter iter;
626 return !gtk_tree_model_get_iter_first(model, &iter);
629 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
631 GtkTreeIter iter, parent;
632 gboolean has_parent;
634 gtk_tree_model_get_iter(model, &iter, path);
636 if (gtk_tree_model_iter_has_child(model, &iter)) {
637 gtk_tree_path_down(path);
638 return TRUE;
641 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
643 if (!gtk_tree_model_iter_next(model, &iter)) {
644 while (has_parent) {
645 GtkTreeIter saved_parent = parent;
646 gtk_tree_path_up(path);
647 if (gtk_tree_model_iter_next(model, &parent)) {
648 gtk_tree_path_next(path);
649 return TRUE;
650 } else {
651 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
655 } else {
656 gtk_tree_path_next(path);
657 return TRUE;
660 return FALSE;
663 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
665 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
666 GtkTreeIter iter, child, parent;
667 gboolean has_parent;
669 gtk_tree_model_get_iter(model, &iter, path);
670 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
672 if (gtk_tree_path_prev(path)) {
673 gtk_tree_model_get_iter(model, &iter, path);
675 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
676 gtk_tree_path_down(path);
678 while (gtk_tree_model_iter_next(model, &child))
679 gtk_tree_path_next(path);
682 return TRUE;
685 if (has_parent) {
686 gtk_tree_path_up(path);
687 return TRUE;
690 return FALSE;
693 gint mimeview_get_selected_part_num(MimeView *mimeview)
695 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
696 GtkTreeIter iter;
697 GtkTreePath *path;
698 gint i = 0;
700 if (!gtk_tree_model_get_iter_first(model, &iter))
701 return -1;
702 path = gtk_tree_model_get_path(model, &iter);
704 do {
705 if (!gtk_tree_path_compare(mimeview->opened, path)) {
706 gtk_tree_path_free(path);
707 return i;
710 i++;
711 } while (mimeview_tree_next(model, path));
713 gtk_tree_path_free(path);
715 return -1;
718 void mimeview_select_part_num(MimeView *mimeview, gint i)
720 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
721 GtkTreeIter iter;
722 GtkTreePath *path;
723 GtkTreeSelection *selection;
724 gint x = 0;
726 if (i < 0)
727 return;
729 if (!gtk_tree_model_get_iter_first(model, &iter))
730 return;
731 path = gtk_tree_model_get_path(model, &iter);
733 while (x != i) {
734 if (!mimeview_tree_next(model, path)) {
735 gtk_tree_path_free(path);
736 return;
738 x++;
741 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
742 gtk_tree_selection_select_path(selection, path);
743 gtk_tree_path_free(path);
746 static void mimeview_set_multipart_tree(MimeView *mimeview,
747 MimeInfo *mimeinfo,
748 GtkTreeIter *parent)
750 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
751 GTK_TREE_VIEW(mimeview->ctree)));
752 GtkTreeIter iter;
753 gchar *content_type, *length, *name;
755 cm_return_if_fail(mimeinfo != NULL);
757 while (mimeinfo != NULL) {
758 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
759 content_type = g_strdup_printf("%s/%s",
760 procmime_get_media_type_str(mimeinfo->type),
761 mimeinfo->subtype);
762 else
763 content_type = g_strdup("UNKNOWN");
765 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
767 if (prefs_common.attach_desc)
768 name = g_strdup(get_part_description(mimeinfo));
769 else
770 name = g_strdup(get_part_name(mimeinfo));
772 gtk_tree_store_append(model, &iter, parent);
773 gtk_tree_store_set(model, &iter,
774 COL_MIMETYPE, content_type,
775 COL_SIZE, length,
776 COL_NAME, name,
777 COL_DATA, mimeinfo, -1);
778 g_free(content_type);
779 g_free(length);
780 g_free(name);
782 if (mimeinfo->node->children)
783 mimeview_set_multipart_tree(mimeview,
784 (MimeInfo *) mimeinfo->node->children->data, &iter);
785 mimeinfo = mimeinfo->node->next != NULL ?
786 (MimeInfo *) mimeinfo->node->next->data : NULL;
790 static const gchar *get_real_part_name(MimeInfo *partinfo)
792 const gchar *name = NULL;
794 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
795 if (name == NULL)
796 name = procmime_mimeinfo_get_parameter(partinfo, "name");
798 return name;
801 static const gchar *get_part_name(MimeInfo *partinfo)
803 const gchar *name;
805 name = get_real_part_name(partinfo);
806 if (name == NULL)
807 name = "";
809 return name;
812 static const gchar *get_part_description(MimeInfo *partinfo)
814 if (partinfo->description)
815 return partinfo->description;
816 else
817 return get_part_name(partinfo);
820 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
822 FILE *fp;
823 const gchar *fname;
825 if (!partinfo) return;
827 fname = mimeview->file;
828 if (!fname) return;
830 if ((fp = claws_fopen(fname, "rb")) == NULL) {
831 FILE_OP_ERROR(fname, "claws_fopen");
832 return;
835 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
836 FILE_OP_ERROR(mimeview->file, "fseek");
837 claws_fclose(fp);
838 return;
841 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
842 textview_show_part(mimeview->textview, partinfo, fp);
844 claws_fclose(fp);
847 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
849 GSList *cur;
850 MimeViewerFactory *factory = NULL;
851 MimeViewer *viewer = NULL;
852 gchar *real_contenttype = NULL, *tmp;
854 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
856 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
857 MimeViewerFactory *curfactory = cur->data;
858 gint i = 0;
860 while (curfactory->content_types[i] != NULL) {
861 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
862 if (g_pattern_match_simple(tmp, real_contenttype)) {
863 debug_print("%s\n", curfactory->content_types[i]);
864 factory = curfactory;
865 g_free(tmp);
866 break;
868 g_free(tmp);
869 i++;
871 if (factory != NULL)
872 break;
874 g_free(real_contenttype);
875 if (factory == NULL)
876 return NULL;
878 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
879 MimeViewer *curviewer = cur->data;
881 if (curviewer->factory == factory)
882 return curviewer;
884 viewer = factory->create_viewer();
885 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
886 GTK_WIDGET(viewer->get_widget(viewer)));
888 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
890 return viewer;
893 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
895 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
898 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
900 gchar *content_type = NULL;
901 MimeViewer *viewer = NULL;
903 if ((partinfo->type == MIMETYPE_APPLICATION) &&
904 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
905 const gchar *filename;
907 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
908 if (filename == NULL)
909 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
910 if (filename != NULL)
911 content_type = procmime_get_mime_type(filename);
912 } else {
913 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
916 if (content_type != NULL) {
917 viewer = get_viewer_for_content_type(mimeview, content_type);
918 g_free(content_type);
921 return viewer;
924 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
926 MimeViewer *viewer;
928 if (mimeview->messageview->partial_display_shown) {
929 noticeview_hide(mimeview->messageview->noticeview);
930 mimeview->messageview->partial_display_shown = FALSE;
933 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
934 if (viewer == NULL) {
935 if (mimeview->mimeviewer != NULL)
936 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
937 mimeview->mimeviewer = NULL;
938 return FALSE;
941 if (mimeview->mimeviewer != NULL)
942 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
944 if (mimeview->mimeviewer != viewer)
945 mimeview->mimeviewer = viewer;
947 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
948 viewer->mimeview = mimeview;
949 viewer->show_mimepart(viewer, mimeview->file, partinfo);
951 return TRUE;
954 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
956 TextView *textview = mimeview->textview;
957 GtkWidget *focused = NULL;
959 if (mainwindow_get_mainwindow())
960 focused = gtkut_get_focused_child(
961 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
963 if ((mimeview->type != MIMEVIEW_VIEWER) &&
964 (mimeview->type == type)) return;
966 switch (type) {
967 case MIMEVIEW_TEXT:
968 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
969 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
970 GTK_WIDGET_PTR(textview)));
971 break;
972 case MIMEVIEW_VIEWER:
973 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
974 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
975 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
976 break;
977 default:
978 return;
980 if (focused)
981 gtk_widget_grab_focus(focused);
982 mimeview->type = type;
985 void mimeview_clear(MimeView *mimeview)
987 GtkTreeModel *model;
989 if (!mimeview)
990 return;
992 if (g_slist_find(mimeviews, mimeview) == NULL)
993 return;
995 if (mimeview->sig_check_timeout_tag != 0) {
996 g_source_remove(mimeview->sig_check_timeout_tag);
997 mimeview->sig_check_timeout_tag = 0;
1000 if (mimeview->sig_check_cancellable != NULL) {
1001 /* Set last_sig_check_task to NULL to discard results in async_cb */
1002 mimeview->siginfo->last_sig_check_task = NULL;
1003 g_cancellable_cancel(mimeview->sig_check_cancellable);
1004 g_object_unref(mimeview->sig_check_cancellable);
1005 mimeview->sig_check_cancellable = NULL;
1008 noticeview_hide(mimeview->siginfoview);
1010 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
1011 gtk_tree_store_clear(GTK_TREE_STORE(model));
1013 textview_clear(mimeview->textview);
1014 if (mimeview->mimeviewer != NULL)
1015 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
1017 mimeview_free_mimeinfo(mimeview);
1019 mimeview->mimeinfo = NULL;
1021 gtk_tree_path_free(mimeview->opened);
1022 mimeview->opened = NULL;
1024 g_free(mimeview->file);
1025 mimeview->file = NULL;
1027 icon_list_clear(mimeview);
1028 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1031 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
1032 GtkUIManager *ui_manager;
1034 if (messageview->window != NULL)
1035 ui_manager = messageview->ui_manager;
1036 else
1037 ui_manager = messageview->mainwin->ui_manager;
1039 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
1042 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1043 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1045 static void update_signature_noticeview(MimeView *mimeview, gboolean special, SignatureStatus code)
1047 gchar *text = NULL, *button_text = NULL;
1048 void *func = NULL;
1049 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1050 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1052 if (mimeview == NULL || mimeview->siginfo == NULL)
1053 g_error("bad call to update noticeview");
1055 if (special)
1056 mycode = code;
1057 else
1058 mycode = privacy_mimeinfo_get_sig_status(mimeview->siginfo);
1060 switch (mycode) {
1061 case SIGNATURE_UNCHECKED:
1062 button_text = _("Check signature");
1063 func = check_signature_cb;
1064 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1065 break;
1066 case SIGNATURE_OK:
1067 button_text = _("View full information");
1068 func = display_full_info_cb;
1069 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1070 break;
1071 case SIGNATURE_WARN:
1072 button_text = _("View full information");
1073 func = display_full_info_cb;
1074 icon = STOCK_PIXMAP_PRIVACY_WARN;
1075 break;
1076 case SIGNATURE_KEY_EXPIRED:
1077 button_text = _("View full information");
1078 func = display_full_info_cb;
1079 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1080 break;
1081 case SIGNATURE_INVALID:
1082 button_text = _("View full information");
1083 func = display_full_info_cb;
1084 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1085 break;
1086 case SIGNATURE_CHECK_ERROR:
1087 case SIGNATURE_CHECK_FAILED:
1088 case SIGNATURE_CHECK_TIMEOUT:
1089 button_text = _("Check again");
1090 func = check_signature_cb;
1091 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1092 break;
1093 default:
1094 break;
1097 if (mycode == SIGNATURE_UNCHECKED) {
1098 gchar *tmp = privacy_mimeinfo_get_sig_info(mimeview->siginfo, FALSE);
1099 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1101 if (*shortcut == '\0')
1102 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1103 else
1104 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."), tmp, shortcut);
1106 g_free(shortcut);
1107 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1108 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1110 if (*shortcut == '\0')
1111 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1112 else
1113 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1115 g_free(shortcut);
1116 } else if (mycode == SIGNATURE_CHECK_ERROR) {
1117 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1119 if (*shortcut == '\0')
1120 text = g_strdup(_("Error checking the signature. Click the icon to try again."));
1121 else
1122 text = g_strdup_printf(_("Error checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1124 g_free(shortcut);
1125 } else {
1126 text = g_strdup(privacy_mimeinfo_get_sig_info(mimeview->siginfo, FALSE));
1129 noticeview_set_text(mimeview->siginfoview, text);
1130 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1131 g_free(text);
1133 noticeview_set_button_text(mimeview->siginfoview, NULL);
1134 noticeview_set_button_press_callback(
1135 mimeview->siginfoview,
1136 G_CALLBACK(func),
1137 (gpointer) mimeview);
1138 noticeview_set_icon(mimeview->siginfoview, icon);
1139 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1141 icon_list_clear(mimeview);
1142 icon_list_create(mimeview, mimeview->mimeinfo);
1145 static void check_signature_async_cb(GObject *source_object,
1146 GAsyncResult *async_result,
1147 gpointer user_data)
1149 GTask *task = G_TASK(async_result);
1150 GCancellable *cancellable;
1151 MimeView *mimeview = (MimeView *)user_data;
1152 gboolean cancelled;
1153 SigCheckTaskResult *result;
1154 GError *error = NULL;
1156 if (mimeview->siginfo == NULL) {
1157 debug_print("discarding stale sig check task result task:%p\n", task);
1158 return;
1159 } else if (task != mimeview->siginfo->last_sig_check_task) {
1160 debug_print("discarding stale sig check task result last_task:%p task:%p\n",
1161 mimeview->siginfo->last_sig_check_task, task);
1162 return;
1163 } else {
1164 debug_print("using sig check task result task:%p\n", task);
1165 mimeview->siginfo->last_sig_check_task = NULL;
1168 cancellable = g_task_get_cancellable(task);
1169 cancelled = g_cancellable_set_error_if_cancelled(cancellable, &error);
1170 if (cancelled) {
1171 debug_print("sig check task was cancelled: task:%p GError: domain:%s code:%d message:\"%s\"\n",
1172 task, g_quark_to_string(error->domain), error->code, error->message);
1173 g_error_free(error);
1174 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_TIMEOUT);
1175 return;
1176 } else {
1177 if (mimeview->sig_check_cancellable == NULL)
1178 g_error("bad cancellable");
1179 if (mimeview->sig_check_timeout_tag == 0)
1180 g_error("bad cancel source tag");
1182 g_source_remove(mimeview->sig_check_timeout_tag);
1183 mimeview->sig_check_timeout_tag = 0;
1184 g_object_unref(mimeview->sig_check_cancellable);
1185 mimeview->sig_check_cancellable = NULL;
1188 result = g_task_propagate_pointer(task, &error);
1190 if (mimeview->siginfo->sig_data) {
1191 privacy_free_signature_data(mimeview->siginfo->sig_data);
1192 mimeview->siginfo->sig_data = NULL;
1195 if (result == NULL) {
1196 debug_print("sig check task propagated NULL task:%p GError: domain:%s code:%d message:\"%s\"\n",
1197 task, g_quark_to_string(error->domain), error->code, error->message);
1198 g_error_free(error);
1199 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_ERROR);
1200 return;
1203 mimeview->siginfo->sig_data = result->sig_data;
1204 update_signature_noticeview(mimeview, FALSE, 0);
1206 if (result->newinfo) {
1207 g_warning("Check sig task returned an unexpected new MimeInfo");
1208 procmime_mimeinfo_free_all(&result->newinfo);
1211 g_free(result);
1214 gboolean mimeview_check_sig_timeout(gpointer user_data)
1216 MimeView *mimeview = (MimeView *)user_data;
1217 GCancellable *cancellable = mimeview->sig_check_cancellable;
1219 mimeview->sig_check_timeout_tag = 0;
1221 if (cancellable == NULL) {
1222 return G_SOURCE_REMOVE;
1225 mimeview->sig_check_cancellable = NULL;
1226 g_cancellable_cancel(cancellable);
1227 g_object_unref(cancellable);
1229 return G_SOURCE_REMOVE;
1232 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1234 MimeView *mimeview = (MimeView *) user_data;
1235 MimeInfo *mimeinfo = mimeview->siginfo;
1236 gint ret;
1238 if (mimeinfo == NULL || !noticeview_is_visible(mimeview->siginfoview))
1239 return;
1241 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1242 GTK_EVENTS_FLUSH();
1244 if (mimeview->sig_check_cancellable != NULL) {
1245 if (mimeview->sig_check_timeout_tag == 0)
1246 g_error("bad cancel source tag");
1247 g_source_remove(mimeview->sig_check_timeout_tag);
1248 g_cancellable_cancel(mimeview->sig_check_cancellable);
1249 g_object_unref(mimeview->sig_check_cancellable);
1252 mimeview->sig_check_cancellable = g_cancellable_new();
1254 ret = privacy_mimeinfo_check_signature(mimeview->siginfo,
1255 mimeview->sig_check_cancellable,
1256 check_signature_async_cb,
1257 mimeview);
1258 if (ret == 0) {
1259 mimeview->sig_check_timeout_tag = g_timeout_add_seconds(prefs_common.io_timeout_secs,
1260 mimeview_check_sig_timeout, mimeview);
1261 } else if (ret < 0) {
1262 g_object_unref(mimeview->sig_check_cancellable);
1263 mimeview->sig_check_cancellable = NULL;
1264 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_ERROR);
1265 } else {
1266 g_object_unref(mimeview->sig_check_cancellable);
1267 mimeview->sig_check_cancellable = NULL;
1268 update_signature_noticeview(mimeview, FALSE, 0);
1272 void mimeview_check_signature(MimeView *mimeview)
1274 check_signature_cb(NULL, mimeview);
1277 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1279 MimeView *mimeview = (MimeView *) user_data;
1280 gtk_tree_path_free(mimeview->opened);
1281 mimeview->opened = NULL;
1282 mimeview_selected(gtk_tree_view_get_selection(
1283 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1286 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1288 MimeView *mimeview = (MimeView *) user_data;
1290 textview_set_text(mimeview->textview, privacy_mimeinfo_get_sig_info(mimeview->siginfo, TRUE));
1291 noticeview_set_button_text(mimeview->siginfoview, NULL);
1292 noticeview_set_button_press_callback(
1293 mimeview->siginfoview,
1294 G_CALLBACK(redisplay_email),
1295 (gpointer) mimeview);
1296 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1299 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1301 MimeInfo *siginfo;
1302 MimeInfo *first_text;
1304 cm_return_if_fail(mimeview != NULL);
1305 cm_return_if_fail(selected != NULL);
1307 if (selected->type == MIMETYPE_MESSAGE
1308 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1309 /* if the first text part is signed, check that */
1310 first_text = selected;
1311 while (first_text && first_text->type != MIMETYPE_TEXT) {
1312 first_text = procmime_mimeinfo_next(first_text);
1314 if (first_text) {
1315 update_signature_info(mimeview, first_text);
1316 return;
1320 siginfo = selected;
1321 while (siginfo != NULL) {
1322 if (privacy_mimeinfo_is_signed(siginfo))
1323 break;
1324 siginfo = procmime_mimeinfo_parent(siginfo);
1326 mimeview->siginfo = siginfo;
1328 /* This shortcut boolean is there to correctly set the menu's
1329 * CheckSignature item sensitivity without killing performance
1330 * each time the menu sensitiveness is updated (a lot).
1332 mimeview->signed_part = (siginfo != NULL);
1334 if (siginfo == NULL) {
1335 noticeview_hide(mimeview->siginfoview);
1336 return;
1339 update_signature_noticeview(mimeview, FALSE, 0);
1340 noticeview_show(mimeview->siginfoview);
1343 void mimeview_show_part_as_text(MimeView *mimeview, MimeInfo *partinfo)
1345 cm_return_if_fail(mimeview != NULL);
1346 cm_return_if_fail(partinfo != NULL);
1348 mimeview_show_message_part(mimeview, partinfo);
1351 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1353 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1354 GtkTreeModel *model;
1355 GtkTreeIter iter;
1356 GtkTreePath *path;
1357 MimeInfo *partinfo;
1358 MainWindow *mainwin;
1359 GdkDisplay *display;
1360 GdkSeat *seat;
1361 GdkDevice *device;
1363 selection = gtk_tree_view_get_selection(ctree);
1364 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1365 return;
1367 path = gtk_tree_model_get_path(model, &iter);
1369 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1370 gtk_tree_path_free(path);
1371 return;
1374 gtk_tree_path_free(mimeview->opened);
1375 mimeview->opened = path;
1376 mimeview->spec_part = NULL;
1377 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1379 partinfo = mimeview_get_node_part(mimeview, path);
1380 if (!partinfo) return;
1382 /* ungrab the mouse event */
1383 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1384 gtk_grab_remove(GTK_WIDGET(ctree));
1385 display = gdk_window_get_display(gtk_widget_get_window(GTK_WIDGET(ctree)));
1386 seat = gdk_display_get_default_seat(display);
1387 device = gdk_seat_get_pointer(seat);
1388 if (gdk_display_device_is_grabbed(display, device))
1389 gdk_seat_ungrab(seat);
1392 mimeview->textview->default_text = FALSE;
1394 update_signature_info(mimeview, partinfo);
1396 if (!mimeview_show_part(mimeview, partinfo)) {
1397 switch (partinfo->type) {
1398 case MIMETYPE_TEXT:
1399 case MIMETYPE_MESSAGE:
1400 case MIMETYPE_MULTIPART:
1401 mimeview_show_message_part(mimeview, partinfo);
1403 break;
1404 default:
1405 mimeview->textview->default_text = TRUE;
1406 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1407 textview_clear(mimeview->textview);
1408 textview_show_mime_part(mimeview->textview, partinfo);
1409 break;
1412 mainwin = mainwindow_get_mainwindow();
1413 if (mainwin)
1414 main_window_set_menu_sensitive(mainwin);
1416 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1417 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1418 mimeview_check_signature(mimeview);
1422 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1423 MimeView *mimeview)
1425 GtkTreeSelection *selection;
1426 GtkTreeIter iter;
1428 if (!event) return FALSE;
1430 if (event->button == 2 || event->button == 3) {
1431 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1432 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1433 return FALSE;
1435 gtk_tree_selection_select_iter(selection, &iter);
1437 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1439 return FALSE;
1442 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1443 MimeView *mimeview)
1445 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1446 if (event->direction == GDK_SCROLL_UP) {
1447 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1448 } else if (event->direction == GDK_SCROLL_DOWN) {
1449 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1450 } else {
1451 gdouble x, y;
1453 if ((event->direction == GDK_SCROLL_SMOOTH) &&
1454 gdk_event_get_scroll_deltas((GdkEvent*)event, &x, &y)) {
1455 if (y < 0)
1456 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1457 else
1458 if (y >0)
1459 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1460 } else
1461 return FALSE; /* Scrolling left or right */
1463 gtk_vscrollbutton_scroll(scrollbutton);
1464 return TRUE;
1467 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1468 MimeInfo *partinfo)
1470 static MimeInfo *lastinfo;
1471 static guint32 lasttime;
1473 gint double_click_time;
1474 g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &double_click_time, NULL);
1476 if (event->button == 2 ||
1477 (event->button == 1 && (event->time - lasttime) < double_click_time && lastinfo == partinfo)) {
1478 /* call external program for image, audio or html */
1479 mimeview_launch(mimeview, partinfo);
1480 return TRUE;
1481 } else if (event->button == 3) {
1482 MainWindow *mainwin = mainwindow_get_mainwindow();
1484 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1485 partinfo->type == MIMETYPE_IMAGE ||
1486 partinfo->type == MIMETYPE_MULTIPART))
1487 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1488 else
1489 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1491 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1492 partinfo->type == MIMETYPE_IMAGE ||
1493 partinfo->type == MIMETYPE_TEXT))
1494 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", TRUE);
1495 else
1496 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", FALSE);
1497 #ifndef G_OS_WIN32
1498 if (partinfo &&
1499 partinfo->type == MIMETYPE_APPLICATION &&
1500 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1501 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1502 else
1503 #endif
1504 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1505 if (mainwin)
1506 main_window_set_menu_sensitive(mainwin);
1507 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1508 "pop_partinfo", partinfo);
1510 gtk_menu_popup_at_pointer(GTK_MENU(mimeview->popupmenu),
1511 (GdkEvent *)event);
1512 return TRUE;
1515 lastinfo = partinfo;
1516 lasttime = event->time;
1517 return FALSE;
1521 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1523 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1526 void mimeview_select_next_part(MimeView *mimeview)
1528 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1529 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1530 GtkTreeSelection *selection;
1531 GtkTreeIter iter;
1532 GtkTreePath *path;
1533 MimeInfo *partinfo = NULL;
1534 gboolean has_next;
1536 if (!mimeview->opened) return;
1538 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1539 path = gtk_tree_model_get_path(model, &iter);
1540 skip:
1541 has_next = mimeview_tree_next(model, path);
1542 if (!has_next) {
1543 has_next = gtk_tree_model_get_iter_first(model, &iter);
1544 gtk_tree_path_free(path);
1545 path = gtk_tree_model_get_path(model, &iter);
1548 if (has_next) {
1549 partinfo = mimeview_get_node_part(mimeview, path);
1550 if (partinfo->type == MIMETYPE_MULTIPART ||
1551 (!prefs_common.show_inline_attachments && partinfo->id))
1552 goto skip;
1553 selection = gtk_tree_view_get_selection(ctree);
1554 gtk_tree_selection_select_path(selection, path);
1555 icon_list_toggle_by_mime_info(mimeview, partinfo);
1558 gtk_tree_path_free(path);
1561 void mimeview_select_prev_part(MimeView *mimeview)
1563 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1564 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1565 GtkTreeSelection *selection;
1566 GtkTreeIter iter;
1567 GtkTreePath *path;
1568 MimeInfo *partinfo = NULL;
1569 gboolean has_prev;
1571 if (!mimeview->opened) return;
1573 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1574 path = gtk_tree_model_get_path(model, &iter);
1575 skip:
1576 has_prev = mimeview_tree_prev(mimeview, path);
1577 if (!has_prev) {
1578 while (mimeview_tree_next(model, path)) {}
1579 has_prev = TRUE;
1582 if (has_prev) {
1583 partinfo = mimeview_get_node_part(mimeview, path);
1584 if (partinfo->type == MIMETYPE_MULTIPART ||
1585 (!prefs_common.show_inline_attachments && partinfo->id))
1586 goto skip;
1587 selection = gtk_tree_view_get_selection(ctree);
1588 gtk_tree_selection_select_path(selection, path);
1589 icon_list_toggle_by_mime_info(mimeview, partinfo);
1592 gtk_tree_path_free(path);
1595 #define BREAK_ON_MODIFIER_KEY() \
1596 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1598 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1599 MimeView *mimeview)
1601 SummaryView *summaryview;
1603 if (!event) return FALSE;
1604 if (!mimeview->opened) return FALSE;
1606 summaryview = mimeview->messageview->mainwin->summaryview;
1608 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1609 return FALSE;
1611 switch (event->keyval) {
1612 case GDK_KEY_Home:
1613 case GDK_KEY_End:
1614 textview_scroll_max(mimeview->textview,
1615 (event->keyval == GDK_KEY_Home));
1616 return TRUE;
1617 case GDK_KEY_Page_Down:
1618 case GDK_KEY_space:
1619 if (mimeview_scroll_page(mimeview,
1620 (event->state & GDK_SHIFT_MASK) != 0))
1621 return TRUE;
1623 if (!(event->state & GDK_SHIFT_MASK))
1624 mimeview_select_next_part(mimeview);
1625 return TRUE;
1626 case GDK_KEY_Page_Up:
1627 case GDK_KEY_BackSpace:
1628 mimeview_scroll_page(mimeview, TRUE);
1629 return TRUE;
1630 case GDK_KEY_Return:
1631 case GDK_KEY_KP_Enter:
1632 mimeview_scroll_one_line(mimeview,
1633 (event->state & GDK_MOD1_MASK) != 0);
1634 return TRUE;
1635 case GDK_KEY_Up:
1636 case GDK_KEY_Down:
1637 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1638 return TRUE;
1639 default:
1640 break;
1643 if (mimeview->messageview->new_window) return FALSE;
1645 return summary_pass_key_press_event(summaryview, event);
1648 static void mimeview_drag_data_get(GtkWidget *widget,
1649 GdkDragContext *drag_context,
1650 GtkSelectionData *selection_data,
1651 guint info,
1652 guint time,
1653 MimeView *mimeview)
1655 gchar *filename = NULL, *uriname, *tmp = NULL;
1656 MimeInfo *partinfo;
1657 gint err;
1658 gint count = 0;
1660 if (!mimeview->opened) return;
1661 if (!mimeview->file) return;
1663 partinfo = mimeview_get_selected_part(mimeview);
1664 if (!partinfo) return;
1666 if (strlen(get_part_name(partinfo)) > 0) {
1667 filename = g_path_get_basename(get_part_name(partinfo));
1668 if (filename) {
1669 if (*filename == '\0') {
1670 g_free(filename);
1671 return;
1674 } else if (partinfo->type == MIMETYPE_MESSAGE
1675 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1676 gchar *name = NULL;
1677 GPtrArray *headers = NULL;
1678 FILE *fp;
1680 fp = claws_fopen(partinfo->data.filename, "rb");
1681 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1682 headers = procheader_get_header_array(fp);
1683 if (headers) {
1684 gint i;
1685 for (i = 0; i < headers->len; i++) {
1686 Header *header = g_ptr_array_index(headers, i);
1687 if (procheader_headername_equal(header->name, "Subject")) {
1688 unfold_line(header->body);
1689 name = g_strconcat(header->body, ".txt", NULL);
1690 subst_for_filename(name);
1693 procheader_header_array_destroy(headers);
1696 if (fp != NULL)
1697 claws_fclose(fp);
1698 if (name)
1699 filename = g_path_get_basename(name);
1700 g_free(name);
1702 if (filename == NULL)
1703 filename = g_path_get_basename("Unnamed part");
1705 if (!g_utf8_validate(filename, -1, NULL))
1706 tmp = conv_codeset_strdup(filename,
1707 conv_get_locale_charset_str(),
1708 CS_UTF_8);
1710 if (tmp == NULL) {
1711 g_warning("filename not in UTF-8");
1712 tmp = g_strdup(filename);
1714 g_free(filename);
1715 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1716 tmp, NULL);
1718 check_new_file:
1719 if (is_file_exist(filename)) {
1720 gchar *ext = NULL;
1721 gchar *prefix = NULL;
1722 gchar *new_name = NULL;
1723 if (strrchr(tmp, '.')) {
1724 prefix = g_strdup(tmp);
1725 ext = g_strdup(strrchr(tmp, '.'));
1726 *(strrchr(prefix, '.')) = '\0';
1727 } else {
1728 prefix = g_strdup(tmp);
1729 ext = g_strdup("");
1731 count++;
1732 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1733 g_free(prefix);
1734 g_free(ext);
1735 g_free(filename);
1736 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1737 new_name, NULL);
1738 g_free(new_name);
1739 goto check_new_file;
1742 g_free(tmp);
1744 if ((err = procmime_get_part(filename, partinfo)) < 0)
1745 alertpanel_error
1746 (_("Couldn't save the part of multipart message: %s"),
1747 g_strerror(-err));
1749 tmp = g_filename_to_uri(filename, NULL, NULL);
1750 uriname = g_strconcat(tmp, "\r\n", NULL);
1751 g_free(tmp);
1753 gtk_selection_data_set(selection_data,
1754 gtk_selection_data_get_target(selection_data), 8,
1755 (guchar *)uriname, strlen(uriname));
1757 g_free(uriname);
1758 g_free(filename);
1762 * Returns a filename (with path) for an attachment
1763 * \param partinfo The attachment to save
1764 * \param basedir The target directory
1765 * \param number Used for dummy filename if attachment is unnamed
1767 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1768 const gchar *basedir,
1769 gint number)
1771 gchar *fullname;
1772 gchar *filename;
1774 filename = g_strdup(get_part_name(partinfo));
1775 if (!filename || !*filename) {
1776 g_free(filename);
1777 filename = g_strdup_printf("noname.%d", number);
1780 if (!g_utf8_validate(filename, -1, NULL)) {
1781 gchar *tmp = conv_filename_to_utf8(filename);
1782 g_free(filename);
1783 filename = tmp;
1786 subst_for_filename(filename);
1788 fullname = g_strconcat
1789 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1790 ? &filename[1] : filename, NULL);
1792 g_free(filename);
1793 filename = conv_filename_from_utf8(fullname);
1794 g_free(fullname);
1795 return filename;
1799 * Write a single attachment to file
1800 * \param filename Filename with path
1801 * \param partinfo Attachment to save
1803 static gint mimeview_write_part(const gchar *filename,
1804 MimeInfo *partinfo,
1805 gboolean handle_error)
1807 gchar *dir;
1808 gint err;
1810 dir= g_path_get_dirname(filename);
1811 if (!is_dir_exist(dir))
1812 make_dir_hier(dir);
1813 g_free(dir);
1815 if (is_file_exist(filename)) {
1816 AlertValue aval;
1817 gchar *res;
1818 gchar *tmp;
1820 if (!g_utf8_validate(filename, -1, NULL))
1821 tmp = conv_filename_to_utf8(filename);
1822 else
1823 tmp = g_strdup(filename);
1825 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1826 tmp);
1827 g_free(tmp);
1828 aval = alertpanel(_("Overwrite"), res, NULL, _("_No"),
1829 NULL, _("_Yes"), NULL, NULL, ALERTFOCUS_FIRST);
1830 g_free(res);
1831 if (G_ALERTALTERNATE != aval)
1832 return 2;
1835 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1836 debug_print("error saving MIME part: %d\n", err);
1837 if (handle_error)
1838 alertpanel_error
1839 (_("Couldn't save the part of multipart message: %s"),
1840 g_strerror(-err));
1841 return 0;
1844 return 1;
1847 static AlertValue mimeview_save_all_error_ask(gint n)
1849 gchar *message = g_strdup_printf(
1850 _("An error has occurred while saving message part %d. "
1851 "Do you want to cancel saving or ignore error and "
1852 "continue?"), n);
1853 AlertValue av = alertpanel_full(_("Error saving message part"),
1854 message, NULL, _("_Cancel"), NULL, _("Ignore"), NULL, _("Ignore all"),
1855 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1856 g_free(message);
1857 return av;
1860 static void mimeview_save_all_info(gint errors, gint total)
1862 AlertValue aval;
1864 if (!errors && prefs_common.show_save_all_success) {
1865 gchar *msg = g_strdup_printf(
1866 ngettext("%d file saved successfully.",
1867 "%d files saved successfully.",
1868 total),
1869 total);
1870 aval = alertpanel_full(_("Notice"), msg, NULL, _("_Close"),
1871 NULL, NULL, NULL, NULL, ALERTFOCUS_FIRST,
1872 TRUE, NULL, ALERT_NOTICE);
1873 g_free(msg);
1874 if (aval & G_ALERTDISABLE)
1875 prefs_common.show_save_all_success = FALSE;
1876 } else if (prefs_common.show_save_all_failure) {
1877 gchar *msg1 = g_strdup_printf(
1878 ngettext("%d file saved successfully",
1879 "%d files saved successfully",
1880 total - errors),
1881 total - errors);
1882 gchar *msg2 = g_strdup_printf(
1883 ngettext("%s, %d file failed.",
1884 "%s, %d files failed.",
1885 errors),
1886 msg1, errors);
1887 aval = alertpanel_full(_("Warning"), msg2, NULL, _("_Close"),
1888 NULL, NULL, NULL, NULL, ALERTFOCUS_FIRST,
1889 TRUE, NULL, ALERT_WARNING);
1890 g_free(msg2);
1891 g_free(msg1);
1892 if (aval & G_ALERTDISABLE)
1893 prefs_common.show_save_all_failure = FALSE;
1898 * Menu callback: Save all attached files
1899 * \param mimeview Current display
1901 static void mimeview_save_all(MimeView *mimeview, gboolean attachments_only)
1903 MimeInfo *partinfo;
1904 gchar *dirname;
1905 gchar *startdir = NULL;
1906 gint number = 0, errors = 0;
1907 gboolean skip_errors = FALSE;
1909 if (!mimeview->opened) return;
1910 if (!mimeview->file) return;
1911 if (!mimeview->mimeinfo) return;
1913 partinfo = mimeview->mimeinfo;
1914 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1915 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1916 else
1917 startdir = g_strdup(get_home_dir());
1919 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1920 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1921 if (!dirname) {
1922 g_free(startdir);
1923 return;
1926 if (!is_dir_exist (dirname)) {
1927 alertpanel_error(_("'%s' is not a directory."), dirname);
1928 g_free(startdir);
1929 g_free(dirname);
1930 return;
1933 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1934 dirname[strlen(dirname)-1] = '\0';
1936 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1937 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1938 partinfo = procmime_mimeinfo_next(partinfo);
1939 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1940 partinfo = procmime_mimeinfo_next(partinfo);
1941 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1942 partinfo = procmime_mimeinfo_next(partinfo);
1945 while (partinfo != NULL) {
1946 if (!attachments_only &&
1947 (partinfo->type != MIMETYPE_MESSAGE &&
1948 partinfo->type != MIMETYPE_MULTIPART &&
1949 (partinfo->disposition != DISPOSITIONTYPE_INLINE ||
1950 get_real_part_name(partinfo) != NULL)) ||
1951 attachments_only &&
1952 (partinfo->disposition == DISPOSITIONTYPE_ATTACHMENT &&
1953 get_real_part_name(partinfo) != NULL)) {
1954 gchar *filename = mimeview_get_filename_for_part(
1955 partinfo, dirname, number++);
1957 gint ok = mimeview_write_part(filename, partinfo, FALSE);
1958 g_free(filename);
1959 if (ok < 1) {
1960 ++errors;
1961 if (!skip_errors) {
1962 AlertValue av = mimeview_save_all_error_ask(number);
1963 skip_errors = (av == G_ALERTOTHER);
1964 if (av == G_ALERTDEFAULT) /* cancel */
1965 break;
1967 } else if (ok == 2)
1968 number--;
1970 partinfo = procmime_mimeinfo_next(partinfo);
1973 g_free(prefs_common.attach_save_dir);
1974 g_free(startdir);
1975 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1976 -1, NULL, NULL, NULL);
1977 g_free(dirname);
1979 mimeview_save_all_info(errors, number);
1982 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1984 MimeInfo *partinfo = NULL;
1985 if (mimeview->spec_part) {
1986 partinfo = mimeview->spec_part;
1987 mimeview->spec_part = NULL;
1988 } else {
1989 partinfo = (MimeInfo *) g_object_get_data
1990 (G_OBJECT(mimeview->popupmenu),
1991 "pop_partinfo");
1992 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1993 "pop_partinfo", NULL);
1994 if (!partinfo) {
1995 partinfo = mimeview_get_selected_part(mimeview);
1999 return partinfo;
2002 * Menu callback: Save the selected attachment
2003 * \param mimeview Current display
2005 void mimeview_save_as(MimeView *mimeview)
2007 gchar *filename;
2008 gchar *filepath = NULL;
2009 gchar *filedir = NULL;
2010 MimeInfo *partinfo;
2011 gchar *partname = NULL;
2013 if (!mimeview->opened) return;
2014 if (!mimeview->file) return;
2016 partinfo = mimeview_get_part_to_use(mimeview);
2018 cm_return_if_fail(partinfo != NULL);
2020 if (get_part_name(partinfo) == NULL) {
2021 return;
2023 partname = g_strdup(get_part_name(partinfo));
2025 if (!g_utf8_validate(partname, -1, NULL)) {
2026 gchar *tmp = conv_filename_to_utf8(partname);
2027 if (!tmp) {
2028 tmp = conv_codeset_strdup(partname,
2029 conv_get_locale_charset_str(),
2030 CS_UTF_8);
2032 if (tmp) {
2033 g_free(partname);
2034 partname = tmp;
2038 subst_for_filename(partname);
2040 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2041 filepath = g_strconcat(prefs_common.attach_save_dir,
2042 G_DIR_SEPARATOR_S, partname, NULL);
2043 else
2044 filepath = g_strdup(partname);
2046 g_free(partname);
2048 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2049 filename = filesel_select_file_save(_("Save as"), filepath);
2050 if (!filename) {
2051 g_free(filepath);
2052 return;
2055 mimeview_write_part(filename, partinfo, TRUE);
2057 filedir = g_path_get_dirname(filename);
2058 if (filedir && strcmp(filedir, ".")) {
2059 g_free(prefs_common.attach_save_dir);
2060 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2063 g_free(filedir);
2064 g_free(filepath);
2067 void mimeview_display_as_text(MimeView *mimeview)
2069 MimeInfo *partinfo;
2071 if (!mimeview->opened) return;
2073 partinfo = mimeview_get_part_to_use(mimeview);
2074 mimeview_select_mimepart_icon(mimeview, partinfo);
2075 cm_return_if_fail(partinfo != NULL);
2076 mimeview_show_message_part(mimeview, partinfo);
2079 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2081 gchar *filename;
2082 gint err;
2084 if (!mimeview->opened) return;
2085 if (!mimeview->file) return;
2087 if (!partinfo)
2088 partinfo = mimeview_get_part_to_use(mimeview);
2090 cm_return_if_fail(partinfo != NULL);
2092 filename = procmime_get_tmp_file_name(partinfo);
2094 if ((err = procmime_get_part(filename, partinfo)) < 0)
2095 alertpanel_error
2096 (_("Couldn't save the part of multipart message: %s"),
2097 g_strerror(-err));
2098 else
2099 mimeview_view_file(filename, partinfo, NULL, mimeview);
2101 g_free(filename);
2104 #ifndef G_OS_WIN32
2105 void mimeview_open_with(MimeView *mimeview)
2107 MimeInfo *partinfo;
2109 if (!mimeview) return;
2110 if (!mimeview->opened) return;
2111 if (!mimeview->file) return;
2113 partinfo = mimeview_get_part_to_use(mimeview);
2115 mimeview_open_part_with(mimeview, partinfo, FALSE);
2118 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2120 gchar *filename;
2121 gchar *cmd;
2122 gchar *mime_command = NULL;
2123 gchar *content_type = NULL;
2124 gint err;
2126 cm_return_if_fail(partinfo != NULL);
2128 filename = procmime_get_tmp_file_name(partinfo);
2130 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2131 alertpanel_error
2132 (_("Couldn't save the part of multipart message: %s"),
2133 g_strerror(-err));
2134 g_free(filename);
2135 return;
2138 if (!prefs_common.mime_open_cmd_history)
2139 prefs_common.mime_open_cmd_history =
2140 add_history(NULL, prefs_common.mime_open_cmd);
2142 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2143 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2144 /* guess content-type from filename */
2145 content_type = procmime_get_mime_type(filename);
2147 if (content_type == NULL) {
2148 content_type = procmime_get_content_type_str(partinfo->type,
2149 partinfo->subtype);
2152 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2153 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2154 mime_command = g_strdup(prefs_common_get_uri_cmd());
2155 g_free(content_type);
2156 content_type = NULL;
2157 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2158 || !prefs_common_get_ext_editor_cmd()[0]) {
2159 mime_command = mailcap_get_command_for_type(content_type, filename);
2160 } else {
2161 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2162 g_free(content_type);
2163 content_type = NULL;
2165 if (mime_command == NULL) {
2166 /* try with extension this time */
2167 g_free(content_type);
2168 content_type = procmime_get_mime_type(filename);
2169 mime_command = mailcap_get_command_for_type(content_type, filename);
2172 if (mime_command == NULL)
2173 automatic = FALSE;
2175 if (!automatic) {
2176 gboolean remember = FALSE;
2177 if (content_type != NULL)
2178 cmd = input_dialog_combo_remember
2179 (_("Open with"),
2180 _("Enter the command-line to open file:\n"
2181 "('%s' will be replaced with file name)"),
2182 mime_command ? mime_command : prefs_common.mime_open_cmd,
2183 prefs_common.mime_open_cmd_history, &remember);
2184 else
2185 cmd = input_dialog_combo
2186 (_("Open with"),
2187 _("Enter the command-line to open file:\n"
2188 "('%s' will be replaced with file name)"),
2189 mime_command ? mime_command : prefs_common.mime_open_cmd,
2190 prefs_common.mime_open_cmd_history);
2191 if (cmd && remember) {
2192 mailcap_update_default(content_type, cmd);
2194 g_free(mime_command);
2195 } else {
2196 cmd = mime_command;
2198 if (cmd) {
2199 mimeview_view_file(filename, partinfo, cmd, mimeview);
2200 g_free(prefs_common.mime_open_cmd);
2201 prefs_common.mime_open_cmd = cmd;
2202 prefs_common.mime_open_cmd_history =
2203 add_history(prefs_common.mime_open_cmd_history, cmd);
2206 g_free(content_type);
2207 g_free(filename);
2209 #endif
2211 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2213 GList *attach_file = NULL;
2214 AttachInfo *ainfo = NULL;
2215 gchar *filename;
2216 gint err;
2218 if (!mimeview->opened) return;
2219 if (!mimeview->file) return;
2221 cm_return_if_fail(partinfo != NULL);
2223 filename = procmime_get_tmp_file_name(partinfo);
2225 if (!(err = procmime_get_part(filename, partinfo))) {
2226 ainfo = g_new0(AttachInfo, 1);
2227 ainfo->file = filename;
2228 ainfo->name = g_strdup(get_part_name(partinfo));
2229 ainfo->content_type = procmime_get_content_type_str(
2230 partinfo->type, partinfo->subtype);
2231 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2232 partinfo, "charset"));
2233 attach_file = g_list_append(attach_file, ainfo);
2235 compose_new(NULL, NULL, attach_file);
2237 g_free(ainfo->name);
2238 g_free(ainfo->content_type);
2239 g_free(ainfo->charset);
2240 g_free(ainfo);
2241 g_list_free(attach_file);
2242 } else
2243 alertpanel_error
2244 (_("Couldn't save the part of multipart message: %s"),
2245 g_strerror(-err));
2246 g_free(filename);
2249 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2250 const gchar *cmd, MimeView *mimeview)
2252 #ifndef G_OS_WIN32
2253 gchar *p;
2254 gchar buf[BUFFSIZE];
2256 if (cmd == NULL)
2257 mimeview_open_part_with(mimeview, partinfo, TRUE);
2258 else {
2259 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2260 !strchr(p + 2, '%')) {
2261 g_snprintf(buf, sizeof(buf), cmd, filename);
2262 if (!prefs_common.save_parts_readwrite)
2263 g_chmod(filename, S_IRUSR);
2264 else
2265 g_chmod(filename, S_IRUSR|S_IWUSR);
2266 } else {
2267 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2268 mimeview_open_part_with(mimeview, partinfo, FALSE);
2270 if (execute_command_line(buf, TRUE, NULL) != 0) {
2271 if (!prefs_common.save_parts_readwrite)
2272 g_chmod(filename, S_IRUSR|S_IWUSR);
2273 mimeview_open_part_with(mimeview, partinfo, FALSE);
2276 #else
2277 SHFILEINFO file_info;
2278 GError *error = NULL;
2279 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2281 if (error != NULL) {
2282 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2283 error->message);
2284 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2285 g_error_free(error);
2286 return;
2289 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2290 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2291 _("This attachment is an executable file. Executing "
2292 "untrusted binaries is dangerous and could compromise "
2293 "your computer.\n\n"
2294 "Do you want to run this file?"), NULL, _("_Cancel"),
2295 NULL, _("Run binary"), NULL, NULL, ALERTFOCUS_FIRST,
2296 FALSE, NULL, ALERT_WARNING);
2297 if (val == G_ALERTALTERNATE) {
2298 debug_print("executing binary\n");
2299 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2301 } else {
2302 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2305 g_free(fn16);
2307 #endif
2310 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2312 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2315 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2317 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2320 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2322 GSList *mimeview_list, *viewer_list;
2324 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2325 MimeView *mimeview = (MimeView *) mimeview_list->data;
2327 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2328 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2329 mimeview->mimeviewer = NULL;
2332 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2333 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2335 mimeviewer->destroy_viewer(mimeviewer);
2336 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2340 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2343 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2345 gint num;
2346 MimeInfo *partinfo;
2348 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2349 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2351 if (event->button == 1) {
2352 icon_selected(mimeview, num, partinfo);
2353 gtk_widget_grab_focus(button);
2354 icon_list_toggle_by_mime_info(mimeview, partinfo);
2356 part_button_pressed(mimeview, event, partinfo);
2358 return FALSE;
2361 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2363 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2364 GtkTreeIter iter;
2365 GtkTreePath *path;
2366 MimeInfo *curr = NULL;
2368 if (!gtk_tree_model_get_iter_first(model, &iter))
2369 return;
2370 path = gtk_tree_model_get_path(model, &iter);
2372 do {
2373 gtk_tree_model_get_iter(model, &iter, path);
2374 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2375 if (curr == partinfo) {
2376 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2377 GTK_TREE_VIEW(mimeview->ctree));
2378 gtk_tree_selection_select_iter(sel, &iter);
2379 gtk_tree_path_free(path);
2380 return;
2382 } while (mimeview_tree_next(model, path));
2384 gtk_tree_path_free(path);
2387 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2389 icon_list_toggle_by_mime_info(mimeview, partinfo);
2390 icon_selected(mimeview, -1, partinfo);
2393 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2394 MimeView *mimeview)
2396 SummaryView *summaryview;
2398 if (!event) return FALSE;
2400 switch (event->keyval) {
2401 case GDK_KEY_space:
2402 if (mimeview_scroll_page(mimeview, FALSE))
2403 return TRUE;
2405 mimeview_select_next_part(mimeview);
2406 return TRUE;
2408 break;
2409 case GDK_KEY_BackSpace:
2410 mimeview_scroll_page(mimeview, TRUE);
2411 return TRUE;
2412 case GDK_KEY_Return:
2413 case GDK_KEY_KP_Enter:
2414 mimeview_scroll_one_line(mimeview,
2415 (event->state & GDK_MOD1_MASK) != 0);
2416 return TRUE;
2417 case GDK_KEY_y:
2418 BREAK_ON_MODIFIER_KEY();
2419 mimeview_save_as(mimeview);
2420 return TRUE;
2421 case GDK_KEY_t:
2422 BREAK_ON_MODIFIER_KEY();
2423 mimeview_display_as_text(mimeview);
2424 return TRUE;
2425 case GDK_KEY_l:
2426 BREAK_ON_MODIFIER_KEY();
2427 mimeview_launch(mimeview, NULL);
2428 return TRUE;
2429 #ifndef G_OS_WIN32
2430 case GDK_KEY_o:
2431 BREAK_ON_MODIFIER_KEY();
2432 mimeview_open_with(mimeview);
2433 return TRUE;
2434 #endif
2435 case GDK_KEY_c:
2436 BREAK_ON_MODIFIER_KEY();
2437 mimeview_check_signature(mimeview);
2438 return TRUE;
2439 case GDK_KEY_a:
2440 BREAK_ON_MODIFIER_KEY();
2441 mimeview_select_next_part(mimeview);
2442 return TRUE;
2443 default:
2444 break;
2447 if (!mimeview->messageview->mainwin) return FALSE;
2448 summaryview = mimeview->messageview->mainwin->summaryview;
2449 return summary_pass_key_press_event(summaryview, event);
2452 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2454 MimeView *mimeview = (MimeView *)data;
2455 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2457 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2458 "pop_partinfo", partinfo);
2459 gtk_menu_popup_at_pointer(GTK_MENU(mimeview->popupmenu), NULL);
2460 return TRUE;
2463 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2465 GtkWidget *pixmap = NULL;
2466 GtkWidget *grid;
2467 GtkWidget *button;
2468 gchar *tip;
2469 gchar *tiptmp;
2470 const gchar *desc = NULL;
2471 gchar *sigshort = NULL;
2472 gchar *content_type;
2473 StockPixmap stockp;
2474 MimeInfo *partinfo;
2475 MimeInfo *siginfo = NULL;
2476 MimeInfo *encrypted = NULL;
2477 #ifdef GENERIC_UMPC
2478 GtkRequisition r;
2479 #endif
2481 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2482 return;
2484 grid = mimeview->icon_grid;
2485 mimeview->icon_count++;
2486 button = gtk_event_box_new();
2488 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2489 G_CALLBACK(mimeview_visi_notify), mimeview);
2490 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2491 G_CALLBACK(mimeview_leave_notify), mimeview);
2492 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2493 G_CALLBACK(mimeview_enter_notify), mimeview);
2495 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2496 g_object_set_data(G_OBJECT(button), "icon_number",
2497 GINT_TO_POINTER(mimeview->icon_count));
2498 g_object_set_data(G_OBJECT(button), "partinfo",
2499 mimeinfo);
2501 switch (mimeinfo->type) {
2503 case MIMETYPE_TEXT:
2504 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2505 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2506 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2507 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2508 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2509 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2510 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2511 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2512 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2513 else
2514 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2515 break;
2516 case MIMETYPE_MESSAGE:
2517 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2518 break;
2519 case MIMETYPE_APPLICATION:
2520 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2521 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2522 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2523 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2524 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2525 stockp = STOCK_PIXMAP_MIME_PDF;
2526 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2527 stockp = STOCK_PIXMAP_MIME_PS;
2528 else
2529 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2530 break;
2531 case MIMETYPE_IMAGE:
2532 stockp = STOCK_PIXMAP_MIME_IMAGE;
2533 break;
2534 case MIMETYPE_AUDIO:
2535 stockp = STOCK_PIXMAP_MIME_AUDIO;
2536 break;
2537 default:
2538 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2539 break;
2542 partinfo = mimeinfo;
2543 while (partinfo != NULL) {
2544 if (privacy_mimeinfo_is_signed(partinfo)) {
2545 siginfo = partinfo;
2546 break;
2548 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2549 encrypted = partinfo;
2550 break;
2552 partinfo = procmime_mimeinfo_parent(partinfo);
2555 if (siginfo != NULL) {
2556 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2557 case SIGNATURE_UNCHECKED:
2558 case SIGNATURE_CHECK_ERROR:
2559 case SIGNATURE_CHECK_FAILED:
2560 case SIGNATURE_CHECK_TIMEOUT:
2561 pixmap = stock_pixmap_widget_with_overlay(stockp,
2562 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2563 break;
2564 case SIGNATURE_OK:
2565 pixmap = stock_pixmap_widget_with_overlay(stockp,
2566 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2567 break;
2568 case SIGNATURE_WARN:
2569 case SIGNATURE_KEY_EXPIRED:
2570 pixmap = stock_pixmap_widget_with_overlay(stockp,
2571 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2572 break;
2573 case SIGNATURE_INVALID:
2574 pixmap = stock_pixmap_widget_with_overlay(stockp,
2575 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2576 break;
2578 sigshort = privacy_mimeinfo_get_sig_info(siginfo, FALSE);
2579 } else if (encrypted != NULL) {
2580 pixmap = stock_pixmap_widget_with_overlay(stockp,
2581 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2582 } else {
2583 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2584 OVERLAY_NONE, 6, 3);
2586 gtk_container_add(GTK_CONTAINER(button), pixmap);
2587 if (!desc) {
2588 if (prefs_common.attach_desc)
2589 desc = get_part_description(mimeinfo);
2590 else
2591 desc = get_part_name(mimeinfo);
2594 content_type = procmime_get_content_type_str(mimeinfo->type,
2595 mimeinfo->subtype);
2597 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2598 "\n<b>", _("Size:"), " </b>",
2599 to_human_readable((goffset)mimeinfo->length), NULL);
2600 g_free(content_type);
2601 if (desc && *desc) {
2602 gchar *tmp = NULL, *escaped = NULL;
2603 if (!g_utf8_validate(desc, -1, NULL)) {
2604 tmp = conv_filename_to_utf8(desc);
2605 } else {
2606 tmp = g_strdup(desc);
2608 escaped = g_markup_escape_text(tmp,-1);
2610 tiptmp = g_strconcat(tip, "\n<b>",
2611 prefs_common.attach_desc && mimeinfo->description ?
2612 _("Description:") : _("Filename:"),
2613 " </b>", escaped, NULL);
2614 g_free(tip);
2615 tip = tiptmp;
2616 g_free(tmp);
2617 g_free(escaped);
2619 if (sigshort && *sigshort) {
2620 gchar *sigshort_escaped =
2621 g_markup_escape_text(sigshort, -1);
2623 tiptmp = g_strjoin("\n", tip, sigshort_escaped, NULL);
2624 g_free(tip);
2625 g_free(sigshort_escaped);
2626 tip = tiptmp;
2629 gtk_widget_set_tooltip_markup(button, tip);
2630 g_free(tip);
2631 gtk_widget_show_all(button);
2632 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2633 mimeview_mime_types, 1, GDK_ACTION_COPY);
2635 g_signal_connect(G_OBJECT(button), "popup-menu",
2636 G_CALLBACK(icon_popup_menu), mimeview);
2637 g_signal_connect(G_OBJECT(button), "button_release_event",
2638 G_CALLBACK(icon_clicked_cb), mimeview);
2639 g_signal_connect(G_OBJECT(button), "key_press_event",
2640 G_CALLBACK(icon_key_pressed), mimeview);
2641 g_signal_connect(G_OBJECT(button), "drag_data_get",
2642 G_CALLBACK(mimeview_drag_data_get), mimeview);
2643 gtk_container_add(GTK_CONTAINER(grid), button);
2644 #ifdef GENERIC_UMPC
2645 gtk_widget_get_preferred_size(pixmap, &r, NULL);
2646 gtk_widget_set_size_request(button, -1, r.height + 4);
2647 #endif
2651 static void icon_list_clear (MimeView *mimeview)
2653 GList *child, *orig;
2654 GtkAdjustment *adj;
2656 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_grid));
2657 for (child = orig; child != NULL; child = g_list_next(child)) {
2658 gtk_container_remove(GTK_CONTAINER(mimeview->icon_grid),
2659 GTK_WIDGET(child->data));
2661 g_list_free(orig);
2662 mimeview->icon_count = 0;
2663 adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(mimeview->icon_scroll));
2664 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2668 *\brief Used to 'click' the next or previous icon.
2670 *\return true if the icon 'number' exists and was selected.
2672 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2673 GtkAllocation *size, MimeView *mimeview)
2675 GtkAllocation grid_size;
2676 GtkAllocation layout_size;
2677 GtkAdjustment *adj;
2678 guint width;
2679 guint height;
2681 adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(mimeview->icon_scroll));
2683 gtk_widget_get_allocation(mimeview->icon_grid, &grid_size);
2684 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2686 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2687 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2688 MIN(grid_size.width, layout_size.width),
2689 MAX(grid_size.height, layout_size.height));
2690 gtk_adjustment_set_step_increment(adj, 10);
2693 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2695 gint min_width, width;
2697 cm_return_if_fail(mimeinfo != NULL);
2699 while (mimeinfo != NULL) {
2700 if (mimeinfo->type != MIMETYPE_MULTIPART)
2701 icon_list_append_icon(mimeview, mimeinfo);
2702 if (mimeinfo->node->children != NULL)
2703 icon_list_create(mimeview,
2704 (MimeInfo *) mimeinfo->node->children->data);
2705 mimeinfo = mimeinfo->node->next != NULL
2706 ? (MimeInfo *) mimeinfo->node->next->data
2707 : NULL;
2709 gtk_widget_get_preferred_width(mimeview->icon_mainbox, &min_width, &width);
2710 if (min_width < width) {
2711 gtk_widget_set_size_request(mimeview->icon_mainbox,
2712 min_width, -1);
2714 if (mimeview->opened)
2715 icon_list_toggle_by_mime_info(mimeview,
2716 mimeview_get_node_part(mimeview, mimeview->opened));
2719 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2720 MimeInfo *mimeinfo)
2722 GList *children, *child;
2724 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_grid));
2725 for (child = children; child != NULL; child = g_list_next(child)) {
2726 gboolean *highlight = NULL;
2727 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2729 if (!GTK_IS_EVENT_BOX(child->data))
2730 continue;
2732 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2733 *highlight = (g_object_get_data(G_OBJECT(child->data),
2734 "partinfo") == (gpointer)mimeinfo);
2736 gtk_widget_queue_draw(icon);
2738 g_list_free(children);
2741 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2742 MimeView *mimeview)
2744 prefs_common.mimeview_tree_height = allocation->height;
2747 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2748 MimeView *mimeview)
2750 g_object_ref(button);
2752 mimeview_leave_notify(button, NULL, NULL);
2754 mimeview->ctree_mode = !mimeview->ctree_mode;
2755 if (mimeview->ctree_mode) {
2756 gtk_image_set_from_icon_name(GTK_IMAGE(mimeview->arrow),
2757 "pan-end-symbolic", GTK_ICON_SIZE_MENU);
2758 gtk_widget_hide(mimeview->icon_mainbox);
2759 gtk_widget_show(mimeview->ctree_mainbox);
2760 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2761 prefs_common.mimeview_tree_height);
2763 gtk_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2764 button);
2765 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2766 button, FALSE, FALSE, 0);
2767 } else {
2768 gtk_image_set_from_icon_name(GTK_IMAGE(mimeview->arrow),
2769 "pan-start-symbolic", GTK_ICON_SIZE_MENU);
2770 gtk_widget_hide(mimeview->ctree_mainbox);
2771 gtk_widget_show(mimeview->icon_mainbox);
2772 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2774 gtk_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2775 button);
2776 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2777 button, FALSE, FALSE, 0);
2778 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2779 if (mimeview->opened)
2780 icon_list_toggle_by_mime_info(mimeview,
2781 mimeview_get_node_part(mimeview, mimeview->opened));
2782 summary_grab_focus(mimeview->mainwin->summaryview);
2784 g_object_unref(button);
2785 return TRUE;
2788 void mimeview_update (MimeView *mimeview)
2790 if (mimeview && mimeview->mimeinfo) {
2791 icon_list_clear(mimeview);
2792 icon_list_create(mimeview, mimeview->mimeinfo);
2796 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2798 MessageView *msgview = NULL;
2799 MainWindow *mainwin = NULL;
2801 if (!cmd)
2802 return;
2804 msgview = mimeview->messageview;
2805 if (!msgview)
2806 return;
2808 mainwin = msgview->mainwin;
2809 if (!mainwin)
2810 return;
2812 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2813 "pop_partinfo", NULL);
2815 if (!strcmp(cmd, "sc://view_log"))
2816 log_window_show(mainwin->logwin);
2817 else if (!strcmp(cmd, "sc://save_as"))
2818 mimeview_save_as(mimeview);
2819 else if (!strcmp(cmd, "sc://display_as_text"))
2820 mimeview_display_as_text(mimeview);
2821 #ifndef G_OS_WIN32
2822 else if (!strcmp(cmd, "sc://open_with"))
2823 mimeview_open_with(mimeview);
2824 #endif
2825 else if (!strcmp(cmd, "sc://open"))
2826 mimeview_launch(mimeview, NULL);
2827 else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
2828 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2829 icon_selected(mimeview, -1, (MimeInfo *)data);
2830 } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
2831 mimeview_launch(mimeview, (MimeInfo *)data);
2832 } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
2833 mimeview->spec_part = (MimeInfo *)data;
2834 part_button_pressed(mimeview, event, (MimeInfo *)data);
2835 } else if (!strncmp(cmd, "sc://search_tags:", strlen("sc://search_tags:"))) {
2836 const gchar *tagname = cmd + strlen("sc://search_tags:");
2837 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2838 gtk_toggle_button_set_active(
2839 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2840 TRUE);
2841 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2842 ADVANCED_SEARCH_EXTENDED, buf);
2843 g_free(buf);
2847 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2849 if (mimeview->type == MIMEVIEW_TEXT)
2850 return textview_scroll_page(mimeview->textview, up);
2851 else if (mimeview->mimeviewer) {
2852 MimeViewer *mimeviewer = mimeview->mimeviewer;
2853 if (mimeviewer->scroll_page)
2854 return mimeviewer->scroll_page(mimeviewer, up);
2856 return TRUE;
2859 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2861 if (mimeview->type == MIMEVIEW_TEXT)
2862 textview_scroll_one_line(mimeview->textview, up);
2863 else if (mimeview->mimeviewer) {
2864 MimeViewer *mimeviewer = mimeview->mimeviewer;
2865 if (mimeviewer->scroll_one_line)
2866 mimeviewer->scroll_one_line(mimeviewer, up);