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);
50 #if 0 /* Not needed, but works. */
52 compare_option_strings_for_name (const gchar* a, const gchar* b)
54 /* printf(" debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
55 const gchar* sep = strchr(a, '=');
63 /* Get the part of the string before the =.
64 * Note that this allocation is inefficient just so we can do a strcmp. */
65 gchar* name = g_malloc (len+1);
67 name[len] = 0; /* Null-termination. */
69 /* printf(" debug: name=%s\n", name); */
71 gint result = strcmp (name, b);
79 modest_server_account_data_get_option_string (GSList* options_list, const gchar* option_name)
85 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
87 /* Get the value part of the key=value pair: */
88 const gchar* pair = (const gchar*)option->data;
90 const gchar* sep = strchr(pair, '=');
92 gint len = sep - pair;
94 result = g_strdup(sep+1);
96 /* Avoid returning an empty string instead of NULL. */
97 if(result && strlen(result) == 0) {
109 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
114 gboolean result = FALSE;
115 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
125 modest_server_account_get_secure_auth (ModestAccountMgr *self,
126 const gchar* account_name)
128 ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
129 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
130 TRUE /* server account */);
132 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
133 result = MODEST_PROTOCOL_AUTH_NONE;
134 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
135 result = MODEST_PROTOCOL_AUTH_PASSWORD;
136 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
137 result = MODEST_PROTOCOL_AUTH_CRAMMD5;
146 modest_server_account_set_secure_auth (ModestAccountMgr *self,
147 const gchar* account_name, ModestProtocol secure_auth)
149 /* Get the conf string for the enum value: */
150 const gchar* str_value = NULL;
151 if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
152 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
153 else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
154 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
155 else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
156 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
158 /* Set it in the configuration: */
159 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
163 modest_server_account_get_security (ModestAccountMgr *self,
164 const gchar* account_name)
166 ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
167 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
168 TRUE /* server account */);
170 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
171 result = MODEST_PROTOCOL_SECURITY_NONE;
172 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
173 result = MODEST_PROTOCOL_SECURITY_TLS;
174 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
175 result = MODEST_PROTOCOL_SECURITY_SSL;
184 modest_server_account_set_security (ModestAccountMgr *self,
185 const gchar* account_name, ModestProtocol security)
187 /* Get the conf string for the enum value: */
188 const gchar* str_value = NULL;
189 if (security == MODEST_PROTOCOL_SECURITY_NONE)
190 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
191 else if (security == MODEST_PROTOCOL_SECURITY_TLS)
192 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
193 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
194 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
196 /* Set it in the configuration: */
197 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
202 modest_account_mgr_get_server_account_option (ModestAccountMgr *self,
203 const gchar* account_name, const gchar* option_name)
205 GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
206 MODEST_CONF_VALUE_STRING, TRUE);
210 gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
212 /* TODO: Should we free the items too, or just the list? */
213 g_slist_free (option_list);
219 static ModestServerAccountData*
220 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
222 ModestServerAccountData *data;
225 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
226 data = g_slice_new0 (ModestServerAccountData);
228 data->account_name = g_strdup (name);
229 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
230 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
231 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
232 data->proto = modest_protocol_info_get_protocol (proto);
235 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
237 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
238 data->uri = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
239 data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
240 MODEST_CONF_VALUE_STRING, TRUE);
246 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
247 ModestServerAccountData* data)
249 g_return_if_fail (self);
252 return; /* not an error */
254 g_free (data->account_name);
255 data->account_name = NULL;
257 g_free (data->hostname);
258 data->hostname = NULL;
260 g_free (data->username);
261 data->username = NULL;
263 g_free (data->password);
264 data->password = NULL;
267 GSList *tmp = data->options;
270 tmp = g_slist_next (tmp);
272 g_slist_free (data->options);
275 g_slice_free (ModestServerAccountData, data);
278 /** You must use modest_account_mgr_free_account_data() on the result.
281 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
283 ModestAccountData *data;
284 gchar *server_account;
285 gchar *default_account;
287 g_return_val_if_fail (self, NULL);
288 g_return_val_if_fail (name, NULL);
289 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);
290 data = g_slice_new0 (ModestAccountData);
292 data->account_name = g_strdup (name);
294 data->display_name = modest_account_mgr_get_string (self, name,
295 MODEST_ACCOUNT_DISPLAY_NAME,
297 data->fullname = modest_account_mgr_get_string (self, name,
298 MODEST_ACCOUNT_FULLNAME,
300 data->email = modest_account_mgr_get_string (self, name,
301 MODEST_ACCOUNT_EMAIL,
303 data->is_enabled = modest_account_mgr_get_enabled (self, name);
305 default_account = modest_account_mgr_get_default_account (self);
306 data->is_default = (default_account && strcmp (default_account, name) == 0);
307 g_free (default_account);
310 server_account = modest_account_mgr_get_string (self, name,
311 MODEST_ACCOUNT_STORE_ACCOUNT,
313 if (server_account) {
314 data->store_account =
315 modest_account_mgr_get_server_account_data (self, server_account);
316 g_free (server_account);
320 server_account = modest_account_mgr_get_string (self, name,
321 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
323 if (server_account) {
324 data->transport_account =
325 modest_account_mgr_get_server_account_data (self, server_account);
326 g_free (server_account);
334 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
336 g_return_if_fail (self);
338 if (!data) /* not an error */
341 g_free (data->account_name);
342 g_free (data->display_name);
343 g_free (data->fullname);
344 g_free (data->email);
346 modest_account_mgr_free_server_account_data (self, data->store_account);
347 modest_account_mgr_free_server_account_data (self, data->transport_account);
349 g_slice_free (ModestAccountData, data);
354 modest_account_mgr_get_default_account (ModestAccountMgr *self)
360 g_return_val_if_fail (self, NULL);
362 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
363 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
366 g_printerr ("modest: failed to get '%s': %s\n",
367 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
373 /* it's not really an error if there is no default account */
378 if (!modest_account_mgr_account_exists (self, account, FALSE)) {
379 g_printerr ("modest: default account does not exist\n");
389 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
393 g_return_val_if_fail (self, FALSE);
394 g_return_val_if_fail (account, FALSE);
395 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
398 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
400 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
406 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
408 gchar *fullname, *email, *from;
410 g_return_val_if_fail (self, NULL);
411 g_return_val_if_fail (name, NULL);
413 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
415 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
417 from = g_strdup_printf ("%s <%s>",
418 fullname ? fullname : "",