2007-05-08 Murray Cumming <murrayc@murrayc.com>
[modest] / src / modest-account-mgr-helpers.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
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>
34 #include <string.h>
35
36 gboolean
37 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
38                                         gboolean enabled)
39 {
40         return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
41 }
42
43
44 gboolean
45 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
46 {
47         return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
48 }
49
50 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name, 
51         const gchar* signature, gboolean use_signature)
52 {
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, 
56                 signature, FALSE);
57         return result;
58 }
59
60 gchar* modest_account_mgr_get_display_name (ModestAccountMgr *self, 
61         const gchar* name)
62 {
63         return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
64 }
65
66
67
68 gchar* modest_account_mgr_get_signature (ModestAccountMgr *self, const gchar* name, 
69         gboolean* use_signature)
70 {
71         if (use_signature) {
72                 *use_signature = 
73                         modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
74         }
75         
76         return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
77 }
78         
79         
80
81 #if 0 /* Not needed, but works. */
82 static gint
83 compare_option_strings_for_name (const gchar* a, const gchar* b)
84 {
85         /* printf("  debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
86         const gchar* sep = strchr(a, '=');
87         if (!sep)
88                 return -1;
89                 
90         gint len = sep - a;
91         if(len <= 0)
92                 return -1;
93                 
94         /* Get the part of the string before the =.
95          * Note that this allocation is inefficient just so we can do a strcmp. */
96         gchar* name = g_malloc (len+1);
97         memcpy(name, a, len);
98         name[len] = 0; /* Null-termination. */
99         
100         /* printf("    debug: name=%s\n", name); */
101
102         gint result = strcmp (name, b);
103         
104         g_free (name);
105         
106         return result;
107 }
108            
109 gchar*
110 modest_server_account_data_get_option_string (GSList* options_list, const gchar* option_name)
111 {
112         if (!options_list)
113                 return NULL;
114         
115         gchar *result = NULL;
116         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
117         if(option) {
118                 /* Get the value part of the key=value pair: */
119                 const gchar* pair = (const gchar*)option->data;
120                 
121                 const gchar* sep = strchr(pair, '=');
122                 if (sep) {
123                         gint len = sep - pair;
124                         if(len > 0) {
125                                 result = g_strdup(sep+1);
126                                 
127                                 /* Avoid returning an empty string instead of NULL. */
128                                 if(result && strlen(result) == 0) {
129                                         g_free(result);
130                                         result = NULL;
131                                 }
132                         }
133                 }
134         }
135                 
136         return result;
137 }
138
139 gboolean
140 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
141 {
142         if (!options_list)
143                 return FALSE;
144         
145         gboolean result = FALSE;
146         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
147         if(option) {
148                 return TRUE;
149         }
150                 
151         return result;
152 }
153 #endif
154
155
156 gboolean modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self, 
157         const gchar* account_name,
158         const gchar* connection_name, const gchar* server_account_name)
159 {
160         modest_account_mgr_remove_connection_specific_smtp (self, account_name, connection_name);
161         
162         GSList *list = modest_account_mgr_get_list (self, account_name, 
163                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
164                                                     MODEST_CONF_VALUE_STRING, TRUE);
165                 
166         /* The server account is in the item after the connection name: */
167         GSList *list_connection = g_slist_append (list, (gpointer)connection_name);
168         list_connection = g_slist_append (list_connection, (gpointer)server_account_name);
169         
170         /* Reset the changed list: */
171         modest_account_mgr_set_list (self, account_name, 
172                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
173                                                     MODEST_CONF_VALUE_STRING, TRUE);
174                                 
175         /* TODO: Should we free the items too, or just the list? */
176         g_slist_free (list);
177         
178         return TRUE;
179 }
180
181 /**
182  * modest_account_mgr_remove_connection_specific_smtp
183  * @self: a ModestAccountMgr instance
184  * @name: the account name
185  * @connection_name: A libconic IAP connection name
186  * 
187  * Disassacoiate a server account to use with the specific connection for this account.
188  *
189  * Returns: TRUE if it worked, FALSE otherwise
190  */                              
191 gboolean modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self, 
192         const gchar* account_name, const gchar* connection_name)
193 {
194         GSList *list = modest_account_mgr_get_list (self, account_name, 
195                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
196                                                     MODEST_CONF_VALUE_STRING, TRUE);
197         if (!list)
198                 return FALSE;
199                 
200         /* The server account is in the item after the connection name: */
201         GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
202         if (list_connection) {
203                 /* remove both items: */
204                 GSList *temp = g_slist_delete_link(list_connection, list_connection);
205                 temp = g_slist_delete_link(temp, g_slist_next(temp));
206         }
207         
208         /* Reset the changed list: */
209         modest_account_mgr_set_list (self, account_name, 
210                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST, list,
211                                                     MODEST_CONF_VALUE_STRING, TRUE);
212                                 
213         /* TODO: Should we free the items too, or just the list? */
214         g_slist_free (list);
215         
216         return TRUE;
217 }
218
219 /**
220  * modest_account_mgr_get_connection_specific_smtp
221  * @self: a ModestAccountMgr instance
222  * @name: the account name
223  * @connection_name: A libconic IAP connection name
224  * 
225  * Retrieve a server account to use with this specific connection for this account.
226  *
227  * Returns: a server account name to use for this connection, or NULL if none is specified.
228  */                      
229 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name,
230                                          const gchar* connection_name)
231 {
232         gchar *result = NULL;
233         
234         GSList *list = modest_account_mgr_get_list (self, account_name, 
235                                                         MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
236                                                     MODEST_CONF_VALUE_STRING, TRUE);
237         if (!list)
238                 return NULL;
239                 
240         /* The server account is in the item after the connection name: */
241         GSList *list_connection = g_slist_find_custom (list, connection_name, (GCompareFunc)strcmp);
242         if (list_connection) {
243                 GSList * list_server_account = g_slist_next(list_connection);
244                 if (list_server_account)
245                         result = g_strdup ((gchar*)(list_server_account->data));
246         }
247                                 
248         /* TODO: Should we free the items too, or just the list? */
249         g_slist_free (list);
250         
251         return result;
252 }
253                                          
254 gchar*
255 modest_server_account_get_username (ModestAccountMgr *self, const gchar* account_name)
256 {
257         return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME, 
258                 TRUE /* server account */);
259 }
260
261 void
262 modest_server_account_set_username (ModestAccountMgr *self, const gchar* account_name, 
263         const gchar* username)
264 {
265         /* Note that this won't work properly as long as the gconf cache is broken 
266          * in Maemo Bora: */
267         gchar *existing_username = modest_server_account_get_username(self, 
268                 account_name);
269         
270         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME, 
271                 username, TRUE /* server account */);
272                 
273         /* We don't know anything about new usernames: */
274         if (strcmp (existing_username, username) != 0)
275                 modest_server_account_set_username_has_succeeded (self, 
276                 account_name, FALSE);
277                 
278         g_free (existing_username);
279 }
280
281 gboolean
282 modest_server_account_get_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
283 {
284         return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED, 
285                 TRUE /* server account */);
286 }
287
288 void
289 modest_server_account_set_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name, 
290         gboolean succeeded)
291 {
292         modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED, 
293                 succeeded, TRUE /* server account */);
294 }
295
296 void
297 modest_server_account_set_password (ModestAccountMgr *self, const gchar* account_name, 
298         const gchar* password)
299 {
300         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD, 
301                 password, TRUE /* server account */);
302 }
303         
304 gchar*
305 modest_server_account_get_hostname (ModestAccountMgr *self, const gchar* account_name)
306 {
307         return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_HOSTNAME, 
308                 TRUE /* server account */);
309 }
310  
311
312 static ModestProtocol
313 get_secure_auth_for_conf_string(const gchar* value)
314 {
315         ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
316         if (value) {
317                 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
318                         result = MODEST_PROTOCOL_AUTH_NONE;
319                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
320                         result = MODEST_PROTOCOL_AUTH_PASSWORD;
321                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
322                         result = MODEST_PROTOCOL_AUTH_CRAMMD5;
323         }
324         
325         return result;
326 }
327
328 ModestProtocol
329 modest_server_account_get_secure_auth (ModestAccountMgr *self, 
330         const gchar* account_name)
331 {
332         ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
333         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, 
334                 TRUE /* server account */);
335         if (value) {
336                 result = get_secure_auth_for_conf_string (value);
337                         
338                 g_free (value);
339         }
340         
341         return result;
342 }
343
344
345 void
346 modest_server_account_set_secure_auth (ModestAccountMgr *self, 
347         const gchar* account_name, ModestProtocol secure_auth)
348 {
349         /* Get the conf string for the enum value: */
350         const gchar* str_value = NULL;
351         if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
352                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
353         else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
354                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
355         else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
356                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
357         
358         /* Set it in the configuration: */
359         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
360 }
361
362 static ModestProtocol
363 get_security_for_conf_string(const gchar* value)
364 {
365         ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
366         if (value) {
367                 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
368                         result = MODEST_PROTOCOL_SECURITY_NONE;
369                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
370                         result = MODEST_PROTOCOL_SECURITY_TLS;
371                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
372                         result = MODEST_PROTOCOL_SECURITY_SSL;
373         }
374         
375         return result;
376 }
377
378 ModestProtocol
379 modest_server_account_get_security (ModestAccountMgr *self, 
380         const gchar* account_name)
381 {
382         ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
383         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY, 
384                 TRUE /* server account */);
385         if (value) {
386                 result = get_security_for_conf_string (value);
387                         
388                 g_free (value);
389         }
390         
391         return result;
392 }
393
394 void
395 modest_server_account_set_security (ModestAccountMgr *self, 
396         const gchar* account_name, ModestProtocol security)
397 {
398         /* Get the conf string for the enum value: */
399         const gchar* str_value = NULL;
400         if (security == MODEST_PROTOCOL_SECURITY_NONE)
401                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
402         else if (security == MODEST_PROTOCOL_SECURITY_TLS)
403                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
404         else if (security == MODEST_PROTOCOL_SECURITY_SSL)
405                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
406         
407         /* Set it in the configuration: */
408         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
409 }
410                      
411 #if 0                     
412 gchar*
413 modest_account_mgr_get_server_account_option (ModestAccountMgr *self, 
414         const gchar* account_name, const gchar* option_name)
415 {
416         GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
417                                                      MODEST_CONF_VALUE_STRING, TRUE);
418         if (!option_list)
419                 return NULL;
420                 
421         gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
422         
423         /* TODO: Should we free the items too, or just the list? */
424         g_slist_free (option_list);
425                 
426         return result;
427 }
428 #endif
429
430 ModestServerAccountData*
431 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
432 {
433         ModestServerAccountData *data;
434         gchar *proto;
435         
436         g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);      
437         data = g_slice_new0 (ModestServerAccountData);
438         
439         data->account_name = g_strdup (name);
440         data->hostname     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
441         data->username     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);  
442         proto              = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
443         data->proto        = modest_protocol_info_get_protocol (proto);
444         g_free (proto);
445
446         data->port         = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE);
447         
448         gchar *secure_auth_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
449         data->secure_auth  = get_secure_auth_for_conf_string(secure_auth_str);
450         g_free (secure_auth_str);
451                 
452         gchar *security_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
453         data->security     = get_security_for_conf_string(security_str);
454         g_free (security_str);
455         
456         data->last_updated = modest_account_mgr_get_int    (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
457         
458         data->password     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
459         data->uri          = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
460         data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
461                                                      MODEST_CONF_VALUE_STRING, TRUE);
462                                                    
463         
464         return data;
465 }
466
467
468 void
469 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
470                                              ModestServerAccountData* data)
471 {
472         g_return_if_fail (self);
473
474         if (!data)
475                 return; /* not an error */
476
477         g_free (data->account_name);
478         data->account_name = NULL;
479         
480         g_free (data->hostname);
481         data->hostname = NULL;
482         
483         g_free (data->username);
484         data->username = NULL;
485
486         g_free (data->password);
487         data->password = NULL;
488         
489         if (data->options) {
490                 GSList *tmp = data->options;
491                 while (tmp) {
492                         g_free (tmp->data);
493                         tmp = g_slist_next (tmp);
494                 }
495                 g_slist_free (data->options);
496         }
497
498         g_slice_free (ModestServerAccountData, data);
499 }
500
501 /** You must use modest_account_mgr_free_account_data() on the result.
502  */
503 ModestAccountData*
504 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
505 {
506         ModestAccountData *data;
507         gchar *server_account;
508         gchar *default_account;
509         
510         g_return_val_if_fail (self, NULL);
511         g_return_val_if_fail (name, NULL);
512         
513         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
514                 /* For instance, maybe you are mistakenly checking for a server account name? */
515                 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
516                 return NULL;
517         }
518         
519         data = g_slice_new0 (ModestAccountData);
520         
521         data->account_name = g_strdup (name);
522
523         data->display_name = modest_account_mgr_get_string (self, name,
524                                                             MODEST_ACCOUNT_DISPLAY_NAME,
525                                                             FALSE);
526         data->fullname     = modest_account_mgr_get_string (self, name,
527                                                               MODEST_ACCOUNT_FULLNAME,
528                                                                FALSE);
529         data->email        = modest_account_mgr_get_string (self, name,
530                                                             MODEST_ACCOUNT_EMAIL,
531                                                             FALSE);
532         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
533
534         default_account    = modest_account_mgr_get_default_account (self);
535         data->is_default   = (default_account && strcmp (default_account, name) == 0);
536         g_free (default_account);
537
538         /* store */
539         server_account     = modest_account_mgr_get_string (self, name,
540                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
541                                                             FALSE);
542         if (server_account) {
543                 data->store_account =
544                         modest_account_mgr_get_server_account_data (self, server_account);
545                 g_free (server_account);
546         }
547
548         /* transport */
549         server_account = modest_account_mgr_get_string (self, name,
550                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
551                                                         FALSE);
552         if (server_account) {
553                 data->transport_account =
554                         modest_account_mgr_get_server_account_data (self, server_account);
555                 g_free (server_account);
556         }
557
558         return data;
559 }
560
561
562 void
563 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
564 {
565         g_return_if_fail (self);
566
567         if (!data) /* not an error */ 
568                 return;
569
570         g_free (data->account_name);
571         g_free (data->display_name);
572         g_free (data->fullname);
573         g_free (data->email);
574
575         modest_account_mgr_free_server_account_data (self, data->store_account);
576         modest_account_mgr_free_server_account_data (self, data->transport_account);
577         
578         g_slice_free (ModestAccountData, data);
579 }
580
581
582 gchar*
583 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
584 {
585         gchar *account; 
586         ModestConf *conf;
587         GError *err = NULL;
588         
589         g_return_val_if_fail (self, NULL);
590
591         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
592         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
593         
594         if (err) {
595                 g_printerr ("modest: failed to get '%s': %s\n",
596                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
597                 g_error_free (err);
598                 g_free (account);
599                 return  NULL;
600         }
601         
602         /* it's not really an error if there is no default account */
603         if (!account) 
604                 return NULL;
605
606         /* sanity check */
607         if (!modest_account_mgr_account_exists (self, account, FALSE)) {
608                 g_printerr ("modest: default account does not exist\n");
609                 g_free (account);
610                 return NULL;
611         }
612
613         return account;
614 }
615
616
617 gboolean
618 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
619 {
620         ModestConf *conf;
621         
622         g_return_val_if_fail (self,    FALSE);
623         g_return_val_if_fail (account, FALSE);
624         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
625                               FALSE);
626         
627         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
628                 
629         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
630                                        account, NULL);
631
632 }
633
634 gboolean
635 modest_account_mgr_unset_default_account  (ModestAccountMgr *self)
636 {
637         ModestConf *conf;
638         
639         g_return_val_if_fail (self,    FALSE);
640
641         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
642                 
643         return modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
644
645 }
646
647 gboolean
648 modest_account_mgr_set_first_account_as_default  (ModestAccountMgr *self)
649 {
650         gboolean result = FALSE;
651         GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
652         if(account_names)
653         {
654                 const gchar* account_name = (const gchar*)account_names->data;
655                 if (account_name)
656                         result = modest_account_mgr_set_default_account (self, account_name);
657         }
658         
659         /* TODO: Free the strings too? */
660         g_slist_free (account_names);
661         
662         return result;
663 }
664
665 gchar*
666 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
667 {
668         gchar *fullname, *email, *from;
669         
670         g_return_val_if_fail (self, NULL);
671         g_return_val_if_fail (name, NULL);
672
673         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
674                                                        FALSE);
675         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
676                                                        FALSE);
677         from = g_strdup_printf ("%s <%s>",
678                                 fullname ? fullname : "",
679                                 email    ? email    : "");
680         g_free (fullname);
681         g_free (email);
682
683         return from;
684 }
685
686 /* Add a number to the end of the text, or increment a number that is already there.
687  */
688 static gchar*
689 util_increment_name (const gchar* text)
690 {
691         /* Get the end character,
692          * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
693          */
694         const gchar* end = NULL;
695         if (!g_utf8_validate (text, -1, &end))
696                 return NULL;
697   
698         if (!end)
699                 return NULL;
700                 
701         --end; /* Go to before the null-termination. */
702                 
703         /* Look at each UTF-8 characer, starting at the end: */
704         const gchar* p = end;
705         const gchar* alpha_end = NULL;
706         while (p)
707         {       
708                 /* Stop when we reach the first character that is not a numeric digit: */
709                 const gunichar ch = g_utf8_get_char (p);
710                 if (!g_unichar_isdigit (ch)) {
711                         alpha_end = p;
712                         break;
713                 }
714                 
715                 p = g_utf8_prev_char (p);       
716         }
717         
718         if(!alpha_end) {
719                 /* The text must consist completely of numeric digits. */
720                 alpha_end = text;
721         }
722         else
723                 ++alpha_end;
724         
725         /* Intepret and increment the number, if any: */
726         gint num = atol (alpha_end);
727         ++num;
728         
729         /* Get the name part: */
730         gint name_len = alpha_end - text;
731         gchar *name_without_number = g_malloc(name_len + 1);
732         memcpy (name_without_number, text, name_len);
733         name_without_number[name_len] = 0;\
734         
735     /* Concatenate the text part and the new number: */ 
736         gchar *result = g_strdup_printf("%s%d", name_without_number, num);
737         g_free (name_without_number);
738         
739         return result;  
740 }
741
742 gchar*
743 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
744         gboolean server_account)
745 {
746         gchar *account_name = g_strdup (starting_name);
747
748         while (modest_account_mgr_account_exists (self, 
749                 account_name, server_account /*  server_account */)) {
750                         
751                 gchar * account_name2 = util_increment_name (account_name);
752                 g_free (account_name);
753                 account_name = account_name2;
754         }
755         
756         return account_name;
757 }
758
759 gchar*
760 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
761 {
762         gchar *account_name = g_strdup (starting_name);
763
764         while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
765                         
766                 gchar * account_name2 = util_increment_name (account_name);
767                 g_free (account_name);
768                 account_name = account_name2;
769         }
770         
771         return account_name;
772 }