1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, see <http://www.gnu.org/licenses/>.
13  *
14  *
15  * Authors:
16  *   Jeffrey Stedfast <fejj@ximian.com>
17  *   Radek Doulik <rodo@ximian.com>
18  *   Jonathon Jongsma <jonathon.jongsma@collabora.co.uk>
19  *
20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21  * Copyright (C) 2009 Intel Corporation
22  *
23  */
24 
25 #include "evolution-config.h"
26 
27 #include <gtk/gtk.h>
28 
29 #include <libedataserver/libedataserver.h>
30 
31 #include "e-mail-folder-utils.h"
32 #include "mail-config.h"
33 #include "mail-tools.h"
34 
35 typedef struct {
36 	GSList *labels;
37 
38 	gboolean address_compress;
39 	gint address_count;
40 	gboolean show_mails_in_preview;
41 
42 	GSList *jh_header;
43 	gboolean jh_check;
44 	gboolean book_lookup;
45 	gboolean book_lookup_local_only;
46 	gchar *local_archive_folder;
47 } MailConfig;
48 
49 extern gint camel_header_param_encode_filenames_in_rfc_2047;
50 
51 static MailConfig *config = NULL;
52 static GSettings *mail_settings = NULL;
53 
54 static void
settings_outlook_filenames_changed(GSettings * settings,const gchar * key,gpointer user_data)55 settings_outlook_filenames_changed (GSettings *settings,
56                                     const gchar *key,
57                                     gpointer user_data)
58 {
59 	/* pass option to the camel */
60 	if (g_settings_get_boolean (settings, key))
61 		camel_header_param_encode_filenames_in_rfc_2047 = 1;
62 	else
63 		camel_header_param_encode_filenames_in_rfc_2047 = 0;
64 }
65 
66 static void
settings_jh_headers_changed(GSettings * settings,const gchar * key,EMailSession * session)67 settings_jh_headers_changed (GSettings *settings,
68                              const gchar *key,
69                              EMailSession *session)
70 {
71 	GSList *node;
72 	GPtrArray *name, *value;
73 	gchar **strv;
74 	gint i;
75 
76 	strv = g_settings_get_strv (settings, "junk-custom-header");
77 	if (key) {
78 		for (i = 0, node = config->jh_header; strv[i] && node; i++, node = g_slist_next (node)) {
79 			if (g_strcmp0 (node->data, strv[i]) != 0)
80 				break;
81 		}
82 
83 		/* both lists are read to the end, thus they are the same */
84 		if (!node && !strv[i]) {
85 			g_strfreev (strv);
86 			return;
87 		}
88 	}
89 
90 	g_slist_foreach (config->jh_header, (GFunc) g_free, NULL);
91 	g_slist_free (config->jh_header);
92 	config->jh_header = NULL;
93 
94 	for (i = 0; strv[i] != NULL; i++)
95 		config->jh_header = g_slist_append (config->jh_header, g_strdup (strv[i]));
96 	g_strfreev (strv);
97 
98 	node = config->jh_header;
99 	name = g_ptr_array_new ();
100 	value = g_ptr_array_new ();
101 	while (node && node->data) {
102 		gchar **tok = g_strsplit (node->data, "=", 2);
103 		g_ptr_array_add (name, g_strdup (tok[0]));
104 		g_ptr_array_add (value, g_strdup (tok[1]));
105 		node = node->next;
106 		g_strfreev (tok);
107 	}
108 	camel_session_set_junk_headers (
109 		CAMEL_SESSION (session),
110 		(const gchar **) name->pdata,
111 		(const gchar **) value->pdata, name->len);
112 
113 	g_ptr_array_foreach (name, (GFunc) g_free, NULL);
114 	g_ptr_array_foreach (value, (GFunc) g_free, NULL);
115 	g_ptr_array_free (name, TRUE);
116 	g_ptr_array_free (value, TRUE);
117 }
118 
119 static void
settings_jh_check_changed(GSettings * settings,const gchar * key,EMailSession * session)120 settings_jh_check_changed (GSettings *settings,
121                            const gchar *key,
122                            EMailSession *session)
123 {
124 	if (key && config->jh_check == g_settings_get_boolean (settings, "junk-check-custom-header"))
125 		return;
126 
127 	config->jh_check = g_settings_get_boolean (settings, "junk-check-custom-header");
128 	if (!config->jh_check) {
129 		camel_session_set_junk_headers (
130 			CAMEL_SESSION (session), NULL, NULL, 0);
131 	} else {
132 		settings_jh_headers_changed (settings, NULL, session);
133 	}
134 }
135 
136 static void
settings_bool_value_changed(GSettings * settings,const gchar * key,gboolean * save_location)137 settings_bool_value_changed (GSettings *settings,
138                              const gchar *key,
139                              gboolean *save_location)
140 {
141 	*save_location = g_settings_get_boolean (settings, key);
142 }
143 
144 static void
settings_int_value_changed(GSettings * settings,const gchar * key,gint * save_location)145 settings_int_value_changed (GSettings *settings,
146                             const gchar *key,
147                             gint *save_location)
148 {
149 	*save_location = g_settings_get_int (settings, key);
150 }
151 
152 static void
settings_string_value_changed(GSettings * settings,const gchar * key,gchar ** save_location)153 settings_string_value_changed (GSettings *settings,
154 			       const gchar *key,
155                                gchar **save_location)
156 {
157 	g_free (*save_location);
158 	*save_location = g_settings_get_string (settings, key);
159 }
160 
161 gint
mail_config_get_address_count(void)162 mail_config_get_address_count (void)
163 {
164 	if (!config->address_compress)
165 		return -1;
166 
167 	return config->address_count;
168 }
169 
170 gboolean
mail_config_get_show_mails_in_preview(void)171 mail_config_get_show_mails_in_preview (void)
172 {
173 	return config->show_mails_in_preview;
174 }
175 
176 /* timeout interval, in seconds, when to call server update */
177 gint
mail_config_get_sync_timeout(void)178 mail_config_get_sync_timeout (void)
179 {
180 	gint res = 60;
181 
182 	res = g_settings_get_int (mail_settings, "sync-interval");
183 
184 	/* do not allow recheck sooner than every 30 seconds */
185 	if (res == 0)
186 		res = 60;
187 	else if (res < 30)
188 		res = 30;
189 
190 	return res;
191 }
192 
193 gchar *
mail_config_folder_to_cachename(CamelFolder * folder,const gchar * prefix)194 mail_config_folder_to_cachename (CamelFolder *folder,
195                                  const gchar *prefix)
196 {
197 	gchar *folder_uri, *basename, *filename;
198 	const gchar *config_dir;
199 
200 	config_dir = mail_session_get_config_dir ();
201 
202 	basename = g_build_filename (config_dir, "folders", NULL);
203 	if (!g_file_test (basename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
204 		/* create the folder if does not exist */
205 		g_mkdir_with_parents (basename, 0700);
206 	}
207 	g_free (basename);
208 
209 	folder_uri = e_mail_folder_uri_from_folder (folder);
210 	e_util_make_safe_filename (folder_uri);
211 	basename = g_strdup_printf ("%s%s", prefix, folder_uri);
212 	filename = g_build_filename (config_dir, "folders", basename, NULL);
213 	g_free (basename);
214 	g_free (folder_uri);
215 
216 	return filename;
217 }
218 
219 void
mail_config_reload_junk_headers(EMailSession * session)220 mail_config_reload_junk_headers (EMailSession *session)
221 {
222 	g_return_if_fail (E_IS_MAIL_SESSION (session));
223 
224 	/* It automatically sets in the session */
225 	if (config == NULL)
226 		mail_config_init (session);
227 	else {
228 		settings_jh_check_changed (mail_settings, NULL, session);
229 	}
230 }
231 
232 gboolean
mail_config_get_lookup_book(void)233 mail_config_get_lookup_book (void)
234 {
235 	g_return_val_if_fail (config != NULL, FALSE);
236 
237 	return config->book_lookup;
238 }
239 
240 gboolean
mail_config_get_lookup_book_local_only(void)241 mail_config_get_lookup_book_local_only (void)
242 {
243 	g_return_val_if_fail (config != NULL, FALSE);
244 
245 	return config->book_lookup_local_only;
246 }
247 
248 gchar *
mail_config_dup_local_archive_folder(void)249 mail_config_dup_local_archive_folder (void)
250 {
251 	g_return_val_if_fail (config != NULL, NULL);
252 
253 	return g_strdup (config->local_archive_folder);
254 }
255 
256 /* Config struct routines */
257 void
mail_config_init(EMailSession * session)258 mail_config_init (EMailSession *session)
259 {
260 	g_return_if_fail (E_IS_MAIL_SESSION (session));
261 
262 	if (config)
263 		return;
264 
265 	config = g_new0 (MailConfig, 1);
266 
267 	mail_settings = e_util_ref_settings ("org.gnome.evolution.mail");
268 
269 	/* Composer Configuration */
270 
271 	settings_outlook_filenames_changed (
272 		mail_settings, "composer-outlook-filenames", NULL);
273 	g_signal_connect (
274 		mail_settings, "changed::composer-outlook-filenames",
275 		G_CALLBACK (settings_outlook_filenames_changed), NULL);
276 
277 	/* Display Configuration */
278 
279 	g_signal_connect (
280 		mail_settings, "changed::address-compress",
281 		G_CALLBACK (settings_bool_value_changed),
282 		&config->address_compress);
283 	config->address_compress = g_settings_get_boolean (
284 		mail_settings, "address-compress");
285 
286 	g_signal_connect (
287 		mail_settings, "changed::address-count",
288 		G_CALLBACK (settings_int_value_changed),
289 		&config->address_count);
290 	config->address_count = g_settings_get_int (
291 		mail_settings, "address-count");
292 
293 	g_signal_connect (
294 		mail_settings, "changed::show-mails-in-preview",
295 		G_CALLBACK (settings_bool_value_changed),
296 		&config->show_mails_in_preview);
297 	config->show_mails_in_preview = g_settings_get_boolean (
298 		mail_settings, "show-mails-in-preview");
299 
300 	/* Junk Configuration */
301 
302 	g_signal_connect (
303 		mail_settings, "changed::junk-check-custom-header",
304 		G_CALLBACK (settings_jh_check_changed), session);
305 	config->jh_check = g_settings_get_boolean (
306 		mail_settings, "junk-check-custom-header");
307 
308 	g_signal_connect (
309 		mail_settings, "changed::junk-custom-header",
310 		G_CALLBACK (settings_jh_headers_changed), session);
311 
312 	g_signal_connect (
313 		mail_settings, "changed::junk-lookup-addressbook",
314 		G_CALLBACK (settings_bool_value_changed), &config->book_lookup);
315 	config->book_lookup = g_settings_get_boolean (
316 		mail_settings, "junk-lookup-addressbook");
317 
318 	g_signal_connect (
319 		mail_settings, "changed::junk-lookup-addressbook-local-only",
320 		G_CALLBACK (settings_bool_value_changed),
321 		&config->book_lookup_local_only);
322 	config->book_lookup_local_only = g_settings_get_boolean (
323 		mail_settings, "junk-lookup-addressbook-local-only");
324 
325 	g_signal_connect (
326 		mail_settings, "changed::local-archive-folder",
327 		G_CALLBACK (settings_string_value_changed),
328 		&config->local_archive_folder);
329 	config->local_archive_folder = g_settings_get_string (
330 		mail_settings, "local-archive-folder");
331 
332 	settings_jh_check_changed (mail_settings, NULL, session);
333 }
334