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 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name,
51 const gchar* signature, gboolean use_signature)
53 gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE,
54 use_signature, FALSE);
55 result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE,
60 gchar* modest_account_mgr_get_display_name (ModestAccountMgr *self,
63 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
68 gchar* modest_account_mgr_get_signature (ModestAccountMgr *self, const gchar* name,
69 gboolean* use_signature)
73 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
76 return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
80 gboolean modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self,
81 const gchar* account_name,
82 const gchar* connection_name, const gchar* server_account_name)
84 modest_account_mgr_remove_connection_specific_smtp (self, account_name, connection_name);
86 GSList *list = modest_account_mgr_get_list (self, account_name,
87 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
88 MODEST_CONF_VALUE_STRING, FALSE);
90 /* The server account is in the item after the connection name: */
91 GSList *list_connection = g_slist_append (list, (gpointer)connection_name);
92 list_connection = g_slist_append (list_connection, (gpointer)server_account_name);
94 /* Reset the changed list: */
95 modest_account_mgr_set_list (self, account_name,
96 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list_connection,
97 MODEST_CONF_VALUE_STRING, FALSE);
99 /* TODO: Should we free the items too, or just the list? */
106 * modest_account_mgr_remove_connection_specific_smtp
107 * @self: a ModestAccountMgr instance
108 * @name: the account name
109 * @connection_name: A libconic IAP connection name
111 * Disassacoiate a server account to use with the specific connection for this account.
113 * Returns: TRUE if it worked, FALSE otherwise
115 gboolean modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self,
116 const gchar* account_name, const gchar* connection_name)
118 GSList *list = modest_account_mgr_get_list (self, account_name,
119 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
120 MODEST_CONF_VALUE_STRING, FALSE);
124 /* The server account is in the item after the connection name: */
125 GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
126 if (list_connection) {
127 /* remove both items: */
128 GSList *temp = g_slist_delete_link(list_connection, list_connection);
129 temp = g_slist_delete_link(temp, g_slist_next(temp));
132 /* Reset the changed list: */
133 modest_account_mgr_set_list (self, account_name,
134 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
135 MODEST_CONF_VALUE_STRING, FALSE);
137 /* TODO: Should we free the items too, or just the list? */
143 gboolean modest_account_mgr_get_has_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
145 GSList *list = modest_account_mgr_get_list (self, account_name,
146 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
147 MODEST_CONF_VALUE_STRING, FALSE);
151 /* TODO: Should we free the items too, or just the list? */
158 * modest_account_mgr_get_connection_specific_smtp
159 * @self: a ModestAccountMgr instance
160 * @name: the account name
161 * @connection_name: A libconic IAP connection name
163 * Retrieve a server account to use with this specific connection for this account.
165 * Returns: a server account name to use for this connection, or NULL if none is specified.
167 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
168 const gchar* connection_name)
170 gchar *result = NULL;
172 GSList *list = modest_account_mgr_get_list (self, account_name,
173 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
174 MODEST_CONF_VALUE_STRING, FALSE);
178 /* The server account is in the item after the connection name: */
181 const gchar* this_connection_name = (const gchar*)(iter->data);
182 if (strcmp (this_connection_name, connection_name) == 0) {
183 iter = g_slist_next (iter);
186 const gchar* account_name = (const gchar*)(iter->data);
188 result = g_strdup (account_name);
194 /* Skip 2 to go to the next connection in the list: */
195 iter = g_slist_next (iter);
197 iter = g_slist_next (iter);
202 printf (" debug: no server found for connection_name=%s.\n", connection_name);
206 /* TODO: Should we free the items too, or just the list? */
213 modest_server_account_get_username (ModestAccountMgr *self, const gchar* account_name)
215 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME,
216 TRUE /* server account */);
220 modest_server_account_set_username (ModestAccountMgr *self, const gchar* account_name,
221 const gchar* username)
223 /* Note that this won't work properly as long as the gconf cache is broken
225 gchar *existing_username = modest_server_account_get_username(self,
228 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
229 username, TRUE /* server account */);
231 /* We don't know anything about new usernames: */
232 if (strcmp (existing_username, username) != 0)
233 modest_server_account_set_username_has_succeeded (self,
234 account_name, FALSE);
236 g_free (existing_username);
240 modest_server_account_get_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
242 return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
243 TRUE /* server account */);
247 modest_server_account_set_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name,
250 modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED,
251 succeeded, TRUE /* server account */);
255 modest_server_account_set_password (ModestAccountMgr *self, const gchar* account_name,
256 const gchar* password)
258 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
259 password, TRUE /* server account */);
263 modest_server_account_get_hostname (ModestAccountMgr *self, const gchar* account_name)
265 return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_HOSTNAME,
266 TRUE /* server account */);
270 static ModestAuthProtocol
271 get_secure_auth_for_conf_string(const gchar* value)
273 ModestAuthProtocol result = MODEST_PROTOCOL_AUTH_NONE;
275 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
276 result = MODEST_PROTOCOL_AUTH_NONE;
277 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
278 result = MODEST_PROTOCOL_AUTH_PASSWORD;
279 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
280 result = MODEST_PROTOCOL_AUTH_CRAMMD5;
287 modest_server_account_get_secure_auth (ModestAccountMgr *self,
288 const gchar* account_name)
290 ModestAuthProtocol result = MODEST_PROTOCOL_AUTH_NONE;
291 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH,
292 TRUE /* server account */);
294 result = get_secure_auth_for_conf_string (value);
304 modest_server_account_set_secure_auth (ModestAccountMgr *self,
305 const gchar* account_name, ModestAuthProtocol secure_auth)
307 /* Get the conf string for the enum value: */
308 const gchar* str_value = NULL;
309 if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
310 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
311 else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
312 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
313 else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
314 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
316 /* Set it in the configuration: */
317 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
320 static ModestConnectionProtocol
321 get_security_for_conf_string(const gchar* value)
323 ModestConnectionProtocol result = MODEST_PROTOCOL_CONNECTION_NORMAL;
325 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
326 result = MODEST_PROTOCOL_CONNECTION_NORMAL;
327 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0) {
328 /* The UI has "Normal (TLS)": */
329 result = MODEST_PROTOCOL_CONNECTION_TLS;
330 } else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
331 result = MODEST_PROTOCOL_CONNECTION_SSL;
337 ModestConnectionProtocol
338 modest_server_account_get_security (ModestAccountMgr *self,
339 const gchar* account_name)
341 ModestConnectionProtocol result = MODEST_PROTOCOL_CONNECTION_NORMAL;
342 gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY,
343 TRUE /* server account */);
345 result = get_security_for_conf_string (value);
354 modest_server_account_set_security (ModestAccountMgr *self,
355 const gchar* account_name, ModestConnectionProtocol security)
357 /* Get the conf string for the enum value: */
358 const gchar* str_value = NULL;
359 if (security == MODEST_PROTOCOL_CONNECTION_NORMAL)
360 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
361 else if (security == MODEST_PROTOCOL_CONNECTION_TLS) {
362 /* The UI has "Normal (TLS)": */
363 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
364 } else if (security == MODEST_PROTOCOL_CONNECTION_SSL)
365 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
367 /* Set it in the configuration: */
368 modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
371 ModestServerAccountData*
372 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
374 ModestServerAccountData *data;
377 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
378 data = g_slice_new0 (ModestServerAccountData);
380 data->account_name = g_strdup (name);
381 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
382 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
383 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
384 data->proto = modest_protocol_info_get_transport_store_protocol (proto);
387 data->port = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE);
389 gchar *secure_auth_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
390 data->secure_auth = get_secure_auth_for_conf_string(secure_auth_str);
391 g_free (secure_auth_str);
393 gchar *security_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
394 data->security = get_security_for_conf_string(security_str);
395 g_free (security_str);
397 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
399 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
406 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
407 ModestServerAccountData* data)
409 g_return_if_fail (self);
412 return; /* not an error */
414 g_free (data->account_name);
415 data->account_name = NULL;
417 g_free (data->hostname);
418 data->hostname = NULL;
420 g_free (data->username);
421 data->username = NULL;
423 g_free (data->password);
424 data->password = NULL;
426 g_slice_free (ModestServerAccountData, data);
429 /** You must use modest_account_mgr_free_account_data() on the result.
432 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
434 ModestAccountData *data;
435 gchar *server_account;
436 gchar *default_account;
438 g_return_val_if_fail (self, NULL);
439 g_return_val_if_fail (name, NULL);
441 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
442 /* For instance, maybe you are mistakenly checking for a server account name? */
443 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
447 data = g_slice_new0 (ModestAccountData);
449 data->account_name = g_strdup (name);
451 data->display_name = modest_account_mgr_get_string (self, name,
452 MODEST_ACCOUNT_DISPLAY_NAME,
454 data->fullname = modest_account_mgr_get_string (self, name,
455 MODEST_ACCOUNT_FULLNAME,
457 data->email = modest_account_mgr_get_string (self, name,
458 MODEST_ACCOUNT_EMAIL,
460 data->is_enabled = modest_account_mgr_get_enabled (self, name);
462 default_account = modest_account_mgr_get_default_account (self);
463 data->is_default = (default_account && strcmp (default_account, name) == 0);
464 g_free (default_account);
467 server_account = modest_account_mgr_get_string (self, name,
468 MODEST_ACCOUNT_STORE_ACCOUNT,
470 if (server_account) {
471 data->store_account =
472 modest_account_mgr_get_server_account_data (self, server_account);
473 g_free (server_account);
477 server_account = modest_account_mgr_get_string (self, name,
478 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
480 if (server_account) {
481 data->transport_account =
482 modest_account_mgr_get_server_account_data (self, server_account);
483 g_free (server_account);
491 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
493 g_return_if_fail (self);
495 if (!data) /* not an error */
498 g_free (data->account_name);
499 g_free (data->display_name);
500 g_free (data->fullname);
501 g_free (data->email);
503 modest_account_mgr_free_server_account_data (self, data->store_account);
504 modest_account_mgr_free_server_account_data (self, data->transport_account);
506 g_slice_free (ModestAccountData, data);
511 modest_account_mgr_get_default_account (ModestAccountMgr *self)
517 g_return_val_if_fail (self, NULL);
519 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
520 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
523 g_printerr ("modest: failed to get '%s': %s\n",
524 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
531 if (account && !modest_account_mgr_account_exists (self, account, FALSE)) {
532 g_printerr ("modest: default account does not exist\n");
542 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
546 g_return_val_if_fail (self, FALSE);
547 g_return_val_if_fail (account, FALSE);
548 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
551 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
553 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
559 modest_account_mgr_unset_default_account (ModestAccountMgr *self)
563 g_return_val_if_fail (self, FALSE);
565 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
567 return modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
571 gint on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
573 return g_utf8_collate((const gchar*)a, (const gchar*)b);
577 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
579 gboolean result = FALSE;
580 GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
582 /* Return TRUE if there is no account */
586 /* Get the first one, alphabetically, by title: */
587 GSList* list_sorted = g_slist_sort (account_names,
588 on_accounts_list_sort_by_title);
590 const gchar* account_name = (const gchar*)list_sorted->data;
592 result = modest_account_mgr_set_default_account (self, account_name);
595 modest_account_mgr_free_account_names (account_names);
596 account_names = NULL;
602 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
604 gchar *fullname, *email, *from;
606 g_return_val_if_fail (self, NULL);
607 g_return_val_if_fail (name, NULL);
609 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
611 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
613 from = g_strdup_printf ("%s <%s>",
614 fullname ? fullname : "",
622 /* Add a number to the end of the text, or increment a number that is already there.
625 util_increment_name (const gchar* text)
627 /* Get the end character,
628 * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
630 const gchar* end = NULL;
631 if (!g_utf8_validate (text, -1, &end))
637 --end; /* Go to before the null-termination. */
639 /* Look at each UTF-8 characer, starting at the end: */
640 const gchar* p = end;
641 const gchar* alpha_end = NULL;
644 /* Stop when we reach the first character that is not a numeric digit: */
645 const gunichar ch = g_utf8_get_char (p);
646 if (!g_unichar_isdigit (ch)) {
651 p = g_utf8_prev_char (p);
655 /* The text must consist completely of numeric digits. */
661 /* Intepret and increment the number, if any: */
662 gint num = atol (alpha_end);
665 /* Get the name part: */
666 gint name_len = alpha_end - text;
667 gchar *name_without_number = g_malloc(name_len + 1);
668 memcpy (name_without_number, text, name_len);
669 name_without_number[name_len] = 0;\
671 /* Concatenate the text part and the new number: */
672 gchar *result = g_strdup_printf("%s%d", name_without_number, num);
673 g_free (name_without_number);
679 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
680 gboolean server_account)
682 gchar *account_name = g_strdup (starting_name);
684 while (modest_account_mgr_account_exists (self,
685 account_name, server_account /* server_account */)) {
687 gchar * account_name2 = util_increment_name (account_name);
688 g_free (account_name);
689 account_name = account_name2;
696 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
698 gchar *account_name = g_strdup (starting_name);
700 while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
702 gchar * account_name2 = util_increment_name (account_name);
703 g_free (account_name);
704 account_name = account_name2;