1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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 #include "claws-features.h"
29 #endif
30 
31 #include "defs.h"
32 
33 #include <glib.h>
34 #include <glib/gi18n.h>
35 #include <gtk/gtk.h>
36 
37 #include "gtkutils.h"
38 #include "utils.h"
39 #include "prefs.h"
40 #include "prefs_gtk.h"
41 #include "prefswindow.h"
42 #include "combobox.h"
43 #include "addressbook.h"
44 
45 #include "dillo_prefs.h"
46 
47 #define PREFS_BLOCK_NAME "Dillo"
48 
49 DilloBrowserPrefs dillo_prefs;
50 
51 typedef struct _DilloBrowserPage DilloBrowserPage;
52 
53 struct _DilloBrowserPage {
54         PrefsPage page;
55         GtkWidget *local;
56 	GtkWidget *whitelist_ab;
57 	GtkWidget *whitelist_ab_folder_combo;
58 	GtkWidget *whitelist_ab_select_btn;
59         GtkWidget *full;
60 };
61 
62 static PrefParam param[] = {
63         {"local_browse", "TRUE", &dillo_prefs.local, P_BOOL, NULL, NULL, NULL},
64         {"full_window", "TRUE", &dillo_prefs.full, P_BOOL, NULL, NULL, NULL},
65 	{"whitelist_ab", "FALSE", &dillo_prefs.whitelist_ab, P_BOOL,
66 	 NULL, NULL, NULL},
67 	{"whitelist_ab_folder", N_("Any"), &dillo_prefs.whitelist_ab_folder, P_STRING,
68 	 NULL, NULL, NULL},
69 
70         {0,0,0,0,0,0,0}
71 };
72 
73 static DilloBrowserPage prefs_page;
74 
75 static void create_dillo_prefs_page	(PrefsPage *page,
76 					 GtkWindow *window,
77 					 gpointer   data);
78 static void destroy_dillo_prefs_page	(PrefsPage *page);
79 static void save_dillo_prefs		(PrefsPage *page);
80 
81 #ifndef USE_ALT_ADDRBOOK
dillo_whitelist_ab_select_cb(GtkWidget * widget,gpointer data)82 static void dillo_whitelist_ab_select_cb(GtkWidget *widget, gpointer data)
83 {
84 	DilloBrowserPage *page = (DilloBrowserPage *) data;
85 	const gchar *folderpath = NULL;
86 	gchar *new_path = NULL;
87 
88 	folderpath = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))));
89 	new_path = addressbook_folder_selection(folderpath);
90 	if (new_path) {
91 		gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), new_path);
92 		g_free(new_path);
93 	}
94 }
95 #endif
96 
local_checkbox_toggled(GtkToggleButton * button,gpointer user_data)97 static void local_checkbox_toggled(GtkToggleButton *button,
98 					  gpointer user_data)
99 {
100 	gboolean active = gtk_toggle_button_get_active(button);
101         DilloBrowserPage *prefs_page = (DilloBrowserPage *) user_data;
102 
103 	gtk_widget_set_sensitive(prefs_page->whitelist_ab, active);
104 	gtk_widget_set_sensitive(prefs_page->whitelist_ab_folder_combo, active &&
105 			gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab)));
106 #ifndef USE_NEW_ADDRBOOK
107 	gtk_widget_set_sensitive(prefs_page->whitelist_ab_select_btn, active &&
108 			gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab)));
109 #endif
110 }
111 
whitelist_checkbox_toggled(GtkToggleButton * button,gpointer user_data)112 static void whitelist_checkbox_toggled(GtkToggleButton *button,
113 					  gpointer user_data)
114 {
115 	gboolean active = gtk_toggle_button_get_active(button);
116         DilloBrowserPage *prefs_page = (DilloBrowserPage *) user_data;
117 
118 	active &= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->local));
119 
120 	gtk_widget_set_sensitive(prefs_page->whitelist_ab_folder_combo, active);
121 	gtk_widget_set_sensitive(prefs_page->whitelist_ab_select_btn, active);
122 }
123 
dillo_prefs_init(void)124 void dillo_prefs_init(void)
125 {
126 	static gchar *path[3];
127 	gchar *rcpath;
128 
129 	path[0] = _("Plugins");
130 	path[1] = _("Dillo Browser");
131 	path[2] = NULL;
132 
133         prefs_set_default(param);
134 	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
135         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
136 	g_free(rcpath);
137 
138         prefs_page.page.path = path;
139         prefs_page.page.create_widget = create_dillo_prefs_page;
140         prefs_page.page.destroy_widget = destroy_dillo_prefs_page;
141         prefs_page.page.save_page = save_dillo_prefs;
142 		prefs_page.page.weight = 35.0;
143         prefs_gtk_register_page((PrefsPage *) &prefs_page);
144 }
145 
dillo_prefs_done(void)146 void dillo_prefs_done(void)
147 {
148         prefs_gtk_unregister_page((PrefsPage *) &prefs_page);
149 }
150 
create_dillo_prefs_page(PrefsPage * page,GtkWindow * window,gpointer data)151 static void create_dillo_prefs_page(PrefsPage *page,
152 				    GtkWindow *window,
153                                     gpointer data)
154 {
155         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
156 
157         GtkWidget *vbox;
158         GtkWidget *local_checkbox;
159         GtkWidget *full_checkbox;
160         GtkWidget *label;
161 	GtkWidget *whitelist_ab_checkbtn;
162 	GtkWidget *whitelist_ab_folder_combo;
163 	GtkWidget *whitelist_ab_select_btn;
164 	GtkWidget *hbox_whitelist, *spacer;
165 
166         vbox = gtk_vbox_new(FALSE, 3);
167         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
168         gtk_widget_show(vbox);
169 
170         local_checkbox = gtk_check_button_new_with_label
171 				(_("Load remote links in mails"));
172         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(local_checkbox),
173                                      !dillo_prefs.local);
174         gtk_box_pack_start(GTK_BOX(vbox), local_checkbox, FALSE, FALSE, 0);
175         gtk_widget_show(local_checkbox);
176 	CLAWS_SET_TIP(local_checkbox,
177 			     _("Equivalent to Dillo's '--local' option"));
178 
179 	label = gtk_label_new(_("You can still load remote links "
180 			      "by reloading the page"));
181         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
182 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
183 	gtkut_widget_set_small_font_size (label);
184         gtk_widget_show(label);
185 
186 
187 	hbox_whitelist = gtk_hbox_new(FALSE, 8);
188 	gtk_widget_show(hbox_whitelist);
189 	gtk_box_pack_start (GTK_BOX (vbox), hbox_whitelist, FALSE, FALSE, 0);
190 
191 	spacer = gtk_hbox_new(FALSE, 0);
192 	gtk_widget_set_size_request(spacer, 12, -1);
193 	gtk_widget_show(spacer);
194 	gtk_box_pack_start(GTK_BOX(hbox_whitelist), spacer, FALSE, FALSE, 0);
195 
196 	whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Only for senders found in address book/folder"));
197 	gtk_widget_show(whitelist_ab_checkbtn);
198 	gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
199 
200 	whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
201 	gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
202 	gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
203 
204 	whitelist_ab_select_btn = gtk_button_new_with_label(_("Select ..."));
205 	gtk_widget_show (whitelist_ab_select_btn);
206 	gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
207 
208 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), dillo_prefs.whitelist_ab);
209 	if (dillo_prefs.whitelist_ab_folder != NULL) {
210 		/* translate "Any" (stored UNtranslated) */
211 		if (strcasecmp(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 					_("Any"));
214 		else
215 		/* backward compatibility (when translated "Any" was stored) */
216 		if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0)
217 			gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
218 					dillo_prefs.whitelist_ab_folder);
219 		else
220 			gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
221 					dillo_prefs.whitelist_ab_folder);
222 	}
223 
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 	CLAWS_SET_TIP(full_checkbox,
231 			     _("Equivalent to Dillo's '--fullwindow' option"));
232 
233 	g_signal_connect(G_OBJECT(local_checkbox), "toggled",
234 			 G_CALLBACK(local_checkbox_toggled),
235 			 prefs_page);
236 
237 	g_signal_connect(G_OBJECT(whitelist_ab_checkbtn), "toggled",
238 			 G_CALLBACK(whitelist_checkbox_toggled),
239 			 prefs_page);
240 
241 #ifndef USE_ALT_ADDRBOOK
242 	g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
243 			 G_CALLBACK(dillo_whitelist_ab_select_cb), prefs_page);
244 #else
245 	gtk_widget_set_sensitive(GTK_WIDGET(whitelist_ab_select_btn), FALSE);
246 #endif
247 	gtk_widget_set_sensitive(whitelist_ab_checkbtn, !dillo_prefs.local);
248 	gtk_widget_set_sensitive(whitelist_ab_folder_combo, !dillo_prefs.local && dillo_prefs.whitelist_ab);
249 	gtk_widget_set_sensitive(whitelist_ab_select_btn, !dillo_prefs.local && dillo_prefs.whitelist_ab);
250 
251 
252         prefs_page->local = local_checkbox;
253         prefs_page->full = full_checkbox;
254 	prefs_page->whitelist_ab = whitelist_ab_checkbtn;
255 	prefs_page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
256 	prefs_page->whitelist_ab_select_btn = whitelist_ab_select_btn;
257         prefs_page->page.widget = vbox;
258 }
259 
destroy_dillo_prefs_page(PrefsPage * page)260 static void destroy_dillo_prefs_page(PrefsPage *page)
261 {
262 	/* Do nothing! */
263 }
264 
save_dillo_prefs(PrefsPage * page)265 static void save_dillo_prefs(PrefsPage *page)
266 {
267         DilloBrowserPage *prefs_page = (DilloBrowserPage *) page;
268         PrefFile *pref_file;
269         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
270                                           COMMON_RC, NULL);
271 
272         dillo_prefs.local = !gtk_toggle_button_get_active
273 				(GTK_TOGGLE_BUTTON(prefs_page->local));
274         dillo_prefs.full = gtk_toggle_button_get_active
275 				(GTK_TOGGLE_BUTTON(prefs_page->full));
276 
277 	dillo_prefs.whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_page->whitelist_ab));
278 	g_free(dillo_prefs.whitelist_ab_folder);
279 	dillo_prefs.whitelist_ab_folder = gtk_editable_get_chars(
280 				GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((prefs_page->whitelist_ab_folder_combo)))), 0, -1);
281 	/* store UNtranslated "Any" */
282 	if (g_utf8_collate(dillo_prefs.whitelist_ab_folder, _("Any")) == 0) {
283 		g_free(dillo_prefs.whitelist_ab_folder);
284 		dillo_prefs.whitelist_ab_folder = g_strdup("Any");
285 	}
286 
287         pref_file = prefs_write_open(rc_file_path);
288         g_free(rc_file_path);
289 
290         if (!(pref_file) ||
291 	    (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
292           return;
293 
294         if (prefs_write_param(param, pref_file->fp) < 0) {
295           g_warning("failed to write Dillo Plugin configuration\n");
296           prefs_file_close_revert(pref_file);
297           return;
298         }
299 
300         if (fprintf(pref_file->fp, "\n") < 0) {
301 		FILE_OP_ERROR(rc_file_path, "fprintf");
302 		prefs_file_close_revert(pref_file);
303 	} else
304 	        prefs_file_close(pref_file);
305 }
306