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