dacfffbd2a50ef44c719c78be48130ff34395ea5
[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         CLAWS_TIP_DECL();
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         local_checkbox = gtk_check_button_new_with_label
167                                 (_("Load remote links in mails"));
168         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(local_checkbox),
169                                      !dillo_prefs.local);
170         gtk_box_pack_start(GTK_BOX(vbox), local_checkbox, FALSE, FALSE, 0);
171         gtk_widget_show(local_checkbox);
172         CLAWS_SET_TIP(local_checkbox,
173                              _("Equivalent to Dillo's '--local' option"));
174         
175         label = gtk_label_new(_("You can still load remote links "
176                               "by reloading the page"));
177         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
178         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
179         gtkut_widget_set_small_font_size (label);
180         gtk_widget_show(label);
181
182         
183         hbox_whitelist = gtk_hbox_new(FALSE, 8);
184         gtk_widget_show(hbox_whitelist);
185         gtk_box_pack_start (GTK_BOX (vbox), hbox_whitelist, FALSE, FALSE, 0);
186         
187         spacer = gtk_hbox_new(FALSE, 0);
188         gtk_widget_set_size_request(spacer, 12, -1);
189         gtk_widget_show(spacer);
190         gtk_box_pack_start(GTK_BOX(hbox_whitelist), spacer, FALSE, FALSE, 0);
191
192         whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Only for senders found in address book/folder"));
193         gtk_widget_show(whitelist_ab_checkbtn);
194         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
195
196         whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
197         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
198         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
199
200         whitelist_ab_select_btn = gtk_button_new_with_label(_("Select ..."));
201         gtk_widget_show (whitelist_ab_select_btn);
202         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
203
204         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), dillo_prefs.whitelist_ab);
205         if (dillo_prefs.whitelist_ab_folder != NULL) {
206                 /* translate "Any" (stored UNtranslated) */
207                 if (strcasecmp(dillo_prefs.whitelist_ab_folder, "Any") == 0)
208                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
209                                         _("Any"));
210                 else
211                 /* backward compatibility (when translated "Any" was stored) */
212                 if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0)
213                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
214                                         dillo_prefs.whitelist_ab_folder);
215                 else
216                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
217                                         dillo_prefs.whitelist_ab_folder);
218         }
219
220         full_checkbox = gtk_check_button_new_with_label
221                                 (_("Full window mode (hide controls)"));
222         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(full_checkbox),
223                                       dillo_prefs.full);
224         gtk_box_pack_start(GTK_BOX(vbox), full_checkbox, FALSE, FALSE, 0);
225         gtk_widget_show(full_checkbox);
226         CLAWS_SET_TIP(full_checkbox,
227                              _("Equivalent to Dillo's '--fullwindow' option"));
228
229         g_signal_connect(G_OBJECT(local_checkbox), "toggled",
230                          G_CALLBACK(local_checkbox_toggled),
231                          prefs_page);
232
233         g_signal_connect(G_OBJECT(whitelist_ab_checkbtn), "toggled",
234                          G_CALLBACK(whitelist_checkbox_toggled),
235                          prefs_page);
236
237         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
238                          G_CALLBACK(dillo_whitelist_ab_select_cb), prefs_page);
239
240         gtk_widget_set_sensitive(whitelist_ab_checkbtn, !dillo_prefs.local);
241         gtk_widget_set_sensitive(whitelist_ab_folder_combo, !dillo_prefs.local && dillo_prefs.whitelist_ab);
242         gtk_widget_set_sensitive(whitelist_ab_select_btn, !dillo_prefs.local && dillo_prefs.whitelist_ab);
243
244         
245         prefs_page->local = local_checkbox;
246         prefs_page->full = full_checkbox;
247         prefs_page->whitelist_ab = whitelist_ab_checkbtn;
248         prefs_page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
249         prefs_page->whitelist_ab_select_btn = whitelist_ab_select_btn;
250         prefs_page->page.widget = vbox;
251 }
252
253 static void destroy_dillo_prefs_page(PrefsPage *page)
254 {
255         /* Do nothing! */
256 }
257
258 static void save_dillo_prefs(PrefsPage *page)
259 {
260         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
261         PrefFile *pref_file;
262         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
263                                           COMMON_RC, NULL);
264         
265         dillo_prefs.local = !gtk_toggle_button_get_active
266                                 (GTK_TOGGLE_BUTTON(prefs_page->local));
267         dillo_prefs.full = gtk_toggle_button_get_active
268                                 (GTK_TOGGLE_BUTTON(prefs_page->full));
269
270         dillo_prefs.whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab));
271         g_free(dillo_prefs.whitelist_ab_folder);
272         dillo_prefs.whitelist_ab_folder = gtk_editable_get_chars(
273                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((prefs_page->whitelist_ab_folder_combo)))), 0, -1);
274         /* store UNtranslated "Any" */
275         if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0) {
276                 g_free(dillo_prefs.whitelist_ab_folder);
277                 dillo_prefs.whitelist_ab_folder = g_strdup("Any");
278         }
279     
280         pref_file = prefs_write_open(rc_file_path);
281         g_free(rc_file_path);
282         
283         if (!(pref_file) ||
284             (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
285           return;
286         
287         if (prefs_write_param(param, pref_file->fp) < 0) {
288           g_warning("failed to write Dillo Plugin configuration\n");
289           prefs_file_close_revert(pref_file);
290           return;
291         }
292
293         if (fprintf(pref_file->fp, "\n") < 0) {
294                 FILE_OP_ERROR(rc_file_path, "fprintf");
295                 prefs_file_close_revert(pref_file);
296         } else
297                 prefs_file_close(pref_file);
298 }