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