Fancy: fix left-click on links not opening in browser.
[claws.git] / src / plugins / fancy / fancy_viewer.c
1 /*
2  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3  * == Fancy Plugin ==
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
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #include "claws-features.h"
26 #endif
27
28 #include <fancy_viewer.h>
29 #include <fancy_prefs.h>
30 #include <alertpanel.h>
31 #include <file-utils.h>
32
33 #include <printing.h>
34 #include <webkit/webkithittestresult.h>
35
36 static void
37 load_start_cb (WebKitWebView *view, gint progress, FancyViewer *viewer);
38
39 static void
40 load_finished_cb (WebKitWebView *view, gint progress, FancyViewer *viewer);
41
42 static void
43 over_link_cb (WebKitWebView *view, const gchar *wtf, const gchar *link,
44               FancyViewer *viewer, void *wtfa);
45
46
47 static void
48 load_progress_cb(WebKitWebView *view, GParamSpec* pspec, FancyViewer *viewer);
49
50 static MimeViewerFactory fancy_viewer_factory;
51
52 static gboolean
53 fancy_text_search(MimeViewer *_viewer, gboolean backward, const gchar *str,
54                   gboolean case_sens);
55
56 static void
57 viewer_menu_handler(GtkWidget *menuitem, FancyViewer *viewer);
58
59 static gint keypress_events_cb (GtkWidget *widget, GdkEventKey *event,
60                                                                 FancyViewer *viewer);
61 static void zoom_in_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer);
62 static void zoom_out_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer);
63 static gboolean fancy_prefs_cb(GtkWidget *widget, GdkEventButton *ev, FancyViewer *viewer);
64 static void zoom_100_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer);
65 static void open_in_browser_cb(GtkWidget *widget, FancyViewer *viewer);
66 static void fancy_create_popup_prefs_menu(FancyViewer *viewer);
67 static void fancy_show_notice(FancyViewer *viewer, const gchar *message);
68 static size_t download_file_curl_write_cb(void *buffer, size_t size,
69                                           size_t nmemb, void *data);
70 static void *download_file_curl (void *data);
71 static void download_file_cb(GtkWidget *widget, FancyViewer *viewer);
72 static gboolean fancy_set_contents(FancyViewer *viewer, gboolean use_defaults);
73
74 /*------*/
75 static GtkWidget *fancy_get_widget(MimeViewer *_viewer)
76 {
77         FancyViewer *viewer = (FancyViewer *) _viewer;
78         debug_print("fancy_get_widget: %p\n", viewer->vbox);
79
80         return GTK_WIDGET(viewer->vbox);
81 }
82
83 static void fancy_apply_prefs(FancyViewer *viewer)
84 {
85         g_object_set(viewer->settings,
86                 "auto-load-images", viewer->override_prefs_images,
87                 "enable-scripts", viewer->override_prefs_scripts,
88                 "enable-plugins", viewer->override_prefs_plugins,
89                 "enable-java-applet", viewer->override_prefs_java,
90                 "enable-dns-prefetching", viewer->override_prefs_remote_content,
91                 "user-stylesheet-uri", viewer->override_stylesheet,
92 #ifdef G_OS_WIN32
93                 "default-font-family", "Arial",
94                 "cursive-font-family", "Comic Sans MS",
95                 "fantasy-font-family", "Comic Sans MS",
96                 "monospace-font-family", "Courier New",
97                 "sans-serif-font-family", "Arial",
98                 "serif-font-family", "Times New Roman",
99 #endif
100                 NULL);
101         webkit_web_view_set_settings(viewer->view, viewer->settings);
102 }
103
104 static void fancy_auto_load_images_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
105         viewer->override_prefs_images = gtk_check_menu_item_get_active(item);
106         fancy_apply_prefs(viewer);
107         fancy_set_contents(viewer, FALSE);
108 }
109
110 static void fancy_enable_remote_content_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
111         viewer->override_prefs_remote_content = gtk_check_menu_item_get_active(item);
112         fancy_apply_prefs(viewer);
113         fancy_set_contents(viewer, FALSE);
114 }
115
116 static void fancy_enable_scripts_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
117         viewer->override_prefs_scripts = gtk_check_menu_item_get_active(item);
118         fancy_apply_prefs(viewer);
119         fancy_set_contents(viewer, FALSE);
120 }
121
122 static void fancy_enable_plugins_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
123         viewer->override_prefs_plugins = gtk_check_menu_item_get_active(item);
124         fancy_apply_prefs(viewer);
125         fancy_set_contents(viewer, FALSE);
126 }
127
128 static void fancy_enable_java_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
129         viewer->override_prefs_java = gtk_check_menu_item_get_active(item);
130         fancy_apply_prefs(viewer);
131         fancy_set_contents(viewer, FALSE);
132 }
133
134 static void fancy_open_external_activated(GtkCheckMenuItem *item, FancyViewer *viewer) {
135         viewer->override_prefs_external = gtk_check_menu_item_get_active(item);
136         fancy_apply_prefs(viewer);
137 }
138
139 static void fancy_set_defaults(FancyViewer *viewer)
140 {
141         viewer->override_prefs_remote_content = fancy_prefs.enable_remote_content;
142         viewer->override_prefs_external = fancy_prefs.open_external;
143         viewer->override_prefs_images = fancy_prefs.enable_images;
144         viewer->override_prefs_scripts = fancy_prefs.enable_scripts;
145         viewer->override_prefs_plugins = fancy_prefs.enable_plugins;
146         viewer->override_prefs_java = fancy_prefs.enable_java;
147
148         gchar *tmp;
149 #ifdef G_OS_WIN32
150         /* Replace backslashes with forward slashes, since we'll be
151          * using this string in an URI. */
152         gchar *tmp2 = g_strdup(fancy_prefs.stylesheet);
153         subst_char(tmp2, '\\', '/');
154
155         /* Escape string for use in an URI, keeping dir separators
156          * and colon for Windows drive name ("C:") intact. */
157         tmp = g_uri_escape_string(tmp2, "/:", TRUE);
158         g_free(tmp2);
159 #else
160         /* Escape string for use in an URI, keeping dir separators
161          * intact. */
162         tmp = g_uri_escape_string(fancy_prefs.stylesheet, "/", TRUE);
163 #endif
164         viewer->override_stylesheet = g_strconcat("file://", tmp, NULL);
165         g_free(tmp);
166         debug_print("Passing '%s' as stylesheet URI to Webkit\n",
167                         viewer->override_stylesheet);
168
169         g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_images),
170                 fancy_auto_load_images_activated, viewer);
171         g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_remote_content),
172                 fancy_enable_remote_content_activated, viewer);
173         g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_scripts),
174                 fancy_enable_scripts_activated, viewer);
175         g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_plugins),
176                 fancy_enable_plugins_activated, viewer);
177         g_signal_handlers_block_by_func(G_OBJECT(viewer->enable_java),
178                 fancy_enable_java_activated, viewer);
179         g_signal_handlers_block_by_func(G_OBJECT(viewer->open_external),
180                 fancy_open_external_activated, viewer);
181
182         gtk_check_menu_item_set_active(
183                 GTK_CHECK_MENU_ITEM(viewer->enable_images),
184                 viewer->override_prefs_images);
185         gtk_check_menu_item_set_active(
186                 GTK_CHECK_MENU_ITEM(viewer->enable_scripts),
187                 viewer->override_prefs_scripts);
188         gtk_check_menu_item_set_active(
189                 GTK_CHECK_MENU_ITEM(viewer->enable_plugins),
190                 viewer->override_prefs_plugins);
191         gtk_check_menu_item_set_active(
192                 GTK_CHECK_MENU_ITEM(viewer->enable_java),
193                 viewer->override_prefs_java);
194         gtk_check_menu_item_set_active(
195                 GTK_CHECK_MENU_ITEM(viewer->enable_remote_content),
196                 viewer->override_prefs_remote_content);
197         gtk_check_menu_item_set_active(
198                 GTK_CHECK_MENU_ITEM(viewer->open_external),
199                 viewer->override_prefs_external);
200
201         g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_images),
202                 fancy_auto_load_images_activated, viewer);
203         g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_remote_content),
204                 fancy_enable_remote_content_activated, viewer);
205         g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_scripts),
206                 fancy_enable_scripts_activated, viewer);
207         g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_plugins),
208                 fancy_enable_plugins_activated, viewer);
209         g_signal_handlers_unblock_by_func(G_OBJECT(viewer->enable_java),
210                 fancy_enable_java_activated, viewer);
211         g_signal_handlers_unblock_by_func(G_OBJECT(viewer->open_external),
212                 fancy_open_external_activated, viewer);
213
214         fancy_apply_prefs(viewer);
215 }
216
217 static gboolean fancy_set_contents(FancyViewer *viewer, gboolean use_defaults)
218 {
219         MessageView *messageview = ((MimeViewer *)viewer)->mimeview
220                                         ? ((MimeViewer *)viewer)->mimeview->messageview
221                                         : NULL;
222         MimeInfo *partinfo = viewer->to_load;
223
224         if (messageview) {
225                 messageview->updating = TRUE;
226                 NoticeView *noticeview = messageview->noticeview;
227                 noticeview_hide(noticeview);
228         }
229
230         if (viewer->filename != NULL) {
231                 g_unlink(viewer->filename);
232                 g_free(viewer->filename);
233                 viewer->filename = NULL;
234         }
235
236         if (partinfo)
237                 viewer->filename = procmime_get_tmp_file_name(partinfo);
238         debug_print("filename: %s\n", viewer->filename);
239         if (!viewer->filename) {
240                 return FALSE;
241         }
242         if (procmime_get_part(viewer->filename, partinfo) < 0) {
243                 g_free(viewer->filename);
244                 viewer->filename = NULL;
245         }
246         else {
247                 const gchar *charset = NULL;
248                 gchar *contents = NULL;
249                 if (messageview && messageview->forced_charset)
250                         charset = ((MimeViewer *)viewer)->mimeview->messageview->forced_charset;
251                 else
252                         charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
253                 if (!charset)
254                         charset = conv_get_locale_charset_str();
255                 debug_print("using %s charset\n", charset);
256                 g_object_set(viewer->settings, "default-encoding", charset, NULL);
257                 
258                 if (use_defaults) {
259                         debug_print("zoom_level: %i\n", fancy_prefs.zoom_level);
260                         webkit_web_view_set_zoom_level(viewer->view, (fancy_prefs.zoom_level / 100.0));
261
262                         fancy_set_defaults(viewer);
263                 }
264
265                 contents = file_read_to_str_no_recode(viewer->filename);
266                 webkit_web_view_load_string(viewer->view,
267                                             contents,
268                                             "text/html",
269                                             charset,
270                                             NULL);
271                 g_free(contents);
272         }
273         viewer->loading = FALSE;
274         return FALSE;
275 }
276
277 static gboolean fancy_show_mimepart_real(MimeViewer *_viewer)
278 {
279         return fancy_set_contents((FancyViewer *)_viewer, TRUE);
280 }
281
282 static void fancy_show_notice(FancyViewer *viewer, const gchar *message)
283 {
284         gtk_label_set_text(GTK_LABEL(viewer->l_link), message);
285 }
286
287 static gboolean fancy_show_mimepart_prepare(MimeViewer *_viewer)
288 {
289         FancyViewer *viewer = (FancyViewer *) _viewer;
290
291         g_timeout_add(5, (GSourceFunc)fancy_show_mimepart_real, viewer);
292         return FALSE;
293 }
294
295 static void fancy_show_mimepart(MimeViewer *_viewer, const gchar *infile,
296                                                                 MimeInfo *partinfo)
297 {
298         FancyViewer *viewer = (FancyViewer *) _viewer;
299         viewer->to_load = partinfo;
300         viewer->loading = TRUE;
301         g_timeout_add(5, (GSourceFunc)fancy_show_mimepart_prepare, viewer);
302 }
303
304 static void fancy_print(MimeViewer *_viewer)
305 {
306         FancyViewer *viewer = (FancyViewer *) _viewer;
307         GtkPrintOperationResult res;
308         GError *error = NULL;
309         GtkPrintOperation *op;
310
311         gtk_widget_realize(GTK_WIDGET(viewer->view));
312
313         while (viewer->loading)
314                 claws_do_idle();
315
316         op = gtk_print_operation_new();
317
318         /* Config for printing */
319         gtk_print_operation_set_print_settings(op, printing_get_settings());
320         gtk_print_operation_set_default_page_setup(op, printing_get_page_setup());
321         /* enable Page Size and Orientation in the print dialog */
322         gtk_print_operation_set_embed_page_setup(op, TRUE);
323
324         /* Start printing process */
325         res = webkit_web_frame_print_full(webkit_web_view_get_main_frame(viewer->view),
326                                           op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
327                                           &error);
328
329         if (res == GTK_PRINT_OPERATION_RESULT_ERROR) {
330                 gtk_print_operation_get_error(op, &error);
331                 debug_print("Error printing message: %s\n",
332                             error ? error->message : "no details");
333         } else if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
334                 /* store settings for next printing session */
335                 printing_store_settings(gtk_print_operation_get_print_settings(op));
336         }
337 }
338
339 static gchar *fancy_get_selection (MimeViewer *_viewer)
340 {
341         debug_print("fancy_get_selection\n");
342         FancyViewer *viewer = (FancyViewer *) _viewer;
343         viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
344         viewer->window = webkit_dom_document_get_default_view (viewer->doc);
345         viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);
346         if (viewer->selection == NULL)
347                 return NULL;
348         viewer->range = webkit_dom_dom_selection_get_range_at(viewer->selection, 0, NULL);
349         if (viewer->range == NULL)
350                 return NULL;
351         gchar *sel = webkit_dom_range_get_text (viewer->range);
352         if (!viewer->view || strlen(sel) == 0) {
353                 g_free(sel);
354                 return NULL;
355         }
356         return sel;
357 }
358
359 static void fancy_clear_viewer(MimeViewer *_viewer)
360 {
361         FancyViewer *viewer = (FancyViewer *) _viewer;
362         GtkAdjustment *vadj;
363         viewer->cur_link = NULL;
364         fancy_set_defaults(viewer);
365
366         webkit_web_view_load_uri(viewer->view, "about:blank");
367
368         debug_print("fancy_clear_viewer\n");
369         fancy_prefs.zoom_level = webkit_web_view_get_zoom_level(viewer->view) * 100;
370         viewer->to_load = NULL;
371         vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
372         gtk_adjustment_set_value(vadj, 0.0);
373         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
374 }
375
376 static void fancy_destroy_viewer(MimeViewer *_viewer)
377 {
378         FancyViewer *viewer = (FancyViewer *) _viewer;
379         fancy_prefs.zoom_level = webkit_web_view_get_zoom_level(viewer->view) * 100;
380         debug_print("fancy_destroy_viewer\n");
381         g_free(viewer->filename);
382         g_free(viewer);
383 }
384
385 static gboolean
386 navigation_policy_cb (WebKitWebView             *web_view,
387                       WebKitWebFrame            *frame,
388                       WebKitNetworkRequest      *request,
389                       WebKitWebNavigationAction *navigation_action,
390                       WebKitWebPolicyDecision   *policy_decision,
391                       FancyViewer               *viewer)
392 {
393         const gchar *uri = webkit_network_request_get_uri(request);
394
395         debug_print("navigation requested to %s\n", uri);
396
397         if (!strncmp(uri, "mailto:", 7)) {
398                 debug_print("Opening message window\n");
399                 compose_new(NULL, uri + 7, NULL);
400                 webkit_web_policy_decision_ignore(policy_decision);
401         } else if (!strncmp(uri, "file://", 7) || !strcmp(uri, "about:blank")) {
402                 debug_print("local navigation request ACCEPTED\n");
403                 webkit_web_policy_decision_use(policy_decision);
404         } else if (viewer->override_prefs_remote_content) {
405                 debug_print("remote navigation request ACCEPTED\n");
406                 webkit_web_policy_decision_use(policy_decision);
407         } else {
408                 debug_print("remote navigation request IGNORED\n");
409                 fancy_show_notice(viewer, _("Remote content loading is disabled."));
410                 webkit_web_policy_decision_ignore(policy_decision);
411         }
412
413         return true;
414 }
415
416 static void resource_request_starting_cb(WebKitWebView          *view,
417                                          WebKitWebFrame         *frame,
418                                          WebKitWebResource      *resource,
419                                          WebKitNetworkRequest   *request,
420                                          WebKitNetworkResponse  *response,
421                                          FancyViewer            *viewer)
422 {
423         const gchar *uri = webkit_network_request_get_uri(request);
424         gchar *filename;
425         gchar *image;
426         gint err;
427         MimeInfo *partinfo = viewer->to_load;
428         
429         filename = viewer->filename;
430         if ((!g_ascii_strncasecmp(uri, "cid:", 4)) || (!g_ascii_strncasecmp(uri, "mid:", 4))) {
431                 image = g_strconcat("<", uri + 4, ">", NULL);
432                 while ((partinfo = procmime_mimeinfo_next(partinfo)) != NULL) {
433                         if (!g_ascii_strcasecmp(image, partinfo->id)) {
434                                 filename = procmime_get_tmp_file_name(partinfo);
435                                 if (!filename) {
436                                         g_free(image);
437                                         return;
438                                 }
439                                 if ((err = procmime_get_part(filename, partinfo)) < 0)
440                                         alertpanel_error(_("Couldn't save the part of multipart message: %s"),
441                                                                                 g_strerror(-err));
442                                 gchar *file_uri = g_filename_to_uri(filename, NULL, NULL);
443                                 webkit_network_request_set_uri(request, file_uri);
444                                 g_free(file_uri);
445                                 g_free(filename);
446                                 break;
447                         }
448                 }
449                 g_free(image);
450         }
451         
452         /* refresh URI that may have changed */
453         uri = webkit_network_request_get_uri(request);
454         if (!viewer->override_prefs_remote_content
455             && strncmp(uri, "file://", 7) && strncmp(uri, "data:", 5)) {
456                 debug_print("Preventing load of %s\n", uri);
457                 webkit_network_request_set_uri(request, "about:blank");
458         }
459         else
460                 debug_print("Starting request of %zu %s\n", strlen(uri), uri);
461 }
462
463 static gboolean fancy_text_search(MimeViewer *_viewer, gboolean backward,
464                                   const gchar *str, gboolean case_sens)
465 {
466         return webkit_web_view_search_text(((FancyViewer*)_viewer)->view, str,
467                                            case_sens, !backward, TRUE);
468 }
469
470 static gboolean fancy_prefs_cb(GtkWidget *widget, GdkEventButton *ev, FancyViewer *viewer)
471 {
472         if ((ev->button == 1) && (ev->type == GDK_BUTTON_PRESS)) {
473                 gtk_menu_popup(GTK_MENU(viewer->fancy_prefs_menu), NULL, NULL, NULL, NULL,
474                                 ev->button, ev->time);
475                 return TRUE;
476         }
477         return FALSE;
478 }
479
480 static void fancy_create_popup_prefs_menu(FancyViewer *viewer) {
481         GtkWidget *enable_images;
482         GtkWidget *enable_remote_content;
483         GtkWidget *enable_scripts;
484         GtkWidget *enable_plugins;
485         GtkWidget *enable_java;
486         GtkWidget *open_external;
487
488         enable_images = gtk_check_menu_item_new_with_label(_("Load images"));
489
490         enable_remote_content = gtk_check_menu_item_new_with_label(_("Enable remote content"));
491
492         enable_scripts = gtk_check_menu_item_new_with_label(_("Enable Javascript"));
493
494         enable_plugins = gtk_check_menu_item_new_with_label(_("Enable Plugins"));
495
496         enable_java = gtk_check_menu_item_new_with_label(_("Enable Java"));
497
498         open_external = gtk_check_menu_item_new_with_label(_("Open links with external browser"));
499
500         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_images);
501         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_remote_content);
502         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_scripts);
503         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_plugins);
504         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_java);
505         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), open_external);
506
507         gtk_menu_attach_to_widget(GTK_MENU(viewer->fancy_prefs_menu), viewer->ev_fancy_prefs, NULL);
508         gtk_widget_show_all(viewer->fancy_prefs_menu);
509
510         viewer->enable_images = enable_images;
511         viewer->enable_scripts = enable_scripts;
512         viewer->enable_plugins = enable_plugins;
513         viewer->enable_java = enable_java;
514         viewer->enable_remote_content = enable_remote_content;
515         viewer->open_external = open_external;
516
517         /* Set sensitivity according to preferences and overrides */
518
519         g_signal_connect(G_OBJECT(enable_images), "toggled",
520                          G_CALLBACK (fancy_auto_load_images_activated), viewer);
521         g_signal_connect(G_OBJECT(enable_remote_content), "toggled",
522                          G_CALLBACK (fancy_enable_remote_content_activated), viewer);
523         g_signal_connect(G_OBJECT(enable_scripts), "toggled",
524                          G_CALLBACK (fancy_enable_scripts_activated), viewer);
525         g_signal_connect(G_OBJECT(enable_plugins), "toggled",
526                          G_CALLBACK (fancy_enable_plugins_activated), viewer);
527         g_signal_connect(G_OBJECT(enable_java), "toggled",
528                          G_CALLBACK (fancy_enable_java_activated), viewer);
529         g_signal_connect(G_OBJECT(open_external), "toggled",
530                          G_CALLBACK (fancy_open_external_activated), viewer);
531
532         fancy_apply_prefs(viewer);
533 }
534
535 static gboolean fancy_scroll_page(MimeViewer *_viewer, gboolean up)
536 {
537         FancyViewer *viewer = (FancyViewer *)_viewer;
538         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
539                                         GTK_SCROLLED_WINDOW(viewer->scrollwin));
540
541         if (viewer->view == NULL)
542                 return FALSE;
543
544         return gtkutils_scroll_page(GTK_WIDGET(viewer->view), vadj, up);
545 }
546
547 static void fancy_scroll_one_line(MimeViewer *_viewer, gboolean up)
548 {
549         FancyViewer *viewer = (FancyViewer *)_viewer;
550         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
551                                         GTK_SCROLLED_WINDOW(viewer->scrollwin));
552
553         if (viewer->view == NULL)
554                 return;
555
556         gtkutils_scroll_one_line(GTK_WIDGET(viewer->view), vadj, up);
557 }
558
559 static void load_start_cb(WebKitWebView *view, gint progress,
560                           FancyViewer *viewer)
561 {
562         gtk_widget_show(viewer->progress);
563         gtk_widget_show(viewer->ev_stop_loading);
564 }
565
566 static void load_finished_cb(WebKitWebView *view, gint progress,
567                              FancyViewer *viewer)
568 {
569         gtk_widget_hide(viewer->progress);
570         gtk_widget_hide(viewer->ev_stop_loading);
571         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(viewer->progress),
572                                       (gdouble) 0.0);
573         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(viewer->progress), "");
574 }
575
576 static void over_link_cb(WebKitWebView *view, const gchar *wtf,
577                 const gchar *link, FancyViewer *viewer, void *wtfa)
578 {
579         /* Display the link in the bottom statusbar. */
580         gtk_label_set_text(GTK_LABEL(viewer->l_link), link);
581 }
582
583 static void load_progress_cb(WebKitWebView *view, GParamSpec* pspec,
584                              FancyViewer *viewer)
585 {
586         WebKitLoadStatus status = webkit_web_view_get_load_status(viewer->view);
587         gdouble pbar = webkit_web_view_get_progress(viewer->view);
588         const gchar *uri = webkit_web_view_get_uri(viewer->view);
589
590         gchar *label = g_strdup_printf("%d%% Loading...", (gint)(pbar * 100));
591         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(viewer->progress), pbar);
592         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(viewer->progress),
593                                   (const gchar*)label);
594         g_free(label);
595
596         switch (status) {
597                 case WEBKIT_LOAD_PROVISIONAL:
598                 case WEBKIT_LOAD_COMMITTED:
599                 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
600                         break;
601                 case WEBKIT_LOAD_FAILED:
602                         debug_print("Load failed: %s\n", uri);
603                         break;
604                 case WEBKIT_LOAD_FINISHED:
605                         debug_print("Load finished: %s\n", uri);
606                         break;
607         }
608 }
609
610 static void stop_loading_cb(GtkWidget *widget, GdkEvent *ev,
611                                                         FancyViewer *viewer)
612 {
613         webkit_web_view_stop_loading (viewer->view);
614         gtk_widget_hide(viewer->progress);
615         gtk_widget_hide(viewer->ev_stop_loading);
616 }
617
618 static void search_the_web_cb(GtkWidget *widget, FancyViewer *viewer)
619 {
620         debug_print("Clicked on Search on Web\n");
621         if (webkit_web_view_has_selection(viewer->view)) {
622                 gchar *search;
623                 viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
624                 viewer->window = webkit_dom_document_get_default_view (viewer->doc);
625                 viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);
626                 viewer->range = webkit_dom_dom_selection_get_range_at(viewer->selection, 0, NULL);
627                 gchar *tmp = webkit_dom_range_get_text (viewer->range);
628                 search = g_strconcat(GOOGLE_SEARCH, tmp, NULL);
629
630                 webkit_web_view_load_uri(viewer->view, search);
631                 g_free(search);
632                 g_free(tmp);
633         }
634 }
635
636 static void open_in_browser_cb(GtkWidget *widget, FancyViewer *viewer)
637 {
638         debug_print("open outer: %s\n", viewer->cur_link);
639         if(viewer->cur_link)
640                 open_uri(viewer->cur_link, prefs_common_get_uri_cmd());
641 }
642
643 static size_t download_file_curl_write_cb(void *buffer, size_t size,
644                                           size_t nmemb, void *data)
645 {
646         FancyViewer *viewer = (FancyViewer *)data;
647         if (!viewer->stream) {
648                 viewer->stream = claws_fopen(viewer->curlfile, "wb");
649                 if (!viewer->stream)
650                         return -1;
651         }
652         return claws_fwrite(buffer, size, nmemb, viewer->stream);
653 }
654 static void *download_file_curl (void *data)
655 {
656         CURL *curl;
657         CURLcode res;
658         FancyViewer *viewer = (FancyViewer *)data;
659
660         curl_global_init(CURL_GLOBAL_DEFAULT);
661         curl = curl_easy_init();
662
663         if (curl) {
664                 curl_easy_setopt(curl, CURLOPT_URL, viewer->cur_link);
665                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_file_curl_write_cb);
666                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, viewer);
667                 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
668                 res = curl_easy_perform(curl);
669                 curl_easy_cleanup(curl);
670
671                 if (CURLE_OK != res)
672                         alertpanel_error(_("An error occurred: %d\n"), res);
673                 if (viewer->stream)
674                         claws_safe_fclose(viewer->stream);
675                 curl_global_cleanup();
676         }
677 #ifdef USE_PTHREAD
678         pthread_exit(NULL);
679 #else
680         return NULL;
681 #endif
682 }
683 static void download_file_cb(GtkWidget *widget, FancyViewer *viewer)
684 {
685 #ifdef USE_PTHREAD
686         pthread_t curljob;
687         gint result;
688 #endif
689         const gchar *link = (const gchar *)viewer->cur_link;
690         gchar *filename = g_utf8_strchr(link, -1, g_utf8_get_char("/"));
691         filename = g_strconcat(g_get_home_dir(), filename, NULL);
692         gchar *fname = filesel_select_file_save(_("Save as"), filename);
693
694         if (viewer->curlfile) viewer->curlfile = NULL;
695         if (viewer->stream) viewer->stream = NULL;
696         viewer->curlfile = (const gchar *)g_strdup(fname);
697         g_free(filename);
698         g_free(fname);
699
700         if (!viewer->curlfile) return;
701
702 #ifdef USE_PTHREAD
703         result = pthread_create(&curljob, NULL, download_file_curl, (void *)viewer);
704         if (result)
705                 alertpanel_error("ERROR; return code from pthread_create() is %d\n", result);
706 #else
707         download_file_curl((void *)viewer);
708 #endif
709 }
710
711 static void save_image_cb(GtkWidget *widget, FancyViewer *viewer)
712 {
713         debug_print("Not Yet Implemented\n");
714 }
715
716 static void open_image_cb(GtkWidget *widget, FancyViewer *viewer)
717 {
718         debug_print("Not Yet Implemented\n");
719 }
720
721 static void copy_image_cb(GtkWidget *widget, FancyViewer *viewer)
722 {
723         debug_print("Not Yet Implemented\n");
724 }
725 static void import_feed_cb(GtkWidget *widget, FancyViewer *viewer)
726 {
727         if (!folder_subscribe(viewer->cur_link))
728                 alertpanel_error(_("%s is a malformed or not supported feed"), viewer->cur_link);
729 }
730 static void viewer_menu_handler(GtkWidget *menuitem, FancyViewer *viewer)
731 {
732         const gchar *g_name = gtk_widget_get_name(GTK_WIDGET(menuitem));
733         if (!g_ascii_strcasecmp(g_name, "GtkImageMenuItem")) {
734
735                 GtkWidget *menul = gtk_bin_get_child(GTK_BIN(menuitem));
736
737         if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
738                                 "Search the Web")) {
739                         gtk_label_set_text(GTK_LABEL(menul), _("Search the Web"));
740                         viewer->cur_link = NULL;
741                         GtkImageMenuItem *m_search = GTK_IMAGE_MENU_ITEM(menuitem);
742                         g_signal_connect(G_OBJECT(m_search), "activate",
743                                          G_CALLBACK(search_the_web_cb),
744                                          (gpointer *) viewer);
745                 }
746
747                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
748                                         "Open Link" )) {
749
750                         if (viewer->override_prefs_remote_content)
751                                 gtk_label_set_text(GTK_LABEL(menul), _("Open in Viewer"));
752                         else
753                                 gtk_label_set_text(GTK_LABEL(menul), _("Open in Viewer (enable remote content)"));
754
755                         GtkImageMenuItem *m_new = GTK_IMAGE_MENU_ITEM(menuitem);
756                         gtk_widget_set_sensitive(GTK_WIDGET(m_new), viewer->override_prefs_remote_content);
757                 }
758
759                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
760                                                                 "Open Link in New Window" )) {
761
762                         gtk_label_set_text(GTK_LABEL(menul), _("Open in Browser"));
763
764                         GtkImageMenuItem *m_new = GTK_IMAGE_MENU_ITEM(menuitem);
765                         g_signal_connect(G_OBJECT(m_new), "activate",
766                                          G_CALLBACK(open_in_browser_cb),
767                                          (gpointer *) viewer);
768                 }
769
770                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
771                                         "Open Image in New Window" )) {
772                         gtk_label_set_text(GTK_LABEL(menul), _("Open Image"));
773                         GtkImageMenuItem *m_image = GTK_IMAGE_MENU_ITEM(menuitem);
774                         g_signal_connect(G_OBJECT(m_image), "activate",
775                                          G_CALLBACK(open_image_cb),
776                                          (gpointer *) viewer);
777                 }
778
779                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
780                                         "Copy Link Location" )) {
781                         gtk_label_set_text(GTK_LABEL(menul), _("Copy Link"));
782                 }
783         if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
784                                 "Download Linked File" )) {
785                         gtk_label_set_text(GTK_LABEL(menul), _("Download Link"));
786
787                         GtkImageMenuItem *m_dlink = GTK_IMAGE_MENU_ITEM(menuitem);
788                         g_signal_connect(G_OBJECT(m_dlink), "activate",
789                                          G_CALLBACK(download_file_cb),
790                                          (gpointer *) viewer);
791                 }
792
793                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
794                                         "Save Image As" )) {
795
796                         gtk_label_set_text(GTK_LABEL(menul), _("Save Image As"));
797
798                         GtkImageMenuItem *m_simage = GTK_IMAGE_MENU_ITEM(menuitem);
799                         g_signal_connect(G_OBJECT(m_simage), "activate",
800                                          G_CALLBACK(save_image_cb),
801                                          (gpointer *) viewer);
802                 }
803
804                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)),
805                                         "Copy Image" )) {
806                         gtk_label_set_text(GTK_LABEL(menul), _("Copy Image"));
807                         GtkImageMenuItem *m_cimage = GTK_IMAGE_MENU_ITEM(menuitem);
808                         g_signal_connect(G_OBJECT(m_cimage), "activate",
809                                          G_CALLBACK(copy_image_cb),
810                                          (gpointer *) viewer);
811                 }
812
813         }
814 }
815
816 static gboolean context_menu_cb (WebKitWebView *view, GtkWidget *menu,
817                 WebKitHitTestResult *hit_test_result,
818                 gboolean triggered_with_keyboard,
819                 gpointer user_data)
820 {
821         FancyViewer *viewer = (FancyViewer *)user_data;
822         Plugin *plugin = plugin_get_loaded_by_name("RSSyl");
823         WebKitHitTestResultContext context;
824         gchar *link_uri = NULL;
825
826         g_object_get(G_OBJECT(hit_test_result),
827                         "context", &context,
828                         "link-uri", &link_uri,
829                         NULL);
830
831         debug_print("context %d, link-uri '%s'\n", context,
832                         (link_uri != NULL ? link_uri : "(null)"));
833         if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK &&
834                         link_uri != NULL) {
835                 if (viewer->cur_link != NULL)
836                         g_free(viewer->cur_link);
837                 /* g_object_get() already made a copy, no need to strdup() here */
838                 viewer->cur_link = link_uri;
839         }
840
841         gtk_container_foreach(GTK_CONTAINER(menu),
842                               (GtkCallback)viewer_menu_handler,
843                               viewer);
844
845         if (plugin) {
846                 GtkWidget *rssyl = gtk_image_menu_item_new_with_label(_("Import feed"));
847                 GtkWidget *img = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
848                 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(rssyl), img);
849                 gtk_widget_show(GTK_WIDGET(rssyl));
850                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), rssyl);
851                 g_signal_connect(G_OBJECT(rssyl), "activate",
852                                  G_CALLBACK(import_feed_cb),
853                                  (gpointer *) viewer);
854         }
855
856         return FALSE;
857 }
858
859 static gint keypress_events_cb (GtkWidget *widget, GdkEventKey *event,
860                                                                 FancyViewer *viewer)
861 {
862         if (event->state == CTRL_KEY) {
863                 switch (event->keyval) {
864                 case GDK_KEY_plus:
865                         zoom_in_cb(viewer->ev_zoom_in, NULL, viewer);
866                         break;
867                 case GDK_KEY_period:
868                         zoom_100_cb(viewer->ev_zoom_100, NULL, viewer);
869                         break;
870                 case GDK_KEY_minus:
871                         zoom_out_cb(viewer->ev_zoom_out, NULL, viewer);
872                         break;
873                 }
874         }
875         return FALSE;
876 }
877
878 static gboolean release_button_cb (WebKitWebView *view, GdkEvent *ev,
879                                    FancyViewer *viewer)
880 {
881         if (ev->button.button == 1 && viewer->cur_link && viewer->override_prefs_external) {
882                 gint x, y;
883                 WebKitHitTestResult *result;
884                 result = webkit_web_view_get_hit_test_result(view, (GdkEventButton *)ev);
885                 g_object_get(G_OBJECT(result),
886                                 "x", &x, "y", &y,
887                                 NULL);
888
889                 /* If this button release is end of a drag or selection event
890                  * (button press happened on different coordinates), we do not
891                  * want to open the link. */
892                 if ((x != viewer->click_x || y != viewer->click_y))
893                         return FALSE;
894
895                 open_uri(viewer->cur_link, prefs_common_get_uri_cmd());
896                 return TRUE;
897         }
898         return FALSE;
899 }
900
901 static gboolean press_button_cb (WebKitWebView *view, GdkEvent *ev,
902                 FancyViewer *viewer)
903 {
904         gint type = 0;
905         gchar *link = NULL;
906
907         WebKitHitTestResult *result =
908                 webkit_web_view_get_hit_test_result(view, (GdkEventButton *)ev);
909
910         g_object_get(G_OBJECT(result),
911                         "context", &type,
912                         "x", &viewer->click_x,
913                         "y", &viewer->click_y,
914                         "link-uri", &link,
915                         NULL);
916
917         if (type & WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION)
918                 return FALSE;
919
920         if (viewer->cur_link) {
921                 g_free(viewer->cur_link);
922                 viewer->cur_link = NULL;
923         }
924         if (link != NULL) {
925                 debug_print("press on %s\n", link);
926                 viewer->cur_link = link; /* g_context returned a newly-allocated string */
927         }
928
929         viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
930         viewer->window = webkit_dom_document_get_default_view (viewer->doc);
931         viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);
932         if (viewer->selection != NULL)
933                 webkit_dom_dom_selection_empty(viewer->selection);
934         return FALSE;
935 }
936
937 static void zoom_100_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
938 {
939         gtk_widget_grab_focus(widget);
940         webkit_web_view_set_zoom_level(viewer->view, 1);
941 }
942
943 static void zoom_in_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
944 {
945         gtk_widget_grab_focus(widget);
946         webkit_web_view_zoom_in(viewer->view);
947 }
948 static void zoom_out_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
949 {
950         gtk_widget_grab_focus(widget);
951         webkit_web_view_zoom_out(viewer->view);
952 }
953
954 static void resource_load_failed_cb(WebKitWebView     *web_view,
955                                     WebKitWebFrame    *web_frame,
956                                     WebKitWebResource *web_resource,
957                                     GError            *error,
958                                     FancyViewer       *viewer)
959 {
960         debug_print("Loading error: %s\n", error->message);
961 }
962
963 static MimeViewer *fancy_viewer_create(void)
964 {
965         FancyViewer    *viewer;
966         GtkWidget      *hbox;
967
968         debug_print("fancy_viewer_create\n");
969
970         viewer = g_new0(FancyViewer, 1);
971         viewer->mimeviewer.factory = &fancy_viewer_factory;
972         viewer->mimeviewer.get_widget = fancy_get_widget;
973         viewer->mimeviewer.get_selection = fancy_get_selection;
974         viewer->mimeviewer.show_mimepart = fancy_show_mimepart;
975         viewer->mimeviewer.print = fancy_print;
976         viewer->mimeviewer.clear_viewer = fancy_clear_viewer;
977         viewer->mimeviewer.destroy_viewer = fancy_destroy_viewer;
978         viewer->mimeviewer.text_search = fancy_text_search;
979         viewer->mimeviewer.scroll_page = fancy_scroll_page;
980         viewer->mimeviewer.scroll_one_line = fancy_scroll_one_line;
981         viewer->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
982
983 #ifdef HAVE_LIBSOUP_GNOME
984  /* Use GNOME proxy settings through libproxy */
985         if (fancy_prefs.enable_gnome_proxy) {
986                 SoupSession *session = webkit_get_default_session();
987                 soup_session_add_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER_GNOME);
988         }
989 #endif
990         
991         if (fancy_prefs.enable_proxy) {
992                 SoupSession *session = webkit_get_default_session();
993                 SoupURI* pURI = soup_uri_new(fancy_prefs.proxy_str);
994                 g_object_set(session, "proxy-uri", pURI, NULL);
995         }
996
997         viewer->settings = webkit_web_settings_new();
998         g_object_set(viewer->settings, "user-agent", "Fancy Viewer", NULL);
999         viewer->scrollwin = gtk_scrolled_window_new(NULL, NULL);
1000         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewer->scrollwin),
1001                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1002         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(viewer->scrollwin),
1003                                             GTK_SHADOW_IN);
1004         gtk_container_add(GTK_CONTAINER(viewer->scrollwin),
1005                           GTK_WIDGET(viewer->view));
1006
1007         viewer->vbox = gtk_vbox_new(FALSE, 0);
1008         hbox = gtk_hbox_new(FALSE, 0);
1009         viewer->progress = gtk_progress_bar_new();
1010         /* Zoom Widgets */
1011         viewer->zoom_100 = gtk_image_new_from_stock(GTK_STOCK_ZOOM_100, GTK_ICON_SIZE_MENU);
1012         viewer->zoom_in = gtk_image_new_from_stock(GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_MENU);
1013         viewer->zoom_out = gtk_image_new_from_stock(GTK_STOCK_ZOOM_OUT, GTK_ICON_SIZE_MENU);
1014         viewer->stop_loading = gtk_image_new_from_stock(GTK_STOCK_CANCEL, GTK_ICON_SIZE_MENU);
1015         /* Event Widgets for the Zoom Widgets  */
1016         viewer->ev_zoom_100 = gtk_event_box_new();
1017         viewer->ev_zoom_in = gtk_event_box_new();
1018         viewer->ev_zoom_out = gtk_event_box_new();
1019         viewer->ev_stop_loading = gtk_event_box_new();
1020
1021         /* Link Label */
1022         viewer->l_link = gtk_label_new("");
1023
1024         /* Preferences Widgets to override preferences on the fly  */
1025         viewer->fancy_prefs = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU);
1026         viewer->ev_fancy_prefs = gtk_event_box_new();
1027
1028         /* Popup Menu for preferences  */
1029         viewer->fancy_prefs_menu = gtk_menu_new();
1030         fancy_create_popup_prefs_menu(viewer);
1031
1032         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_100), FALSE);
1033         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_in), FALSE);
1034         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_out), FALSE);
1035         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_fancy_prefs), FALSE);
1036         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_stop_loading), FALSE);
1037
1038         gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_100), viewer->zoom_100);
1039         gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_in), viewer->zoom_in);
1040         gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_out), viewer->zoom_out);
1041         gtk_container_add(GTK_CONTAINER(viewer->ev_fancy_prefs), viewer->fancy_prefs);
1042         gtk_container_add(GTK_CONTAINER(viewer->ev_stop_loading), viewer->stop_loading);
1043
1044         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_100, FALSE, FALSE, 1);
1045         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_in, FALSE, FALSE, 2);
1046         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_out, FALSE, FALSE, 2);
1047         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_fancy_prefs, FALSE, FALSE, 2);
1048         gtk_box_pack_start(GTK_BOX(hbox), viewer->l_link, FALSE, FALSE, 8);
1049         gtk_box_pack_end(GTK_BOX(hbox), viewer->progress, FALSE, FALSE, 0);
1050         gtk_box_pack_end(GTK_BOX(hbox), viewer->ev_stop_loading, FALSE, FALSE, 0);
1051
1052         gtk_box_pack_start(GTK_BOX(viewer->vbox), viewer->scrollwin, TRUE, TRUE,
1053                        1);
1054         gtk_box_pack_start(GTK_BOX(viewer->vbox), hbox, FALSE, FALSE, 0);
1055
1056         gtk_widget_show(viewer->ev_zoom_100);
1057         gtk_widget_show(viewer->ev_zoom_in);
1058         gtk_widget_show(viewer->ev_zoom_out);
1059         gtk_widget_show(viewer->ev_fancy_prefs);
1060
1061         gtk_widget_show(viewer->scrollwin);
1062         gtk_widget_show(viewer->zoom_100);
1063         gtk_widget_show(viewer->zoom_in);
1064         gtk_widget_show(viewer->zoom_out);
1065         gtk_widget_show(viewer->fancy_prefs);
1066         gtk_widget_show(viewer->stop_loading);
1067
1068         gtk_widget_show(viewer->l_link);
1069         gtk_widget_show(viewer->vbox);
1070         gtk_widget_show(hbox);
1071         gtk_widget_show(GTK_WIDGET(viewer->view));
1072
1073         g_signal_connect(G_OBJECT(viewer->view), "load-started",
1074                          G_CALLBACK(load_start_cb), viewer);
1075         g_signal_connect(G_OBJECT(viewer->view), "load-finished",
1076                          G_CALLBACK(load_finished_cb), viewer);
1077         g_signal_connect(G_OBJECT(viewer->view), "hovering-over-link",
1078                         G_CALLBACK(over_link_cb), viewer);
1079
1080         g_signal_connect(G_OBJECT(viewer->view), "notify::progress",
1081                          G_CALLBACK(load_progress_cb), viewer);
1082         g_signal_connect(G_OBJECT(viewer->view), "notify::load-status",
1083                          G_CALLBACK(load_progress_cb), viewer);
1084
1085         g_signal_connect(G_OBJECT(viewer->view), "navigation-policy-decision-requested",
1086                          G_CALLBACK(navigation_policy_cb), viewer);
1087
1088         g_signal_connect(G_OBJECT(viewer->view), "resource-request-starting",
1089                         G_CALLBACK(resource_request_starting_cb), viewer);
1090         g_signal_connect(G_OBJECT(viewer->view), "context-menu",
1091                         G_CALLBACK(context_menu_cb), viewer);
1092         g_signal_connect(G_OBJECT(viewer->view), "button-press-event",
1093                          G_CALLBACK(press_button_cb), viewer);
1094         g_signal_connect(G_OBJECT(viewer->view), "button-release-event",
1095                          G_CALLBACK(release_button_cb), viewer);
1096         g_signal_connect(G_OBJECT(viewer->ev_zoom_100), "button-press-event",
1097                          G_CALLBACK(zoom_100_cb), (gpointer*)viewer);
1098         g_signal_connect(G_OBJECT(viewer->ev_zoom_in), "button-press-event",
1099                          G_CALLBACK(zoom_in_cb), (gpointer *)viewer);
1100         g_signal_connect(G_OBJECT(viewer->ev_zoom_out), "button-press-event",
1101                          G_CALLBACK(zoom_out_cb), (gpointer *)viewer);
1102         g_signal_connect(G_OBJECT(viewer->ev_fancy_prefs), "button-press-event",
1103                          G_CALLBACK(fancy_prefs_cb), (gpointer *)viewer);
1104         g_signal_connect(G_OBJECT(viewer->ev_stop_loading), "button-press-event",
1105                          G_CALLBACK(stop_loading_cb), viewer);
1106         g_signal_connect(G_OBJECT(viewer->view), "key_press_event",
1107                          G_CALLBACK(keypress_events_cb), viewer);
1108
1109         g_signal_connect(G_OBJECT(viewer->view), "resource-load-failed",
1110                          G_CALLBACK(resource_load_failed_cb), viewer);
1111
1112         viewer->filename = NULL;
1113         return (MimeViewer *) viewer;
1114 }
1115
1116 static gchar *content_types[] = {"text/html", NULL};
1117
1118 static MimeViewerFactory fancy_viewer_factory =
1119 {
1120         content_types,
1121         0,
1122         fancy_viewer_create,
1123 };
1124
1125 gint plugin_init(gchar **error)
1126 {
1127         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
1128                                   VERSION_NUMERIC, _("Fancy"), error))
1129                 return -1;
1130         gchar *directory = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1131                                 "fancy", NULL);
1132         if (!is_dir_exist(directory))
1133                 if (make_dir (directory) < 0) {
1134                         g_free(directory);
1135                         return -1;
1136                 }
1137         g_free(directory);
1138
1139         fancy_prefs_init();
1140
1141         mimeview_register_viewer_factory(&fancy_viewer_factory);
1142
1143         return 0;
1144 }
1145
1146 gboolean plugin_done(void)
1147 {
1148         mimeview_unregister_viewer_factory(&fancy_viewer_factory);
1149         fancy_prefs_done();
1150         return FALSE;
1151 }
1152
1153 const gchar *plugin_name(void)
1154 {
1155         /* i18n: 'Fancy' here is name of the plugin, not the english word. */
1156         return _("Fancy HTML Viewer");
1157 }
1158
1159 const gchar *plugin_desc(void)
1160 {
1161         return g_strdup_printf(_("This plugin renders HTML mail using the WebKit "
1162                                "%d.%d.%d library.\nBy default all remote content is "
1163                                "blocked. Options "
1164                                "can be found in /Configuration/Preferences/Plugins/Fancy"),
1165                                WEBKIT_MAJOR_VERSION, WEBKIT_MINOR_VERSION,
1166                                WEBKIT_MICRO_VERSION);
1167 }
1168
1169 const gchar *plugin_type(void)
1170 {
1171         return "GTK2";
1172 }
1173
1174 const gchar *plugin_licence(void)
1175 {
1176         return "GPL3";
1177 }
1178
1179 const gchar *plugin_version(void)
1180 {
1181         return VERSION;
1182 }
1183
1184 struct PluginFeature *plugin_provides(void)
1185 {
1186         static struct PluginFeature features[] = {
1187                                         {PLUGIN_MIMEVIEWER, "text/html"},
1188                                 {PLUGIN_NOTHING, NULL}
1189         };
1190         return features;
1191 }