1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <modest-account-mgr-helpers.h>
31 #include <modest-account-mgr-priv.h>
32 #include <tny-simple-list.h>
33 #include <modest-runtime.h>
37 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
40 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
45 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
47 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
51 compare_option_strings_for_name (const gchar* a, const gchar* b)
53 /* printf(" debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
54 const gchar* sep = strchr(a, '=');
62 /* Get the part of the string before the =.
63 * Note that this allocation is inefficient just so we can do a strcmp. */
64 gchar* name = g_malloc (len+1);
66 name[len] = 0; /* Null-termination. */
68 /* printf(" debug: name=%s\n", name); */
70 gint result = strcmp (name, b);
78 modest_server_account_data_get_option_value (GSList* options_list, const gchar* option_name)
84 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
86 /* Get the value part of the key=value pair: */
87 const gchar* pair = (const gchar*)option->data;
89 const gchar* sep = strchr(pair, '=');
91 gint len = sep - pair;
93 result = g_strdup(sep+1);
95 /* Avoid returning an empty string instead of NULL. */
96 if(result && strlen(result) == 0) {
108 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
113 gboolean result = FALSE;
114 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
123 modest_server_account_get_option_secure_auth (ModestAccountMgr *self,
124 const gchar* account_name)
126 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
127 gchar* value =modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
128 TRUE /* server account */);
130 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
131 result = MODEST_PROTOCOL_AUTH_NONE;
132 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
133 result = MODEST_PROTOCOL_AUTH_PASSWORD;
134 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
135 result = MODEST_PROTOCOL_AUTH_CRAMMD5;
144 modest_server_account_set_option_secure_auth (ModestAccountMgr *self,
145 const gchar* account_name, ModestProtocol secure_auth)
147 /* Get the conf string for the enum value: */
148 const gchar* str_value = NULL;
149 if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
150 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
151 else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
152 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
153 else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
154 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
156 /* Set it in the configuration: */
157 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
161 modest_server_account_data_get_option_security (ModestServerAccountData *account_data)
163 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
164 gchar* value = modest_server_account_data_get_option_value (account_data->options,
165 MODEST_ACCOUNT_OPTION_SECURITY);
167 if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NONE) == 0)
168 result = MODEST_PROTOCOL_SECURITY_NONE;
169 else if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NORMAL) == 0)
170 result = MODEST_PROTOCOL_SECURITY_TLS;
171 else if (strcmp(value, MODEST_ACCOUNT_OPTION_SECURITY_VALUE_SSL) == 0)
172 result = MODEST_PROTOCOL_SECURITY_SSL;
181 modest_server_account_set_option_security (ModestAccountMgr *self,
182 const gchar* account_name, ModestProtocol security)
184 GSList *options_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
185 MODEST_CONF_VALUE_STRING, TRUE);
188 /* Remove the item if it exists already: */
189 GSList* option = NULL;
191 g_slist_find_custom(options_list, MODEST_ACCOUNT_OPTION_SECURITY, (GCompareFunc)compare_option_strings_for_name);
193 options_list = g_slist_remove (options_list, option->data);
197 /* Add the new item to the list: */
198 const gchar* str_value = NULL;
199 if (security == MODEST_PROTOCOL_SECURITY_NONE)
200 str_value = MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NONE;
201 else if (security == MODEST_PROTOCOL_SECURITY_TLS)
202 str_value = MODEST_ACCOUNT_OPTION_SECURITY_VALUE_NORMAL;
203 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
204 str_value = MODEST_ACCOUNT_OPTION_SECURITY_VALUE_SSL;
207 gchar* pair = g_strdup_printf(MODEST_ACCOUNT_OPTION_SECURITY "=%s", str_value);
208 options_list = g_slist_append(options_list, pair);
211 /* Set it in the configuration: */
212 modest_account_mgr_set_list (self, account_name, MODEST_ACCOUNT_OPTIONS, options_list,
213 MODEST_CONF_VALUE_STRING, TRUE);
215 /* TODO: Should we free the items too, or just the list? */
216 g_slist_free (options_list);
220 modest_account_mgr_get_server_account_option (ModestAccountMgr *self,
221 const gchar* account_name, const gchar* option_name)
223 GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
224 MODEST_CONF_VALUE_STRING, TRUE);
228 gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
230 /* TODO: Should we free the items too, or just the list? */
231 g_slist_free (option_list);
236 static ModestServerAccountData*
237 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
239 ModestServerAccountData *data;
242 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
243 data = g_slice_new0 (ModestServerAccountData);
245 data->account_name = g_strdup (name);
246 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
247 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
248 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
249 data->proto = modest_protocol_info_get_protocol (proto);
252 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
254 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
255 data->uri = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
256 data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
257 MODEST_CONF_VALUE_STRING, TRUE);
263 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
264 ModestServerAccountData* data)
266 g_return_if_fail (self);
269 return; /* not an error */
271 g_free (data->account_name);
272 data->account_name = NULL;
274 g_free (data->hostname);
275 data->hostname = NULL;
277 g_free (data->username);
278 data->username = NULL;
280 g_free (data->password);
281 data->password = NULL;
284 GSList *tmp = data->options;
287 tmp = g_slist_next (tmp);
289 g_slist_free (data->options);
292 g_slice_free (ModestServerAccountData, data);
295 /** You must use modest_account_mgr_free_account_data() on the result.
298 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
300 ModestAccountData *data;
301 gchar *server_account;
302 gchar *default_account;
304 g_return_val_if_fail (self, NULL);
305 g_return_val_if_fail (name, NULL);
306 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);
307 data = g_slice_new0 (ModestAccountData);
309 data->account_name = g_strdup (name);
311 data->display_name = modest_account_mgr_get_string (self, name,
312 MODEST_ACCOUNT_DISPLAY_NAME,
314 data->fullname = modest_account_mgr_get_string (self, name,
315 MODEST_ACCOUNT_FULLNAME,
317 data->email = modest_account_mgr_get_string (self, name,
318 MODEST_ACCOUNT_EMAIL,
320 data->is_enabled = modest_account_mgr_get_enabled (self, name);
322 default_account = modest_account_mgr_get_default_account (self);
323 data->is_default = (default_account && strcmp (default_account, name) == 0);
324 g_free (default_account);
327 server_account = modest_account_mgr_get_string (self, name,
328 MODEST_ACCOUNT_STORE_ACCOUNT,
330 if (server_account) {
331 data->store_account =
332 modest_account_mgr_get_server_account_data (self, server_account);
333 g_free (server_account);
337 server_account = modest_account_mgr_get_string (self, name,
338 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
340 if (server_account) {
341 data->transport_account =
342 modest_account_mgr_get_server_account_data (self, server_account);
343 g_free (server_account);
351 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
353 g_return_if_fail (self);
355 if (!data) /* not an error */
358 g_free (data->account_name);
359 g_free (data->display_name);
360 g_free (data->fullname);
361 g_free (data->email);
363 modest_account_mgr_free_server_account_data (self, data->store_account);
364 modest_account_mgr_free_server_account_data (self, data->transport_account);
366 g_slice_free (ModestAccountData, data);
371 modest_account_mgr_get_default_account (ModestAccountMgr *self)
377 g_return_val_if_fail (self, NULL);
379 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
380 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
383 g_printerr ("modest: failed to get '%s': %s\n",
384 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
390 /* it's not really an error if there is no default account */
395 if (!modest_account_mgr_account_exists (self, account, FALSE)) {
396 g_printerr ("modest: default account does not exist\n");
406 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
410 g_return_val_if_fail (self, FALSE);
411 g_return_val_if_fail (account, FALSE);
412 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
415 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
417 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
423 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
425 gchar *fullname, *email, *from;
427 g_return_val_if_fail (self, NULL);
428 g_return_val_if_fail (name, NULL);
430 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
432 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
434 from = g_strdup_printf ("%s <%s>",
435 fullname ? fullname : "",