05338247d02e55739d1196035278bc416e6e44f8
[claws.git] / src / plugins / dillo_viewer / dillo_prefs.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 /*
21  * The structure of this file has been borrowed from the structure of
22  * the image_viewer plugin file. I also used it as an example of how to
23  * build the preferences for the dillo plugin.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "defs.h"
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <gtk/gtk.h>
35
36 #include "gtkutils.h"
37 #include "utils.h"
38 #include "prefs.h"
39 #include "prefs_gtk.h"
40 #include "prefswindow.h"
41 #include "combobox.h"
42 #include "addressbook.h"
43
44 #include "dillo_prefs.h"
45
46 #define PREFS_BLOCK_NAME "Dillo"
47
48 DilloBrowserPrefs dillo_prefs;
49
50 typedef struct _DilloBrowserPage DilloBrowserPage;
51
52 struct _DilloBrowserPage {
53         PrefsPage page;
54         GtkWidget *local;
55         GtkWidget *whitelist_ab;
56         GtkWidget *whitelist_ab_folder_combo;
57         GtkWidget *whitelist_ab_select_btn;
58         GtkWidget *full;
59 };
60
61 static PrefParam param[] = {
62         {"local_browse", "TRUE", &dillo_prefs.local, P_BOOL, NULL, NULL, NULL},
63         {"full_window", "TRUE", &dillo_prefs.full, P_BOOL, NULL, NULL, NULL},
64         {"whitelist_ab", "FALSE", &dillo_prefs.whitelist_ab, P_BOOL,
65          NULL, NULL, NULL},
66         {"whitelist_ab_folder", N_("Any"), &dillo_prefs.whitelist_ab_folder, P_STRING,
67          NULL, NULL, NULL},
68
69         {0,0,0,0,0,0,0}
70 };
71
72 static DilloBrowserPage prefs_page;
73
74 static void create_dillo_prefs_page     (PrefsPage *page,
75                                          GtkWindow *window,
76                                          gpointer   data);
77 static void destroy_dillo_prefs_page    (PrefsPage *page);
78 static void save_dillo_prefs            (PrefsPage *page);
79
80 static void dillo_whitelist_ab_select_cb(GtkWidget *widget, gpointer data)
81 {
82         DilloBrowserPage *page = (DilloBrowserPage *) data;
83         const gchar *folderpath = NULL;
84         gchar *new_path = NULL;
85
86         folderpath = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))));
87         new_path = addressbook_folder_selection(folderpath);
88         if (new_path) {
89                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), new_path);
90                 g_free(new_path);
91         } 
92 }
93
94 static void local_checkbox_toggled(GtkToggleButton *button,
95                                           gpointer user_data)
96 {
97         gboolean active = gtk_toggle_button_get_active(button);
98         DilloBrowserPage *prefs_page = (DilloBrowserPage *) user_data;
99
100         gtk_widget_set_sensitive(prefs_page->whitelist_ab, active);
101         gtk_widget_set_sensitive(prefs_page->whitelist_ab_folder_combo, active && 
102                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab)));
103         gtk_widget_set_sensitive(prefs_page->whitelist_ab_select_btn, active && 
104                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab)));
105 }
106
107 static void whitelist_checkbox_toggled(GtkToggleButton *button,
108                                           gpointer user_data)
109 {
110         gboolean active = gtk_toggle_button_get_active(button);
111         DilloBrowserPage *prefs_page = (DilloBrowserPage *) user_data;
112
113         active &= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->local));
114
115         gtk_widget_set_sensitive(prefs_page->whitelist_ab_folder_combo, active);
116         gtk_widget_set_sensitive(prefs_page->whitelist_ab_select_btn, active);
117 }
118
119 void dillo_prefs_init(void)
120 {
121         static gchar *path[3];
122         gchar *rcpath;
123
124         path[0] = _("Plugins");
125         path[1] = _("Dillo Browser");
126         path[2] = NULL;
127
128         prefs_set_default(param);
129         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
130         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
131         g_free(rcpath);
132         
133         prefs_page.page.path = path;
134         prefs_page.page.create_widget = create_dillo_prefs_page;
135         prefs_page.page.destroy_widget = destroy_dillo_prefs_page;
136         prefs_page.page.save_page = save_dillo_prefs;
137                 prefs_page.page.weight = 35.0;
138         prefs_gtk_register_page((PrefsPage *) &prefs_page);
139 }
140
141 void dillo_prefs_done(void)
142 {
143         prefs_gtk_unregister_page((PrefsPage *) &prefs_page);
144 }
145
146 static void create_dillo_prefs_page(PrefsPage *page,
147                                     GtkWindow *window,
148                                     gpointer data)
149 {
150         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
151
152         GtkWidget *vbox;
153         GtkWidget *local_checkbox;
154         GtkWidget *full_checkbox;
155         GtkWidget *label;
156         GtkWidget *whitelist_ab_checkbtn;
157         GtkWidget *whitelist_ab_folder_combo;
158         GtkWidget *whitelist_ab_select_btn;
159         GtkWidget *hbox_whitelist, *spacer;
160
161         vbox = gtk_vbox_new(FALSE, 3);
162         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
163         gtk_widget_show(vbox);
164         
165         local_checkbox = gtk_check_button_new_with_label
166                                 (_("Load remote links in mails"));
167         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(local_checkbox),
168                                      !dillo_prefs.local);
169         gtk_box_pack_start(GTK_BOX(vbox), local_checkbox, FALSE, FALSE, 0);
170         gtk_widget_show(local_checkbox);
171         CLAWS_SET_TIP(local_checkbox,
172                              _("Equivalent to Dillo's '--local' option"));
173         
174         label = gtk_label_new(_("You can still load remote links "
175                               "by reloading the page"));
176         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
177         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
178         gtkut_widget_set_small_font_size (label);
179         gtk_widget_show(label);
180
181         
182         hbox_whitelist = gtk_hbox_new(FALSE, 8);
183         gtk_widget_show(hbox_whitelist);
184         gtk_box_pack_start (GTK_BOX (vbox), hbox_whitelist, FALSE, FALSE, 0);
185         
186         spacer = gtk_hbox_new(FALSE, 0);
187         gtk_widget_set_size_request(spacer, 12, -1);
188         gtk_widget_show(spacer);
189         gtk_box_pack_start(GTK_BOX(hbox_whitelist), spacer, FALSE, FALSE, 0);
190
191         whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Only for senders found in address book/folder"));
192         gtk_widget_show(whitelist_ab_checkbtn);
193         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
194
195         whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
196         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
197         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
198
199         whitelist_ab_select_btn = gtk_button_new_with_label(_("Select ..."));
200         gtk_widget_show (whitelist_ab_select_btn);
201         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
202
203         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), dillo_prefs.whitelist_ab);
204         if (dillo_prefs.whitelist_ab_folder != NULL) {
205                 /* translate "Any" (stored UNtranslated) */
206                 if (strcasecmp(dillo_prefs.whitelist_ab_folder, "Any") == 0)
207                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
208                                         _("Any"));
209                 else
210                 /* backward compatibility (when translated "Any" was stored) */
211                 if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0)
212                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
213                                         dillo_prefs.whitelist_ab_folder);
214                 else
215                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
216                                         dillo_prefs.whitelist_ab_folder);
217         }
218
219         full_checkbox = gtk_check_button_new_with_label
220                                 (_("Full window mode (hide controls)"));
221         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(full_checkbox),
222                                       dillo_prefs.full);
223         gtk_box_pack_start(GTK_BOX(vbox), full_checkbox, FALSE, FALSE, 0);
224         gtk_widget_show(full_checkbox);
225         CLAWS_SET_TIP(full_checkbox,
226                              _("Equivalent to Dillo's '--fullwindow' option"));
227
228         g_signal_connect(G_OBJECT(local_checkbox), "toggled",
229                          G_CALLBACK(local_checkbox_toggled),
230                          prefs_page);
231
232         g_signal_connect(G_OBJECT(whitelist_ab_checkbtn), "toggled",
233                          G_CALLBACK(whitelist_checkbox_toggled),
234                          prefs_page);
235
236         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
237                          G_CALLBACK(dillo_whitelist_ab_select_cb), prefs_page);
238
239         gtk_widget_set_sensitive(whitelist_ab_checkbtn, !dillo_prefs.local);
240         gtk_widget_set_sensitive(whitelist_ab_folder_combo, !dillo_prefs.local && dillo_prefs.whitelist_ab);
241         gtk_widget_set_sensitive(whitelist_ab_select_btn, !dillo_prefs.local && dillo_prefs.whitelist_ab);
242
243         
244         prefs_page->local = local_checkbox;
245         prefs_page->full = full_checkbox;
246         prefs_page->whitelist_ab = whitelist_ab_checkbtn;
247         prefs_page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
248         prefs_page->whitelist_ab_select_btn = whitelist_ab_select_btn;
249         prefs_page->page.widget = vbox;
250 }
251
252 static void destroy_dillo_prefs_page(PrefsPage *page)
253 {
254         /* Do nothing! */
255 }
256
257 static void save_dillo_prefs(PrefsPage *page)
258 {
259         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
260         PrefFile *pref_file;
261         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
262                                           COMMON_RC, NULL);
263         
264         dillo_prefs.local = !gtk_toggle_button_get_active
265                                 (GTK_TOGGLE_BUTTON(prefs_page->local));
266         dillo_prefs.full = gtk_toggle_button_get_active
267                                 (GTK_TOGGLE_BUTTON(prefs_page->full));
268
269         dillo_prefs.whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab));
270         g_free(dillo_prefs.whitelist_ab_folder);
271         dillo_prefs.whitelist_ab_folder = gtk_editable_get_chars(
272                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((prefs_page->whitelist_ab_folder_combo)))), 0, -1);
273         /* store UNtranslated "Any" */
274         if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0) {
275                 g_free(dillo_prefs.whitelist_ab_folder);
276                 dillo_prefs.whitelist_ab_folder = g_strdup("Any");
277         }
278     
279         pref_file = prefs_write_open(rc_file_path);
280         g_free(rc_file_path);
281         
282         if (!(pref_file) ||
283             (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
284           return;
285         
286         if (prefs_write_param(param, pref_file->fp) < 0) {
287           g_warning("failed to write Dillo Plugin configuration\n");
288           prefs_file_close_revert(pref_file);
289           return;
290         }
291
292         if (fprintf(pref_file->fp, "\n") < 0) {
293                 FILE_OP_ERROR(rc_file_path, "fprintf");
294                 prefs_file_close_revert(pref_file);
295         } else
296                 prefs_file_close(pref_file);
297 }