2008-05-07 [colin] 3.4.0cvs29
[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                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(whitelist_ab_folder_combo)->child),
210                                 dillo_prefs.whitelist_ab_folder);
211
212         full_tooltip = gtk_tooltips_new();
213         full_checkbox = gtk_check_button_new_with_label
214                                 (_("Full window mode (hide controls)"));
215         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(full_checkbox),
216                                       dillo_prefs.full);
217         gtk_box_pack_start(GTK_BOX(vbox), full_checkbox, FALSE, FALSE, 0);
218         gtk_widget_show(full_checkbox);
219         gtk_tooltips_set_tip(GTK_TOOLTIPS(full_tooltip), full_checkbox,
220                              _("Equivalent to Dillo's '--fullwindow' option"),
221                              NULL);
222
223         g_signal_connect(G_OBJECT(local_checkbox), "toggled",
224                          G_CALLBACK(local_checkbox_toggled),
225                          prefs_page);
226
227         g_signal_connect(G_OBJECT(whitelist_ab_checkbtn), "toggled",
228                          G_CALLBACK(whitelist_checkbox_toggled),
229                          prefs_page);
230
231         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
232                          G_CALLBACK(dillo_whitelist_ab_select_cb), prefs_page);
233
234         gtk_widget_set_sensitive(whitelist_ab_checkbtn, !dillo_prefs.local);
235         gtk_widget_set_sensitive(whitelist_ab_folder_combo, !dillo_prefs.local && dillo_prefs.whitelist_ab);
236         gtk_widget_set_sensitive(whitelist_ab_select_btn, !dillo_prefs.local && dillo_prefs.whitelist_ab);
237
238         
239         prefs_page->local = local_checkbox;
240         prefs_page->full = full_checkbox;
241         prefs_page->whitelist_ab = whitelist_ab_checkbtn;
242         prefs_page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
243         prefs_page->whitelist_ab_select_btn = whitelist_ab_select_btn;
244         prefs_page->page.widget = vbox;
245 }
246
247 static void destroy_dillo_prefs_page(PrefsPage *page)
248 {
249         /* Do nothing! */
250 }
251
252 static void save_dillo_prefs(PrefsPage *page)
253 {
254         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
255         PrefFile *pref_file;
256         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
257                                           COMMON_RC, NULL);
258         
259         dillo_prefs.local = !gtk_toggle_button_get_active
260                                 (GTK_TOGGLE_BUTTON(prefs_page->local));
261         dillo_prefs.full = gtk_toggle_button_get_active
262                                 (GTK_TOGGLE_BUTTON(prefs_page->full));
263
264         dillo_prefs.whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab));
265         g_free(dillo_prefs.whitelist_ab_folder);
266         dillo_prefs.whitelist_ab_folder = gtk_editable_get_chars(
267                                 GTK_EDITABLE(GTK_BIN(prefs_page->whitelist_ab_folder_combo)->child), 0, -1);
268         
269         pref_file = prefs_write_open(rc_file_path);
270         g_free(rc_file_path);
271         
272         if (!(pref_file) ||
273             (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
274           return;
275         
276         if (prefs_write_param(param, pref_file->fp) < 0) {
277           g_warning("failed to write Dillo Plugin configuration\n");
278           prefs_file_close_revert(pref_file);
279           return;
280         }
281
282         if (fprintf(pref_file->fp, "\n") < 0) {
283                 FILE_OP_ERROR(rc_file_path, "fprintf");
284                 prefs_file_close_revert(pref_file);
285         } else
286                 prefs_file_close(pref_file);
287 }