2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
4 * Copyright(C) 1999-2015 the Claws Mail Team
5 * This file Copyright (C) 2009-2014 Salvatore De Paolis
6 * <iwkse@claws-mail.org> and the Claws Mail Team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "claws-features.h"
28 #include <fancy_viewer.h>
29 #include <fancy_prefs.h>
30 #include <alertpanel.h>
33 #include <webkit/webkithittestresult.h>
36 load_start_cb (WebKitWebView *view, gint progress, FancyViewer *viewer);
39 load_finished_cb (WebKitWebView *view, gint progress, FancyViewer *viewer);
42 over_link_cb (WebKitWebView *view, const gchar *wtf, const gchar *link,
43 FancyViewer *viewer, void *wtfa);
47 load_progress_cb(WebKitWebView *view, GParamSpec* pspec, FancyViewer *viewer);
49 static MimeViewerFactory fancy_viewer_factory;
52 fancy_text_search(MimeViewer *_viewer, gboolean backward, const gchar *str,
56 viewer_menu_handler(GtkWidget *menuitem, FancyViewer *viewer);
58 static gint keypress_events_cb (GtkWidget *widget, GdkEventKey *event,
60 static void zoom_in_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer);
61 static void zoom_out_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer);
62 static gboolean fancy_prefs_cb(GtkWidget *widget, GdkEventButton *ev, FancyViewer *viewer);
63 static void zoom_100_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer);
64 static void open_in_browser_cb(GtkWidget *widget, FancyViewer *viewer);
65 static void fancy_create_popup_prefs_menu(FancyViewer *viewer);
66 static void fancy_show_notice(FancyViewer *viewer, const gchar *message);
67 static size_t download_file_curl_write_cb(void *buffer, size_t size,
68 size_t nmemb, void *data);
69 static void *download_file_curl (void *data);
70 static void download_file_cb(GtkWidget *widget, FancyViewer *viewer);
71 static gboolean fancy_set_contents(FancyViewer *viewer, gboolean use_defaults);
73 #if !WEBKIT_CHECK_VERSION (1,5,1)
74 gchar* webkit_web_view_get_selected_text(WebKitWebView* web_view);
78 static GtkWidget *fancy_get_widget(MimeViewer *_viewer)
80 FancyViewer *viewer = (FancyViewer *) _viewer;
81 debug_print("fancy_get_widget: %p\n", viewer->vbox);
83 return GTK_WIDGET(viewer->vbox);
86 static void fancy_apply_prefs(FancyViewer *viewer)
88 g_object_set(viewer->settings,
89 "auto-load-images", viewer->override_prefs_images,
90 "enable-scripts", viewer->override_prefs_scripts,
91 "enable-plugins", viewer->override_prefs_plugins,
92 "enable-java-applet", viewer->override_prefs_java,
93 "enable-dns-prefetching", viewer->override_prefs_remote_content,
94 "user-stylesheet-uri", viewer->override_stylesheet,
96 "default-font-family", "Arial",
97 "cursive-font-family", "Comic Sans MS",
98 "fantasy-font-family", "Comic Sans MS",
99 "monospace-font-family", "Courier New",
100 "sans-serif-font-family", "Arial",
101 "serif-font-family", "Times New Roman",
104 webkit_web_view_set_settings(viewer->view, viewer->settings);
107 static void fancy_auto_load_images_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
108 viewer->override_prefs_images = gtk_check_menu_item_get_active(item);
109 fancy_apply_prefs(viewer);
110 fancy_set_contents(viewer, FALSE);
113 static void fancy_enable_remote_content_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
114 viewer->override_prefs_remote_content = gtk_check_menu_item_get_active(item);
115 fancy_apply_prefs(viewer);
116 fancy_set_contents(viewer, FALSE);
119 static void fancy_enable_scripts_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
120 viewer->override_prefs_scripts = gtk_check_menu_item_get_active(item);
121 fancy_apply_prefs(viewer);
122 fancy_set_contents(viewer, FALSE);
125 static void fancy_enable_plugins_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
126 viewer->override_prefs_plugins = gtk_check_menu_item_get_active(item);
127 fancy_apply_prefs(viewer);
128 fancy_set_contents(viewer, FALSE);
131 static void fancy_enable_java_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
132 viewer->override_prefs_java = gtk_check_menu_item_get_active(item);
133 fancy_apply_prefs(viewer);
134 fancy_set_contents(viewer, FALSE);
137 static void fancy_open_external_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
138 viewer->override_prefs_external = gtk_check_menu_item_get_active(item);
139 fancy_apply_prefs(viewer);
142 static void fancy_set_defaults(FancyViewer *viewer)
144 viewer->override_prefs_remote_content = fancy_prefs.enable_remote_content;
145 viewer->override_prefs_external = fancy_prefs.open_external;
146 viewer->override_prefs_images = fancy_prefs.enable_images;
147 viewer->override_prefs_scripts = fancy_prefs.enable_scripts;
148 viewer->override_prefs_plugins = fancy_prefs.enable_plugins;
149 viewer->override_prefs_java = fancy_prefs.enable_java;
153 /* Replace backslashes with forward slashes, since we'll be
154 * using this string in an URI. */
155 gchar *tmp2 = g_strdup(fancy_prefs.stylesheet);
156 subst_char(tmp2, '\\', '/');
158 /* Escape string for use in an URI, keeping dir separators
159 * and colon for Windows drive name ("C:") intact. */
160 tmp = g_uri_escape_string(tmp2, "/:", TRUE);
163 /* Escape string for use in an URI, keeping dir separators
165 tmp = g_uri_escape_string(fancy_prefs.stylesheet, "/", TRUE);
167 viewer->override_stylesheet = g_strconcat("file://", tmp, NULL);
169 debug_print("Passing '%s' as stylesheet URI to Webkit\n",
170 viewer->override_stylesheet);
172 g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_images),
173 fancy_auto_load_images_activated, viewer);
174 g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_remote_content),
175 fancy_enable_remote_content_activated, viewer);
176 g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_scripts),
177 fancy_enable_scripts_activated, viewer);
178 g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_plugins),
179 fancy_enable_plugins_activated, viewer);
180 g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_java),
181 fancy_enable_java_activated, viewer);
182 g_signal_handlers_block_by_func(G_OBJECT(viewer->open_external),
183 fancy_open_external_activated, viewer);
185 gtk_check_menu_item_set_active(
186 GTK_CHECK_MENU_ITEM(viewer->enable_images),
187 viewer->override_prefs_images);
188 gtk_check_menu_item_set_active(
189 GTK_CHECK_MENU_ITEM(viewer->enable_scripts),
190 viewer->override_prefs_scripts);
191 gtk_check_menu_item_set_active(
192 GTK_CHECK_MENU_ITEM(viewer->enable_plugins),
193 viewer->override_prefs_plugins);
194 gtk_check_menu_item_set_active(
195 GTK_CHECK_MENU_ITEM(viewer->enable_java),
196 viewer->override_prefs_java);
197 gtk_check_menu_item_set_active(
198 GTK_CHECK_MENU_ITEM(viewer->enable_remote_content),
199 viewer->override_prefs_remote_content);
200 gtk_check_menu_item_set_active(
201 GTK_CHECK_MENU_ITEM(viewer->open_external),
202 viewer->override_prefs_external);
204 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_images),
205 fancy_auto_load_images_activated, viewer);
206 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_remote_content),
207 fancy_enable_remote_content_activated, viewer);
208 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_scripts),
209 fancy_enable_scripts_activated, viewer);
210 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_plugins),
211 fancy_enable_plugins_activated, viewer);
212 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_java),
213 fancy_enable_java_activated, viewer);
214 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->open_external),
215 fancy_open_external_activated, viewer);
217 fancy_apply_prefs(viewer);
220 static gboolean fancy_set_contents(FancyViewer *viewer, gboolean use_defaults)
222 MessageView *messageview = ((MimeViewer *)viewer)->mimeview
223 ? ((MimeViewer *)viewer)->mimeview->messageview
225 MimeInfo *partinfo = viewer->to_load;
228 messageview->updating = TRUE;
229 NoticeView *noticeview = messageview->noticeview;
230 noticeview_hide(noticeview);
233 if (viewer->filename != NULL) {
234 g_unlink(viewer->filename);
235 g_free(viewer->filename);
236 viewer->filename = NULL;
240 viewer->filename = procmime_get_tmp_file_name(partinfo);
241 debug_print("filename: %s\n", viewer->filename);
242 if (!viewer->filename) {
245 if (procmime_get_part(viewer->filename, partinfo) < 0) {
246 g_free(viewer->filename);
247 viewer->filename = NULL;
250 const gchar *charset = NULL;
251 gchar *contents = NULL;
252 if (messageview && messageview->forced_charset)
253 charset = ((MimeViewer *)viewer)->mimeview->messageview->forced_charset;
255 charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
257 charset = conv_get_locale_charset_str();
258 debug_print("using %s charset\n", charset);
259 g_object_set(viewer->settings, "default-encoding", charset, NULL);
262 debug_print("zoom_level: %i\n", fancy_prefs.zoom_level);
263 webkit_web_view_set_zoom_level(viewer->view, (fancy_prefs.zoom_level / 100.0));
265 fancy_set_defaults(viewer);
268 contents = file_read_to_str_no_recode(viewer->filename);
269 webkit_web_view_load_string(viewer->view,
276 viewer->loading = FALSE;
280 static gboolean fancy_show_mimepart_real(MimeViewer *_viewer)
282 return fancy_set_contents((FancyViewer *)_viewer, TRUE);
285 static void fancy_show_notice(FancyViewer *viewer, const gchar *message)
287 gtk_label_set_text(GTK_LABEL(viewer->l_link), message);
290 static gint fancy_show_mimepart_prepare(MimeViewer *_viewer)
292 FancyViewer *viewer = (FancyViewer *) _viewer;
294 g_timeout_add(5, (GSourceFunc)fancy_show_mimepart_real, viewer);
298 static void fancy_show_mimepart(MimeViewer *_viewer, const gchar *infile,
301 FancyViewer *viewer = (FancyViewer *) _viewer;
302 viewer->to_load = partinfo;
303 viewer->loading = TRUE;
304 g_timeout_add(5, (GtkFunction)fancy_show_mimepart_prepare, viewer);
307 static void fancy_print(MimeViewer *_viewer)
309 FancyViewer *viewer = (FancyViewer *) _viewer;
310 GtkPrintOperationResult res;
311 GError *error = NULL;
312 GtkPrintOperation *op;
314 gtk_widget_realize(GTK_WIDGET(viewer->view));
316 while (viewer->loading)
319 op = gtk_print_operation_new();
321 /* Config for printing */
322 gtk_print_operation_set_print_settings(op, printing_get_settings());
323 gtk_print_operation_set_default_page_setup(op, printing_get_page_setup());
324 #if GTK_CHECK_VERSION(2,18,0)
325 /* enable Page Size and Orientation in the print dialog */
326 gtk_print_operation_set_embed_page_setup(op, TRUE);
329 /* Start printing process */
330 res = webkit_web_frame_print_full(webkit_web_view_get_main_frame(viewer->view),
331 op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
334 if (res == GTK_PRINT_OPERATION_RESULT_ERROR) {
335 gtk_print_operation_get_error(op, &error);
336 debug_print("Error printing message: %s\n",
337 error ? error->message : "no details");
338 } else if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
339 /* store settings for next printing session */
340 printing_store_settings(gtk_print_operation_get_print_settings(op));
344 static gchar *fancy_get_selection (MimeViewer *_viewer)
346 debug_print("fancy_get_selection\n");
347 FancyViewer *viewer = (FancyViewer *) _viewer;
348 #if WEBKIT_CHECK_VERSION(1,5,1)
349 viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
350 viewer->window = webkit_dom_document_get_default_view (viewer->doc);
351 viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);
352 if (viewer->selection == NULL)
354 viewer->range = webkit_dom_dom_selection_get_range_at(viewer->selection, 0, NULL);
355 if (viewer->range == NULL)
357 gchar *sel = webkit_dom_range_get_text (viewer->range);
359 gchar *sel = webkit_web_view_get_selected_text(viewer->view);
361 if (!viewer->view || strlen(sel) == 0) {
368 static void fancy_clear_viewer(MimeViewer *_viewer)
370 FancyViewer *viewer = (FancyViewer *) _viewer;
372 viewer->cur_link = NULL;
373 fancy_set_defaults(viewer);
375 webkit_web_view_load_uri(viewer->view, "about:blank");
377 debug_print("fancy_clear_viewer\n");
378 fancy_prefs.zoom_level = webkit_web_view_get_zoom_level(viewer->view) * 100;
379 viewer->to_load = NULL;
380 vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
381 gtk_adjustment_set_value(vadj, 0.0);
382 g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
385 static void fancy_destroy_viewer(MimeViewer *_viewer)
387 FancyViewer *viewer = (FancyViewer *) _viewer;
388 fancy_prefs.zoom_level = webkit_web_view_get_zoom_level(viewer->view) * 100;
389 debug_print("fancy_destroy_viewer\n");
390 g_free(viewer->filename);
395 navigation_policy_cb (WebKitWebView *web_view,
396 WebKitWebFrame *frame,
397 WebKitNetworkRequest *request,
398 WebKitWebNavigationAction *navigation_action,
399 WebKitWebPolicyDecision *policy_decision,
402 const gchar *uri = webkit_network_request_get_uri(request);
404 debug_print("navigation requested to %s\n", uri);
406 if (!strncmp(uri, "mailto:", 7)) {
407 debug_print("Opening message window\n");
408 compose_new(NULL, uri + 7, NULL);
409 webkit_web_policy_decision_ignore(policy_decision);
410 } else if (!strncmp(uri, "file://", 7) || !strcmp(uri, "about:blank")) {
411 debug_print("local navigation request ACCEPTED\n");
412 webkit_web_policy_decision_use(policy_decision);
413 } else if (viewer->override_prefs_remote_content) {
414 debug_print("remote navigation request ACCEPTED\n");
415 webkit_web_policy_decision_use(policy_decision);
417 debug_print("remote navigation request IGNORED\n");
418 fancy_show_notice(viewer, _("Remote content loading is disabled."));
419 webkit_web_policy_decision_ignore(policy_decision);
425 static void resource_request_starting_cb(WebKitWebView *view,
426 WebKitWebFrame *frame,
427 WebKitWebResource *resource,
428 WebKitNetworkRequest *request,
429 WebKitNetworkResponse *response,
432 const gchar *uri = webkit_network_request_get_uri(request);
436 MimeInfo *partinfo = viewer->to_load;
438 filename = viewer->filename;
439 if ((!g_ascii_strncasecmp(uri, "cid:", 4)) || (!g_ascii_strncasecmp(uri, "mid:", 4))) {
440 image = g_strconcat("<", uri + 4, ">", NULL);
441 while ((partinfo = procmime_mimeinfo_next(partinfo)) != NULL) {
442 if (!g_ascii_strcasecmp(image, partinfo->id)) {
443 filename = procmime_get_tmp_file_name(partinfo);
448 if ((err = procmime_get_part(filename, partinfo)) < 0)
449 alertpanel_error(_("Couldn't save the part of multipart message: %s"),
451 gchar *file_uri = g_filename_to_uri(filename, NULL, NULL);
452 webkit_network_request_set_uri(request, file_uri);
461 /* refresh URI that may have changed */
462 uri = webkit_network_request_get_uri(request);
463 if (!viewer->override_prefs_remote_content
464 && strncmp(uri, "file://", 7) && strncmp(uri, "data:", 5)) {
465 debug_print("Preventing load of %s\n", uri);
466 webkit_network_request_set_uri(request, "about:blank");
469 debug_print("Starting request of %zu %s\n", strlen(uri), uri);
472 static gboolean fancy_text_search(MimeViewer *_viewer, gboolean backward,
473 const gchar *str, gboolean case_sens)
475 return webkit_web_view_search_text(((FancyViewer*)_viewer)->view, str,
476 case_sens, !backward, TRUE);
479 static gboolean fancy_prefs_cb(GtkWidget *widget, GdkEventButton *ev, FancyViewer *viewer)
481 if ((ev->button == 1) && (ev->type == GDK_BUTTON_PRESS)) {
482 gtk_menu_popup(GTK_MENU(viewer->fancy_prefs_menu), NULL, NULL, NULL, NULL,
483 ev->button, ev->time);
489 static void fancy_create_popup_prefs_menu(FancyViewer *viewer) {
490 GtkWidget *enable_images;
491 GtkWidget *enable_remote_content;
492 GtkWidget *enable_scripts;
493 GtkWidget *enable_plugins;
494 GtkWidget *enable_java;
495 GtkWidget *open_external;
497 enable_images = gtk_check_menu_item_new_with_label(_("Load images"));
499 enable_remote_content = gtk_check_menu_item_new_with_label(_("Enable remote content"));
501 enable_scripts = gtk_check_menu_item_new_with_label(_("Enable Javascript"));
503 enable_plugins = gtk_check_menu_item_new_with_label(_("Enable Plugins"));
505 enable_java = gtk_check_menu_item_new_with_label(_("Enable Java"));
507 open_external = gtk_check_menu_item_new_with_label(_("Open links with external browser"));
509 gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_images);
510 gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_remote_content);
511 gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_scripts);
512 gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_plugins);
513 gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_java);
514 gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), open_external);
516 gtk_menu_attach_to_widget(GTK_MENU(viewer->fancy_prefs_menu), viewer->ev_fancy_prefs, NULL);
517 gtk_widget_show_all(viewer->fancy_prefs_menu);
519 viewer->enable_images = enable_images;
520 viewer->enable_scripts = enable_scripts;
521 viewer->enable_plugins = enable_plugins;
522 viewer->enable_java = enable_java;
523 viewer->enable_remote_content = enable_remote_content;
524 viewer->open_external = open_external;
526 /* Set sensitivity according to preferences and overrides */
528 g_signal_connect(G_OBJECT(enable_images), "toggled",
529 G_CALLBACK (fancy_auto_load_images_activated), viewer);
530 g_signal_connect(G_OBJECT(enable_remote_content), "toggled",
531 G_CALLBACK (fancy_enable_remote_content_activated), viewer);
532 g_signal_connect(G_OBJECT(enable_scripts), "toggled",
533 G_CALLBACK (fancy_enable_scripts_activated), viewer);
534 g_signal_connect(G_OBJECT(enable_plugins), "toggled",
535 G_CALLBACK (fancy_enable_plugins_activated), viewer);
536 g_signal_connect(G_OBJECT(enable_java), "toggled",
537 G_CALLBACK (fancy_enable_java_activated), viewer);
538 g_signal_connect(G_OBJECT(open_external), "toggled",
539 G_CALLBACK (fancy_open_external_activated), viewer);
541 fancy_apply_prefs(viewer);
544 static gboolean fancy_scroll_page(MimeViewer *_viewer, gboolean up)
546 FancyViewer *viewer = (FancyViewer *)_viewer;
547 GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
548 GTK_SCROLLED_WINDOW(viewer->scrollwin));
550 if (viewer->view == NULL)
553 return gtkutils_scroll_page(GTK_WIDGET(viewer->view), vadj, up);
556 static void fancy_scroll_one_line(MimeViewer *_viewer, gboolean up)
558 FancyViewer *viewer = (FancyViewer *)_viewer;
559 GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
560 GTK_SCROLLED_WINDOW(viewer->scrollwin));
562 if (viewer->view == NULL)
565 gtkutils_scroll_one_line(GTK_WIDGET(viewer->view), vadj, up);
568 static void load_start_cb(WebKitWebView *view, gint progress,
571 gtk_widget_show(viewer->progress);
572 gtk_widget_show(viewer->ev_stop_loading);
575 static void load_finished_cb(WebKitWebView *view, gint progress,
578 gtk_widget_hide(viewer->progress);
579 gtk_widget_hide(viewer->ev_stop_loading);
580 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(viewer->progress),
582 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(viewer->progress), "");
585 static void over_link_cb(WebKitWebView *view, const gchar *wtf,
586 const gchar *link, FancyViewer *viewer, void *wtfa)
588 gtk_label_set_text(GTK_LABEL(viewer->l_link), link);
589 g_free(viewer->cur_link);
590 viewer->cur_link = NULL;
592 viewer->cur_link = g_strdup(link);
596 static void load_progress_cb(WebKitWebView *view, GParamSpec* pspec,
599 WebKitLoadStatus status = webkit_web_view_get_load_status(viewer->view);
600 gdouble pbar = webkit_web_view_get_progress(viewer->view);
601 const gchar *uri = webkit_web_view_get_uri(viewer->view);
603 gchar *label = g_strdup_printf("%d%% Loading...", (gint)(pbar * 100));
604 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(viewer->progress), pbar);
605 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(viewer->progress),
606 (const gchar*)label);
610 case WEBKIT_LOAD_PROVISIONAL:
611 case WEBKIT_LOAD_COMMITTED:
612 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
614 case WEBKIT_LOAD_FAILED:
615 debug_print("Load failed: %s\n", uri);
617 case WEBKIT_LOAD_FINISHED:
618 debug_print("Load finished: %s\n", uri);
623 static void stop_loading_cb(GtkWidget *widget, GdkEvent *ev,
626 webkit_web_view_stop_loading (viewer->view);
627 gtk_widget_hide(viewer->progress);
628 gtk_widget_hide(viewer->ev_stop_loading);
631 static void search_the_web_cb(GtkWidget *widget, FancyViewer *viewer)
633 debug_print("Clicked on Search on Web\n");
634 if (webkit_web_view_has_selection(viewer->view)) {
636 #if WEBKIT_CHECK_VERSION(1,5,1)
637 viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
638 viewer->window = webkit_dom_document_get_default_view (viewer->doc);
639 viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);
640 viewer->range = webkit_dom_dom_selection_get_range_at(viewer->selection, 0, NULL);
641 gchar *tmp = webkit_dom_range_get_text (viewer->range);
643 gchar *tmp = webkit_web_view_get_selected_text(viewer->view);
645 search = g_strconcat(GOOGLE_SEARCH, tmp, NULL);
647 webkit_web_view_load_uri(viewer->view, search);
653 static void open_in_browser_cb(GtkWidget *widget, FancyViewer *viewer)
655 debug_print("open outer: %s\n", viewer->cur_link);
657 open_uri(viewer->cur_link, prefs_common_get_uri_cmd());
660 static size_t download_file_curl_write_cb(void *buffer, size_t size,
661 size_t nmemb, void *data)
663 FancyViewer *viewer = (FancyViewer *)data;
664 if (!viewer->stream) {
665 viewer->stream = fopen(viewer->curlfile, "wb");
669 return fwrite(buffer, size, nmemb, viewer->stream);
671 static void *download_file_curl (void *data)
675 FancyViewer *viewer = (FancyViewer *)data;
677 curl_global_init(CURL_GLOBAL_DEFAULT);
678 curl = curl_easy_init();
681 curl_easy_setopt(curl, CURLOPT_URL, viewer->cur_link);
682 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_file_curl_write_cb);
683 curl_easy_setopt(curl, CURLOPT_WRITEDATA, viewer);
684 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
685 res = curl_easy_perform(curl);
686 curl_easy_cleanup(curl);
689 alertpanel_error(_("An error occurred: %d\n"), res);
691 fclose(viewer->stream);
692 curl_global_cleanup();
700 static void download_file_cb(GtkWidget *widget, FancyViewer *viewer)
706 const gchar *link = (const gchar *)viewer->cur_link;
707 gchar *filename = g_utf8_strchr(link, -1, g_utf8_get_char("/"));
708 filename = g_strconcat(g_get_home_dir(), filename, NULL);
709 gchar *fname = filesel_select_file_save(_("Save as"), filename);
711 if (viewer->curlfile) viewer->curlfile = NULL;
712 if (viewer->stream) viewer->stream = NULL;
713 viewer->curlfile = (const gchar *)g_strdup(fname);
717 if (!viewer->curlfile) return;
720 result = pthread_create(&curljob, NULL, download_file_curl, (void *)viewer);
722 alertpanel_error("ERROR; return code from pthread_create() is %d\n", result);
724 download_file_curl((void *)viewer);
728 static void save_image_cb(GtkWidget *widget, FancyViewer *viewer)
730 debug_print("Not Yet Implemented\n");
733 static void open_image_cb(GtkWidget *widget, FancyViewer *viewer)
735 debug_print("Not Yet Implemented\n");
738 static void copy_image_cb(GtkWidget *widget, FancyViewer *viewer)
740 debug_print("Not Yet Implemented\n");
742 static void import_feed_cb(GtkWidget *widget, FancyViewer *viewer)
744 if (!folder_subscribe(viewer->cur_link))
745 alertpanel_error(_("%s is a malformed or not supported feed"), viewer->cur_link);
747 static void viewer_menu_handler(GtkWidget *menuitem, FancyViewer *viewer)
749 const gchar *g_name = gtk_widget_get_name(GTK_WIDGET(menuitem));
750 if (!g_ascii_strcasecmp(g_name, "GtkImageMenuItem")) {
752 GtkWidget *menul = gtk_bin_get_child(GTK_BIN(menuitem));
754 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
756 gtk_label_set_text(GTK_LABEL(menul), _("Search the Web"));
757 viewer->cur_link = NULL;
758 GtkImageMenuItem *m_search = GTK_IMAGE_MENU_ITEM(menuitem);
759 g_signal_connect(G_OBJECT(m_search), "activate",
760 G_CALLBACK(search_the_web_cb),
761 (gpointer *) viewer);
764 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
767 if (viewer->override_prefs_remote_content)
768 gtk_label_set_text(GTK_LABEL(menul), _("Open in Viewer"));
770 gtk_label_set_text(GTK_LABEL(menul), _("Open in Viewer (enable remote content)"));
772 GtkImageMenuItem *m_new = GTK_IMAGE_MENU_ITEM(menuitem);
773 gtk_widget_set_sensitive(GTK_WIDGET(m_new), viewer->override_prefs_remote_content);
776 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
777 "Open Link in New Window" )) {
779 gtk_label_set_text(GTK_LABEL(menul), _("Open in Browser"));
781 GtkImageMenuItem *m_new = GTK_IMAGE_MENU_ITEM(menuitem);
782 g_signal_connect(G_OBJECT(m_new), "activate",
783 G_CALLBACK(open_in_browser_cb),
784 (gpointer *) viewer);
787 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
788 "Open Image in New Window" )) {
789 gtk_label_set_text(GTK_LABEL(menul), _("Open Image"));
790 GtkImageMenuItem *m_image = GTK_IMAGE_MENU_ITEM(menuitem);
791 g_signal_connect(G_OBJECT(m_image), "activate",
792 G_CALLBACK(open_image_cb),
793 (gpointer *) viewer);
796 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
797 "Copy Link Location" )) {
798 gtk_label_set_text(GTK_LABEL(menul), _("Copy Link"));
800 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
801 "Download Linked File" )) {
802 gtk_label_set_text(GTK_LABEL(menul), _("Download Link"));
804 GtkImageMenuItem *m_dlink = GTK_IMAGE_MENU_ITEM(menuitem);
805 g_signal_connect(G_OBJECT(m_dlink), "activate",
806 G_CALLBACK(download_file_cb),
807 (gpointer *) viewer);
810 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
813 gtk_label_set_text(GTK_LABEL(menul), _("Save Image As"));
815 GtkImageMenuItem *m_simage = GTK_IMAGE_MENU_ITEM(menuitem);
816 g_signal_connect(G_OBJECT(m_simage), "activate",
817 G_CALLBACK(save_image_cb),
818 (gpointer *) viewer);
821 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
823 gtk_label_set_text(GTK_LABEL(menul), _("Copy Image"));
824 GtkImageMenuItem *m_cimage = GTK_IMAGE_MENU_ITEM(menuitem);
825 g_signal_connect(G_OBJECT(m_cimage), "activate",
826 G_CALLBACK(copy_image_cb),
827 (gpointer *) viewer);
833 static gboolean populate_popup_cb (WebKitWebView *view, GtkWidget *menu,
836 Plugin *plugin = plugin_get_loaded_by_name("RSSyl");
837 gtk_container_foreach(GTK_CONTAINER(menu),
838 (GtkCallback)viewer_menu_handler,
841 GtkWidget *rssyl = gtk_image_menu_item_new_with_label(_("Import feed"));
842 GtkWidget *img = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
843 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(rssyl), img);
844 gtk_widget_show(GTK_WIDGET(rssyl));
845 gtk_menu_shell_append(GTK_MENU_SHELL(menu), rssyl);
846 g_signal_connect(G_OBJECT(rssyl), "activate",
847 G_CALLBACK(import_feed_cb),
848 (gpointer *) viewer);
853 static gint keypress_events_cb (GtkWidget *widget, GdkEventKey *event,
856 if (event->state == CTRL_KEY) {
857 switch (event->keyval) {
859 zoom_in_cb(viewer->ev_zoom_in, NULL, viewer);
862 zoom_100_cb(viewer->ev_zoom_100, NULL, viewer);
865 zoom_out_cb(viewer->ev_zoom_out, NULL, viewer);
872 static gboolean release_button_cb (WebKitWebView *view, GdkEvent *ev,
875 if (ev->button.button == 1 && viewer->cur_link && viewer->override_prefs_external) {
876 #if WEBKIT_CHECK_VERSION(1,9,3)
877 /* The x and y properties were added in 1.9.3 */
879 WebKitHitTestResult *result;
880 result = webkit_web_view_get_hit_test_result(view, (GdkEventButton *)ev);
881 g_object_get(G_OBJECT(result),
885 /* If this button release is end of a drag or selection event
886 * (button press happened on different coordinates), we do not
887 * want to open the link. */
888 if ((x != viewer->click_x || y != viewer->click_y))
892 open_uri(viewer->cur_link, prefs_common_get_uri_cmd());
898 static gboolean press_button_cb (WebKitWebView *view, GdkEvent *ev,
901 #if WEBKIT_CHECK_VERSION(1,5,1)
903 WebKitHitTestResult *result =
904 webkit_web_view_get_hit_test_result(view, (GdkEventButton *)ev);
906 g_object_get(G_OBJECT(result),
908 # if WEBKIT_CHECK_VERSION(1,9,3)
909 "x", &viewer->click_x, "y", &viewer->click_y,
913 if (type & WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION)
916 viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
917 viewer->window = webkit_dom_document_get_default_view (viewer->doc);
918 viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);
919 if (viewer->selection != NULL)
920 webkit_dom_dom_selection_empty(viewer->selection);
925 static void zoom_100_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
927 gtk_widget_grab_focus(widget);
928 webkit_web_view_set_zoom_level(viewer->view, 1);
931 static void zoom_in_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
933 gtk_widget_grab_focus(widget);
934 webkit_web_view_zoom_in(viewer->view);
936 static void zoom_out_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
938 gtk_widget_grab_focus(widget);
939 webkit_web_view_zoom_out(viewer->view);
942 #if WEBKIT_CHECK_VERSION (1,7,5)
943 static void resource_load_failed_cb(WebKitWebView *web_view,
944 WebKitWebFrame *web_frame,
945 WebKitWebResource *web_resource,
949 debug_print("Loading error: %s\n", error->message);
953 static MimeViewer *fancy_viewer_create(void)
958 debug_print("fancy_viewer_create\n");
960 viewer = g_new0(FancyViewer, 1);
961 viewer->mimeviewer.factory = &fancy_viewer_factory;
962 viewer->mimeviewer.get_widget = fancy_get_widget;
963 viewer->mimeviewer.get_selection = fancy_get_selection;
964 viewer->mimeviewer.show_mimepart = fancy_show_mimepart;
965 viewer->mimeviewer.print = fancy_print;
966 viewer->mimeviewer.clear_viewer = fancy_clear_viewer;
967 viewer->mimeviewer.destroy_viewer = fancy_destroy_viewer;
968 viewer->mimeviewer.text_search = fancy_text_search;
969 viewer->mimeviewer.scroll_page = fancy_scroll_page;
970 viewer->mimeviewer.scroll_one_line = fancy_scroll_one_line;
971 viewer->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
973 #ifdef HAVE_LIBSOUP_GNOME
974 /* Use GNOME proxy settings through libproxy */
975 if (fancy_prefs.enable_gnome_proxy) {
976 SoupSession *session = webkit_get_default_session();
977 soup_session_add_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER_GNOME);
981 if (fancy_prefs.enable_proxy) {
982 SoupSession *session = webkit_get_default_session();
983 SoupURI* pURI = soup_uri_new(fancy_prefs.proxy_str);
984 g_object_set(session, "proxy-uri", pURI, NULL);
987 viewer->settings = webkit_web_settings_new();
988 g_object_set(viewer->settings, "user-agent", "Fancy Viewer", NULL);
989 viewer->scrollwin = gtk_scrolled_window_new(NULL, NULL);
990 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewer->scrollwin),
991 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
992 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(viewer->scrollwin),
994 gtk_container_add(GTK_CONTAINER(viewer->scrollwin),
995 GTK_WIDGET(viewer->view));
997 viewer->vbox = gtk_vbox_new(FALSE, 0);
998 hbox = gtk_hbox_new(FALSE, 0);
999 viewer->progress = gtk_progress_bar_new();
1001 viewer->zoom_100 = gtk_image_new_from_stock(GTK_STOCK_ZOOM_100, GTK_ICON_SIZE_MENU);
1002 viewer->zoom_in = gtk_image_new_from_stock(GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_MENU);
1003 viewer->zoom_out = gtk_image_new_from_stock(GTK_STOCK_ZOOM_OUT, GTK_ICON_SIZE_MENU);
1004 viewer->stop_loading = gtk_image_new_from_stock(GTK_STOCK_CANCEL, GTK_ICON_SIZE_MENU);
1005 /* Event Widgets for the Zoom Widgets */
1006 viewer->ev_zoom_100 = gtk_event_box_new();
1007 viewer->ev_zoom_in = gtk_event_box_new();
1008 viewer->ev_zoom_out = gtk_event_box_new();
1009 viewer->ev_stop_loading = gtk_event_box_new();
1012 viewer->l_link = gtk_label_new("");
1014 /* Preferences Widgets to override preferences on the fly */
1015 viewer->fancy_prefs = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU);
1016 viewer->ev_fancy_prefs = gtk_event_box_new();
1018 /* Popup Menu for preferences */
1019 viewer->fancy_prefs_menu = gtk_menu_new();
1020 fancy_create_popup_prefs_menu(viewer);
1022 gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_100), FALSE);
1023 gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_in), FALSE);
1024 gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_out), FALSE);
1025 gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_fancy_prefs), FALSE);
1026 gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_stop_loading), FALSE);
1028 gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_100), viewer->zoom_100);
1029 gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_in), viewer->zoom_in);
1030 gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_out), viewer->zoom_out);
1031 gtk_container_add(GTK_CONTAINER(viewer->ev_fancy_prefs), viewer->fancy_prefs);
1032 gtk_container_add(GTK_CONTAINER(viewer->ev_stop_loading), viewer->stop_loading);
1034 gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_100, FALSE, FALSE, 1);
1035 gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_in, FALSE, FALSE, 2);
1036 gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_out, FALSE, FALSE, 2);
1037 gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_fancy_prefs, FALSE, FALSE, 2);
1038 gtk_box_pack_start(GTK_BOX(hbox), viewer->l_link, FALSE, FALSE, 8);
1039 gtk_box_pack_end(GTK_BOX(hbox), viewer->progress, FALSE, FALSE, 0);
1040 gtk_box_pack_end(GTK_BOX(hbox), viewer->ev_stop_loading, FALSE, FALSE, 0);
1042 gtk_box_pack_start(GTK_BOX(viewer->vbox), viewer->scrollwin, TRUE, TRUE,
1044 gtk_box_pack_start(GTK_BOX(viewer->vbox), hbox, FALSE, FALSE, 0);
1046 gtk_widget_show(viewer->ev_zoom_100);
1047 gtk_widget_show(viewer->ev_zoom_in);
1048 gtk_widget_show(viewer->ev_zoom_out);
1049 gtk_widget_show(viewer->ev_fancy_prefs);
1051 gtk_widget_show(viewer->scrollwin);
1052 gtk_widget_show(viewer->zoom_100);
1053 gtk_widget_show(viewer->zoom_in);
1054 gtk_widget_show(viewer->zoom_out);
1055 gtk_widget_show(viewer->fancy_prefs);
1056 gtk_widget_show(viewer->stop_loading);
1058 gtk_widget_show(viewer->l_link);
1059 gtk_widget_show(viewer->vbox);
1060 gtk_widget_show(hbox);
1061 gtk_widget_show(GTK_WIDGET(viewer->view));
1063 g_signal_connect(G_OBJECT(viewer->view), "load-started",
1064 G_CALLBACK(load_start_cb), viewer);
1065 g_signal_connect(G_OBJECT(viewer->view), "load-finished",
1066 G_CALLBACK(load_finished_cb), viewer);
1067 g_signal_connect(G_OBJECT(viewer->view), "hovering-over-link",
1068 G_CALLBACK(over_link_cb), viewer);
1070 g_signal_connect(G_OBJECT(viewer->view), "notify::progress",
1071 G_CALLBACK(load_progress_cb), viewer);
1072 g_signal_connect(G_OBJECT(viewer->view), "notify::load-status",
1073 G_CALLBACK(load_progress_cb), viewer);
1075 g_signal_connect(G_OBJECT(viewer->view), "navigation-policy-decision-requested",
1076 G_CALLBACK(navigation_policy_cb), viewer);
1078 g_signal_connect(G_OBJECT(viewer->view), "resource-request-starting",
1079 G_CALLBACK(resource_request_starting_cb), viewer);
1080 g_signal_connect(G_OBJECT(viewer->view), "populate-popup",
1081 G_CALLBACK(populate_popup_cb), viewer);
1082 g_signal_connect(G_OBJECT(viewer->view), "button-press-event",
1083 G_CALLBACK(press_button_cb), viewer);
1084 g_signal_connect(G_OBJECT(viewer->view), "button-release-event",
1085 G_CALLBACK(release_button_cb), viewer);
1086 g_signal_connect(G_OBJECT(viewer->ev_zoom_100), "button-press-event",
1087 G_CALLBACK(zoom_100_cb), (gpointer*)viewer);
1088 g_signal_connect(G_OBJECT(viewer->ev_zoom_in), "button-press-event",
1089 G_CALLBACK(zoom_in_cb), (gpointer *)viewer);
1090 g_signal_connect(G_OBJECT(viewer->ev_zoom_out), "button-press-event",
1091 G_CALLBACK(zoom_out_cb), (gpointer *)viewer);
1092 g_signal_connect(G_OBJECT(viewer->ev_fancy_prefs), "button-press-event",
1093 G_CALLBACK(fancy_prefs_cb), (gpointer *)viewer);
1094 g_signal_connect(G_OBJECT(viewer->ev_stop_loading), "button-press-event",
1095 G_CALLBACK(stop_loading_cb), viewer);
1096 g_signal_connect(G_OBJECT(viewer->view), "key_press_event",
1097 G_CALLBACK(keypress_events_cb), viewer);
1099 #if WEBKIT_CHECK_VERSION (1,7,5)
1100 g_signal_connect(G_OBJECT(viewer->view), "resource-load-failed",
1101 G_CALLBACK(resource_load_failed_cb), viewer);
1104 viewer->filename = NULL;
1105 return (MimeViewer *) viewer;
1108 static gchar *content_types[] = {"text/html", NULL};
1110 static MimeViewerFactory fancy_viewer_factory =
1114 fancy_viewer_create,
1117 gint plugin_init(gchar **error)
1119 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
1120 VERSION_NUMERIC, _("Fancy"), error))
1122 gchar *directory = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1124 if (!is_dir_exist(directory))
1125 if (make_dir (directory) < 0) {
1133 mimeview_register_viewer_factory(&fancy_viewer_factory);
1138 gboolean plugin_done(void)
1140 mimeview_unregister_viewer_factory(&fancy_viewer_factory);
1145 const gchar *plugin_name(void)
1147 /* i18n: 'Fancy' here is name of the plugin, not the english word. */
1148 return _("Fancy HTML Viewer");
1151 const gchar *plugin_desc(void)
1153 return g_strdup_printf(_("This plugin renders HTML mail using the WebKit "
1154 "%d.%d.%d library.\nBy default all remote content is "
1156 "can be found in /Configuration/Preferences/Plugins/Fancy"),
1157 WEBKIT_MAJOR_VERSION, WEBKIT_MINOR_VERSION,
1158 WEBKIT_MICRO_VERSION);
1161 const gchar *plugin_type(void)
1166 const gchar *plugin_licence(void)
1171 const gchar *plugin_version(void)
1176 struct PluginFeature *plugin_provides(void)
1178 static struct PluginFeature features[] = {
1179 {PLUGIN_MIMEVIEWER, "text/html"},
1180 {PLUGIN_NOTHING, NULL}