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