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