mail-config.c 8.32 KB
Newer Older
1
/*
2 3 4
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
5
 *
6 7 8 9
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
10
 *
11 12
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
13 14 15
 *
 *
 * Authors:
16 17 18
 *   Jeffrey Stedfast <fejj@ximian.com>
 *   Radek Doulik <rodo@ximian.com>
 *   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
#ifdef HAVE_CONFIG_H
26
#include <config.h>
27 28
#endif

29
#include <gtk/gtk.h>
30

31
#include <libedataserver/libedataserver.h>
32

33
#include "e-mail-folder-utils.h"
34
#include "mail-config.h"
35
#include "mail-tools.h"
36

37
typedef struct {
38
	GSList *labels;
39

40
	gboolean address_compress;
41
	gint address_count;
42 43 44 45

	GSList *jh_header;
	gboolean jh_check;
	gboolean book_lookup;
Milan Crha's avatar
Milan Crha committed
46
	gboolean book_lookup_local_only;
47
	gchar *local_archive_folder;
JP Rosevear's avatar
JP Rosevear committed
48 49
} MailConfig;

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,
Matthew Barnes's avatar
Matthew Barnes committed
57 58
                                    const gchar *key,
                                    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 65 66
		camel_header_param_encode_filenames_in_rfc_2047 = 0;
}

67
static void
68
settings_jh_headers_changed (GSettings *settings,
Matthew Barnes's avatar
Matthew Barnes committed
69 70
                             const gchar *key,
                             EMailSession *session)
71
{
72
	GSList *node;
73
	GPtrArray *name, *value;
74 75
	gchar **strv;
	gint i;
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90
	strv = g_settings_get_strv (settings, "junk-custom-header");
	if (key) {
		for (i = 0, node = config->jh_header; strv[i] && node; i++, node = g_slist_next (node)) {
			if (g_strcmp0 (node->data, strv[i]) != 0)
				break;
		}

		/* both lists are read to the end, thus they are the same */
		if (!node && !strv[i]) {
			g_strfreev (strv);
			return;
		}
	}

91 92
	g_slist_foreach (config->jh_header, (GFunc) g_free, NULL);
	g_slist_free (config->jh_header);
93
	config->jh_header = NULL;
94

95 96 97
	for (i = 0; strv[i] != NULL; i++)
		config->jh_header = g_slist_append (config->jh_header, g_strdup (strv[i]));
	g_strfreev (strv);
98

99
	node = config->jh_header;
100 101 102
	name = g_ptr_array_new ();
	value = g_ptr_array_new ();
	while (node && node->data) {
103
		gchar **tok = g_strsplit (node->data, "=", 2);
Matthew Barnes's avatar
Matthew Barnes committed
104 105
		g_ptr_array_add (name, g_strdup (tok[0]));
		g_ptr_array_add (value, g_strdup (tok[1]));
106 107 108
		node = node->next;
		g_strfreev (tok);
	}
109
	camel_session_set_junk_headers (
110 111
		CAMEL_SESSION (session),
		(const gchar **) name->pdata,
112
		(const gchar **) value->pdata, name->len);
113 114 115 116 117 118 119 120

	g_ptr_array_foreach (name, (GFunc) g_free, NULL);
	g_ptr_array_foreach (value, (GFunc) g_free, NULL);
	g_ptr_array_free (name, TRUE);
	g_ptr_array_free (value, TRUE);
}

static void
121
settings_jh_check_changed (GSettings *settings,
Matthew Barnes's avatar
Matthew Barnes committed
122 123
                           const gchar *key,
                           EMailSession *session)
124
{
125 126 127
	if (key && config->jh_check == g_settings_get_boolean (settings, "junk-check-custom-header"))
		return;

128
	config->jh_check = g_settings_get_boolean (settings, "junk-check-custom-header");
129
	if (!config->jh_check) {
130 131
		camel_session_set_junk_headers (
			CAMEL_SESSION (session), NULL, NULL, 0);
132
	} else {
133
		settings_jh_headers_changed (settings, NULL, session);
134
	}
135 136
}

137
static void
138
settings_bool_value_changed (GSettings *settings,
Matthew Barnes's avatar
Matthew Barnes committed
139 140
                             const gchar *key,
                             gboolean *save_location)
141
{
142
	*save_location = g_settings_get_boolean (settings, key);
143 144 145
}

static void
146
settings_int_value_changed (GSettings *settings,
Matthew Barnes's avatar
Matthew Barnes committed
147 148
                            const gchar *key,
                            gint *save_location)
149
{
150
	*save_location = g_settings_get_int (settings, key);
151 152
}

153 154 155 156 157 158 159 160 161
static void
settings_string_value_changed (GSettings *settings,
			       const gchar *key,
                               gchar **save_location)
{
	g_free (*save_location);
	*save_location = g_settings_get_string (settings, key);
}

162
gint
163
mail_config_get_address_count (void)
164
{
165 166 167 168 169 170
	if (!config->address_compress)
		return -1;

	return config->address_count;
}

Milan Crha's avatar
Milan Crha committed
171 172 173 174 175 176
/* timeout interval, in seconds, when to call server update */
gint
mail_config_get_sync_timeout (void)
{
	gint res = 60;

177
	res = g_settings_get_int (mail_settings, "sync-interval");
Milan Crha's avatar
Milan Crha committed
178

179
	/* do not allow recheck sooner than every 30 seconds */
180
	if (res == 0)
181 182 183
		res = 60;
	else if (res < 30)
		res = 30;
Milan Crha's avatar
Milan Crha committed
184 185 186 187

	return res;
}

