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