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