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