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