6b2c10d9c630aa67c2e001337015eb118ee7a173
[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         
450         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
451                 /* For instance, maybe you are mistakenly checking for a server account name? */
452                 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
453                 return NULL;
454         }
455         
456         data = g_slice_new0 (ModestAccountData);
457         
458         data->account_name = g_strdup (name);
459
460         data->display_name = modest_account_mgr_get_string (self, name,
461                                                             MODEST_ACCOUNT_DISPLAY_NAME,
462                                                             FALSE);
463         data->fullname     = modest_account_mgr_get_string (self, name,
464                                                               MODEST_ACCOUNT_FULLNAME,
465                                                                FALSE);
466         data->email        = modest_account_mgr_get_string (self, name,
467                                                             MODEST_ACCOUNT_EMAIL,
468                                                             FALSE);
469         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
470
471         default_account    = modest_account_mgr_get_default_account (self);
472         data->is_default   = (default_account && strcmp (default_account, name) == 0);
473         g_free (default_account);
474
475         /* store */
476         server_account     = modest_account_mgr_get_string (self, name,
477                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
478                                                             FALSE);
479         if (server_account) {
480                 data->store_account =
481                         modest_account_mgr_get_server_account_data (self, server_account);
482                 g_free (server_account);
483         }
484
485         /* transport */
486         server_account = modest_account_mgr_get_string (self, name,
487                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
488                                                         FALSE);
489         if (server_account) {
490                 data->transport_account =
491                         modest_account_mgr_get_server_account_data (self, server_account);
492                 g_free (server_account);
493         }
494
495         return data;
496 }
497
498
499 void
500 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
501 {
502         g_return_if_fail (self);
503
504         if (!data) /* not an error */ 
505                 return;
506
507         g_free (data->account_name);
508         g_free (data->display_name);
509         g_free (data->fullname);
510         g_free (data->email);
511
512         modest_account_mgr_free_server_account_data (self, data->store_account);
513         modest_account_mgr_free_server_account_data (self, data->transport_account);
514         
515         g_slice_free (ModestAccountData, data);
516 }
517
518
519 gchar*
520 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
521 {
522         gchar *account; 
523         ModestConf *conf;
524         GError *err = NULL;
525         
526         g_return_val_if_fail (self, NULL);
527
528         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
529         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
530         
531         if (err) {
532                 g_printerr ("modest: failed to get '%s': %s\n",
533                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
534                 g_error_free (err);
535                 g_free (account);
536                 return  NULL;
537         }
538         
539         /* it's not really an error if there is no default account */
540         if (!account) 
541                 return NULL;
542
543         /* sanity check */
544         if (!modest_account_mgr_account_exists (self, account, FALSE)) {
545                 g_printerr ("modest: default account does not exist\n");
546                 g_free (account);
547                 return NULL;
548         }
549
550         return account;
551 }
552
553
554 gboolean
555 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
556 {
557         ModestConf *conf;
558         
559         g_return_val_if_fail (self,    FALSE);
560         g_return_val_if_fail (account, FALSE);
561         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
562                               FALSE);
563         
564         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
565                 
566         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
567                                        account, NULL);
568
569 }
570
571 gchar*
572 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
573 {
574         gchar *fullname, *email, *from;
575         
576         g_return_val_if_fail (self, NULL);
577         g_return_val_if_fail (name, NULL);
578
579         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
580                                                        FALSE);
581         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
582                                                        FALSE);
583         from = g_strdup_printf ("%s <%s>",
584                                 fullname ? fullname : "",
585                                 email    ? email    : "");
586         g_free (fullname);
587         g_free (email);
588
589         return from;
590 }
591
592 /* Add a number to the end of the text, or increment a number that is already there.
593  */
594 static gchar*
595 util_increment_name (const gchar* text)
596 {
597         /* Get the end character,
598          * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
599          */
600         const gchar* end = NULL;
601         if (!g_utf8_validate (text, -1, &end))
602                 return NULL;
603   
604         if (!end)
605                 return NULL;
606                 
607         --end; /* Go to before the null-termination. */
608                 
609         /* Look at each UTF-8 characer, starting at the end: */
610         const gchar* p = end;
611         const gchar* alpha_end = NULL;
612         while (p)
613         {       
614                 /* Stop when we reach the first character that is not a numeric digit: */
615                 const gunichar ch = g_utf8_get_char (p);
616                 if (!g_unichar_isdigit (ch)) {
617                         alpha_end = p;
618                         break;
619                 }
620                 
621                 p = g_utf8_prev_char (p);       
622         }
623         
624         if(!alpha_end) {
625                 /* The text must consist completely of numeric digits. */
626                 alpha_end = text;
627         }
628         else
629                 ++alpha_end;
630         
631         /* Intepret and increment the number, if any: */
632         gint num = atol (alpha_end);
633         ++num;
634         
635         /* Get the name part: */
636         gint name_len = alpha_end - text;
637         gchar *name_without_number = g_malloc(name_len + 1);
638         memcpy (name_without_number, text, name_len);
639         name_without_number[name_len] = 0;\
640         
641     /* Concatenate the text part and the new number: */ 
642         gchar *result = g_strdup_printf("%s%d", name_without_number, num);
643         g_free (name_without_number);
644         
645         return result;  
646 }
647
648 gchar*
649 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
650         gboolean server_account)
651 {
652         gchar *account_name = g_strdup (starting_name);
653
654         while (modest_account_mgr_account_exists (self, 
655                 account_name, server_account /*  server_account */)) {
656                         
657                 gchar * account_name2 = util_increment_name (account_name);
658                 g_free (account_name);
659                 account_name = account_name2;
660         }
661         
662         return account_name;
663 }
664
665 gchar*
666 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
667 {
668         gchar *account_name = g_strdup (starting_name);
669
670         while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
671                         
672                 gchar * account_name2 = util_increment_name (account_name);
673                 g_free (account_name);
674                 account_name = account_name2;
675         }
676         
677         return account_name;
678 }