Fix layout
[claws.git] / src / plugins / fancy / fancy_prefs.c
1 /*
2  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 1999-2013 the Claws Mail Team
4  * == Fancy Plugin ==
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27
28 #include "defs.h"
29 #include "version.h"
30 #include "claws.h"
31 #include "plugin.h"
32
33
34 #include "gtkutils.h"
35 #include "utils.h"
36 #include "prefs.h"
37 #include "prefs_common.h"
38 #include "prefs_gtk.h"
39 #include "prefswindow.h"
40 #include "combobox.h"
41 #include "addressbook.h"
42
43 #include "fancy_prefs.h"
44
45 #define PREFS_BLOCK_NAME "fancy"
46
47 FancyPrefs fancy_prefs;
48
49 static void prefs_set_proxy_entry_sens(GtkWidget *button, GtkEntry *entry_str);
50
51 #ifdef HAVE_LIBSOUP_GNOME
52 static void prefs_disable_fancy_proxy(GtkWidget *checkbox, GtkWidget *block);
53 #endif
54 typedef struct _FancyPrefsPage FancyPrefsPage;
55
56 struct _FancyPrefsPage {
57         PrefsPage page;
58         GtkWidget *enable_images;
59         GtkWidget *enable_remote_content;
60         GtkWidget *enable_scripts;
61         GtkWidget *enable_plugins;
62         GtkWidget *enable_java;
63         GtkWidget *open_external;
64 #ifdef HAVE_LIBSOUP_GNOME
65         GtkWidget *gnome_proxy_checkbox;
66 #endif
67         GtkWidget *proxy_checkbox;
68         GtkWidget *proxy_str;
69 };
70
71 static PrefParam param[] = {
72                 {"enable_images", "TRUE", &fancy_prefs.enable_images, P_BOOL,
73                 NULL, NULL, NULL},
74                 {"enable_remote_content", "FALSE", &fancy_prefs.enable_remote_content, P_BOOL,
75                 NULL, NULL, NULL},
76                 {"enable_scripts", "FALSE", &fancy_prefs.enable_scripts, P_BOOL,
77                 NULL, NULL, NULL},
78                 {"enable_plugins", "FALSE", &fancy_prefs.enable_plugins, P_BOOL,
79                 NULL, NULL, NULL},
80                 {"open_external", "TRUE", &fancy_prefs.open_external, P_BOOL,
81                 NULL, NULL, NULL},
82                 {"zoom_level", "100", &fancy_prefs.zoom_level, P_INT,
83                 NULL, NULL, NULL},
84                 {"enable_java", "FALSE", &fancy_prefs.enable_java, P_BOOL,
85                 NULL, NULL, NULL},
86 #ifdef HAVE_LIBSOUP_GNOME
87                 {"enable_gnome_proxy","FALSE", &fancy_prefs.enable_gnome_proxy, P_BOOL,
88                 NULL, NULL, NULL},
89 #endif
90                 {"enable_proxy", "FALSE", &fancy_prefs.enable_proxy, P_BOOL,
91                 NULL, NULL, NULL},
92                 {"proxy_server", "http://SERVERNAME:PORT", &fancy_prefs.proxy_str, P_STRING,
93                 NULL, NULL, NULL},
94                 {0,0,0,0}
95 };
96
97 static FancyPrefsPage fancy_prefs_page;
98
99 static void create_fancy_prefs_page     (PrefsPage *page, GtkWindow *window, gpointer   data);
100 static void destroy_fancy_prefs_page    (PrefsPage *page);
101 static void save_fancy_prefs_page       (PrefsPage *page);
102 static void save_fancy_prefs            (PrefsPage *page);
103
104 void fancy_prefs_init(void)
105 {
106         static gchar *path[3];
107         gchar *rcpath;
108
109         path[0] = _("Plugins");
110         path[1] = "Fancy";
111         path[2] = NULL;
112
113         prefs_set_default(param);
114         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
115         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
116         g_free(rcpath);
117         
118         fancy_prefs_page.page.path = path;
119         fancy_prefs_page.page.create_widget = create_fancy_prefs_page;
120         fancy_prefs_page.page.destroy_widget = destroy_fancy_prefs_page;
121         fancy_prefs_page.page.save_page = save_fancy_prefs_page;
122         fancy_prefs_page.page.weight = 30.0;
123         prefs_gtk_register_page((PrefsPage *) &fancy_prefs_page);
124 }
125
126 void fancy_prefs_done(void)
127 {
128         save_fancy_prefs((PrefsPage *) &fancy_prefs_page);
129         prefs_gtk_unregister_page((PrefsPage *) &fancy_prefs_page);
130 }
131
132 static void remote_content_set_labels_cb(GtkWidget *button, FancyPrefsPage *prefs_page)
133 {
134         GtkTreeModel *model;
135         GtkTreeIter iter;
136         gboolean remote_enabled = gtk_toggle_button_get_active(
137                                         GTK_TOGGLE_BUTTON(prefs_page->enable_remote_content));
138
139         /* Enable images */
140         gtk_button_set_label(GTK_BUTTON(prefs_page->enable_images),
141                              remote_enabled ? _("Display images")
142                                             : _("Display embedded images"));
143
144         /* Enable Javascript */
145         gtk_button_set_label(GTK_BUTTON(prefs_page->enable_scripts),
146                              remote_enabled ? _("Execute javascript")
147                                             : _("Execute embedded javascript"));
148
149         /* Enable java */
150         gtk_button_set_label(GTK_BUTTON(prefs_page->enable_java),
151                              remote_enabled ? _("Execute Java applets")
152                                             : _("Execute embedded Java applets"));
153
154         /* Enable plugins */
155         gtk_button_set_label(GTK_BUTTON(prefs_page->enable_plugins),
156                              remote_enabled ? _("Render objects using plugins")
157                                             : _("Render embedded objects using plugins"));
158
159         /* Open links */
160         model = gtk_combo_box_get_model(GTK_COMBO_BOX(prefs_page->open_external));
161         if (gtk_tree_model_get_iter_first (model, &iter)) {
162                 if (remote_enabled)
163                         gtk_list_store_set(model, &iter, COMBOBOX_TEXT,
164                                            _("Open in viewer (remote content is enabled)"), -1);
165                 else
166                         gtk_list_store_set(model, &iter, COMBOBOX_TEXT,
167                                            _("Do nothing (remote content is disabled)"), -1);
168         }
169
170 }
171 static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window,
172                                                                         gpointer data)
173 {
174         FancyPrefsPage *prefs_page = (FancyPrefsPage *) page;
175
176         GtkWidget *vbox;
177 #ifdef HAVE_LIBSOUP_GNOME
178         GtkWidget *gnome_proxy_checkbox;
179 #endif
180         GtkWidget *proxy_checkbox;
181         GtkWidget *proxy_str;
182         GtkWidget *vbox_proxy;
183         GtkWidget *frame_proxy;
184
185         GtkWidget *frame_remote;
186         GtkWidget *vbox_remote;
187         GtkWidget *remote_label;
188         GtkWidget *enable_remote_content;
189         GtkWidget *enable_images;
190         GtkWidget *enable_scripts;
191         GtkWidget *enable_plugins;
192         GtkWidget *enable_java;
193
194         vbox = gtk_vbox_new(FALSE, 3);
195         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
196         gtk_widget_show(vbox);
197
198         GtkWidget *block = gtk_hbox_new(FALSE, FALSE);
199
200         vbox_proxy = gtkut_get_options_frame(vbox, &frame_proxy, _("Proxy"));
201 #ifdef HAVE_LIBSOUP_GNOME
202         gnome_proxy_checkbox = gtk_check_button_new_with_label(_("Use GNOME's proxy settings"));        
203         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gnome_proxy_checkbox),
204                                      fancy_prefs.enable_gnome_proxy);
205         if (fancy_prefs.enable_gnome_proxy)
206                 gtk_widget_set_sensitive(proxy_checkbox, FALSE);
207         gtk_box_pack_start(GTK_BOX(vbox_proxy), gnome_proxy_checkbox, FALSE, FALSE, 0);
208         gtk_widget_show(gnome_proxy_checkbox);
209         g_signal_connect(G_OBJECT(gnome_proxy_checkbox), "toggled",
210                          G_CALLBACK(prefs_disable_fancy_proxy), block);
211 #endif
212         proxy_checkbox = gtk_check_button_new_with_label(_("Use proxy:"));
213         proxy_str = gtk_entry_new();
214         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(proxy_checkbox),
215                                      fancy_prefs.enable_proxy);
216         prefs_set_proxy_entry_sens(proxy_checkbox, GTK_ENTRY(proxy_str));
217         g_signal_connect(G_OBJECT(proxy_checkbox), "toggled",
218                          G_CALLBACK(prefs_set_proxy_entry_sens), proxy_str);
219         pref_set_entry_from_pref(GTK_ENTRY(proxy_str), fancy_prefs.proxy_str);
220
221         gtk_box_pack_start(GTK_BOX(block), proxy_checkbox, FALSE, FALSE, 0);
222         gtk_box_pack_start(GTK_BOX(block), proxy_str, FALSE, TRUE, 0);
223         gtk_box_pack_start(GTK_BOX(vbox_proxy), block, FALSE, FALSE, 0);
224         gtk_widget_show_all(vbox_proxy);
225
226         vbox_remote = gtkut_get_options_frame(vbox, &frame_remote, _("Remote resources"));
227         remote_label = gtk_label_new(_("Loading remote resources can lead to some privacy issues.\n"
228                                         "When remote content loading is disabled, nothing will be requested\n"
229                                         "from the network. Rendering of images, scripts, plugin objects or\n"
230                                         "Java applets can still be enabled for content that is attached\n"
231                                         "in the email."));
232         gtk_misc_set_alignment(GTK_MISC(remote_label), 0, 0);
233         enable_remote_content = gtk_check_button_new_with_label(_("Enable loading of remote content"));
234         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_remote_content),
235                                      fancy_prefs.enable_remote_content);
236         gtk_box_pack_start (GTK_BOX (vbox_remote), remote_label, FALSE, FALSE, 0);
237         gtk_box_pack_start (GTK_BOX (vbox_remote), enable_remote_content, FALSE, FALSE, 0);
238         gtk_widget_show_all(vbox_remote);
239         
240         enable_images = gtk_check_button_new_with_label(("IMAGES"));
241         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_images),
242                                      fancy_prefs.enable_images);
243         gtk_box_pack_start(GTK_BOX(vbox), enable_images, FALSE, FALSE, 0);
244         gtk_widget_show(enable_images);
245
246         enable_scripts = gtk_check_button_new_with_label("SCRIPTS");
247         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_scripts),
248                                      fancy_prefs.enable_scripts);
249         gtk_box_pack_start(GTK_BOX(vbox), enable_scripts, FALSE, FALSE, 0);
250         gtk_widget_show(enable_scripts);
251
252         enable_java = gtk_check_button_new_with_label("JAVA");
253         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_java),
254                                      fancy_prefs.enable_java);
255         gtk_box_pack_start(GTK_BOX(vbox), enable_java, FALSE, FALSE, 0);
256         gtk_widget_show(enable_java);
257
258         enable_plugins = gtk_check_button_new_with_label("PLUGINS");
259         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_plugins),
260                                      fancy_prefs.enable_plugins);
261         gtk_box_pack_start(GTK_BOX(vbox), enable_plugins, FALSE, FALSE, 0);
262         gtk_widget_show(enable_plugins);
263
264         GtkWidget *hbox_ext = gtk_hbox_new(FALSE, 8);
265         GtkWidget *open_external_label = gtk_label_new(_("When clicking on a link, by default:"));
266         GtkWidget *optmenu_open_external = gtkut_sc_combobox_create(NULL, FALSE);
267         GtkListStore *menu = GTK_LIST_STORE(gtk_combo_box_get_model(
268                                 GTK_COMBO_BOX(optmenu_open_external)));
269         gtk_widget_show (optmenu_open_external);
270         GtkTreeIter iter;
271
272         COMBOBOX_ADD (menu, "DEFAULT_ACTION", FALSE);
273         COMBOBOX_ADD (menu, _("Open in external browser"), TRUE);
274
275         gtk_box_pack_start(GTK_BOX(hbox_ext), open_external_label, FALSE, FALSE, 0);
276         gtk_box_pack_start(GTK_BOX(hbox_ext), optmenu_open_external, FALSE, FALSE, 0);
277         gtk_widget_show_all(hbox_ext);
278         gtk_box_pack_start(GTK_BOX(vbox), hbox_ext, FALSE, FALSE, 0);
279
280         combobox_select_by_data(GTK_COMBO_BOX(optmenu_open_external),
281                         fancy_prefs.open_external);
282
283 #ifdef HAVE_LIBSOUP_GNOME
284         prefs_page->gnome_proxy_checkbox = gnome_proxy_checkbox;
285 #endif
286         prefs_page->proxy_checkbox = proxy_checkbox;
287         prefs_page->proxy_str = proxy_str;
288         prefs_page->enable_remote_content = enable_remote_content;
289         prefs_page->enable_images = enable_images;
290         prefs_page->enable_scripts = enable_scripts;
291         prefs_page->enable_plugins = enable_plugins;
292         prefs_page->enable_java = enable_java;
293         prefs_page->open_external = optmenu_open_external;
294         prefs_page->page.widget = vbox;
295
296         g_signal_connect(G_OBJECT(prefs_page->enable_remote_content), "toggled",
297                          G_CALLBACK(remote_content_set_labels_cb), prefs_page);
298         remote_content_set_labels_cb(NULL, prefs_page);
299 }
300
301 static void prefs_set_proxy_entry_sens(GtkWidget *button, GtkEntry *entry_str) {
302         gtk_widget_set_sensitive(GTK_WIDGET(entry_str),
303                                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
304 }
305
306 #ifdef HAVE_LIBSOUP_GNOME
307 static void prefs_disable_fancy_proxy(GtkWidget *checkbox, GtkWidget *block) {
308         gboolean toggle = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox));
309         gtk_widget_set_sensitive(block, !toggle);
310         GList *list = g_list_first(gtk_container_get_children(GTK_CONTAINER(block)));
311         if (toggle) {
312                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(list->data), FALSE);
313         }
314         else {
315                 gtk_widget_set_sensitive(GTK_WIDGET(list->data), TRUE);
316         }
317 }
318 #endif
319 static void destroy_fancy_prefs_page(PrefsPage *page)
320 {
321         /* Do nothing! */
322 }
323 static void save_fancy_prefs(PrefsPage *page)
324 {
325         PrefFile *pref_file;
326         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
327                                           COMMON_RC, NULL);
328         pref_file = prefs_write_open(rc_file_path);
329         g_free(rc_file_path);
330         if (!(pref_file) ||
331         (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
332                 return;
333         
334         if (prefs_write_param(param, pref_file->fp) < 0) {
335                 g_warning("failed to write Fancy Plugin configuration\n");
336                 prefs_file_close_revert(pref_file);
337                 return;
338         }
339
340         if (fprintf(pref_file->fp, "\n") < 0) {
341         FILE_OP_ERROR(rc_file_path, "fprintf");
342         prefs_file_close_revert(pref_file);
343         } else
344                 prefs_file_close(pref_file);
345 }
346
347 static void save_fancy_prefs_page(PrefsPage *page)
348 {
349                 FancyPrefsPage *prefs_page = (FancyPrefsPage *) page;
350         
351 #ifdef HAVE_LIBSOUP_GNOME
352                 fancy_prefs.enable_gnome_proxy = gtk_toggle_button_get_active
353                                 (GTK_TOGGLE_BUTTON(prefs_page->gnome_proxy_checkbox));
354 #endif
355                 fancy_prefs.enable_images = gtk_toggle_button_get_active
356                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_images));
357                 fancy_prefs.enable_remote_content = gtk_toggle_button_get_active
358                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_remote_content));
359                 fancy_prefs.enable_scripts = gtk_toggle_button_get_active
360                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_scripts));
361                 fancy_prefs.enable_plugins = gtk_toggle_button_get_active
362                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_plugins));
363                 fancy_prefs.enable_java = gtk_toggle_button_get_active
364                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_java));
365                 fancy_prefs.open_external = combobox_get_active_data
366                                 (GTK_COMBO_BOX(prefs_page->open_external));
367                 fancy_prefs.enable_proxy = gtk_toggle_button_get_active
368                                 (GTK_TOGGLE_BUTTON(prefs_page->proxy_checkbox));
369                 fancy_prefs.proxy_str = pref_get_pref_from_entry(GTK_ENTRY(prefs_page->proxy_str));
370
371                 save_fancy_prefs(page);
372 }