f9ef4381dcbe2ecf75f20cc21eb69dc94b17688f
[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         g_object_set(viewer->settings, "auto-load-images",
385                 fancy_prefs.auto_load_images || viewer->override_prefs_images,
386                 NULL);
387         g_object_set(viewer->settings, "enable-scripts",
388                 fancy_prefs.enable_scripts || viewer->override_prefs_scripts,
389                 NULL);
390         g_object_set(viewer->settings, "enable-plugins",
391                 fancy_prefs.enable_plugins || viewer->override_prefs_plugins,
392                 NULL);
393         g_object_set(viewer->settings, "enable-java-applet",
394                 fancy_prefs.enable_java || viewer->override_prefs_java,
395                 NULL);
396
397         webkit_web_view_set_settings(viewer->view, viewer->settings);
398
399         if (fancy_prefs.block_extern_content && !viewer->override_prefs_block_extern_content) {
400                 if (viewer->load_page) {
401                         gchar *message = g_strdup_printf(_("Navigation to %s blocked"), viewer->cur_link);
402                         fancy_show_notice(viewer, message);
403                         g_free(message);
404                         return WEBKIT_NAVIGATION_RESPONSE_IGNORE; 
405                 }
406         } 
407
408         if (viewer->cur_link) {
409                 if (!fancy_prefs.open_external && !viewer->override_prefs_external)
410                         return fancy_open_uri(viewer, FALSE);
411                 else 
412                         return fancy_open_uri(viewer, TRUE);
413         }
414
415         viewer->load_page = TRUE;
416         return WEBKIT_NAVIGATION_RESPONSE_ACCEPT;
417 }
418 #if WEBKIT_CHECK_VERSION (1,1,14)
419 static void resource_request_starting_cb(WebKitWebView                  *view, 
420                                                                                  WebKitWebFrame                 *frame,
421                                                                                  WebKitWebResource              *resource,
422                                                                                  WebKitNetworkRequest   *request,
423                                                                                  WebKitNetworkResponse  *response,
424                                                                                  FancyViewer                    *viewer)
425 {
426         const gchar *uri = webkit_network_request_get_uri(request);
427         gchar *filename;
428         gchar *image;
429         gint err;
430         MimeInfo *partinfo = viewer->to_load;
431         
432         filename = viewer->filename;
433         if ((!g_ascii_strncasecmp(uri, "cid:", 4)) || (!g_ascii_strncasecmp(uri, "mid:", 4))) {
434                 image = g_strconcat("<", uri + 4, ">", NULL);
435                 while ((partinfo = procmime_mimeinfo_next(partinfo)) != NULL) {
436                         if (!g_ascii_strcasecmp(image, partinfo->id)) {
437                                 filename = procmime_get_tmp_file_name(partinfo);
438                                 if (!filename) {
439                                         g_free(image);
440                                         return;
441                                 }
442                                 if ((err = procmime_get_part(filename, partinfo)) < 0)
443                                         alertpanel_error(_("Couldn't save the part of multipart message: %s"),
444                                                                                 strerror(-err));
445                                 gchar *file_uri = g_strconcat("file://", filename, NULL);
446                                 webkit_network_request_set_uri(request, file_uri);
447                                 g_free(file_uri);
448                                 g_free(filename);
449                                 break;
450                         }
451                 }
452                 g_free(image);
453         }
454 }
455 #endif
456 static gboolean fancy_text_search(MimeViewer *_viewer, gboolean backward, 
457                                                                   const gchar *str, gboolean case_sens)
458 {
459         return webkit_web_view_search_text(((FancyViewer*)_viewer)->view, str,
460                                                                            case_sens, !backward, TRUE);
461 }
462
463 static void fancy_auto_load_images_activated(GtkMenuItem *item, FancyViewer *viewer) {
464         viewer->load_page = FALSE;
465         viewer->override_prefs_images = TRUE;
466         webkit_web_view_reload (viewer->view);
467 }
468 static void fancy_block_extern_content_activated(GtkMenuItem *item, FancyViewer *viewer) {
469         viewer->override_prefs_block_extern_content = TRUE;
470         gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
471 }
472 static void fancy_enable_scripts_activated(GtkMenuItem *item, FancyViewer *viewer) {
473         viewer->load_page = FALSE;
474         viewer->override_prefs_scripts = TRUE;
475         gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
476         webkit_web_view_reload (viewer->view);
477 }
478 static void fancy_enable_plugins_activated(GtkMenuItem *item, FancyViewer *viewer) {
479         viewer->load_page = FALSE;
480         viewer->override_prefs_plugins = TRUE;
481         gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
482         webkit_web_view_reload (viewer->view);
483 }
484 static void fancy_enable_java_activated(GtkMenuItem *item, FancyViewer *viewer) {
485         viewer->load_page = FALSE;
486         viewer->override_prefs_java = TRUE;
487         gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
488         webkit_web_view_reload (viewer->view);
489 }
490 static void fancy_open_external_activated(GtkMenuItem *item, FancyViewer *viewer) {
491         viewer->override_prefs_external = TRUE;
492         gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
493 }
494
495 static gboolean fancy_prefs_cb(GtkWidget *widget, GdkEventButton *ev, FancyViewer *viewer)
496 {
497         if ((ev->button == 1) && (ev->type == GDK_BUTTON_PRESS)) {
498                 /* Set sensitivity according to preferences and overrides */
499                 if (fancy_prefs.auto_load_images)
500                         gtk_widget_set_sensitive(viewer->auto_load_images, FALSE);
501                 else { 
502                         if (viewer->override_prefs_images)
503                                 gtk_widget_set_sensitive(viewer->auto_load_images, FALSE);
504                         else
505                                 gtk_widget_set_sensitive(viewer->auto_load_images, TRUE);
506                 }
507                 if (fancy_prefs.enable_scripts)
508                         gtk_widget_set_sensitive(viewer->enable_scripts, FALSE);
509                 else {
510                         if (viewer->override_prefs_scripts)
511                                 gtk_widget_set_sensitive(viewer->enable_scripts, FALSE);
512                         else
513                                 gtk_widget_set_sensitive(viewer->enable_scripts, TRUE);
514                 }
515
516                 if (fancy_prefs.enable_plugins)
517                         gtk_widget_set_sensitive(viewer->enable_plugins, FALSE);
518                 else {
519                         if (viewer->override_prefs_plugins) 
520                                 gtk_widget_set_sensitive(viewer->enable_plugins, FALSE);
521                         else
522                                 gtk_widget_set_sensitive(viewer->enable_plugins, TRUE);
523                 }
524                 if (fancy_prefs.enable_java)
525                         gtk_widget_set_sensitive(viewer->enable_java, FALSE);
526                 else {
527                         if (viewer->override_prefs_java) 
528                                 gtk_widget_set_sensitive(viewer->enable_java, FALSE);
529                         else
530                                 gtk_widget_set_sensitive(viewer->enable_java, TRUE);
531                 }
532                 if (!fancy_prefs.block_extern_content)
533                         gtk_widget_set_sensitive(viewer->block_extern_content, FALSE);
534                 else {
535                         if (viewer->override_prefs_block_extern_content)
536                                 gtk_widget_set_sensitive(viewer->block_extern_content, FALSE);
537                         else
538                                 gtk_widget_set_sensitive(viewer->block_extern_content, TRUE);
539                 }
540                 if (fancy_prefs.open_external)
541                         gtk_widget_set_sensitive(viewer->open_external, FALSE);
542                 else {
543                         if (viewer->override_prefs_external)
544                                 gtk_widget_set_sensitive(viewer->open_external, FALSE);
545                         else
546                                 gtk_widget_set_sensitive(viewer->open_external, TRUE);
547                 }
548
549                 gtk_menu_popup(GTK_MENU(viewer->fancy_prefs_menu), NULL, NULL, NULL, NULL,
550                                            ev->button, ev->time);
551                 return TRUE;
552         }
553         return FALSE;
554 }
555
556 static void fancy_create_popup_prefs_menu(FancyViewer *viewer) {
557         GtkWidget *auto_load_images;
558         GtkWidget *item_image;
559         GtkWidget *block_extern_content;
560         GtkWidget *enable_scripts;
561         GtkWidget *enable_plugins;
562         GtkWidget *enable_java;
563         GtkWidget *open_external;
564
565         auto_load_images = gtk_image_menu_item_new_with_label(_("Load images"));
566         item_image = gtk_image_new_from_stock(GTK_STOCK_EXECUTE, GTK_ICON_SIZE_MENU);
567         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(auto_load_images), item_image);
568
569         block_extern_content = gtk_image_menu_item_new_with_label(_("Unblock external content"));
570         item_image = gtk_image_new_from_stock(GTK_STOCK_EXECUTE, GTK_ICON_SIZE_MENU);
571         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(block_extern_content), item_image);
572
573         enable_scripts = gtk_image_menu_item_new_with_label(_("Enable Javascript"));
574         item_image = gtk_image_new_from_stock(GTK_STOCK_EXECUTE, GTK_ICON_SIZE_MENU);
575         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(enable_scripts), item_image);
576         item_image = gtk_image_new_from_stock(GTK_STOCK_EXECUTE, GTK_ICON_SIZE_MENU);
577         enable_plugins = gtk_image_menu_item_new_with_label(_("Enable Plugins"));
578         item_image = gtk_image_new_from_stock(GTK_STOCK_EXECUTE, GTK_ICON_SIZE_MENU);
579         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(enable_plugins), item_image);
580         enable_java = gtk_image_menu_item_new_with_label(_("Enable Java"));
581         item_image = gtk_image_new_from_stock(GTK_STOCK_EXECUTE, GTK_ICON_SIZE_MENU);
582         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(enable_java), item_image);
583         open_external = gtk_image_menu_item_new_with_label(_("Open links with external browser"));
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(open_external), item_image);
586
587         g_signal_connect(G_OBJECT(auto_load_images), "activate",
588                                          G_CALLBACK (fancy_auto_load_images_activated), viewer);
589         g_signal_connect(G_OBJECT(block_extern_content), "activate",
590                                          G_CALLBACK (fancy_block_extern_content_activated), viewer);
591         g_signal_connect(G_OBJECT(enable_scripts), "activate",
592                                          G_CALLBACK (fancy_enable_scripts_activated), viewer);
593         g_signal_connect(G_OBJECT(enable_plugins), "activate",
594                                          G_CALLBACK (fancy_enable_plugins_activated), viewer);
595         g_signal_connect(G_OBJECT(enable_java), "activate",
596                                          G_CALLBACK (fancy_enable_java_activated), viewer);
597         g_signal_connect(G_OBJECT(open_external), "activate",
598                                          G_CALLBACK (fancy_open_external_activated), viewer);
599
600         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), auto_load_images);
601         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), block_extern_content);
602         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_scripts);
603         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_plugins);
604         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), enable_java);
605         gtk_menu_shell_append(GTK_MENU_SHELL(viewer->fancy_prefs_menu), open_external);
606
607         gtk_menu_attach_to_widget(GTK_MENU(viewer->fancy_prefs_menu), viewer->ev_fancy_prefs, NULL);
608         gtk_widget_show_all(viewer->fancy_prefs_menu);
609
610         viewer->auto_load_images = auto_load_images;
611         viewer->enable_scripts = enable_scripts;
612         viewer->enable_plugins = enable_plugins;
613         viewer->enable_java = enable_java;
614         viewer->block_extern_content = block_extern_content;
615         viewer->open_external = open_external;
616
617 }
618
619 static gboolean fancy_scroll_page(MimeViewer *_viewer, gboolean up)
620 {
621         FancyViewer *viewer = (FancyViewer *)_viewer;
622         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
623                                                   GTK_SCROLLED_WINDOW(viewer->scrollwin));
624
625         if (viewer->view == NULL)
626                 return FALSE;
627
628         return gtkutils_scroll_page(GTK_WIDGET(viewer->view), vadj, up);
629 }
630
631 static void fancy_scroll_one_line(MimeViewer *_viewer, gboolean up)
632 {
633         FancyViewer *viewer = (FancyViewer *)_viewer;
634         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
635                                                   GTK_SCROLLED_WINDOW(viewer->scrollwin));
636
637         if (viewer->view == NULL)
638                 return;
639
640         gtkutils_scroll_one_line(GTK_WIDGET(viewer->view), vadj, up);
641 }
642
643 static void load_start_cb(WebKitWebView *view, gint progress, 
644                           FancyViewer *viewer)
645 {
646         gtk_widget_show(viewer->progress);
647         gtk_widget_show(viewer->ev_stop_loading);
648 }
649
650 static void load_finished_cb(WebKitWebView *view, gint progress, 
651                                                          FancyViewer *viewer)
652 {
653         gtk_widget_hide(viewer->progress);
654         gtk_widget_hide(viewer->ev_stop_loading);
655         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(viewer->progress), 
656                                                                  (gdouble) 0.0);
657         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(viewer->progress), "");
658 }
659
660 static void over_link_cb(WebKitWebView *view, const gchar *wtf, 
661                                                  const gchar *link, FancyViewer *viewer, void *wtfa)
662 {
663         gtk_label_set_text(GTK_LABEL(viewer->l_link), link);
664         if(link) {
665                 if (viewer->cur_link)
666                         g_free(viewer->cur_link);
667                 viewer->cur_link = g_strdup(link);
668         }
669 }
670
671 static void load_progress_cb(WebKitWebView *view, gint progress, 
672                                                          FancyViewer *viewer)
673 {
674         gdouble pbar;
675         gchar *label = g_strdup_printf("%d%% Loading...", progress);
676         pbar = (gdouble) progress / 100;
677         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(viewer->progress), pbar);
678         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(viewer->progress), 
679                                                          (const gchar*)label);
680         g_free(label);
681 }
682 static void stop_loading_cb(GtkWidget *widget, GdkEvent *ev, 
683                                                         FancyViewer *viewer)
684 {
685         webkit_web_view_stop_loading (viewer->view);
686         gtk_widget_hide(viewer->progress);
687         gtk_widget_hide(viewer->ev_stop_loading);
688 }
689
690 static void search_the_web_cb(GtkWidget *widget, FancyViewer *viewer)
691 {
692         debug_print("Clicked on Search on Web\n");
693         if (webkit_web_view_has_selection(viewer->view)) {
694                 gchar *search;
695 #if WEBKIT_CHECK_VERSION(1,5,1)
696                 viewer->doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(viewer->view));
697                 viewer->window = webkit_dom_document_get_default_view (viewer->doc);
698                 viewer->selection = webkit_dom_dom_window_get_selection (viewer->window);
699                 viewer->range = webkit_dom_dom_selection_get_range_at(viewer->selection, 0, NULL);
700                 gchar *tmp = webkit_dom_range_get_text (viewer->range);
701 #else
702                 gchar *tmp = webkit_web_view_get_selected_text(viewer->view);
703 #endif
704                 search = g_strconcat(GOOGLE_SEARCH, tmp, NULL);
705 #if WEBKIT_CHECK_VERSION(1,1,1)
706                 webkit_web_view_load_uri(viewer->view, search);
707 #else
708                 webkit_web_view_open(viewer->view, search);
709 #endif
710                 g_free(search);
711                 g_free(tmp);
712         }
713 }
714
715 static void open_in_browser_cb(GtkWidget *widget, FancyViewer *viewer)
716 {
717         debug_print("link: %s\n", viewer->cur_link);
718         open_uri(viewer->cur_link, prefs_common_get_uri_cmd());
719 }
720
721 static size_t download_file_curl_write_cb(void *buffer, size_t size, 
722                                                                                   size_t nmemb, void *data)
723 {
724         FancyViewer *viewer = (FancyViewer *)data;
725         if (!viewer->stream) {
726                 viewer->stream = fopen(viewer->curlfile, "wb");
727                 if (!viewer->stream)
728                         return -1;
729         }
730         return fwrite(buffer, size, nmemb, viewer->stream);
731 }
732 static void *download_file_curl (void *data)
733 {
734         CURL *curl;
735         CURLcode res;
736         FancyViewer *viewer = (FancyViewer *)data;
737
738         curl_global_init(CURL_GLOBAL_DEFAULT);
739         curl = curl_easy_init();
740
741         if (curl) {
742                 curl_easy_setopt(curl, CURLOPT_URL, viewer->cur_link);
743                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_file_curl_write_cb);
744                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, viewer);
745                 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
746                 res = curl_easy_perform(curl);
747                 curl_easy_cleanup(curl);
748
749                 if (CURLE_OK != res)
750                         alertpanel_error(_("An error occurred: %d\n"), res);
751                 if (viewer->stream)
752                         fclose(viewer->stream);
753                 curl_global_cleanup();
754         }
755 #ifdef USE_PTHREAD
756         pthread_exit(NULL);
757 #else
758         return NULL;
759 #endif
760 }
761 static void download_file_cb(GtkWidget *widget, FancyViewer *viewer)
762 {
763 #ifdef USE_PTHREAD
764         pthread_t curljob;
765         gint result;
766 #endif
767         const gchar *link = (const gchar *)viewer->cur_link;
768         gchar *filename = g_utf8_strchr(link, -1, g_utf8_get_char("/"));
769         filename = g_strconcat(g_get_home_dir(), filename, NULL);
770         gchar *fname = filesel_select_file_save(_("Save as"), filename);
771
772         if (viewer->curlfile) viewer->curlfile = NULL;
773         if (viewer->stream) viewer->stream = NULL;
774         viewer->curlfile = (const gchar *)g_strdup(fname);
775         g_free(filename);
776         g_free(fname);
777
778         if (!viewer->curlfile) return;
779     
780 #ifdef USE_PTHREAD
781         result = pthread_create(&curljob, NULL, download_file_curl, (void *)viewer);
782         if (result)   
783                 alertpanel_error("ERROR; return code from pthread_create() is %d\n", result);
784 #else
785         download_file_curl((void *)viewer);
786 #endif
787 }
788
789 static void open_image_cb(GtkWidget *widget, FancyViewer *viewer)
790 {
791         debug_print("Not Yet Implemented\n");
792 }
793
794 static void copy_image_cb(GtkWidget *widget, FancyViewer *viewer)
795 {
796         debug_print("Not Yet Implemented\n");
797 }
798 static void import_feed_cb(GtkWidget *widget, FancyViewer *viewer)
799 {
800         if (!folder_subscribe(viewer->cur_link))
801                 alertpanel_error(_("%s is a malformed or not supported feed"), viewer->cur_link);
802 }
803 static void viewer_menu_handler(GtkWidget *menuitem, FancyViewer *viewer)
804 {
805         const gchar *g_name = gtk_widget_get_name(GTK_WIDGET(menuitem));
806         if (!g_ascii_strcasecmp(g_name, "GtkImageMenuItem")) {
807                 
808                 GtkWidget *menul = gtk_bin_get_child(GTK_BIN(menuitem));
809         
810         if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)), 
811                                                                 "Search the Web")) {
812                         gtk_label_set_text(GTK_LABEL(menul), _("Search the Web"));
813                 
814                         if (fancy_prefs.block_extern_content) {
815                                 gtk_widget_set_sensitive(GTK_WIDGET(menul), FALSE);
816                         } else {
817                                 viewer->cur_link = NULL;
818                                 GtkImageMenuItem *m_search = GTK_IMAGE_MENU_ITEM(menuitem);
819                                 g_signal_connect(G_OBJECT(m_search), "activate",
820                                                                  G_CALLBACK(search_the_web_cb),
821                                                                  (gpointer *) viewer);
822                         }
823                 }
824
825                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)), 
826                                                                 "Open Link in New Window" )) {
827                 
828                         gtk_label_set_text(GTK_LABEL(menul), _("Open in Browser"));
829                
830                         GtkImageMenuItem *m_new = GTK_IMAGE_MENU_ITEM(menuitem);
831                         g_signal_connect(G_OBJECT(m_new), "activate",
832                                                          G_CALLBACK(open_in_browser_cb),
833                                                          (gpointer *) viewer);
834                 }
835             
836                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)), 
837                                                                 "Open Image in New Window" )) {
838                         gtk_label_set_text(GTK_LABEL(menul), _("Open Image"));
839                         GtkImageMenuItem *m_image = GTK_IMAGE_MENU_ITEM(menuitem);
840                         g_signal_connect(G_OBJECT(m_image), "activate",
841                                                          G_CALLBACK(open_image_cb),
842                                                          (gpointer *) viewer);
843                 }
844
845                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)), 
846                                                                 "Copy Link Location" )) {
847                         gtk_label_set_text(GTK_LABEL(menul), _("Copy Link"));
848                 }
849     
850         if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)), 
851                                                                 "Download Linked File" )) {
852                 
853                         gtk_label_set_text(GTK_LABEL(menul), _("Download Link"));
854
855                         GtkImageMenuItem *m_dlink = GTK_IMAGE_MENU_ITEM(menuitem);
856                         if (!fancy_prefs.block_extern_content) {
857                                 gtk_widget_set_sensitive(GTK_WIDGET(menul), TRUE);
858                         }
859                         else {
860                                 if (viewer->override_prefs_block_extern_content) {
861                                         gtk_widget_set_sensitive(GTK_WIDGET(menul), TRUE);
862                                 }
863                                 else {
864                                         gtk_widget_set_sensitive(GTK_WIDGET(menul), FALSE);
865                                 }
866                         }
867                         g_signal_connect(G_OBJECT(m_dlink), "activate",
868                                                          G_CALLBACK(download_file_cb),
869                                                          (gpointer *) viewer);
870                 }
871
872                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)), 
873                                                                 "Save Image As" )) {
874
875                         gtk_label_set_text(GTK_LABEL(menul), _("Save Image As"));
876
877                         GtkImageMenuItem *m_simage = GTK_IMAGE_MENU_ITEM(menuitem);
878                         if (!fancy_prefs.block_extern_content) {
879                                 gtk_widget_set_sensitive(GTK_WIDGET(menul), TRUE);
880                         }
881                         else {
882                                 if (viewer->override_prefs_block_extern_content) {
883                                         gtk_widget_set_sensitive(GTK_WIDGET(menul), TRUE);
884                                 }
885                                 else {
886                                         gtk_widget_set_sensitive(GTK_WIDGET(menul), FALSE);
887                 }
888                         }
889                         g_signal_connect(G_OBJECT(m_simage), "activate", 
890                                                          G_CALLBACK(download_file_cb), (gpointer *) viewer);
891                 }
892
893                 if (!g_ascii_strcasecmp(gtk_label_get_text(GTK_LABEL(menul)), 
894                                                                 "Copy Image" )) {
895                 
896                         gtk_label_set_text(GTK_LABEL(menul), _("Copy Image"));
897                 
898                         GtkImageMenuItem *m_cimage = GTK_IMAGE_MENU_ITEM(menuitem);
899                         g_signal_connect(G_OBJECT(m_cimage), "activate",
900                                                          G_CALLBACK(copy_image_cb),
901                                                          (gpointer *) viewer);
902                 }
903
904         }
905 }
906
907 static gboolean populate_popup_cb (WebKitWebView *view, GtkWidget *menu, 
908                                                                    FancyViewer *viewer)
909 {
910         Plugin *plugin = plugin_get_loaded_by_name("RSSyl");
911         gtk_container_foreach(GTK_CONTAINER(menu), 
912                                                   (GtkCallback)viewer_menu_handler, 
913                                                   viewer);
914         if (plugin) {
915                 GtkWidget *rssyl = gtk_image_menu_item_new_with_label(_("Import feed"));
916                 GtkWidget *img = gtk_image_new_from_stock(GTK_STOCK_ADD, 
917                                                                                                   GTK_ICON_SIZE_MENU);
918                 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(rssyl), img);
919                 gtk_widget_show(GTK_WIDGET(rssyl));
920                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), rssyl);    
921                 g_signal_connect(G_OBJECT(rssyl), "activate",
922                                                  G_CALLBACK(import_feed_cb),
923                                                  (gpointer *) viewer);
924         }
925         return TRUE;
926 }
927
928 static gint keypress_events_cb (GtkWidget *widget, GdkEventKey *event,
929                                                                 FancyViewer *viewer)
930 {
931         if (event->state == CTRL_KEY) {
932                 switch (event->keyval) {
933                 case GDK_plus:
934                         zoom_in_cb(viewer->ev_zoom_in, NULL, viewer);
935                         break;
936                 case GDK_period:
937                         zoom_100_cb(viewer->ev_zoom_100, NULL, viewer);
938                         break;
939                 case GDK_minus:
940                         zoom_out_cb(viewer->ev_zoom_out, NULL, viewer);
941                         break;
942                 }
943         }
944         return FALSE;
945 }
946 #if !WEBKIT_CHECK_VERSION (1,1,12)
947 static gboolean release_button_cb (WebKitWebView *view, GdkEvent *ev, 
948                                                                    gpointer data)
949 {
950         /* Make the copy/paste works as usual  */
951         if (webkit_web_view_can_copy_clipboard(view)) {
952                 GtkClipboard *wv_clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
953                 const gchar *sel_text;
954                 sel_text = (const *gchar)webkit_web_view_get_selected_text(viewer->view);
955                 gtk_clipboard_set_text(wv_clipboard, sel_text, -1);
956         }
957         return FALSE;
958 }
959 #endif
960 static void zoom_100_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
961 {
962         gtk_widget_grab_focus(widget);
963         webkit_web_view_set_zoom_level(viewer->view, 1);
964 }
965
966 static void zoom_in_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
967 {
968         gtk_widget_grab_focus(widget);
969         webkit_web_view_zoom_in(viewer->view);
970 }
971 static void zoom_out_cb(GtkWidget *widget, GdkEvent *ev, FancyViewer *viewer)
972 {
973         gtk_widget_grab_focus(widget);
974         webkit_web_view_zoom_out(viewer->view);
975 }
976
977 static MimeViewer *fancy_viewer_create(void)
978 {
979         FancyViewer    *viewer;
980         GtkWidget      *hbox;
981     
982         debug_print("fancy_viewer_create\n");
983
984         viewer = g_new0(FancyViewer, 1);
985         viewer->mimeviewer.factory = &fancy_viewer_factory;
986         viewer->mimeviewer.get_widget = fancy_get_widget;
987         viewer->mimeviewer.get_selection = fancy_get_selection;
988         viewer->mimeviewer.show_mimepart = fancy_show_mimepart;
989 #if GTK_CHECK_VERSION(2,10,0) && HAVE_GTKPRINTUNIX
990         viewer->mimeviewer.print = fancy_print;
991 #endif
992         viewer->mimeviewer.clear_viewer = fancy_clear_viewer;
993         viewer->mimeviewer.destroy_viewer = fancy_destroy_viewer;
994         viewer->mimeviewer.text_search = fancy_text_search;
995         viewer->mimeviewer.scroll_page = fancy_scroll_page;
996         viewer->mimeviewer.scroll_one_line = fancy_scroll_one_line;
997         viewer->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
998
999 #ifdef HAVE_LIBSOUP_GNOME    
1000  /* Use GNOME proxy settings through libproxy */
1001         if (fancy_prefs.enable_gnome_proxy) {
1002                 SoupSession *session = webkit_get_default_session();
1003                 soup_session_add_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER_GNOME);
1004         }
1005 #endif
1006         
1007         if (fancy_prefs.enable_proxy) {
1008                 SoupSession *session = webkit_get_default_session();
1009                 SoupURI* pURI = soup_uri_new(fancy_prefs.proxy_str);
1010                 g_object_set(session, "proxy-uri", pURI, NULL);
1011         }
1012     
1013         viewer->settings = webkit_web_settings_new();    
1014         g_object_set(viewer->settings, "user-agent", "Fancy Viewer", NULL);
1015         viewer->scrollwin = gtk_scrolled_window_new(NULL, NULL);
1016         viewer->tag = -1;
1017         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewer->scrollwin), 
1018                                                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1019         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(viewer->scrollwin),
1020                                                                                 GTK_SHADOW_IN);
1021         gtk_container_add(GTK_CONTAINER(viewer->scrollwin),
1022                                           GTK_WIDGET(viewer->view));
1023
1024         viewer->vbox = gtk_vbox_new(FALSE, 0);
1025         hbox = gtk_hbox_new(FALSE, 0);
1026         viewer->progress = gtk_progress_bar_new();
1027         /* Zoom Widgets */
1028         viewer->zoom_100 = gtk_image_new_from_stock(GTK_STOCK_ZOOM_100,
1029                                                                                                 GTK_ICON_SIZE_MENU);
1030         viewer->zoom_in = gtk_image_new_from_stock(GTK_STOCK_ZOOM_IN, 
1031                                                                                            GTK_ICON_SIZE_MENU);
1032         viewer->zoom_out = gtk_image_new_from_stock(GTK_STOCK_ZOOM_OUT, 
1033                                                                                                 GTK_ICON_SIZE_MENU);
1034         viewer->stop_loading = gtk_image_new_from_stock(GTK_STOCK_CANCEL, 
1035                                                                                                         GTK_ICON_SIZE_MENU);
1036         /* Event Widgets for the Zoom Widgets  */
1037         viewer->ev_zoom_100 = gtk_event_box_new();
1038         viewer->ev_zoom_in = gtk_event_box_new();
1039         viewer->ev_zoom_out = gtk_event_box_new();
1040         viewer->ev_stop_loading = gtk_event_box_new();
1041    
1042         /* Link Label */
1043         viewer->l_link = gtk_label_new("");
1044
1045         /* Preferences Widgets to override preferences on the fly  */
1046         viewer->fancy_prefs = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES,
1047                                                                                                    GTK_ICON_SIZE_MENU);
1048         viewer->ev_fancy_prefs = gtk_event_box_new();
1049
1050         /* Popup Menu for preferences  */
1051         viewer->fancy_prefs_menu = gtk_menu_new();
1052         fancy_create_popup_prefs_menu(viewer);
1053
1054         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_100), FALSE);
1055         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_in), FALSE);
1056         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_zoom_out), FALSE);
1057         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_fancy_prefs), FALSE);
1058         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->ev_stop_loading), FALSE);
1059
1060         gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_100), viewer->zoom_100);
1061         gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_in), viewer->zoom_in);
1062         gtk_container_add(GTK_CONTAINER(viewer->ev_zoom_out), viewer->zoom_out);
1063         gtk_container_add(GTK_CONTAINER(viewer->ev_fancy_prefs), viewer->fancy_prefs);
1064         gtk_container_add(GTK_CONTAINER(viewer->ev_stop_loading), viewer->stop_loading);
1065
1066         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_100, FALSE, FALSE, 1);
1067         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_in, FALSE, FALSE, 2);
1068         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_zoom_out, FALSE, FALSE, 2);
1069         gtk_box_pack_start(GTK_BOX(hbox), viewer->ev_fancy_prefs, FALSE, FALSE, 2);
1070         gtk_box_pack_start(GTK_BOX(hbox), viewer->l_link, FALSE, FALSE, 8);
1071         gtk_box_pack_end(GTK_BOX(hbox), viewer->progress, FALSE, FALSE, 0);
1072         gtk_box_pack_end(GTK_BOX(hbox), viewer->ev_stop_loading, FALSE, FALSE, 0);
1073
1074         gtk_box_pack_start(GTK_BOX(viewer->vbox), viewer->scrollwin, TRUE, TRUE, 
1075                        1);
1076         gtk_box_pack_start(GTK_BOX(viewer->vbox), hbox, FALSE, FALSE, 0);
1077
1078         gtk_widget_show(viewer->ev_zoom_100);
1079         gtk_widget_show(viewer->ev_zoom_in);
1080         gtk_widget_show(viewer->ev_zoom_out);
1081         gtk_widget_show(viewer->ev_fancy_prefs);
1082
1083         gtk_widget_show(viewer->scrollwin);
1084         gtk_widget_show(viewer->zoom_100);
1085         gtk_widget_show(viewer->zoom_in);
1086         gtk_widget_show(viewer->zoom_out);
1087         gtk_widget_show(viewer->fancy_prefs);
1088         gtk_widget_show(viewer->stop_loading);
1089
1090         gtk_widget_show(viewer->l_link);
1091         gtk_widget_show(viewer->vbox);
1092         gtk_widget_show(hbox);
1093         gtk_widget_show(GTK_WIDGET(viewer->view));
1094    
1095         g_signal_connect(G_OBJECT(viewer->view), "load-started", 
1096                                          G_CALLBACK(load_start_cb), viewer);
1097         g_signal_connect(G_OBJECT(viewer->view), "load-finished", 
1098                                          G_CALLBACK(load_finished_cb), viewer);
1099         g_signal_connect(G_OBJECT(viewer->view), "hovering-over-link", 
1100                                          G_CALLBACK(over_link_cb), viewer);
1101         g_signal_connect(G_OBJECT(viewer->view), "load-progress-changed", 
1102                                          G_CALLBACK(load_progress_cb), viewer);
1103         g_signal_connect(G_OBJECT(viewer->view), "navigation-requested",
1104                                          G_CALLBACK(navigation_requested_cb), viewer);
1105 #if WEBKIT_CHECK_VERSION (1,1,14)
1106         g_signal_connect(G_OBJECT(viewer->view), "resource-request-starting",
1107                         G_CALLBACK(resource_request_starting_cb), viewer);
1108 #endif
1109         g_signal_connect(G_OBJECT(viewer->view), "populate-popup",
1110                                          G_CALLBACK(populate_popup_cb), viewer);
1111 #if !WEBKIT_CHECK_VERSION (1,1,12)
1112         g_signal_connect(G_OBJECT(viewer->view), "button-release-event",
1113                                          G_CALLBACK(release_button_cb), viewer);
1114 #endif
1115         g_signal_connect(G_OBJECT(viewer->ev_zoom_100), "button-press-event",
1116                                          G_CALLBACK(zoom_100_cb), (gpointer*)viewer);
1117         g_signal_connect(G_OBJECT(viewer->ev_zoom_in), "button-press-event",
1118                                          G_CALLBACK(zoom_in_cb), (gpointer *)viewer);
1119         g_signal_connect(G_OBJECT(viewer->ev_zoom_out), "button-press-event",
1120                                          G_CALLBACK(zoom_out_cb), (gpointer *)viewer);
1121         g_signal_connect(G_OBJECT(viewer->ev_fancy_prefs), "button-press-event",
1122                                          G_CALLBACK(fancy_prefs_cb), (gpointer *)viewer);
1123         g_signal_connect(G_OBJECT(viewer->ev_stop_loading), "button-press-event",
1124                                          G_CALLBACK(stop_loading_cb), viewer);
1125         g_signal_connect(G_OBJECT(viewer->view), "key_press_event",
1126                                          G_CALLBACK(keypress_events_cb), viewer);
1127
1128         viewer->filename = NULL;
1129         return (MimeViewer *) viewer;
1130 }
1131
1132 static gchar *content_types[] = {"text/html", NULL};
1133
1134 static MimeViewerFactory fancy_viewer_factory =
1135 {
1136         content_types,
1137         0,
1138         fancy_viewer_create,
1139 };
1140
1141 gint plugin_init(gchar **error)
1142 {
1143         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
1144                                                           VERSION_NUMERIC, _("Fancy"), error))
1145                 return -1;
1146         gchar *directory = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1147                                 "fancy", NULL);
1148         if (!is_dir_exist(directory))
1149                 make_dir (directory);
1150         g_free(directory);
1151
1152         fancy_prefs_init();
1153     
1154         mimeview_register_viewer_factory(&fancy_viewer_factory);
1155
1156         return 0;
1157 }
1158
1159 gboolean plugin_done(void)
1160 {
1161         mimeview_unregister_viewer_factory(&fancy_viewer_factory);
1162         fancy_prefs_done();
1163         return FALSE;
1164 }
1165
1166 const gchar *plugin_name(void)
1167 {
1168         /* i18n: 'Fancy' here is name of the plugin, not the english word. */
1169         return _("Fancy HTML Viewer");
1170 }
1171
1172 const gchar *plugin_desc(void)
1173 {
1174         return g_strdup_printf(_("This plugin renders HTML mail using the WebKit " 
1175                                                    "%d.%d.%d library.\nBy default all remote content is "
1176                                                    "blocked and images are not automatically loaded. Options "
1177                                                    "can be found in /Configuration/Preferences/Plugins/Fancy"), 
1178                                                    WEBKIT_MAJOR_VERSION, WEBKIT_MINOR_VERSION, 
1179                                                    WEBKIT_MICRO_VERSION);
1180 }
1181
1182 const gchar *plugin_type(void)
1183 {
1184         return "GTK2";
1185 }
1186
1187 const gchar *plugin_licence(void)
1188 {
1189         return "GPL3";
1190 }
1191
1192 const gchar *plugin_version(void)
1193 {
1194         return VERSION;
1195 }
1196
1197 struct PluginFeature *plugin_provides(void)
1198 {
1199         static struct PluginFeature features[] = {
1200                                         {PLUGIN_MIMEVIEWER, "text/html"},
1201                                 {PLUGIN_NOTHING, NULL}
1202         };
1203         return features;
1204 }