evolution-3.6.4/libemail-engine/mail-config.c

No issues found

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