2008-05-12 [wwp] 3.4.0cvs30
[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-2007 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(page->whitelist_ab_folder_combo)->child));
87         new_path = addressbook_folder_selection(folderpath);
88         if (new_path) {
89                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(page->whitelist_ab_folder_combo)->child), 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         GtkTooltips *local_tooltip;
157         GtkTooltips *full_tooltip;
158         GtkWidget *whitelist_ab_checkbtn;
159         GtkWidget *whitelist_ab_folder_combo;
160         GtkWidget *whitelist_ab_select_btn;
161         GtkWidget *hbox_whitelist, *spacer;
162
163
164         vbox = gtk_vbox_new(FALSE, 3);
165         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
166         gtk_widget_show(vbox);
167         
168         local_tooltip = gtk_tooltips_new();
169         local_checkbox = gtk_check_button_new_with_label
170                                 (_("Load remote links in mails"));
171         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(local_checkbox),
172                                      !dillo_prefs.local);
173         gtk_box_pack_start(GTK_BOX(vbox), local_checkbox, FALSE, FALSE, 0);
174         gtk_widget_show(local_checkbox);
175         gtk_tooltips_set_tip(GTK_TOOLTIPS(local_tooltip), local_checkbox,
176                              _("Equivalent to Dillo's '--local' option"), NULL);
177         
178         label = gtk_label_new(_("You can still load remote links "
179                               "by reloading the page"));
180         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
181         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
182         gtkut_widget_set_small_font_size (label);
183         gtk_widget_show(label);
184
185         
186         hbox_whitelist = gtk_hbox_new(FALSE, 8);
187         gtk_widget_show(hbox_whitelist);
188         gtk_box_pack_start (GTK_BOX (vbox), hbox_whitelist, FALSE, FALSE, 0);
189         
190         spacer = gtk_hbox_new(FALSE, 0);
191         gtk_widget_set_size_request(spacer, 12, -1);
192         gtk_widget_show(spacer);
193         gtk_box_pack_start(GTK_BOX(hbox_whitelist), spacer, FALSE, FALSE, 0);
194
195         whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Only for senders found in address book/folder"));
196         gtk_widget_show(whitelist_ab_checkbtn);
197         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
198
199         whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
200         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
201         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
202
203         whitelist_ab_select_btn = gtk_button_new_with_label(_("Select ..."));
204         gtk_widget_show (whitelist_ab_select_btn);
205         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
206
207         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), dillo_prefs.whitelist_ab);
208         if (dillo_prefs.whitelist_ab_folder != NULL) {
209                 /* translate "Any" (stored UNtranslated) */
210                 if (strcasecmp(dillo_prefs.whitelist_ab_folder, "Any") == 0)
211                         gtk_entry_set_text(GTK_ENTRY(GTK_BIN(whitelist_ab_folder_combo)->child),
212                                         _("Any"));
213                 else
214                 /* backward compatibility (when translated "Any" was stored) */
215                 if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0)
216                         gtk_entry_set_text(GTK_ENTRY(GTK_BIN(whitelist_ab_folder_combo)->child),
217                                         dillo_prefs.whitelist_ab_folder);
218                 else
219                         gtk_entry_set_text(GTK_ENTRY(GTK_BIN(whitelist_ab_folder_combo)->child),
220                                         dillo_prefs.whitelist_ab_folder);
221         }
222
223         full_tooltip = gtk_tooltips_new();
224         full_checkbox = gtk_check_button_new_with_label
225                                 (_("Full window mode (hide controls)"));
226         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(full_checkbox),
227                                       dillo_prefs.full);
228         gtk_box_pack_start(GTK_BOX(vbox), full_checkbox, FALSE, FALSE, 0);
229         gtk_widget_show(full_checkbox);
230         gtk_tooltips_set_tip(GTK_TOOLTIPS(full_tooltip), full_checkbox,
231                              _("Equivalent to Dillo's '--fullwindow' option"),
232                              NULL);
233
234         g_signal_connect(G_OBJECT(local_checkbox), "toggled",
235                          G_CALLBACK(local_checkbox_toggled),
236                          prefs_page);
237
238         g_signal_connect(G_OBJECT(whitelist_ab_checkbtn), "toggled",
239                          G_CALLBACK(whitelist_checkbox_toggled),
240                          prefs_page);
241
242         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
243                          G_CALLBACK(dillo_whitelist_ab_select_cb), prefs_page);
244
245         gtk_widget_set_sensitive(whitelist_ab_checkbtn, !dillo_prefs.local);
246         gtk_widget_set_sensitive(whitelist_ab_folder_combo, !dillo_prefs.local && dillo_prefs.whitelist_ab);
247         gtk_widget_set_sensitive(whitelist_ab_select_btn, !dillo_prefs.local && dillo_prefs.whitelist_ab);
248
249         
250         prefs_page->local = local_checkbox;
251         prefs_page->full = full_checkbox;
252         prefs_page->whitelist_ab = whitelist_ab_checkbtn;
253         prefs_page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
254         prefs_page->whitelist_ab_select_btn = whitelist_ab_select_btn;
255         prefs_page->page.widget = vbox;
256 }
257
258 static void destroy_dillo_prefs_page(PrefsPage *page)
259 {
260         /* Do nothing! */
261 }
262
263 static void save_dillo_prefs(PrefsPage *page)
264 {
265         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
266         PrefFile *pref_file;
267         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
268                                           COMMON_RC, NULL);
269         
270         dillo_prefs.local = !gtk_toggle_button_get_active
271                                 (GTK_TOGGLE_BUTTON(prefs_page->local));
272         dillo_prefs.full = gtk_toggle_button_get_active
273                                 (GTK_TOGGLE_BUTTON(prefs_page->full));
274
275         dillo_prefs.whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab));
276         g_free(dillo_prefs.whitelist_ab_folder);
277         dillo_prefs.whitelist_ab_folder = gtk_editable_get_chars(
278                                 GTK_EDITABLE(GTK_BIN(prefs_page->whitelist_ab_folder_combo)->child), 0, -1);
279         /* store UNtranslated "Any" */
280         if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0) {
281                 g_free(dillo_prefs.whitelist_ab_folder);
282                 dillo_prefs.whitelist_ab_folder = g_strdup("Any");
283         }
284     
285         pref_file = prefs_write_open(rc_file_path);
286         g_free(rc_file_path);
287         
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 Dillo 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 }