Remove some deprecation warnings from python-shell
[claws.git] / src / mimeview.c
blob60a0ded0ab960defd55ba80536e601fc76114ea0
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 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_headers_visible(GTK_TREE_VIEW(ctree),
329 prefs_common.show_col_headers);
331 renderer = gtk_cell_renderer_text_new();
332 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
333 -1, titles[COL_MIMETYPE],renderer,
334 "text", COL_MIMETYPE, NULL);
335 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
337 renderer = gtk_cell_renderer_text_new();
338 gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
339 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
340 -1, titles[COL_SIZE], renderer,
341 "text", COL_SIZE, NULL);
342 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
343 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(column), 1);
345 renderer = gtk_cell_renderer_text_new();
346 cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
347 -1, titles[COL_NAME], renderer,
348 "text", COL_NAME, NULL);
349 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
350 gtk_tree_view_column_set_expand(GTK_TREE_VIEW_COLUMN(column), TRUE);
352 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ctree));
353 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
355 gtk_widget_show(ctree);
356 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
357 gtk_drag_source_set(ctree, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
358 mimeview_mime_types, 1, GDK_ACTION_COPY);
360 g_signal_connect(G_OBJECT(selection), "changed",
361 G_CALLBACK(mimeview_selected), mimeview);
362 g_signal_connect(G_OBJECT(ctree), "button_release_event",
363 G_CALLBACK(mimeview_button_pressed), mimeview);
364 g_signal_connect(G_OBJECT(ctree), "key_press_event",
365 G_CALLBACK(mimeview_key_pressed), mimeview);
366 g_signal_connect(G_OBJECT(ctree), "drag_data_get",
367 G_CALLBACK(mimeview_drag_data_get), mimeview);
369 mime_notebook = gtk_notebook_new();
370 gtk_widget_set_name(GTK_WIDGET(mime_notebook), "mime_notebook");
371 gtk_widget_show(mime_notebook);
372 gtk_widget_set_can_focus(mime_notebook, FALSE);
373 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
374 gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
376 icon_grid = gtk_grid_new();
377 gtk_orientable_set_orientation(GTK_ORIENTABLE(icon_grid),
378 GTK_ORIENTATION_VERTICAL);
379 gtk_grid_set_row_spacing(GTK_GRID(icon_grid), 0);
380 gtk_widget_show(icon_grid);
381 icon_scroll = gtk_layout_new(NULL, NULL);
382 gtk_widget_show(icon_scroll);
383 gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_grid, 0, 0);
384 scrollbutton = gtk_vscrollbutton_new(gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(icon_scroll)));
385 gtk_widget_show(scrollbutton);
387 g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
388 G_CALLBACK(mimeview_scrolled), mimeview);
390 mime_toggle = gtk_event_box_new();
391 gtk_event_box_set_visible_window(GTK_EVENT_BOX(mime_toggle), FALSE);
393 g_signal_connect(G_OBJECT(mime_toggle), "motion-notify-event",
394 G_CALLBACK(mimeview_visi_notify), mimeview);
395 g_signal_connect(G_OBJECT(mime_toggle), "leave-notify-event",
396 G_CALLBACK(mimeview_leave_notify), mimeview);
397 g_signal_connect(G_OBJECT(mime_toggle), "enter-notify-event",
398 G_CALLBACK(mimeview_enter_notify), mimeview);
400 gtk_container_set_border_width(GTK_CONTAINER(mime_toggle), 2);
401 gtk_widget_show(mime_toggle);
402 mimeview->ctree_mode = FALSE;
403 arrow = gtk_image_new_from_icon_name("pan-start-symbolic", GTK_ICON_SIZE_MENU);
404 gtk_widget_show(arrow);
405 gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
406 g_signal_connect(G_OBJECT(mime_toggle), "button_release_event",
407 G_CALLBACK(mime_toggle_button_cb), mimeview);
409 icon_mainbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
410 gtk_widget_set_name(GTK_WIDGET(icon_mainbox), "mimeview_icon_mainbox");
411 gtk_widget_show(icon_mainbox);
412 gtk_widget_set_size_request(icon_mainbox, 32, -1);
413 gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
414 gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
415 gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
416 g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
417 G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
419 ctree_mainbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
420 gtk_widget_set_name(GTK_WIDGET(ctree_mainbox), "mimeview_ctree_mainbox");
421 gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
422 g_signal_connect(G_OBJECT(ctree_mainbox), "size_allocate",
423 G_CALLBACK(ctree_size_allocate_cb), mimeview);
425 mimeview->ui_manager = gtk_ui_manager_new();
426 mimeview->action_group = cm_menu_create_action_group_full(mimeview->ui_manager,
427 "MimeView", mimeview_menu_actions,
428 G_N_ELEMENTS(mimeview_menu_actions), (gpointer)mimeview);
430 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
431 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
432 "/Menus/", "MimeView", "MimeView", GTK_UI_MANAGER_MENU);
433 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
434 "/Menus/MimeView/", "Open", "MimeView/Open",
435 GTK_UI_MANAGER_MENUITEM);
436 #if (!defined G_OS_WIN32)
437 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
438 "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
439 GTK_UI_MANAGER_MENUITEM);
440 #endif
441 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
442 "/Menus/MimeView/", "Copy", "MimeView/Copy",
443 GTK_UI_MANAGER_MENUITEM);
444 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
445 "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
446 GTK_UI_MANAGER_MENUITEM);
447 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
448 "/Menus/MimeView/", "DisplayAsText", "MimeView/DisplayAsText",
449 GTK_UI_MANAGER_MENUITEM);
450 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
451 "/Menus/MimeView/", "SaveAs", "MimeView/SaveAs",
452 GTK_UI_MANAGER_MENUITEM);
453 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
454 "/Menus/MimeView/", "SaveAll", "MimeView/SaveAll",
455 GTK_UI_MANAGER_MENUITEM);
456 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
457 "/Menus/MimeView/", "SaveAllAttachments", "MimeView/SaveAllAttachments",
458 GTK_UI_MANAGER_MENUITEM);
459 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
460 "/Menus/MimeView/", "NextPart", "MimeView/NextPart",
461 GTK_UI_MANAGER_MENUITEM);
462 MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
463 "/Menus/MimeView/", "PrevPart", "MimeView/PrevPart",
464 GTK_UI_MANAGER_MENUITEM);
466 popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
467 gtk_ui_manager_get_widget(mimeview->ui_manager, "/Menus/MimeView")) );
470 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
471 gtk_widget_show(vbox);
472 siginfoview = noticeview_create(mainwin);
473 gtk_widget_set_name(GTK_WIDGET(siginfoview->vgrid), "siginfoview");
474 noticeview_hide(siginfoview);
475 noticeview_set_icon_clickable(siginfoview, TRUE);
476 gtk_box_pack_start(GTK_BOX(vbox), mime_notebook, TRUE, TRUE, 0);
477 gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET_PTR(siginfoview), FALSE, FALSE, 0);
479 paned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
480 gtk_widget_show(paned);
481 gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
482 gtk_paned_pack2(GTK_PANED(paned), vbox, TRUE, TRUE);
484 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
485 gtk_widget_set_name(GTK_WIDGET(hbox), "mimeview");
486 gtk_box_pack_start(GTK_BOX(hbox), paned, TRUE, TRUE, 0);
487 gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
489 gtk_widget_show(hbox);
490 gtk_widget_hide(ctree_mainbox);
491 #ifdef GENERIC_UMPC
492 gtk_widget_set_size_request(mime_toggle, -1, r.height + 8);
493 #endif
494 mimeview->hbox = hbox;
495 mimeview->paned = paned;
496 mimeview->scrolledwin = scrolledwin;
497 mimeview->ctree = ctree;
498 mimeview->mime_notebook = mime_notebook;
499 mimeview->popupmenu = popupmenu;
500 mimeview->type = -1;
501 mimeview->ctree_mainbox = ctree_mainbox;
502 mimeview->icon_scroll = icon_scroll;
503 mimeview->icon_grid = icon_grid;
504 mimeview->icon_mainbox = icon_mainbox;
505 mimeview->icon_count = 0;
506 mimeview->mainwin = mainwin;
507 mimeview->mime_toggle = mime_toggle;
508 mimeview->siginfoview = siginfoview;
509 mimeview->scrollbutton = scrollbutton;
510 mimeview->arrow = arrow;
511 mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
513 mimeviews = g_slist_prepend(mimeviews, mimeview);
515 return mimeview;
518 void mimeview_init(MimeView *mimeview)
520 textview_init(mimeview->textview);
522 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
523 GTK_WIDGET_PTR(mimeview->textview));
526 static gboolean any_part_is_signed(MimeInfo *mimeinfo)
528 while (mimeinfo) {
529 if (privacy_mimeinfo_is_signed(mimeinfo))
530 return TRUE;
531 mimeinfo = procmime_mimeinfo_next(mimeinfo);
534 return FALSE;
537 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
538 const gchar *file)
540 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
542 mimeview_clear(mimeview);
544 cm_return_if_fail(file != NULL);
545 cm_return_if_fail(mimeinfo != NULL);
547 mimeview->mimeinfo = mimeinfo;
549 mimeview->file = g_strdup(file);
551 g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
552 mimeview);
554 /* check if the mail's signed - it can change the mail structure */
556 if (any_part_is_signed(mimeinfo))
557 debug_print("signed mail\n");
559 mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
560 gtk_tree_view_expand_all(ctree);
561 icon_list_clear(mimeview);
562 icon_list_create(mimeview, mimeinfo);
564 g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
565 mimeview_selected, mimeview);
568 static void mimeview_free_mimeinfo(MimeView *mimeview)
570 if (mimeview->mimeinfo != NULL) {
571 procmime_mimeinfo_free_all(&mimeview->mimeinfo);
572 mimeview->mimeinfo = NULL;
576 void mimeview_destroy(MimeView *mimeview)
578 GSList *cur;
580 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
581 MimeViewer *viewer = (MimeViewer *) cur->data;
582 viewer->destroy_viewer(viewer);
584 g_slist_free(mimeview->viewers);
585 gtk_target_list_unref(mimeview->target_list);
587 if (mimeview->sig_check_timeout_tag != 0)
588 g_source_remove(mimeview->sig_check_timeout_tag);
589 if (mimeview->sig_check_cancellable != NULL) {
590 /* Set last_sig_check_task to NULL to discard results in async_cb */
591 mimeview->siginfo->last_sig_check_task = NULL;
592 g_cancellable_cancel(mimeview->sig_check_cancellable);
593 g_object_unref(mimeview->sig_check_cancellable);
595 mimeview_free_mimeinfo(mimeview);
596 gtk_tree_path_free(mimeview->opened);
597 g_free(mimeview->file);
598 mimeviews = g_slist_remove(mimeviews, mimeview);
599 g_free(mimeview);
602 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
604 return gtkut_tree_view_get_selected_pointer(
605 GTK_TREE_VIEW(mimeview->ctree), COL_DATA,
606 NULL, NULL, NULL);
609 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
611 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
612 GtkTreeIter iter;
613 MimeInfo *partinfo;
615 gtk_tree_model_get_iter(model, &iter, path);
616 gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
617 return partinfo;
620 gboolean mimeview_tree_is_empty(MimeView *mimeview)
622 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
623 GtkTreeIter iter;
624 return !gtk_tree_model_get_iter_first(model, &iter);
627 static gboolean mimeview_tree_next(GtkTreeModel *model, GtkTreePath *path)
629 GtkTreeIter iter, parent;
630 gboolean has_parent;
632 gtk_tree_model_get_iter(model, &iter, path);
634 if (gtk_tree_model_iter_has_child(model, &iter)) {
635 gtk_tree_path_down(path);
636 return TRUE;
639 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
641 if (!gtk_tree_model_iter_next(model, &iter)) {
642 while (has_parent) {
643 GtkTreeIter saved_parent = parent;
644 gtk_tree_path_up(path);
645 if (gtk_tree_model_iter_next(model, &parent)) {
646 gtk_tree_path_next(path);
647 return TRUE;
648 } else {
649 has_parent = gtk_tree_model_iter_parent(model, &parent, &saved_parent);
653 } else {
654 gtk_tree_path_next(path);
655 return TRUE;
658 return FALSE;
661 static gboolean mimeview_tree_prev(MimeView *mimeview, GtkTreePath *path)
663 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
664 GtkTreeIter iter, child, parent;
665 gboolean has_parent;
667 gtk_tree_model_get_iter(model, &iter, path);
668 has_parent = gtk_tree_model_iter_parent(model, &parent, &iter);
670 if (gtk_tree_path_prev(path)) {
671 gtk_tree_model_get_iter(model, &iter, path);
673 if (gtk_tree_model_iter_nth_child(model, &child, &iter, 0)) {
674 gtk_tree_path_down(path);
676 while (gtk_tree_model_iter_next(model, &child))
677 gtk_tree_path_next(path);
680 return TRUE;
683 if (has_parent) {
684 gtk_tree_path_up(path);
685 return TRUE;
688 return FALSE;
691 gint mimeview_get_selected_part_num(MimeView *mimeview)
693 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
694 GtkTreeIter iter;
695 GtkTreePath *path;
696 gint i = 0;
698 if (!gtk_tree_model_get_iter_first(model, &iter))
699 return -1;
700 path = gtk_tree_model_get_path(model, &iter);
702 do {
703 if (!gtk_tree_path_compare(mimeview->opened, path)) {
704 gtk_tree_path_free(path);
705 return i;
708 i++;
709 } while (mimeview_tree_next(model, path));
711 gtk_tree_path_free(path);
713 return -1;
716 void mimeview_select_part_num(MimeView *mimeview, gint i)
718 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
719 GtkTreeIter iter;
720 GtkTreePath *path;
721 GtkTreeSelection *selection;
722 gint x = 0;
724 if (i < 0)
725 return;
727 if (!gtk_tree_model_get_iter_first(model, &iter))
728 return;
729 path = gtk_tree_model_get_path(model, &iter);
731 while (x != i) {
732 if (!mimeview_tree_next(model, path)) {
733 gtk_tree_path_free(path);
734 return;
736 x++;
739 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
740 gtk_tree_selection_select_path(selection, path);
741 gtk_tree_path_free(path);
744 static void mimeview_set_multipart_tree(MimeView *mimeview,
745 MimeInfo *mimeinfo,
746 GtkTreeIter *parent)
748 GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(
749 GTK_TREE_VIEW(mimeview->ctree)));
750 GtkTreeIter iter;
751 gchar *content_type, *length, *name;
753 cm_return_if_fail(mimeinfo != NULL);
755 while (mimeinfo != NULL) {
756 if (mimeinfo->type != MIMETYPE_UNKNOWN && mimeinfo->subtype)
757 content_type = g_strdup_printf("%s/%s",
758 procmime_get_media_type_str(mimeinfo->type),
759 mimeinfo->subtype);
760 else
761 content_type = g_strdup("UNKNOWN");
763 length = g_strdup(to_human_readable((goffset) mimeinfo->length));
765 if (prefs_common.attach_desc)
766 name = g_strdup(get_part_description(mimeinfo));
767 else
768 name = g_strdup(get_part_name(mimeinfo));
770 gtk_tree_store_append(model, &iter, parent);
771 gtk_tree_store_set(model, &iter,
772 COL_MIMETYPE, content_type,
773 COL_SIZE, length,
774 COL_NAME, name,
775 COL_DATA, mimeinfo, -1);
776 g_free(content_type);
777 g_free(length);
778 g_free(name);
780 if (mimeinfo->node->children)
781 mimeview_set_multipart_tree(mimeview,
782 (MimeInfo *) mimeinfo->node->children->data, &iter);
783 mimeinfo = mimeinfo->node->next != NULL ?
784 (MimeInfo *) mimeinfo->node->next->data : NULL;
788 static const gchar *get_real_part_name(MimeInfo *partinfo)
790 const gchar *name = NULL;
792 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
793 if (name == NULL)
794 name = procmime_mimeinfo_get_parameter(partinfo, "name");
796 return name;
799 static const gchar *get_part_name(MimeInfo *partinfo)
801 const gchar *name;
803 name = get_real_part_name(partinfo);
804 if (name == NULL)
805 name = "";
807 return name;
810 static const gchar *get_part_description(MimeInfo *partinfo)
812 if (partinfo->description)
813 return partinfo->description;
814 else
815 return get_part_name(partinfo);
818 static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
820 FILE *fp;
821 const gchar *fname;
823 if (!partinfo) return;
825 fname = mimeview->file;
826 if (!fname) return;
828 if ((fp = claws_fopen(fname, "rb")) == NULL) {
829 FILE_OP_ERROR(fname, "claws_fopen");
830 return;
833 if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
834 FILE_OP_ERROR(mimeview->file, "fseek");
835 claws_fclose(fp);
836 return;
839 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
840 textview_show_part(mimeview->textview, partinfo, fp);
842 claws_fclose(fp);
845 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
847 GSList *cur;
848 MimeViewerFactory *factory = NULL;
849 MimeViewer *viewer = NULL;
850 gchar *real_contenttype = NULL, *tmp;
852 real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
854 for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
855 MimeViewerFactory *curfactory = cur->data;
856 gint i = 0;
858 while (curfactory->content_types[i] != NULL) {
859 tmp = g_utf8_strdown(curfactory->content_types[i], -1);
860 if (g_pattern_match_simple(tmp, real_contenttype)) {
861 debug_print("%s\n", curfactory->content_types[i]);
862 factory = curfactory;
863 g_free(tmp);
864 break;
866 g_free(tmp);
867 i++;
869 if (factory != NULL)
870 break;
872 g_free(real_contenttype);
873 if (factory == NULL)
874 return NULL;
876 for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
877 MimeViewer *curviewer = cur->data;
879 if (curviewer->factory == factory)
880 return curviewer;
882 viewer = factory->create_viewer();
883 gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
884 GTK_WIDGET(viewer->get_widget(viewer)));
886 mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
888 return viewer;
891 gboolean mimeview_has_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
893 return (get_viewer_for_content_type(mimeview, content_type) != NULL);
896 static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
898 gchar *content_type = NULL;
899 MimeViewer *viewer = NULL;
901 if ((partinfo->type == MIMETYPE_APPLICATION) &&
902 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
903 const gchar *filename;
905 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
906 if (filename == NULL)
907 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
908 if (filename != NULL)
909 content_type = procmime_get_mime_type(filename);
910 } else {
911 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
914 if (content_type != NULL) {
915 viewer = get_viewer_for_content_type(mimeview, content_type);
916 g_free(content_type);
919 return viewer;
922 gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
924 MimeViewer *viewer;
926 if (mimeview->messageview->partial_display_shown) {
927 noticeview_hide(mimeview->messageview->noticeview);
928 mimeview->messageview->partial_display_shown = FALSE;
931 viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
932 if (viewer == NULL) {
933 if (mimeview->mimeviewer != NULL)
934 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
935 mimeview->mimeviewer = NULL;
936 return FALSE;
939 if (mimeview->mimeviewer != NULL)
940 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
942 if (mimeview->mimeviewer != viewer)
943 mimeview->mimeviewer = viewer;
945 mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
946 viewer->mimeview = mimeview;
947 viewer->show_mimepart(viewer, mimeview->file, partinfo);
949 return TRUE;
952 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
954 TextView *textview = mimeview->textview;
955 GtkWidget *focused = NULL;
957 if (mainwindow_get_mainwindow())
958 focused = gtkut_get_focused_child(
959 GTK_CONTAINER(mainwindow_get_mainwindow()->window));
961 if ((mimeview->type != MIMEVIEW_VIEWER) &&
962 (mimeview->type == type)) return;
964 switch (type) {
965 case MIMEVIEW_TEXT:
966 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
967 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
968 GTK_WIDGET_PTR(textview)));
969 break;
970 case MIMEVIEW_VIEWER:
971 gtk_notebook_set_current_page(GTK_NOTEBOOK(mimeview->mime_notebook),
972 gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
973 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
974 break;
975 default:
976 return;
978 if (focused)
979 gtk_widget_grab_focus(focused);
980 mimeview->type = type;
983 void mimeview_clear(MimeView *mimeview)
985 GtkTreeModel *model;
987 if (!mimeview)
988 return;
990 if (g_slist_find(mimeviews, mimeview) == NULL)
991 return;
993 if (mimeview->sig_check_timeout_tag != 0) {
994 g_source_remove(mimeview->sig_check_timeout_tag);
995 mimeview->sig_check_timeout_tag = 0;
998 if (mimeview->sig_check_cancellable != NULL) {
999 /* Set last_sig_check_task to NULL to discard results in async_cb */
1000 mimeview->siginfo->last_sig_check_task = NULL;
1001 g_cancellable_cancel(mimeview->sig_check_cancellable);
1002 g_object_unref(mimeview->sig_check_cancellable);
1003 mimeview->sig_check_cancellable = NULL;
1006 noticeview_hide(mimeview->siginfoview);
1008 model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
1009 gtk_tree_store_clear(GTK_TREE_STORE(model));
1011 textview_clear(mimeview->textview);
1012 if (mimeview->mimeviewer != NULL)
1013 mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
1015 mimeview_free_mimeinfo(mimeview);
1017 mimeview->mimeinfo = NULL;
1019 gtk_tree_path_free(mimeview->opened);
1020 mimeview->opened = NULL;
1022 g_free(mimeview->file);
1023 mimeview->file = NULL;
1025 icon_list_clear(mimeview);
1026 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1029 gchar * get_message_check_signature_shortcut(MessageView *messageview) {
1030 GtkUIManager *ui_manager;
1032 if (messageview->window != NULL)
1033 ui_manager = messageview->ui_manager;
1034 else
1035 ui_manager = messageview->mainwin->ui_manager;
1037 return cm_menu_item_get_shortcut(ui_manager, "Menu/Message/CheckSignature");
1040 static void check_signature_cb(GtkWidget *widget, gpointer user_data);
1041 static void display_full_info_cb(GtkWidget *widget, gpointer user_data);
1043 static void update_signature_noticeview(MimeView *mimeview, gboolean special, SignatureStatus code)
1045 gchar *text = NULL, *button_text = NULL;
1046 void *func = NULL;
1047 StockPixmap icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1048 SignatureStatus mycode = SIGNATURE_UNCHECKED;
1050 if (mimeview == NULL || mimeview->siginfo == NULL)
1051 g_error("bad call to update noticeview");
1053 if (special)
1054 mycode = code;
1055 else
1056 mycode = privacy_mimeinfo_get_sig_status(mimeview->siginfo);
1058 switch (mycode) {
1059 case SIGNATURE_UNCHECKED:
1060 button_text = _("Check signature");
1061 func = check_signature_cb;
1062 icon = STOCK_PIXMAP_PRIVACY_SIGNED;
1063 break;
1064 case SIGNATURE_OK:
1065 button_text = _("View full information");
1066 func = display_full_info_cb;
1067 icon = STOCK_PIXMAP_PRIVACY_PASSED;
1068 break;
1069 case SIGNATURE_WARN:
1070 button_text = _("View full information");
1071 func = display_full_info_cb;
1072 icon = STOCK_PIXMAP_PRIVACY_WARN;
1073 break;
1074 case SIGNATURE_KEY_EXPIRED:
1075 button_text = _("View full information");
1076 func = display_full_info_cb;
1077 icon = STOCK_PIXMAP_PRIVACY_EXPIRED;
1078 break;
1079 case SIGNATURE_INVALID:
1080 button_text = _("View full information");
1081 func = display_full_info_cb;
1082 icon = STOCK_PIXMAP_PRIVACY_FAILED;
1083 break;
1084 case SIGNATURE_CHECK_ERROR:
1085 case SIGNATURE_CHECK_FAILED:
1086 case SIGNATURE_CHECK_TIMEOUT:
1087 button_text = _("Check again");
1088 func = check_signature_cb;
1089 icon = STOCK_PIXMAP_PRIVACY_UNKNOWN;
1090 break;
1091 default:
1092 break;
1095 if (mycode == SIGNATURE_UNCHECKED) {
1096 gchar *tmp = privacy_mimeinfo_get_sig_info(mimeview->siginfo, FALSE);
1097 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1099 if (*shortcut == '\0')
1100 text = g_strdup_printf(_("%s Click the icon to check it."), tmp);
1101 else
1102 text = g_strdup_printf(_("%s Click the icon or hit '%s' to check it."), tmp, shortcut);
1104 g_free(shortcut);
1105 } else if (mycode == SIGNATURE_CHECK_TIMEOUT) {
1106 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1108 if (*shortcut == '\0')
1109 text = g_strdup(_("Timeout checking the signature. Click the icon to try again."));
1110 else
1111 text = g_strdup_printf(_("Timeout checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1113 g_free(shortcut);
1114 } else if (mycode == SIGNATURE_CHECK_ERROR) {
1115 gchar *shortcut = get_message_check_signature_shortcut(mimeview->messageview);
1117 if (*shortcut == '\0')
1118 text = g_strdup(_("Error checking the signature. Click the icon to try again."));
1119 else
1120 text = g_strdup_printf(_("Error checking the signature. Click the icon or hit '%s' to try again."), shortcut);
1122 g_free(shortcut);
1123 } else {
1124 text = g_strdup(privacy_mimeinfo_get_sig_info(mimeview->siginfo, FALSE));
1127 noticeview_set_text(mimeview->siginfoview, text);
1128 gtk_label_set_selectable(GTK_LABEL(mimeview->siginfoview->text), TRUE);
1129 g_free(text);
1131 noticeview_set_button_text(mimeview->siginfoview, NULL);
1132 noticeview_set_button_press_callback(
1133 mimeview->siginfoview,
1134 G_CALLBACK(func),
1135 (gpointer) mimeview);
1136 noticeview_set_icon(mimeview->siginfoview, icon);
1137 noticeview_set_tooltip(mimeview->siginfoview, button_text);
1139 icon_list_clear(mimeview);
1140 icon_list_create(mimeview, mimeview->mimeinfo);
1143 static void check_signature_async_cb(GObject *source_object,
1144 GAsyncResult *async_result,
1145 gpointer user_data)
1147 GTask *task = G_TASK(async_result);
1148 GCancellable *cancellable;
1149 MimeView *mimeview = (MimeView *)user_data;
1150 gboolean cancelled;
1151 SigCheckTaskResult *result;
1152 GError *error = NULL;
1154 if (mimeview->siginfo == NULL) {
1155 debug_print("discarding stale sig check task result task:%p\n", task);
1156 return;
1157 } else if (task != mimeview->siginfo->last_sig_check_task) {
1158 debug_print("discarding stale sig check task result last_task:%p task:%p\n",
1159 mimeview->siginfo->last_sig_check_task, task);
1160 return;
1161 } else {
1162 debug_print("using sig check task result task:%p\n", task);
1163 mimeview->siginfo->last_sig_check_task = NULL;
1166 cancellable = g_task_get_cancellable(task);
1167 cancelled = g_cancellable_set_error_if_cancelled(cancellable, &error);
1168 if (cancelled) {
1169 debug_print("sig check task was cancelled: task:%p GError: domain:%s code:%d message:\"%s\"\n",
1170 task, g_quark_to_string(error->domain), error->code, error->message);
1171 g_error_free(error);
1172 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_TIMEOUT);
1173 return;
1174 } else {
1175 if (mimeview->sig_check_cancellable == NULL)
1176 g_error("bad cancellable");
1177 if (mimeview->sig_check_timeout_tag == 0)
1178 g_error("bad cancel source tag");
1180 g_source_remove(mimeview->sig_check_timeout_tag);
1181 mimeview->sig_check_timeout_tag = 0;
1182 g_object_unref(mimeview->sig_check_cancellable);
1183 mimeview->sig_check_cancellable = NULL;
1186 result = g_task_propagate_pointer(task, &error);
1188 if (mimeview->siginfo->sig_data) {
1189 privacy_free_signature_data(mimeview->siginfo->sig_data);
1190 mimeview->siginfo->sig_data = NULL;
1193 if (result == NULL) {
1194 debug_print("sig check task propagated NULL task:%p GError: domain:%s code:%d message:\"%s\"\n",
1195 task, g_quark_to_string(error->domain), error->code, error->message);
1196 g_error_free(error);
1197 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_ERROR);
1198 return;
1201 mimeview->siginfo->sig_data = result->sig_data;
1202 update_signature_noticeview(mimeview, FALSE, 0);
1204 if (result->newinfo) {
1205 g_warning("Check sig task returned an unexpected new MimeInfo");
1206 procmime_mimeinfo_free_all(&result->newinfo);
1209 g_free(result);
1212 gboolean mimeview_check_sig_timeout(gpointer user_data)
1214 MimeView *mimeview = (MimeView *)user_data;
1215 GCancellable *cancellable = mimeview->sig_check_cancellable;
1217 mimeview->sig_check_timeout_tag = 0;
1219 if (cancellable == NULL) {
1220 return G_SOURCE_REMOVE;
1223 mimeview->sig_check_cancellable = NULL;
1224 g_cancellable_cancel(cancellable);
1225 g_object_unref(cancellable);
1227 return G_SOURCE_REMOVE;
1230 static void check_signature_cb(GtkWidget *widget, gpointer user_data)
1232 MimeView *mimeview = (MimeView *) user_data;
1233 MimeInfo *mimeinfo = mimeview->siginfo;
1234 gint ret;
1236 if (mimeinfo == NULL || !noticeview_is_visible(mimeview->siginfoview))
1237 return;
1239 noticeview_set_text(mimeview->siginfoview, _("Checking signature..."));
1240 GTK_EVENTS_FLUSH();
1242 if (mimeview->sig_check_cancellable != NULL) {
1243 if (mimeview->sig_check_timeout_tag == 0)
1244 g_error("bad cancel source tag");
1245 g_source_remove(mimeview->sig_check_timeout_tag);
1246 g_cancellable_cancel(mimeview->sig_check_cancellable);
1247 g_object_unref(mimeview->sig_check_cancellable);
1250 mimeview->sig_check_cancellable = g_cancellable_new();
1252 ret = privacy_mimeinfo_check_signature(mimeview->siginfo,
1253 mimeview->sig_check_cancellable,
1254 check_signature_async_cb,
1255 mimeview);
1256 if (ret == 0) {
1257 mimeview->sig_check_timeout_tag = g_timeout_add_seconds(prefs_common.io_timeout_secs,
1258 mimeview_check_sig_timeout, mimeview);
1259 } else if (ret < 0) {
1260 g_object_unref(mimeview->sig_check_cancellable);
1261 mimeview->sig_check_cancellable = NULL;
1262 update_signature_noticeview(mimeview, TRUE, SIGNATURE_CHECK_ERROR);
1263 } else {
1264 g_object_unref(mimeview->sig_check_cancellable);
1265 mimeview->sig_check_cancellable = NULL;
1266 update_signature_noticeview(mimeview, FALSE, 0);
1270 void mimeview_check_signature(MimeView *mimeview)
1272 check_signature_cb(NULL, mimeview);
1275 static void redisplay_email(GtkWidget *widget, gpointer user_data)
1277 MimeView *mimeview = (MimeView *) user_data;
1278 gtk_tree_path_free(mimeview->opened);
1279 mimeview->opened = NULL;
1280 mimeview_selected(gtk_tree_view_get_selection(
1281 GTK_TREE_VIEW(mimeview->ctree)), mimeview);
1284 static void display_full_info_cb(GtkWidget *widget, gpointer user_data)
1286 MimeView *mimeview = (MimeView *) user_data;
1288 textview_set_text(mimeview->textview, privacy_mimeinfo_get_sig_info(mimeview->siginfo, TRUE));
1289 noticeview_set_button_text(mimeview->siginfoview, NULL);
1290 noticeview_set_button_press_callback(
1291 mimeview->siginfoview,
1292 G_CALLBACK(redisplay_email),
1293 (gpointer) mimeview);
1294 noticeview_set_tooltip(mimeview->siginfoview, _("Go back to email"));
1297 static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
1299 MimeInfo *siginfo;
1300 MimeInfo *first_text;
1302 cm_return_if_fail(mimeview != NULL);
1303 cm_return_if_fail(selected != NULL);
1305 if (selected->type == MIMETYPE_MESSAGE
1306 && !g_ascii_strcasecmp(selected->subtype, "rfc822")) {
1307 /* if the first text part is signed, check that */
1308 first_text = selected;
1309 while (first_text && first_text->type != MIMETYPE_TEXT) {
1310 first_text = procmime_mimeinfo_next(first_text);
1312 if (first_text) {
1313 update_signature_info(mimeview, first_text);
1314 return;
1318 siginfo = selected;
1319 while (siginfo != NULL) {
1320 if (privacy_mimeinfo_is_signed(siginfo))
1321 break;
1322 siginfo = procmime_mimeinfo_parent(siginfo);
1324 mimeview->siginfo = siginfo;
1326 /* This shortcut boolean is there to correctly set the menu's
1327 * CheckSignature item sensitivity without killing performance
1328 * each time the menu sensitiveness is updated (a lot).
1330 mimeview->signed_part = (siginfo != NULL);
1332 if (siginfo == NULL) {
1333 noticeview_hide(mimeview->siginfoview);
1334 return;
1337 update_signature_noticeview(mimeview, FALSE, 0);
1338 noticeview_show(mimeview->siginfoview);
1341 void mimeview_show_part_as_text(MimeView *mimeview, MimeInfo *partinfo)
1343 cm_return_if_fail(mimeview != NULL);
1344 cm_return_if_fail(partinfo != NULL);
1346 mimeview_show_message_part(mimeview, partinfo);
1349 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
1351 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1352 GtkTreeModel *model;
1353 GtkTreeIter iter;
1354 GtkTreePath *path;
1355 MimeInfo *partinfo;
1356 MainWindow *mainwin;
1357 GdkDisplay *display;
1358 GdkSeat *seat;
1359 GdkDevice *device;
1361 selection = gtk_tree_view_get_selection(ctree);
1362 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1363 return;
1365 path = gtk_tree_model_get_path(model, &iter);
1367 if (mimeview->opened && !gtk_tree_path_compare(mimeview->opened, path)) {
1368 gtk_tree_path_free(path);
1369 return;
1372 gtk_tree_path_free(mimeview->opened);
1373 mimeview->opened = path;
1374 mimeview->spec_part = NULL;
1375 gtk_tree_view_scroll_to_cell(ctree, path, NULL, TRUE, 0.5, 0);
1377 partinfo = mimeview_get_node_part(mimeview, path);
1378 if (!partinfo) return;
1380 /* ungrab the mouse event */
1381 if (gtk_widget_has_grab(GTK_WIDGET(ctree))) {
1382 gtk_grab_remove(GTK_WIDGET(ctree));
1383 display = gdk_window_get_display(gtk_widget_get_window(GTK_WIDGET(ctree)));
1384 seat = gdk_display_get_default_seat(display);
1385 device = gdk_seat_get_pointer(seat);
1386 if (gdk_display_device_is_grabbed(display, device))
1387 gdk_seat_ungrab(seat);
1390 mimeview->textview->default_text = FALSE;
1392 update_signature_info(mimeview, partinfo);
1394 if (!mimeview_show_part(mimeview, partinfo)) {
1395 switch (partinfo->type) {
1396 case MIMETYPE_TEXT:
1397 case MIMETYPE_MESSAGE:
1398 case MIMETYPE_MULTIPART:
1399 mimeview_show_message_part(mimeview, partinfo);
1401 break;
1402 default:
1403 mimeview->textview->default_text = TRUE;
1404 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
1405 textview_clear(mimeview->textview);
1406 textview_show_mime_part(mimeview->textview, partinfo);
1407 break;
1410 mainwin = mainwindow_get_mainwindow();
1411 if (mainwin)
1412 main_window_set_menu_sensitive(mainwin);
1414 if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
1415 && privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
1416 mimeview_check_signature(mimeview);
1420 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1421 MimeView *mimeview)
1423 GtkTreeSelection *selection;
1424 GtkTreeIter iter;
1426 if (!event) return FALSE;
1428 if (event->button == 2 || event->button == 3) {
1429 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1430 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1431 return FALSE;
1433 gtk_tree_selection_select_iter(selection, &iter);
1435 part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
1437 return FALSE;
1440 static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
1441 MimeView *mimeview)
1443 GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
1444 if (event->direction == GDK_SCROLL_UP) {
1445 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1446 } else if (event->direction == GDK_SCROLL_DOWN) {
1447 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1448 } else {
1449 gdouble x, y;
1451 if ((event->direction == GDK_SCROLL_SMOOTH) &&
1452 gdk_event_get_scroll_deltas((GdkEvent*)event, &x, &y)) {
1453 if (y < 0)
1454 scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
1455 else
1456 if (y >0)
1457 scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
1458 } else
1459 return FALSE; /* Scrolling left or right */
1461 gtk_vscrollbutton_scroll(scrollbutton);
1462 return TRUE;
1465 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
1466 MimeInfo *partinfo)
1468 static MimeInfo *lastinfo;
1469 static guint32 lasttime;
1471 gint double_click_time;
1472 g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &double_click_time, NULL);
1474 if (event->button == 2 ||
1475 (event->button == 1 && (event->time - lasttime) < double_click_time && lastinfo == partinfo)) {
1476 /* call external program for image, audio or html */
1477 mimeview_launch(mimeview, partinfo);
1478 return TRUE;
1479 } else if (event->button == 3) {
1480 MainWindow *mainwin = mainwindow_get_mainwindow();
1482 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1483 partinfo->type == MIMETYPE_IMAGE ||
1484 partinfo->type == MIMETYPE_MULTIPART))
1485 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
1486 else
1487 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
1489 if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
1490 partinfo->type == MIMETYPE_IMAGE ||
1491 partinfo->type == MIMETYPE_TEXT))
1492 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", TRUE);
1493 else
1494 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", FALSE);
1495 #ifndef G_OS_WIN32
1496 if (partinfo &&
1497 partinfo->type == MIMETYPE_APPLICATION &&
1498 !g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))
1499 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", FALSE);
1500 else
1501 #endif
1502 cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
1503 if (mainwin)
1504 main_window_set_menu_sensitive(mainwin);
1505 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1506 "pop_partinfo", partinfo);
1508 gtk_menu_popup_at_pointer(GTK_MENU(mimeview->popupmenu),
1509 (GdkEvent *)event);
1510 return TRUE;
1513 lastinfo = partinfo;
1514 lasttime = event->time;
1515 return FALSE;
1519 gboolean mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
1521 return mimeview_key_pressed(mimeview->ctree, event, mimeview);
1524 void mimeview_select_next_part(MimeView *mimeview)
1526 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1527 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1528 GtkTreeSelection *selection;
1529 GtkTreeIter iter;
1530 GtkTreePath *path;
1531 MimeInfo *partinfo = NULL;
1532 gboolean has_next;
1534 if (!mimeview->opened) return;
1536 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1537 path = gtk_tree_model_get_path(model, &iter);
1538 skip:
1539 has_next = mimeview_tree_next(model, path);
1540 if (!has_next) {
1541 has_next = gtk_tree_model_get_iter_first(model, &iter);
1542 gtk_tree_path_free(path);
1543 path = gtk_tree_model_get_path(model, &iter);
1546 if (has_next) {
1547 partinfo = mimeview_get_node_part(mimeview, path);
1548 if (partinfo->type == MIMETYPE_MULTIPART ||
1549 (!prefs_common.show_inline_attachments && partinfo->id))
1550 goto skip;
1551 selection = gtk_tree_view_get_selection(ctree);
1552 gtk_tree_selection_select_path(selection, path);
1553 icon_list_toggle_by_mime_info(mimeview, partinfo);
1556 gtk_tree_path_free(path);
1559 void mimeview_select_prev_part(MimeView *mimeview)
1561 GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
1562 GtkTreeModel *model = gtk_tree_view_get_model(ctree);
1563 GtkTreeSelection *selection;
1564 GtkTreeIter iter;
1565 GtkTreePath *path;
1566 MimeInfo *partinfo = NULL;
1567 gboolean has_prev;
1569 if (!mimeview->opened) return;
1571 gtk_tree_model_get_iter(model, &iter, mimeview->opened);
1572 path = gtk_tree_model_get_path(model, &iter);
1573 skip:
1574 has_prev = mimeview_tree_prev(mimeview, path);
1575 if (!has_prev) {
1576 while (mimeview_tree_next(model, path)) {}
1577 has_prev = TRUE;
1580 if (has_prev) {
1581 partinfo = mimeview_get_node_part(mimeview, path);
1582 if (partinfo->type == MIMETYPE_MULTIPART ||
1583 (!prefs_common.show_inline_attachments && partinfo->id))
1584 goto skip;
1585 selection = gtk_tree_view_get_selection(ctree);
1586 gtk_tree_selection_select_path(selection, path);
1587 icon_list_toggle_by_mime_info(mimeview, partinfo);
1590 gtk_tree_path_free(path);
1593 #define BREAK_ON_MODIFIER_KEY() \
1594 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
1596 static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1597 MimeView *mimeview)
1599 SummaryView *summaryview;
1601 if (!event) return FALSE;
1602 if (!mimeview->opened) return FALSE;
1604 summaryview = mimeview->messageview->mainwin->summaryview;
1606 if (summaryview && quicksearch_has_focus(summaryview->quicksearch))
1607 return FALSE;
1609 switch (event->keyval) {
1610 case GDK_KEY_Home:
1611 case GDK_KEY_End:
1612 textview_scroll_max(mimeview->textview,
1613 (event->keyval == GDK_KEY_Home));
1614 return TRUE;
1615 case GDK_KEY_Page_Down:
1616 case GDK_KEY_space:
1617 if (mimeview_scroll_page(mimeview,
1618 (event->state & GDK_SHIFT_MASK) != 0))
1619 return TRUE;
1621 if (!(event->state & GDK_SHIFT_MASK))
1622 mimeview_select_next_part(mimeview);
1623 return TRUE;
1624 case GDK_KEY_Page_Up:
1625 case GDK_KEY_BackSpace:
1626 mimeview_scroll_page(mimeview, TRUE);
1627 return TRUE;
1628 case GDK_KEY_Return:
1629 case GDK_KEY_KP_Enter:
1630 mimeview_scroll_one_line(mimeview,
1631 (event->state & GDK_MOD1_MASK) != 0);
1632 return TRUE;
1633 case GDK_KEY_Up:
1634 case GDK_KEY_Down:
1635 mimeview_scroll_one_line(mimeview, (event->keyval == GDK_KEY_Up));
1636 return TRUE;
1637 default:
1638 break;
1641 if (mimeview->messageview->new_window) return FALSE;
1643 return summary_pass_key_press_event(summaryview, event);
1646 static void mimeview_drag_data_get(GtkWidget *widget,
1647 GdkDragContext *drag_context,
1648 GtkSelectionData *selection_data,
1649 guint info,
1650 guint time,
1651 MimeView *mimeview)
1653 gchar *filename = NULL, *uriname, *tmp = NULL;
1654 MimeInfo *partinfo;
1655 gint err;
1656 gint count = 0;
1658 if (!mimeview->opened) return;
1659 if (!mimeview->file) return;
1661 partinfo = mimeview_get_selected_part(mimeview);
1662 if (!partinfo) return;
1664 if (strlen(get_part_name(partinfo)) > 0) {
1665 filename = g_path_get_basename(get_part_name(partinfo));
1666 if (filename) {
1667 if (*filename == '\0') {
1668 g_free(filename);
1669 return;
1672 } else if (partinfo->type == MIMETYPE_MESSAGE
1673 && !g_ascii_strcasecmp(partinfo->subtype, "rfc822")) {
1674 gchar *name = NULL;
1675 GPtrArray *headers = NULL;
1676 FILE *fp;
1678 fp = claws_fopen(partinfo->data.filename, "rb");
1679 if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
1680 headers = procheader_get_header_array(fp);
1681 if (headers) {
1682 gint i;
1683 for (i = 0; i < headers->len; i++) {
1684 Header *header = g_ptr_array_index(headers, i);
1685 if (procheader_headername_equal(header->name, "Subject")) {
1686 unfold_line(header->body);
1687 name = g_strconcat(header->body, ".txt", NULL);
1688 subst_for_filename(name);
1691 procheader_header_array_destroy(headers);
1694 if (fp != NULL)
1695 claws_fclose(fp);
1696 if (name)
1697 filename = g_path_get_basename(name);
1698 g_free(name);
1700 if (filename == NULL)
1701 filename = g_path_get_basename("Unnamed part");
1703 if (!g_utf8_validate(filename, -1, NULL))
1704 tmp = conv_codeset_strdup(filename,
1705 conv_get_locale_charset_str(),
1706 CS_UTF_8);
1708 if (tmp == NULL) {
1709 g_warning("filename not in UTF-8");
1710 tmp = g_strdup(filename);
1712 g_free(filename);
1713 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1714 tmp, NULL);
1716 check_new_file:
1717 if (is_file_exist(filename)) {
1718 gchar *ext = NULL;
1719 gchar *prefix = NULL;
1720 gchar *new_name = NULL;
1721 if (strrchr(tmp, '.')) {
1722 prefix = g_strdup(tmp);
1723 ext = g_strdup(strrchr(tmp, '.'));
1724 *(strrchr(prefix, '.')) = '\0';
1725 } else {
1726 prefix = g_strdup(tmp);
1727 ext = g_strdup("");
1729 count++;
1730 new_name = g_strdup_printf("%s.%d%s", prefix, count, ext);
1731 g_free(prefix);
1732 g_free(ext);
1733 g_free(filename);
1734 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1735 new_name, NULL);
1736 g_free(new_name);
1737 goto check_new_file;
1740 g_free(tmp);
1742 if ((err = procmime_get_part(filename, partinfo)) < 0)
1743 alertpanel_error
1744 (_("Couldn't save the part of multipart message: %s"),
1745 g_strerror(-err));
1747 tmp = g_filename_to_uri(filename, NULL, NULL);
1748 uriname = g_strconcat(tmp, "\r\n", NULL);
1749 g_free(tmp);
1751 gtk_selection_data_set(selection_data,
1752 gtk_selection_data_get_target(selection_data), 8,
1753 (guchar *)uriname, strlen(uriname));
1755 g_free(uriname);
1756 g_free(filename);
1760 * Returns a filename (with path) for an attachment
1761 * \param partinfo The attachment to save
1762 * \param basedir The target directory
1763 * \param number Used for dummy filename if attachment is unnamed
1765 static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
1766 const gchar *basedir,
1767 gint number)
1769 gchar *fullname;
1770 gchar *filename;
1772 filename = g_strdup(get_part_name(partinfo));
1773 if (!filename || !*filename) {
1774 g_free(filename);
1775 filename = g_strdup_printf("noname.%d", number);
1778 if (!g_utf8_validate(filename, -1, NULL)) {
1779 gchar *tmp = conv_filename_to_utf8(filename);
1780 g_free(filename);
1781 filename = tmp;
1784 subst_for_filename(filename);
1786 fullname = g_strconcat
1787 (basedir, G_DIR_SEPARATOR_S, (g_path_is_absolute(filename))
1788 ? &filename[1] : filename, NULL);
1790 g_free(filename);
1791 filename = conv_filename_from_utf8(fullname);
1792 g_free(fullname);
1793 return filename;
1797 * Write a single attachment to file
1798 * \param filename Filename with path
1799 * \param partinfo Attachment to save
1801 static gint mimeview_write_part(const gchar *filename,
1802 MimeInfo *partinfo,
1803 gboolean handle_error)
1805 gchar *dir;
1806 gint err;
1808 dir= g_path_get_dirname(filename);
1809 if (!is_dir_exist(dir))
1810 make_dir_hier(dir);
1811 g_free(dir);
1813 if (is_file_exist(filename)) {
1814 AlertValue aval;
1815 gchar *res;
1816 gchar *tmp;
1818 if (!g_utf8_validate(filename, -1, NULL))
1819 tmp = conv_filename_to_utf8(filename);
1820 else
1821 tmp = g_strdup(filename);
1823 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
1824 tmp);
1825 g_free(tmp);
1826 aval = alertpanel(_("Overwrite"), res, NULL, _("_No"),
1827 NULL, _("_Yes"), NULL, NULL, ALERTFOCUS_FIRST);
1828 g_free(res);
1829 if (G_ALERTALTERNATE != aval)
1830 return 2;
1833 if ((err = procmime_get_part(filename, partinfo)) < 0) {
1834 debug_print("error saving MIME part: %d\n", err);
1835 if (handle_error)
1836 alertpanel_error
1837 (_("Couldn't save the part of multipart message: %s"),
1838 g_strerror(-err));
1839 return 0;
1842 if (prefs_common.attach_save_chmod) {
1843 if (chmod(filename, prefs_common.attach_save_chmod) < 0)
1844 FILE_OP_ERROR(filename, "chmod");
1847 return 1;
1850 static AlertValue mimeview_save_all_error_ask(gint n)
1852 gchar *message = g_strdup_printf(
1853 _("An error has occurred while saving message part %d. "
1854 "Do you want to cancel saving or ignore error and "
1855 "continue?"), n);
1856 AlertValue av = alertpanel_full(_("Error saving message part"),
1857 message, NULL, _("_Cancel"), NULL, _("Ignore"), NULL, _("Ignore all"),
1858 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
1859 g_free(message);
1860 return av;
1863 static void mimeview_save_all_info(gint errors, gint total)
1865 AlertValue aval;
1867 if (!errors && prefs_common.show_save_all_success) {
1868 gchar *msg = g_strdup_printf(
1869 ngettext("%d file saved successfully.",
1870 "%d files saved successfully.",
1871 total),
1872 total);
1873 aval = alertpanel_full(_("Notice"), msg, NULL, _("_Close"),
1874 NULL, NULL, NULL, NULL, ALERTFOCUS_FIRST,
1875 TRUE, NULL, ALERT_NOTICE);
1876 g_free(msg);
1877 if (aval & G_ALERTDISABLE)
1878 prefs_common.show_save_all_success = FALSE;
1879 } else if (prefs_common.show_save_all_failure) {
1880 gchar *msg1 = g_strdup_printf(
1881 ngettext("%d file saved successfully",
1882 "%d files saved successfully",
1883 total - errors),
1884 total - errors);
1885 gchar *msg2 = g_strdup_printf(
1886 ngettext("%s, %d file failed.",
1887 "%s, %d files failed.",
1888 errors),
1889 msg1, errors);
1890 aval = alertpanel_full(_("Warning"), msg2, NULL, _("_Close"),
1891 NULL, NULL, NULL, NULL, ALERTFOCUS_FIRST,
1892 TRUE, NULL, ALERT_WARNING);
1893 g_free(msg2);
1894 g_free(msg1);
1895 if (aval & G_ALERTDISABLE)
1896 prefs_common.show_save_all_failure = FALSE;
1901 * Menu callback: Save all attached files
1902 * \param mimeview Current display
1904 static void mimeview_save_all(MimeView *mimeview, gboolean attachments_only)
1906 MimeInfo *partinfo;
1907 gchar *dirname;
1908 gchar *startdir = NULL;
1909 gint number = 0, errors = 0;
1910 gboolean skip_errors = FALSE;
1912 if (!mimeview->opened) return;
1913 if (!mimeview->file) return;
1914 if (!mimeview->mimeinfo) return;
1916 partinfo = mimeview->mimeinfo;
1917 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
1918 startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
1919 else
1920 startdir = g_strdup(get_home_dir());
1922 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
1923 dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
1924 if (!dirname) {
1925 g_free(startdir);
1926 return;
1929 if (!is_dir_exist (dirname)) {
1930 alertpanel_error(_("'%s' is not a directory."), dirname);
1931 g_free(startdir);
1932 g_free(dirname);
1933 return;
1936 if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
1937 dirname[strlen(dirname)-1] = '\0';
1939 /* Skip the first part, that is sometimes DISPOSITIONTYPE_UNKNOWN */
1940 if (partinfo && partinfo->type == MIMETYPE_MESSAGE)
1941 partinfo = procmime_mimeinfo_next(partinfo);
1942 if (partinfo && partinfo->type == MIMETYPE_MULTIPART) {
1943 partinfo = procmime_mimeinfo_next(partinfo);
1944 if (partinfo && partinfo->type == MIMETYPE_TEXT)
1945 partinfo = procmime_mimeinfo_next(partinfo);
1948 while (partinfo != NULL) {
1949 if ((!attachments_only &&
1950 (partinfo->type != MIMETYPE_MESSAGE &&
1951 partinfo->type != MIMETYPE_MULTIPART &&
1952 (partinfo->disposition != DISPOSITIONTYPE_INLINE ||
1953 get_real_part_name(partinfo) != NULL))) ||
1954 (attachments_only &&
1955 (partinfo->disposition == DISPOSITIONTYPE_ATTACHMENT &&
1956 get_real_part_name(partinfo) != NULL))) {
1957 gchar *filename = mimeview_get_filename_for_part(
1958 partinfo, dirname, number++);
1960 gint ok = mimeview_write_part(filename, partinfo, FALSE);
1961 g_free(filename);
1962 if (ok < 1) {
1963 ++errors;
1964 if (!skip_errors) {
1965 AlertValue av = mimeview_save_all_error_ask(number);
1966 skip_errors = (av == G_ALERTOTHER);
1967 if (av == G_ALERTDEFAULT) /* cancel */
1968 break;
1970 } else if (ok == 2)
1971 number--;
1973 partinfo = procmime_mimeinfo_next(partinfo);
1976 g_free(prefs_common.attach_save_dir);
1977 g_free(startdir);
1978 prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
1979 -1, NULL, NULL, NULL);
1980 g_free(dirname);
1982 mimeview_save_all_info(errors, number);
1985 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
1987 MimeInfo *partinfo = NULL;
1988 if (mimeview->spec_part) {
1989 partinfo = mimeview->spec_part;
1990 mimeview->spec_part = NULL;
1991 } else {
1992 partinfo = (MimeInfo *) g_object_get_data
1993 (G_OBJECT(mimeview->popupmenu),
1994 "pop_partinfo");
1995 g_object_set_data(G_OBJECT(mimeview->popupmenu),
1996 "pop_partinfo", NULL);
1997 if (!partinfo) {
1998 partinfo = mimeview_get_selected_part(mimeview);
2002 return partinfo;
2005 * Menu callback: Save the selected attachment
2006 * \param mimeview Current display
2008 void mimeview_save_as(MimeView *mimeview)
2010 gchar *filename;
2011 gchar *filepath = NULL;
2012 gchar *filedir = NULL;
2013 MimeInfo *partinfo;
2014 gchar *partname = NULL;
2016 if (!mimeview->opened) return;
2017 if (!mimeview->file) return;
2019 partinfo = mimeview_get_part_to_use(mimeview);
2021 cm_return_if_fail(partinfo != NULL);
2023 if (get_part_name(partinfo) == NULL) {
2024 return;
2026 partname = g_strdup(get_part_name(partinfo));
2028 if (!g_utf8_validate(partname, -1, NULL)) {
2029 gchar *tmp = conv_filename_to_utf8(partname);
2030 if (!tmp) {
2031 tmp = conv_codeset_strdup(partname,
2032 conv_get_locale_charset_str(),
2033 CS_UTF_8);
2035 if (tmp) {
2036 g_free(partname);
2037 partname = tmp;
2041 subst_for_filename(partname);
2043 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2044 filepath = g_strconcat(prefs_common.attach_save_dir,
2045 G_DIR_SEPARATOR_S, partname, NULL);
2046 else
2047 filepath = g_strdup(partname);
2049 g_free(partname);
2051 manage_window_focus_in(gtk_widget_get_ancestor(mimeview->hbox, GTK_TYPE_WINDOW), NULL, NULL);
2052 filename = filesel_select_file_save(_("Save as"), filepath);
2053 if (!filename) {
2054 g_free(filepath);
2055 return;
2058 mimeview_write_part(filename, partinfo, TRUE);
2060 filedir = g_path_get_dirname(filename);
2061 if (filedir && strcmp(filedir, ".")) {
2062 g_free(prefs_common.attach_save_dir);
2063 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2066 g_free(filedir);
2067 g_free(filepath);
2070 void mimeview_display_as_text(MimeView *mimeview)
2072 MimeInfo *partinfo;
2074 if (!mimeview->opened) return;
2076 partinfo = mimeview_get_part_to_use(mimeview);
2077 mimeview_select_mimepart_icon(mimeview, partinfo);
2078 cm_return_if_fail(partinfo != NULL);
2079 mimeview_show_message_part(mimeview, partinfo);
2082 void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
2084 gchar *filename;
2085 gint err;
2087 if (!mimeview->opened) return;
2088 if (!mimeview->file) return;
2090 if (!partinfo)
2091 partinfo = mimeview_get_part_to_use(mimeview);
2093 cm_return_if_fail(partinfo != NULL);
2095 filename = procmime_get_tmp_file_name(partinfo);
2097 if ((err = procmime_get_part(filename, partinfo)) < 0)
2098 alertpanel_error
2099 (_("Couldn't save the part of multipart message: %s"),
2100 g_strerror(-err));
2101 else
2102 mimeview_view_file(filename, partinfo, NULL, mimeview);
2104 g_free(filename);
2107 #ifndef G_OS_WIN32
2108 void mimeview_open_with(MimeView *mimeview)
2110 MimeInfo *partinfo;
2112 if (!mimeview) return;
2113 if (!mimeview->opened) return;
2114 if (!mimeview->file) return;
2116 partinfo = mimeview_get_part_to_use(mimeview);
2118 mimeview_open_part_with(mimeview, partinfo, FALSE);
2121 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
2123 gchar *filename;
2124 gchar *cmd;
2125 gchar *mime_command = NULL;
2126 gchar *content_type = NULL;
2127 gint err;
2129 cm_return_if_fail(partinfo != NULL);
2131 filename = procmime_get_tmp_file_name(partinfo);
2133 if ((err = procmime_get_part(filename, partinfo)) < 0) {
2134 alertpanel_error
2135 (_("Couldn't save the part of multipart message: %s"),
2136 g_strerror(-err));
2137 g_free(filename);
2138 return;
2141 if (!prefs_common.mime_open_cmd_history)
2142 prefs_common.mime_open_cmd_history =
2143 add_history(NULL, prefs_common.mime_open_cmd);
2145 if ((partinfo->type == MIMETYPE_APPLICATION) &&
2146 (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
2147 /* guess content-type from filename */
2148 content_type = procmime_get_mime_type(filename);
2150 if (content_type == NULL) {
2151 content_type = procmime_get_content_type_str(partinfo->type,
2152 partinfo->subtype);
2155 if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
2156 && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
2157 mime_command = g_strdup(prefs_common_get_uri_cmd());
2158 g_free(content_type);
2159 content_type = NULL;
2160 } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common_get_ext_editor_cmd()
2161 || !prefs_common_get_ext_editor_cmd()[0]) {
2162 mime_command = mailcap_get_command_for_type(content_type, filename);
2163 } else {
2164 mime_command = g_strdup(prefs_common_get_ext_editor_cmd());
2165 g_free(content_type);
2166 content_type = NULL;
2168 if (mime_command == NULL) {
2169 /* try with extension this time */
2170 g_free(content_type);
2171 content_type = procmime_get_mime_type(filename);
2172 mime_command = mailcap_get_command_for_type(content_type, filename);
2175 if (mime_command == NULL)
2176 automatic = FALSE;
2178 if (!automatic) {
2179 gboolean remember = FALSE;
2180 if (content_type != NULL)
2181 cmd = input_dialog_combo_remember
2182 (_("Open with"),
2183 _("Enter the command-line to open file:\n"
2184 "('%s' will be replaced with file name)"),
2185 mime_command ? mime_command : prefs_common.mime_open_cmd,
2186 prefs_common.mime_open_cmd_history, &remember);
2187 else
2188 cmd = input_dialog_combo
2189 (_("Open with"),
2190 _("Enter the command-line to open file:\n"
2191 "('%s' will be replaced with file name)"),
2192 mime_command ? mime_command : prefs_common.mime_open_cmd,
2193 prefs_common.mime_open_cmd_history);
2194 if (cmd && remember) {
2195 mailcap_update_default(content_type, cmd);
2197 g_free(mime_command);
2198 } else {
2199 cmd = mime_command;
2201 if (cmd) {
2202 mimeview_view_file(filename, partinfo, cmd, mimeview);
2203 g_free(prefs_common.mime_open_cmd);
2204 prefs_common.mime_open_cmd = cmd;
2205 prefs_common.mime_open_cmd_history =
2206 add_history(prefs_common.mime_open_cmd_history, cmd);
2209 g_free(content_type);
2210 g_free(filename);
2212 #endif
2214 static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
2216 GList *attach_file = NULL;
2217 AttachInfo *ainfo = NULL;
2218 gchar *filename;
2219 gint err;
2221 if (!mimeview->opened) return;
2222 if (!mimeview->file) return;
2224 cm_return_if_fail(partinfo != NULL);
2226 filename = procmime_get_tmp_file_name(partinfo);
2228 if (!(err = procmime_get_part(filename, partinfo))) {
2229 ainfo = g_new0(AttachInfo, 1);
2230 ainfo->file = filename;
2231 ainfo->name = g_strdup(get_part_name(partinfo));
2232 ainfo->content_type = procmime_get_content_type_str(
2233 partinfo->type, partinfo->subtype);
2234 ainfo->charset = g_strdup(procmime_mimeinfo_get_parameter(
2235 partinfo, "charset"));
2236 attach_file = g_list_append(attach_file, ainfo);
2238 compose_new(NULL, NULL, attach_file);
2240 g_free(ainfo->name);
2241 g_free(ainfo->content_type);
2242 g_free(ainfo->charset);
2243 g_free(ainfo);
2244 g_list_free(attach_file);
2245 } else
2246 alertpanel_error
2247 (_("Couldn't save the part of multipart message: %s"),
2248 g_strerror(-err));
2249 g_free(filename);
2252 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
2253 const gchar *cmd, MimeView *mimeview)
2255 #ifndef G_OS_WIN32
2256 gchar *p;
2257 gchar buf[BUFFSIZE];
2259 if (cmd == NULL)
2260 mimeview_open_part_with(mimeview, partinfo, TRUE);
2261 else {
2262 if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2263 !strchr(p + 2, '%')) {
2264 g_snprintf(buf, sizeof(buf), cmd, filename);
2265 if (!prefs_common.save_parts_readwrite)
2266 g_chmod(filename, S_IRUSR);
2267 else
2268 g_chmod(filename, S_IRUSR|S_IWUSR);
2269 } else {
2270 g_warning("MIME viewer command-line is invalid: '%s'", cmd);
2271 cmd = NULL;
2272 mimeview_open_part_with(mimeview, partinfo, FALSE);
2274 if (cmd != NULL && execute_command_line(buf, TRUE, NULL) != 0) {
2275 if (!prefs_common.save_parts_readwrite)
2276 g_chmod(filename, S_IRUSR|S_IWUSR);
2277 mimeview_open_part_with(mimeview, partinfo, FALSE);
2280 #else
2281 SHFILEINFO file_info;
2282 GError *error = NULL;
2283 gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
2285 if (error != NULL) {
2286 alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
2287 error->message);
2288 debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
2289 g_error_free(error);
2290 return;
2293 if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
2294 AlertValue val = alertpanel_full(_("Execute untrusted binary?"),
2295 _("This attachment is an executable file. Executing "
2296 "untrusted binaries is dangerous and could compromise "
2297 "your computer.\n\n"
2298 "Do you want to run this file?"), NULL, _("_Cancel"),
2299 NULL, _("Run binary"), NULL, NULL, ALERTFOCUS_FIRST,
2300 FALSE, NULL, ALERT_WARNING);
2301 if (val == G_ALERTALTERNATE) {
2302 debug_print("executing binary\n");
2303 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2305 } else {
2306 ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
2309 g_free(fn16);
2311 #endif
2314 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
2316 mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
2319 static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
2321 return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
2324 void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
2326 GSList *mimeview_list, *viewer_list;
2328 for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
2329 MimeView *mimeview = (MimeView *) mimeview_list->data;
2331 if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
2332 mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2333 mimeview->mimeviewer = NULL;
2336 while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
2337 MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
2339 mimeviewer->destroy_viewer(mimeviewer);
2340 mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
2344 mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
2347 static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
2349 gint num;
2350 MimeInfo *partinfo;
2352 num = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
2353 partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
2355 if (event->button == 1) {
2356 icon_selected(mimeview, num, partinfo);
2357 gtk_widget_grab_focus(button);
2358 icon_list_toggle_by_mime_info(mimeview, partinfo);
2360 part_button_pressed(mimeview, event, partinfo);
2362 return FALSE;
2365 static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
2367 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
2368 GtkTreeIter iter;
2369 GtkTreePath *path;
2370 MimeInfo *curr = NULL;
2372 if (!gtk_tree_model_get_iter_first(model, &iter))
2373 return;
2374 path = gtk_tree_model_get_path(model, &iter);
2376 do {
2377 gtk_tree_model_get_iter(model, &iter, path);
2378 gtk_tree_model_get(model, &iter, COL_DATA, &curr, -1);
2379 if (curr == partinfo) {
2380 GtkTreeSelection *sel = gtk_tree_view_get_selection(
2381 GTK_TREE_VIEW(mimeview->ctree));
2382 gtk_tree_selection_select_iter(sel, &iter);
2383 gtk_tree_path_free(path);
2384 return;
2386 } while (mimeview_tree_next(model, path));
2388 gtk_tree_path_free(path);
2391 void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
2393 icon_list_toggle_by_mime_info(mimeview, partinfo);
2394 icon_selected(mimeview, -1, partinfo);
2397 static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
2398 MimeView *mimeview)
2400 SummaryView *summaryview;
2402 if (!event) return FALSE;
2404 switch (event->keyval) {
2405 case GDK_KEY_space:
2406 if (mimeview_scroll_page(mimeview, FALSE))
2407 return TRUE;
2409 mimeview_select_next_part(mimeview);
2410 return TRUE;
2412 break;
2413 case GDK_KEY_BackSpace:
2414 mimeview_scroll_page(mimeview, TRUE);
2415 return TRUE;
2416 case GDK_KEY_Return:
2417 case GDK_KEY_KP_Enter:
2418 mimeview_scroll_one_line(mimeview,
2419 (event->state & GDK_MOD1_MASK) != 0);
2420 return TRUE;
2421 case GDK_KEY_y:
2422 BREAK_ON_MODIFIER_KEY();
2423 mimeview_save_as(mimeview);
2424 return TRUE;
2425 case GDK_KEY_t:
2426 BREAK_ON_MODIFIER_KEY();
2427 mimeview_display_as_text(mimeview);
2428 return TRUE;
2429 case GDK_KEY_l:
2430 BREAK_ON_MODIFIER_KEY();
2431 mimeview_launch(mimeview, NULL);
2432 return TRUE;
2433 #ifndef G_OS_WIN32
2434 case GDK_KEY_o:
2435 BREAK_ON_MODIFIER_KEY();
2436 mimeview_open_with(mimeview);
2437 return TRUE;
2438 #endif
2439 case GDK_KEY_c:
2440 BREAK_ON_MODIFIER_KEY();
2441 mimeview_check_signature(mimeview);
2442 return TRUE;
2443 case GDK_KEY_a:
2444 BREAK_ON_MODIFIER_KEY();
2445 mimeview_select_next_part(mimeview);
2446 return TRUE;
2447 default:
2448 break;
2451 if (!mimeview->messageview->mainwin) return FALSE;
2452 summaryview = mimeview->messageview->mainwin->summaryview;
2453 return summary_pass_key_press_event(summaryview, event);
2456 static gboolean icon_popup_menu(GtkWidget *widget, gpointer data)
2458 MimeView *mimeview = (MimeView *)data;
2459 MimeInfo *partinfo = g_object_get_data(G_OBJECT(widget), "partinfo");
2461 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2462 "pop_partinfo", partinfo);
2463 gtk_menu_popup_at_pointer(GTK_MENU(mimeview->popupmenu), NULL);
2464 return TRUE;
2467 static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
2469 GtkWidget *pixmap = NULL;
2470 GtkWidget *grid;
2471 GtkWidget *button;
2472 gchar *tip;
2473 gchar *tiptmp;
2474 const gchar *desc = NULL;
2475 gchar *sigshort = NULL;
2476 gchar *content_type;
2477 StockPixmap stockp;
2478 MimeInfo *partinfo;
2479 MimeInfo *siginfo = NULL;
2480 MimeInfo *encrypted = NULL;
2481 #ifdef GENERIC_UMPC
2482 GtkRequisition r;
2483 #endif
2485 if (!prefs_common.show_inline_attachments && mimeinfo->id)
2486 return;
2488 grid = mimeview->icon_grid;
2489 mimeview->icon_count++;
2490 button = gtk_event_box_new();
2492 g_signal_connect(G_OBJECT(button), "motion-notify-event",
2493 G_CALLBACK(mimeview_visi_notify), mimeview);
2494 g_signal_connect(G_OBJECT(button), "leave-notify-event",
2495 G_CALLBACK(mimeview_leave_notify), mimeview);
2496 g_signal_connect(G_OBJECT(button), "enter-notify-event",
2497 G_CALLBACK(mimeview_enter_notify), mimeview);
2499 gtk_container_set_border_width(GTK_CONTAINER(button), 2);
2500 g_object_set_data(G_OBJECT(button), "icon_number",
2501 GINT_TO_POINTER(mimeview->icon_count));
2502 g_object_set_data(G_OBJECT(button), "partinfo",
2503 mimeinfo);
2505 switch (mimeinfo->type) {
2507 case MIMETYPE_TEXT:
2508 if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2509 stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
2510 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched"))
2511 stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
2512 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "calendar"))
2513 stockp = STOCK_PIXMAP_MIME_TEXT_CALENDAR;
2514 else if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
2515 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff")))
2516 stockp = STOCK_PIXMAP_MIME_TEXT_PATCH;
2517 else
2518 stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
2519 break;
2520 case MIMETYPE_MESSAGE:
2521 stockp = STOCK_PIXMAP_MIME_MESSAGE;
2522 break;
2523 case MIMETYPE_APPLICATION:
2524 if (mimeinfo->subtype && (!g_ascii_strcasecmp(mimeinfo->subtype, "pgp-signature")
2525 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-pkcs7-signature")
2526 || !g_ascii_strcasecmp(mimeinfo->subtype, "pkcs7-signature")))
2527 stockp = STOCK_PIXMAP_MIME_PGP_SIG;
2528 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "pdf"))
2529 stockp = STOCK_PIXMAP_MIME_PDF;
2530 else if (mimeinfo->subtype && !g_ascii_strcasecmp(mimeinfo->subtype, "postscript"))
2531 stockp = STOCK_PIXMAP_MIME_PS;
2532 else
2533 stockp = STOCK_PIXMAP_MIME_APPLICATION;
2534 break;
2535 case MIMETYPE_IMAGE:
2536 stockp = STOCK_PIXMAP_MIME_IMAGE;
2537 break;
2538 case MIMETYPE_AUDIO:
2539 stockp = STOCK_PIXMAP_MIME_AUDIO;
2540 break;
2541 default:
2542 stockp = STOCK_PIXMAP_MIME_UNKNOWN;
2543 break;
2546 partinfo = mimeinfo;
2547 while (partinfo != NULL) {
2548 if (privacy_mimeinfo_is_signed(partinfo)) {
2549 siginfo = partinfo;
2550 break;
2552 if (privacy_mimeinfo_is_encrypted(partinfo)) {
2553 encrypted = partinfo;
2554 break;
2556 partinfo = procmime_mimeinfo_parent(partinfo);
2559 if (siginfo != NULL) {
2560 switch (privacy_mimeinfo_get_sig_status(siginfo)) {
2561 case SIGNATURE_UNCHECKED:
2562 case SIGNATURE_CHECK_ERROR:
2563 case SIGNATURE_CHECK_FAILED:
2564 case SIGNATURE_CHECK_TIMEOUT:
2565 pixmap = stock_pixmap_widget_with_overlay(stockp,
2566 STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2567 break;
2568 case SIGNATURE_OK:
2569 pixmap = stock_pixmap_widget_with_overlay(stockp,
2570 STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2571 break;
2572 case SIGNATURE_WARN:
2573 case SIGNATURE_KEY_EXPIRED:
2574 pixmap = stock_pixmap_widget_with_overlay(stockp,
2575 STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
2576 break;
2577 case SIGNATURE_INVALID:
2578 pixmap = stock_pixmap_widget_with_overlay(stockp,
2579 STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2580 break;
2582 sigshort = privacy_mimeinfo_get_sig_info(siginfo, FALSE);
2583 } else if (encrypted != NULL) {
2584 pixmap = stock_pixmap_widget_with_overlay(stockp,
2585 STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);
2586 } else {
2587 pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
2588 OVERLAY_NONE, 6, 3);
2590 gtk_container_add(GTK_CONTAINER(button), pixmap);
2591 if (!desc) {
2592 if (prefs_common.attach_desc)
2593 desc = get_part_description(mimeinfo);
2594 else
2595 desc = get_part_name(mimeinfo);
2598 content_type = procmime_get_content_type_str(mimeinfo->type,
2599 mimeinfo->subtype);
2601 tip = g_strconcat("<b>", _("Type:"), " </b>", content_type,
2602 "\n<b>", _("Size:"), " </b>",
2603 to_human_readable((goffset)mimeinfo->length), NULL);
2604 g_free(content_type);
2605 if (desc && *desc) {
2606 gchar *tmp = NULL, *escaped = NULL;
2607 if (!g_utf8_validate(desc, -1, NULL)) {
2608 tmp = conv_filename_to_utf8(desc);
2609 } else {
2610 tmp = g_strdup(desc);
2612 escaped = g_markup_escape_text(tmp,-1);
2614 tiptmp = g_strconcat(tip, "\n<b>",
2615 prefs_common.attach_desc && mimeinfo->description ?
2616 _("Description:") : _("Filename:"),
2617 " </b>", escaped, NULL);
2618 g_free(tip);
2619 tip = tiptmp;
2620 g_free(tmp);
2621 g_free(escaped);
2623 if (sigshort && *sigshort) {
2624 gchar *sigshort_escaped =
2625 g_markup_escape_text(sigshort, -1);
2627 tiptmp = g_strjoin("\n", tip, sigshort_escaped, NULL);
2628 g_free(tip);
2629 g_free(sigshort_escaped);
2630 tip = tiptmp;
2633 gtk_widget_set_tooltip_markup(button, tip);
2634 g_free(tip);
2635 gtk_widget_show_all(button);
2636 gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
2637 mimeview_mime_types, 1, GDK_ACTION_COPY);
2639 g_signal_connect(G_OBJECT(button), "popup-menu",
2640 G_CALLBACK(icon_popup_menu), mimeview);
2641 g_signal_connect(G_OBJECT(button), "button_release_event",
2642 G_CALLBACK(icon_clicked_cb), mimeview);
2643 g_signal_connect(G_OBJECT(button), "key_press_event",
2644 G_CALLBACK(icon_key_pressed), mimeview);
2645 g_signal_connect(G_OBJECT(button), "drag_data_get",
2646 G_CALLBACK(mimeview_drag_data_get), mimeview);
2647 gtk_container_add(GTK_CONTAINER(grid), button);
2648 #ifdef GENERIC_UMPC
2649 gtk_widget_get_preferred_size(pixmap, &r, NULL);
2650 gtk_widget_set_size_request(button, -1, r.height + 4);
2651 #endif
2655 static void icon_list_clear (MimeView *mimeview)
2657 GList *child, *orig;
2658 GtkAdjustment *adj;
2660 orig = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_grid));
2661 for (child = orig; child != NULL; child = g_list_next(child)) {
2662 gtk_container_remove(GTK_CONTAINER(mimeview->icon_grid),
2663 GTK_WIDGET(child->data));
2665 g_list_free(orig);
2666 mimeview->icon_count = 0;
2667 adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(mimeview->icon_scroll));
2668 gtk_adjustment_set_value(adj, gtk_adjustment_get_lower(adj));
2672 *\brief Used to 'click' the next or previous icon.
2674 *\return true if the icon 'number' exists and was selected.
2676 static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2677 GtkAllocation *size, MimeView *mimeview)
2679 GtkAllocation grid_size;
2680 GtkAllocation layout_size;
2681 GtkAdjustment *adj;
2682 guint width;
2683 guint height;
2685 adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(mimeview->icon_scroll));
2687 gtk_widget_get_allocation(mimeview->icon_grid, &grid_size);
2688 gtk_widget_get_allocation(mimeview->icon_scroll, &layout_size);
2690 gtk_layout_get_size(GTK_LAYOUT(mimeview->icon_scroll), &width, &height);
2691 gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
2692 MIN(grid_size.width, layout_size.width),
2693 MAX(grid_size.height, layout_size.height));
2694 gtk_adjustment_set_step_increment(adj, 10);
2697 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
2699 gint min_width, width;
2701 cm_return_if_fail(mimeinfo != NULL);
2703 while (mimeinfo != NULL) {
2704 if (mimeinfo->type != MIMETYPE_MULTIPART)
2705 icon_list_append_icon(mimeview, mimeinfo);
2706 if (mimeinfo->node->children != NULL)
2707 icon_list_create(mimeview,
2708 (MimeInfo *) mimeinfo->node->children->data);
2709 mimeinfo = mimeinfo->node->next != NULL
2710 ? (MimeInfo *) mimeinfo->node->next->data
2711 : NULL;
2713 gtk_widget_get_preferred_width(mimeview->icon_mainbox, &min_width, &width);
2714 if (min_width < width) {
2715 gtk_widget_set_size_request(mimeview->icon_mainbox,
2716 min_width, -1);
2718 if (mimeview->opened)
2719 icon_list_toggle_by_mime_info(mimeview,
2720 mimeview_get_node_part(mimeview, mimeview->opened));
2723 static void icon_list_toggle_by_mime_info (MimeView *mimeview,
2724 MimeInfo *mimeinfo)
2726 GList *children, *child;
2728 children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_grid));
2729 for (child = children; child != NULL; child = g_list_next(child)) {
2730 gboolean *highlight = NULL;
2731 GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
2733 if (!GTK_IS_EVENT_BOX(child->data))
2734 continue;
2736 highlight = g_object_get_data(G_OBJECT(icon), "highlight");
2737 *highlight = (g_object_get_data(G_OBJECT(child->data),
2738 "partinfo") == (gpointer)mimeinfo);
2740 gtk_widget_queue_draw(icon);
2742 g_list_free(children);
2745 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
2746 MimeView *mimeview)
2748 prefs_common.mimeview_tree_height = allocation->height;
2751 static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
2752 MimeView *mimeview)
2754 g_object_ref(button);
2756 mimeview_leave_notify(button, NULL, NULL);
2758 mimeview->ctree_mode = !mimeview->ctree_mode;
2759 if (mimeview->ctree_mode) {
2760 gtk_image_set_from_icon_name(GTK_IMAGE(mimeview->arrow),
2761 "pan-end-symbolic", GTK_ICON_SIZE_MENU);
2762 gtk_widget_hide(mimeview->icon_mainbox);
2763 gtk_widget_show(mimeview->ctree_mainbox);
2764 gtk_paned_set_position(GTK_PANED(mimeview->paned),
2765 prefs_common.mimeview_tree_height);
2767 gtk_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
2768 button);
2769 gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
2770 button, FALSE, FALSE, 0);
2771 } else {
2772 gtk_image_set_from_icon_name(GTK_IMAGE(mimeview->arrow),
2773 "pan-start-symbolic", GTK_ICON_SIZE_MENU);
2774 gtk_widget_hide(mimeview->ctree_mainbox);
2775 gtk_widget_show(mimeview->icon_mainbox);
2776 gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
2778 gtk_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
2779 button);
2780 gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
2781 button, FALSE, FALSE, 0);
2782 gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(button)), button, 0);
2783 if (mimeview->opened)
2784 icon_list_toggle_by_mime_info(mimeview,
2785 mimeview_get_node_part(mimeview, mimeview->opened));
2786 summary_grab_focus(mimeview->mainwin->summaryview);
2788 g_object_unref(button);
2789 return TRUE;
2792 void mimeview_update (MimeView *mimeview)
2794 if (mimeview && mimeview->mimeinfo) {
2795 icon_list_clear(mimeview);
2796 icon_list_create(mimeview, mimeview->mimeinfo);
2800 void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
2802 MessageView *msgview = NULL;
2803 MainWindow *mainwin = NULL;
2805 if (!cmd)
2806 return;
2808 msgview = mimeview->messageview;
2809 if (!msgview)
2810 return;
2812 mainwin = msgview->mainwin;
2813 if (!mainwin)
2814 return;
2816 g_object_set_data(G_OBJECT(mimeview->popupmenu),
2817 "pop_partinfo", NULL);
2819 if (!strcmp(cmd, "cm://view_log"))
2820 log_window_show(mainwin->logwin);
2821 else if (!strcmp(cmd, "cm://save_as"))
2822 mimeview_save_as(mimeview);
2823 else if (!strcmp(cmd, "cm://display_as_text"))
2824 mimeview_display_as_text(mimeview);
2825 #ifndef G_OS_WIN32
2826 else if (!strcmp(cmd, "cm://open_with"))
2827 mimeview_open_with(mimeview);
2828 #endif
2829 else if (!strcmp(cmd, "cm://open"))
2830 mimeview_launch(mimeview, NULL);
2831 else if (!strcmp(cmd, "cm://select_attachment") && data != NULL) {
2832 icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
2833 icon_selected(mimeview, -1, (MimeInfo *)data);
2834 } else if (!strcmp(cmd, "cm://open_attachment") && data != NULL) {
2835 mimeview_launch(mimeview, (MimeInfo *)data);
2836 } else if (!strcmp(cmd, "cm://menu_attachment") && data != NULL) {
2837 mimeview->spec_part = (MimeInfo *)data;
2838 part_button_pressed(mimeview, event, (MimeInfo *)data);
2839 } else if (!strncmp(cmd, "cm://search_tags:", strlen("cm://search_tags:"))) {
2840 const gchar *tagname = cmd + strlen("cm://search_tags:");
2841 gchar *buf = g_strdup_printf("tag matchcase \"%s\"", tagname);
2842 gtk_toggle_button_set_active(
2843 GTK_TOGGLE_BUTTON(mimeview->messageview->mainwin->summaryview->toggle_search),
2844 TRUE);
2845 quicksearch_set(mimeview->messageview->mainwin->summaryview->quicksearch,
2846 ADVANCED_SEARCH_EXTENDED, buf);
2847 g_free(buf);
2851 gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
2853 if (mimeview->type == MIMEVIEW_TEXT)
2854 return textview_scroll_page(mimeview->textview, up);
2855 else if (mimeview->mimeviewer) {
2856 MimeViewer *mimeviewer = mimeview->mimeviewer;
2857 if (mimeviewer->scroll_page)
2858 return mimeviewer->scroll_page(mimeviewer, up);
2860 return TRUE;
2863 void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
2865 if (mimeview->type == MIMEVIEW_TEXT)
2866 textview_scroll_one_line(mimeview->textview, up);
2867 else if (mimeview->mimeviewer) {
2868 MimeViewer *mimeviewer = mimeview->mimeviewer;
2869 if (mimeviewer->scroll_one_line)
2870 mimeviewer->scroll_one_line(mimeviewer, up);