188
gchar *
189 190
mail_config_folder_to_cachename (CamelFolder *folder,
                                 const gchar *prefix)
191
{
192
	gchar *folder_uri, *basename, *filename;
193
	const gchar *config_dir;
194

195
	config_dir = mail_session_get_config_dir ();
196 197 198 199 200 201 202 203

	basename = g_build_filename (config_dir, "folders", NULL);
	if (!g_file_test (basename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		/* create the folder if does not exist */
		g_mkdir_with_parents (basename, 0700);
	}
	g_free (basename);

204 205 206
	folder_uri = e_mail_folder_uri_from_folder (folder);
	e_filename_make_safe (folder_uri);
	basename = g_strdup_printf ("%s%s", prefix, folder_uri);
207
	filename = g_build_filename (config_dir, "folders", basename, NULL);
208
	g_free (basename);
209
	g_free (folder_uri);
210

211 212 213
	return filename;
}

214
void
215
mail_config_reload_junk_headers (EMailSession *session)
216
{
217
	g_return_if_fail (E_IS_MAIL_SESSION (session));
218

219 220
	/* It automatically sets in the session */
	if (config == NULL)
221
		mail_config_init (session);
222
	else {
223
		settings_jh_check_changed (mail_settings, NULL, session);
224
	}
225 226 227
}

gboolean
Milan Crha's avatar
Milan Crha committed
228
mail_config_get_lookup_book (void)
229
{
230
	g_return_val_if_fail (config != NULL, FALSE);
231 232

	return config->book_lookup;
Milan Crha's avatar
Milan Crha committed
233 234 235 236 237
}

gboolean
mail_config_get_lookup_book_local_only (void)
{
238
	g_return_val_if_fail (config != NULL, FALSE);
239

Milan Crha's avatar
Milan Crha committed
240
	return config->book_lookup_local_only;
241
}
242

243 244 245 246 247 248 249 250
gchar *
mail_config_dup_local_archive_folder (void)
{
	g_return_val_if_fail (config != NULL, NULL);

	return g_strdup (config->local_archive_folder);
}

251 252
/* Config struct routines */
void
253
mail_config_init (EMailSession *session)
254
{
255
	g_return_if_fail (E_IS_MAIL_SESSION (session));
256

257 258 259 260 261
	if (config)
		return;

	config = g_new0 (MailConfig, 1);

262
	mail_settings = g_settings_new ("org.gnome.evolution.mail");
263

264 265
	/* Composer Configuration */

266 267
	settings_outlook_filenames_changed (
		mail_settings, "composer-outlook-filenames", NULL);
Matthew Barnes's avatar
Matthew Barnes committed
268 269 270
	g_signal_connect (
		mail_settings, "changed::composer-outlook-filenames",
		G_CALLBACK (settings_outlook_filenames_changed), NULL);
271 272 273

	/* Display Configuration */

Matthew Barnes's avatar
Matthew Barnes committed
274 275
	g_signal_connect (
		mail_settings, "changed::address-compress",
276 277 278 279
		G_CALLBACK (settings_bool_value_changed),
		&config->address_compress);
	config->address_compress = g_settings_get_boolean (
		mail_settings, "address-compress");
280

Matthew Barnes's avatar
Matthew Barnes committed
281 282
	g_signal_connect (
		mail_settings, "changed::address-count",
283 284 285 286
		G_CALLBACK (settings_int_value_changed),
		&config->address_count);
	config->address_count = g_settings_get_int (
		mail_settings, "address-count");
287

288
	/* Junk Configuration */
289

Matthew Barnes's avatar
Matthew Barnes committed
290 291 292
	g_signal_connect (
		mail_settings, "changed::junk-check-custom-header",
		G_CALLBACK (settings_jh_check_changed), session);
293 294
	config->jh_check = g_settings_get_boolean (
		mail_settings, "junk-check-custom-header");
295

Matthew Barnes's avatar
Matthew Barnes committed
296 297 298
	g_signal_connect (
		mail_settings, "changed::junk-custom-header",
		G_CALLBACK (settings_jh_headers_changed), session);
299

Matthew Barnes's avatar
Matthew Barnes committed
300 301 302
	g_signal_connect (
		mail_settings, "changed::junk-lookup-addressbook",
		G_CALLBACK (settings_bool_value_changed), &config->book_lookup);
303 304
	config->book_lookup = g_settings_get_boolean (
		mail_settings, "junk-lookup-addressbook");
305

Matthew Barnes's avatar
Matthew Barnes committed
306 307
	g_signal_connect (
		mail_settings, "changed::junk-lookup-addressbook-local-only",
308 309 310 311
		G_CALLBACK (settings_bool_value_changed),
		&config->book_lookup_local_only);
	config->book_lookup_local_only = g_settings_get_boolean (
		mail_settings, "junk-lookup-addressbook-local-only");
312

313 314 315 316 317 318 319
	g_signal_connect (
		mail_settings, "changed::local-archive-folder",
		G_CALLBACK (settings_string_value_changed),
		&config->local_archive_folder);
	config->local_archive_folder = g_settings_get_string (
		mail_settings, "local-archive-folder");

320
	settings_jh_check_changed (mail_settings, NULL, session);
321
}