2007-10-28 [colin] 3.0.2cvs105
[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         gchar *folderpath = NULL;
84         gboolean ret = FALSE;
85
86         folderpath = (gchar *) gtk_entry_get_text(GTK_ENTRY(GTK_BIN(page->whitelist_ab_folder_combo)->child));
87         ret = addressbook_folder_selection(&folderpath);
88         if ( ret != FALSE && folderpath != NULL)
89                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(page->whitelist_ab_folder_combo)->child), folderpath);
90 }
91
92 static void local_checkbox_toggled(GtkToggleButton *button,
93                                           gpointer user_data)
94 {
95         gboolean active = gtk_toggle_button_get_active(button);
96         DilloBrowserPage *prefs_page = (DilloBrowserPage *) user_data;
97
98         gtk_widget_set_sensitive(prefs_page->whitelist_ab, active);
99         gtk_widget_set_sensitive(prefs_page->whitelist_ab_folder_combo, active && 
100                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab)));
101         gtk_widget_set_sensitive(prefs_page->whitelist_ab_select_btn, active && 
102                         gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab)));
103 }
104
105 static void whitelist_checkbox_toggled(GtkToggleButton *button,
106                                           gpointer user_data)
107 {
108         gboolean active = gtk_toggle_button_get_active(button);
109         DilloBrowserPage *prefs_page = (DilloBrowserPage *) user_data;
110
111         active &= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->local));
112
113         gtk_widget_set_sensitive(prefs_page->whitelist_ab_folder_combo, active);
114         gtk_widget_set_sensitive(prefs_page->whitelist_ab_select_btn, active);
115 }
116
117 void dillo_prefs_init(void)
118 {
119         static gchar *path[3];
120         gchar *rcpath;
121
122         path[0] = _("Plugins");
123         path[1] = _("Dillo Browser");
124         path[2] = NULL;
125
126         prefs_set_default(param);
127         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
128         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
129         g_free(rcpath);
130         
131         prefs_page.page.path = path;
132         prefs_page.page.create_widget = create_dillo_prefs_page;
133         prefs_page.page.destroy_widget = destroy_dillo_prefs_page;
134         prefs_page.page.save_page = save_dillo_prefs;
135
136         prefs_gtk_register_page((PrefsPage *) &prefs_page);
137 }
138
139 void dillo_prefs_done(void)
140 {
141         prefs_gtk_unregister_page((PrefsPage *) &prefs_page);
142 }
143
144 static void create_dillo_prefs_page(PrefsPage *page,
145                                     GtkWindow *window,
146                                     gpointer data)
147 {
148         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
149
150         GtkWidget *vbox;
151         GtkWidget *local_checkbox;
152         GtkWidget *full_checkbox;
153         GtkWidget *label;
154         GtkTooltips *local_tooltip;
155         GtkTooltips *full_tooltip;
156         GtkWidget *whitelist_ab_checkbtn;
157         GtkWidget *whitelist_ab_folder_combo;
158         GtkWidget *whitelist_ab_select_btn;
159         GtkWidget *hbox_whitelist, *spacer;
160
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_tooltip = gtk_tooltips_new();
167         local_checkbox = gtk_check_button_new_with_label
168                                 (_("Load remote links in mails"));
169         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(local_checkbox),
170                                      !dillo_prefs.local);
171         gtk_box_pack_start(GTK_BOX(vbox), local_checkbox, FALSE, FALSE, 0);
172         gtk_widget_show(local_checkbox);
173         gtk_tooltips_set_tip(GTK_TOOLTIPS(local_tooltip), local_checkbox,
174                              _("Equivalent to Dillo's '--local' option"), NULL);
175         
176         label = gtk_label_new(_("You can still load remote links "
177                               "by reloading the page"));
178         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
179         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
180         gtkut_widget_set_small_font_size (label);
181         gtk_widget_show(label);
182
183         
184         hbox_whitelist = gtk_hbox_new(FALSE, 8);
185         gtk_widget_show(hbox_whitelist);
186         gtk_box_pack_start (GTK_BOX (vbox), hbox_whitelist, FALSE, FALSE, 0);
187         
188         spacer = gtk_hbox_new(FALSE, 0);
189         gtk_widget_set_size_request(spacer, 12, -1);
190         gtk_widget_show(spacer);
191         gtk_box_pack_start(GTK_BOX(hbox_whitelist), spacer, FALSE, FALSE, 0);
192
193         whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Only for senders found in address book/folder"));
194         gtk_widget_show(whitelist_ab_checkbtn);
195         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
196
197         whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
198         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
199         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
200
201         whitelist_ab_select_btn = gtk_button_new_with_label(_("Select ..."));
202         gtk_widget_show (whitelist_ab_select_btn);
203         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
204
205         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), dillo_prefs.whitelist_ab);
206         if (dillo_prefs.whitelist_ab_folder != NULL)
207                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(whitelist_ab_folder_combo)->child),
208                                 dillo_prefs.whitelist_ab_folder);
209
210         full_tooltip = gtk_tooltips_new();
211         full_checkbox = gtk_check_button_new_with_label
212                                 (_("Full window mode (hide controls)"));
213         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(full_checkbox),
214                                       dillo_prefs.full);
215         gtk_box_pack_start(GTK_BOX(vbox), full_checkbox, FALSE, FALSE, 0);
216         gtk_widget_show(full_checkbox);
217         gtk_tooltips_set_tip(GTK_TOOLTIPS(full_tooltip), full_checkbox,
218                              _("Equivalent to Dillo's '--fullwindow' option"),
219                              NULL);
220
221         g_signal_connect(G_OBJECT(local_checkbox), "toggled",
222                          G_CALLBACK(local_checkbox_toggled),
223                          prefs_page);
224
225         g_signal_connect(G_OBJECT(whitelist_ab_checkbtn), "toggled",
226                          G_CALLBACK(whitelist_checkbox_toggled),
227                          prefs_page);
228
229         gtk_widget_set_sensitive(whitelist_ab_checkbtn, !dillo_prefs.local);
230         gtk_widget_set_sensitive(whitelist_ab_folder_combo, !dillo_prefs.local && dillo_prefs.whitelist_ab);
231         gtk_widget_set_sensitive(whitelist_ab_select_btn, !dillo_prefs.local && dillo_prefs.whitelist_ab);
232
233         
234         prefs_page->local = local_checkbox;
235         prefs_page->full = full_checkbox;
236         prefs_page->whitelist_ab = whitelist_ab_checkbtn;
237         prefs_page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
238         prefs_page->whitelist_ab_select_btn = whitelist_ab_select_btn;
239         prefs_page->page.widget = vbox;
240 }
241
242 static void destroy_dillo_prefs_page(PrefsPage *page)
243 {
244         /* Do nothing! */
245 }
246
247 static void save_dillo_prefs(PrefsPage *page)
248 {
249         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
250         PrefFile *pref_file;
251         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
252                                           COMMON_RC, NULL);
253         
254         dillo_prefs.local = !gtk_toggle_button_get_active
255                                 (GTK_TOGGLE_BUTTON(prefs_page->local));
256         dillo_prefs.full = gtk_toggle_button_get_active
257                                 (GTK_TOGGLE_BUTTON(prefs_page->full));
258
259         dillo_prefs.whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab));
260         g_free(dillo_prefs.whitelist_ab_folder);
261         dillo_prefs.whitelist_ab_folder = gtk_editable_get_chars(
262                                 GTK_EDITABLE(GTK_BIN(prefs_page->whitelist_ab_folder_combo)->child), 0, -1);
263         
264         pref_file = prefs_write_open(rc_file_path);
265         g_free(rc_file_path);
266         
267         if (!(pref_file) ||
268             (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
269           return;
270         
271         if (prefs_write_param(param, pref_file->fp) < 0) {
272           g_warning("failed to write Dillo Plugin configuration\n");
273           prefs_file_close_revert(pref_file);
274           return;
275         }
276
277         if (fprintf(pref_file->fp, "\n") < 0) {
278                 FILE_OP_ERROR(rc_file_path, "fprintf");
279                 prefs_file_close_revert(pref_file);
280         } else
281                 prefs_file_close(pref_file);
282 }