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