Fix possible crash when making use of proxy_checkbox before initiliazing it.
[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(GTK_LIST_STORE(model), &iter, COMBOBOX_TEXT,
164                                            _("Open in viewer (remote content is enabled)"), -1);
165                 else
166                         gtk_list_store_set(GTK_LIST_STORE(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         gtk_box_pack_start(GTK_BOX(vbox_proxy), gnome_proxy_checkbox, FALSE, FALSE, 0);
206         gtk_widget_show(gnome_proxy_checkbox);
207         g_signal_connect(G_OBJECT(gnome_proxy_checkbox), "toggled",
208                          G_CALLBACK(prefs_disable_fancy_proxy), block);
209 #endif
210         proxy_checkbox = gtk_check_button_new_with_label(_("Use proxy:"));
211         proxy_str = gtk_entry_new();
212 #ifdef HAVE_LIBSOUP_GNOME
213         if (fancy_prefs.enable_gnome_proxy)
214                 gtk_widget_set_sensitive(proxy_checkbox, FALSE);
215 #endif
216         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(proxy_checkbox),
217                                      fancy_prefs.enable_proxy);
218         prefs_set_proxy_entry_sens(proxy_checkbox, GTK_ENTRY(proxy_str));
219         g_signal_connect(G_OBJECT(proxy_checkbox), "toggled",
220                          G_CALLBACK(prefs_set_proxy_entry_sens), proxy_str);
221         pref_set_entry_from_pref(GTK_ENTRY(proxy_str), fancy_prefs.proxy_str);
222
223         gtk_box_pack_start(GTK_BOX(block), proxy_checkbox, FALSE, FALSE, 0);
224         gtk_box_pack_start(GTK_BOX(block), proxy_str, FALSE, TRUE, 0);
225         gtk_box_pack_start(GTK_BOX(vbox_proxy), block, FALSE, FALSE, 0);
226         gtk_widget_show_all(vbox_proxy);
227
228         vbox_remote = gtkut_get_options_frame(vbox, &frame_remote, _("Remote resources"));
229         remote_label = gtk_label_new(_("Loading remote resources can lead to some privacy issues.\n"
230                                         "When remote content loading is disabled, nothing will be requested\n"
231                                         "from the network. Rendering of images, scripts, plugin objects or\n"
232                                         "Java applets can still be enabled for content that is attached\n"
233                                         "in the email."));
234         gtk_misc_set_alignment(GTK_MISC(remote_label), 0, 0);
235         enable_remote_content = gtk_check_button_new_with_label(_("Enable loading of remote content"));
236         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_remote_content),
237                                      fancy_prefs.enable_remote_content);
238         gtk_box_pack_start (GTK_BOX (vbox_remote), remote_label, FALSE, FALSE, 0);
239         gtk_box_pack_start (GTK_BOX (vbox_remote), enable_remote_content, FALSE, FALSE, 0);
240         gtk_widget_show_all(vbox_remote);
241         
242         enable_images = gtk_check_button_new_with_label(("IMAGES"));
243         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_images),
244                                      fancy_prefs.enable_images);
245         gtk_box_pack_start(GTK_BOX(vbox), enable_images, FALSE, FALSE, 0);
246         gtk_widget_show(enable_images);
247
248         enable_scripts = gtk_check_button_new_with_label("SCRIPTS");
249         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_scripts),
250                                      fancy_prefs.enable_scripts);
251         gtk_box_pack_start(GTK_BOX(vbox), enable_scripts, FALSE, FALSE, 0);
252         gtk_widget_show(enable_scripts);
253
254         enable_java = gtk_check_button_new_with_label("JAVA");
255         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_java),
256                                      fancy_prefs.enable_java);
257         gtk_box_pack_start(GTK_BOX(vbox), enable_java, FALSE, FALSE, 0);
258         gtk_widget_show(enable_java);
259
260         enable_plugins = gtk_check_button_new_with_label("PLUGINS");
261         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_plugins),
262                                      fancy_prefs.enable_plugins);
263         gtk_box_pack_start(GTK_BOX(vbox), enable_plugins, FALSE, FALSE, 0);
264         gtk_widget_show(enable_plugins);
265
266         GtkWidget *hbox_ext = gtk_hbox_new(FALSE, 8);
267         GtkWidget *open_external_label = gtk_label_new(_("When clicking on a link, by default:"));
268         GtkWidget *optmenu_open_external = gtkut_sc_combobox_create(NULL, FALSE);
269         GtkListStore *menu = GTK_LIST_STORE(gtk_combo_box_get_model(
270                                 GTK_COMBO_BOX(optmenu_open_external)));
271         gtk_widget_show (optmenu_open_external);
272         GtkTreeIter iter;
273
274         COMBOBOX_ADD (menu, "DEFAULT_ACTION", FALSE);
275         COMBOBOX_ADD (menu, _("Open in external browser"), TRUE);
276
277         gtk_box_pack_start(GTK_BOX(hbox_ext), open_external_label, FALSE, FALSE, 0);
278         gtk_box_pack_start(GTK_BOX(hbox_ext), optmenu_open_external, FALSE, FALSE, 0);
279         gtk_widget_show_all(hbox_ext);
280         gtk_box_pack_start(GTK_BOX(vbox), hbox_ext, FALSE, FALSE, 0);
281
282         combobox_select_by_data(GTK_COMBO_BOX(optmenu_open_external),
283                         fancy_prefs.open_external);
284
285 #ifdef HAVE_LIBSOUP_GNOME
286         prefs_page->gnome_proxy_checkbox = gnome_proxy_checkbox;
287 #endif
288         prefs_page->proxy_checkbox = proxy_checkbox;
289         prefs_page->proxy_str = proxy_str;
290         prefs_page->enable_remote_content = enable_remote_content;
291         prefs_page->enable_images = enable_images;
292         prefs_page->enable_scripts = enable_scripts;
293         prefs_page->enable_plugins = enable_plugins;
294         prefs_page->enable_java = enable_java;
295         prefs_page->open_external = optmenu_open_external;
296         prefs_page->page.widget = vbox;
297
298         g_signal_connect(G_OBJECT(prefs_page->enable_remote_content), "toggled",
299                          G_CALLBACK(remote_content_set_labels_cb), prefs_page);
300         remote_content_set_labels_cb(NULL, prefs_page);
301 }
302
303 static void prefs_set_proxy_entry_sens(GtkWidget *button, GtkEntry *entry_str) {
304         gtk_widget_set_sensitive(GTK_WIDGET(entry_str),
305                                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
306 }
307
308 #ifdef HAVE_LIBSOUP_GNOME
309 static void prefs_disable_fancy_proxy(GtkWidget *checkbox, GtkWidget *block) {
310         gboolean toggle = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox));
311         gtk_widget_set_sensitive(block, !toggle);
312         GList *list = g_list_first(gtk_container_get_children(GTK_CONTAINER(block)));
313         if (toggle) {
314                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(list->data), FALSE);
315         }
316         else {
317                 gtk_widget_set_sensitive(GTK_WIDGET(list->data), TRUE);
318         }
319 }
320 #endif
321 static void destroy_fancy_prefs_page(PrefsPage *page)
322 {
323         /* Do nothing! */
324 }
325 static void save_fancy_prefs(PrefsPage *page)
326 {
327         PrefFile *pref_file;
328         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
329                                           COMMON_RC, NULL);
330         pref_file = prefs_write_open(rc_file_path);
331         g_free(rc_file_path);
332         if (!(pref_file) ||
333         (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
334                 return;
335         
336         if (prefs_write_param(param, pref_file->fp) < 0) {
337                 g_warning("failed to write Fancy Plugin configuration\n");
338                 prefs_file_close_revert(pref_file);
339                 return;
340         }
341
342         if (fprintf(pref_file->fp, "\n") < 0) {
343         FILE_OP_ERROR(rc_file_path, "fprintf");
344         prefs_file_close_revert(pref_file);
345         } else
346                 prefs_file_close(pref_file);
347 }
348
349 static void save_fancy_prefs_page(PrefsPage *page)
350 {
351                 FancyPrefsPage *prefs_page = (FancyPrefsPage *) page;
352         
353 #ifdef HAVE_LIBSOUP_GNOME
354                 fancy_prefs.enable_gnome_proxy = gtk_toggle_button_get_active
355                                 (GTK_TOGGLE_BUTTON(prefs_page->gnome_proxy_checkbox));
356 #endif
357                 fancy_prefs.enable_images = gtk_toggle_button_get_active
358                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_images));
359                 fancy_prefs.enable_remote_content = gtk_toggle_button_get_active
360                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_remote_content));
361                 fancy_prefs.enable_scripts = gtk_toggle_button_get_active
362                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_scripts));
363                 fancy_prefs.enable_plugins = gtk_toggle_button_get_active
364                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_plugins));
365                 fancy_prefs.enable_java = gtk_toggle_button_get_active
366                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_java));
367                 fancy_prefs.open_external = combobox_get_active_data
368                                 (GTK_COMBO_BOX(prefs_page->open_external));
369                 fancy_prefs.enable_proxy = gtk_toggle_button_get_active
370                                 (GTK_TOGGLE_BUTTON(prefs_page->proxy_checkbox));
371                 fancy_prefs.proxy_str = pref_get_pref_from_entry(GTK_ENTRY(prefs_page->proxy_str));
372
373                 save_fancy_prefs(page);
374 }