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