cc1013aac57274366b36ccb3a1152bcf69856f58
[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 *auto_load_images;
59         GtkWidget *enable_inner_navigation;
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                 {"auto_load_images", "FALSE", &fancy_prefs.auto_load_images, P_BOOL, 
73                 NULL, NULL, NULL},
74                 {"enable_inner_navigation", "FALSE", &fancy_prefs.enable_inner_navigation, 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, 
100                                                                                  gpointer   data);
101 static void destroy_fancy_prefs_page    (PrefsPage *page);
102 static void save_fancy_prefs_page       (PrefsPage *page);
103 static void save_fancy_prefs            (PrefsPage *page);
104
105 void fancy_prefs_init(void)
106 {
107         static gchar *path[3];
108         gchar *rcpath;
109
110         path[0] = _("Plugins");
111         path[1] = "Fancy";
112         path[2] = NULL;
113
114         prefs_set_default(param);
115         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
116         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
117         g_free(rcpath);
118             
119         fancy_prefs_page.page.path = path;
120         fancy_prefs_page.page.create_widget = create_fancy_prefs_page;
121         fancy_prefs_page.page.destroy_widget = destroy_fancy_prefs_page;
122         fancy_prefs_page.page.save_page = save_fancy_prefs_page;
123         fancy_prefs_page.page.weight = 30.0;
124         prefs_gtk_register_page((PrefsPage *) &fancy_prefs_page);
125 }
126
127 void fancy_prefs_done(void)
128 {
129         save_fancy_prefs((PrefsPage *) &fancy_prefs_page);
130         prefs_gtk_unregister_page((PrefsPage *) &fancy_prefs_page);
131 }
132
133 static void open_external_set_label_cb(GtkWidget *button, FancyPrefsPage *prefs_page)
134 {
135         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(prefs_page->open_external));
136         GtkTreeIter iter;
137         if (gtk_tree_model_get_iter_first (model, &iter)) {
138                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->enable_inner_navigation)))
139                         gtk_list_store_set(model, &iter, COMBOBOX_TEXT, _("Open in viewer"), -1);
140                 else
141                         gtk_list_store_set(model, &iter, COMBOBOX_TEXT, _("Do nothing"), -1);
142         }
143
144 }
145 static void create_fancy_prefs_page(PrefsPage *page, GtkWindow *window, 
146                                                                         gpointer data)
147 {
148         FancyPrefsPage *prefs_page = (FancyPrefsPage *) page;
149
150         GtkWidget *vbox;
151 #ifdef HAVE_LIBSOUP_GNOME    
152         GtkWidget *gnome_proxy_checkbox;
153 #endif
154         GtkWidget *proxy_checkbox;
155         GtkWidget *proxy_str;
156         GtkWidget *checkbox1;
157         GtkWidget *checkbox2;
158         GtkWidget *checkbox3;
159         GtkWidget *checkbox4;
160         GtkWidget *checkbox6;
161
162         vbox = gtk_vbox_new(FALSE, 3);
163         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
164         gtk_widget_show(vbox);
165             
166         GtkWidget *block = gtk_vbox_new(FALSE, FALSE);
167         proxy_checkbox = gtk_check_button_new_with_label(_("Proxy Setting"));
168         proxy_str = gtk_entry_new();
169         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(proxy_checkbox),
170                                                                  fancy_prefs.enable_proxy);
171         prefs_set_proxy_entry_sens(proxy_checkbox, GTK_ENTRY(proxy_str));
172         g_signal_connect(G_OBJECT(proxy_checkbox), "toggled",
173                                          G_CALLBACK(prefs_set_proxy_entry_sens), proxy_str);
174         pref_set_entry_from_pref(GTK_ENTRY(proxy_str), fancy_prefs.proxy_str);
175
176         gtk_box_pack_start(GTK_BOX(block), proxy_checkbox, FALSE, FALSE, 0);
177         gtk_box_pack_start(GTK_BOX(block), proxy_str, FALSE, FALSE, 0);
178         gtk_box_pack_start(GTK_BOX(vbox), block, FALSE, FALSE, 0);
179         gtk_widget_show(proxy_checkbox);
180         gtk_widget_show(proxy_str);
181         gtk_widget_show(block);
182 #ifdef HAVE_LIBSOUP_GNOME
183         gnome_proxy_checkbox = gtk_check_button_new_with_label(_("Use GNOME proxy setting"));   
184         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gnome_proxy_checkbox),
185                                                                  fancy_prefs.enable_gnome_proxy);
186         if (fancy_prefs.enable_gnome_proxy) 
187                 gtk_widget_set_sensitive(proxy_checkbox, FALSE);
188         gtk_box_pack_start(GTK_BOX(vbox), gnome_proxy_checkbox, FALSE, FALSE, 0);
189         gtk_widget_show(gnome_proxy_checkbox);
190         g_signal_connect(G_OBJECT(gnome_proxy_checkbox), "toggled",
191                                          G_CALLBACK(prefs_disable_fancy_proxy), block);
192 #endif
193         checkbox1 = gtk_check_button_new_with_label(_("Auto-Load images"));
194         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox1),
195                                                                  fancy_prefs.auto_load_images);
196         gtk_box_pack_start(GTK_BOX(vbox), checkbox1, FALSE, FALSE, 0);
197         gtk_widget_show(checkbox1);
198
199         checkbox2 = gtk_check_button_new_with_label(_("Enable inner navigation"));
200         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox2),
201                                                                  fancy_prefs.enable_inner_navigation);
202         gtk_box_pack_start(GTK_BOX(vbox), checkbox2, FALSE, FALSE, 0);
203         gtk_widget_show(checkbox2);
204         
205         checkbox3 = gtk_check_button_new_with_label(_("Enable Javascript"));
206         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox3),
207                                                                  fancy_prefs.enable_scripts);
208         gtk_box_pack_start(GTK_BOX(vbox), checkbox3, FALSE, FALSE, 0);
209         gtk_widget_show(checkbox3);
210
211         checkbox4 = gtk_check_button_new_with_label(_("Enable Plugins"));
212         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox4),
213                                                                  fancy_prefs.enable_plugins);
214         gtk_box_pack_start(GTK_BOX(vbox), checkbox4, FALSE, FALSE, 0);
215         gtk_widget_show(checkbox4);
216         checkbox6 = gtk_check_button_new_with_label(_("Enable Java"));
217         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox6),
218                                                                  fancy_prefs.enable_java);
219         gtk_box_pack_start(GTK_BOX(vbox), checkbox6, FALSE, FALSE, 0);
220         gtk_widget_show(checkbox6);
221
222         GtkWidget *hbox_ext = gtk_hbox_new(FALSE, 8);
223         GtkWidget *open_external_label = gtk_label_new(_("When clicking on a link, by default:"));
224         GtkWidget *optmenu_open_external = gtkut_sc_combobox_create(NULL, FALSE);
225         GtkListStore *menu = GTK_LIST_STORE(gtk_combo_box_get_model(
226                                 GTK_COMBO_BOX(optmenu_open_external)));
227         gtk_widget_show (optmenu_open_external);
228         GtkTreeIter iter;
229
230         COMBOBOX_ADD (menu, "DEFAULT_ACTION", FALSE);
231         COMBOBOX_ADD (menu, _("Open in external browser"), TRUE);
232
233         gtk_box_pack_start(GTK_BOX(hbox_ext), open_external_label, FALSE, FALSE, 0);
234         gtk_box_pack_start(GTK_BOX(hbox_ext), optmenu_open_external, FALSE, FALSE, 0);
235         gtk_widget_show_all(hbox_ext);
236         gtk_box_pack_start(GTK_BOX(vbox), hbox_ext, FALSE, FALSE, 0);
237
238         combobox_select_by_data(GTK_COMBO_BOX(optmenu_open_external),
239                         fancy_prefs.open_external);
240
241 #ifdef HAVE_LIBSOUP_GNOME    
242         prefs_page->gnome_proxy_checkbox = gnome_proxy_checkbox;
243 #endif
244         prefs_page->proxy_checkbox = proxy_checkbox;
245         prefs_page->proxy_str = proxy_str;
246         prefs_page->auto_load_images = checkbox1;
247         prefs_page->enable_inner_navigation = checkbox2;
248         prefs_page->enable_scripts = checkbox3;
249         prefs_page->enable_plugins = checkbox4;
250         prefs_page->enable_java = checkbox6;
251         prefs_page->open_external = optmenu_open_external;
252         prefs_page->page.widget = vbox;
253
254         g_signal_connect(G_OBJECT(prefs_page->enable_inner_navigation), "toggled",
255                                          G_CALLBACK(open_external_set_label_cb), prefs_page);
256         open_external_set_label_cb(NULL, prefs_page);
257 }
258
259 static void prefs_set_proxy_entry_sens(GtkWidget *button, GtkEntry *entry_str) {
260         gtk_widget_set_sensitive(GTK_WIDGET(entry_str), 
261                                                            gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
262 }
263
264 #ifdef HAVE_LIBSOUP_GNOME    
265 static void prefs_disable_fancy_proxy(GtkWidget *checkbox, GtkWidget *block) {
266         gboolean toggle = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox));
267         gtk_widget_set_sensitive(block, !toggle);
268         GList *list = g_list_first(gtk_container_get_children(GTK_CONTAINER(block)));
269         if (toggle) {
270                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(list->data), FALSE);
271         }
272         else {
273                 gtk_widget_set_sensitive(GTK_WIDGET(list->data), TRUE);
274         }
275 }
276 #endif
277 static void destroy_fancy_prefs_page(PrefsPage *page)
278 {
279         /* Do nothing! */
280 }
281 static void save_fancy_prefs(PrefsPage *page)
282 {
283         PrefFile *pref_file;
284         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
285                                                                           COMMON_RC, NULL);
286         pref_file = prefs_write_open(rc_file_path);
287         g_free(rc_file_path);
288         if (!(pref_file) ||
289         (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
290                 return;
291         
292         if (prefs_write_param(param, pref_file->fp) < 0) {
293                 g_warning("failed to write Fancy Plugin configuration\n");
294                 prefs_file_close_revert(pref_file);
295                 return;
296         }
297
298         if (fprintf(pref_file->fp, "\n") < 0) {
299         FILE_OP_ERROR(rc_file_path, "fprintf");
300         prefs_file_close_revert(pref_file);
301         } else
302                 prefs_file_close(pref_file);
303 }
304
305 static void save_fancy_prefs_page(PrefsPage *page)
306 {
307                 FancyPrefsPage *prefs_page = (FancyPrefsPage *) page;
308             
309 #ifdef HAVE_LIBSOUP_GNOME    
310                 fancy_prefs.enable_gnome_proxy = gtk_toggle_button_get_active
311                                 (GTK_TOGGLE_BUTTON(prefs_page->gnome_proxy_checkbox));
312 #endif
313                 fancy_prefs.auto_load_images = gtk_toggle_button_get_active
314                                 (GTK_TOGGLE_BUTTON(prefs_page->auto_load_images));
315                 fancy_prefs.enable_inner_navigation = gtk_toggle_button_get_active
316                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_inner_navigation));
317                 fancy_prefs.enable_scripts = gtk_toggle_button_get_active
318                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_scripts));
319                 fancy_prefs.enable_plugins = gtk_toggle_button_get_active
320                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_plugins));
321                 fancy_prefs.enable_java = gtk_toggle_button_get_active
322                                 (GTK_TOGGLE_BUTTON(prefs_page->enable_java));
323                 fancy_prefs.open_external = combobox_get_active_data
324                                 (GTK_COMBO_BOX(prefs_page->open_external));
325                 fancy_prefs.enable_proxy = gtk_toggle_button_get_active
326                                 (GTK_TOGGLE_BUTTON(prefs_page->proxy_checkbox));
327                 fancy_prefs.proxy_str = pref_get_pref_from_entry(GTK_ENTRY(prefs_page->proxy_str));
328
329                 save_fancy_prefs(page);
330 }