316e746653b6a14c16658868c066e96272586118
[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 <modest-defs.h>
35 #include <string.h>
36 #include <strings.h>
37
38 static const gchar * null_means_empty (const gchar * str);
39
40 static const gchar *
41 null_means_empty (const gchar * str)
42 {
43         return str ? str : "";
44 }
45
46 gboolean
47 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
48                                         gboolean enabled)
49 {
50         return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
51 }
52
53
54 gboolean
55 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
56 {
57         return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
58 }
59
60 gboolean modest_account_mgr_set_signature (ModestAccountMgr *self, const gchar* name, 
61         const gchar* signature, gboolean use_signature)
62 {
63         gboolean result = modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, 
64                 use_signature, FALSE);
65         result = result && modest_account_mgr_set_string (self, name, MODEST_ACCOUNT_SIGNATURE, 
66                                                           null_means_empty (signature), FALSE);
67         return result;
68 }
69
70 gchar* 
71 modest_account_mgr_get_signature (ModestAccountMgr *self, 
72                                   const gchar* name, 
73                                   gboolean* use_signature)
74 {
75         *use_signature = 
76                 modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_USE_SIGNATURE, FALSE);
77         
78         return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SIGNATURE, FALSE);
79 }
80
81 ModestProtocolType modest_account_mgr_get_store_protocol (ModestAccountMgr *self, const gchar* name)
82 {
83        ModestProtocolType result = MODEST_PROTOCOLS_STORE_POP; /* Arbitrary default */
84        
85        gchar *server_account_name = modest_account_mgr_get_string (self, name,
86                                                                    MODEST_ACCOUNT_STORE_ACCOUNT,
87                                                                    FALSE);
88        if (server_account_name) {
89                ModestServerAccountSettings* server_settings = 
90                        modest_account_mgr_load_server_settings (self, server_account_name, FALSE);
91                result = modest_server_account_settings_get_protocol (server_settings);
92                
93                g_object_unref (server_settings);
94                
95                g_free (server_account_name);
96        }
97        
98        return result;
99 }
100
101
102 gboolean 
103 modest_account_mgr_set_connection_specific_smtp (ModestAccountMgr *self, 
104                                                  const gchar* connection_id, 
105                                                  const gchar* server_account_name)
106 {
107         modest_account_mgr_remove_connection_specific_smtp (self, connection_id);
108         
109         ModestConf *conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
110
111         gboolean result = TRUE;
112         GError *err = NULL;
113         GSList *list = modest_conf_get_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
114                                                     MODEST_CONF_VALUE_STRING, &err);
115         if (err) {
116                 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
117                 g_error_free (err);
118                 err = NULL;
119                 result = FALSE;
120         } else {        
121                 /* The server account is in the item after the connection name: */
122                 list = g_slist_append (list, g_strdup (connection_id));
123                 list = g_slist_append (list, g_strdup (server_account_name));
124         
125                 /* Reset the changed list: */
126                 modest_conf_set_list (conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
127                                                     MODEST_CONF_VALUE_STRING, &err);
128                 if (err) {
129                         g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
130                         g_error_free (err);
131                         result = FALSE;
132                 }
133         }
134                                 
135         /* Free the list */
136         if (list) {
137                 g_slist_foreach (list, (GFunc) g_free, NULL);
138                 g_slist_free (list);
139         }
140         
141         return result;
142 }
143
144 /**
145  * modest_account_mgr_remove_connection_specific_smtp
146  * @self: a ModestAccountMgr instance
147  * @name: the account name
148  * @connection_id: A libconic IAP connection id
149  * 
150  * Disassacoiate a server account to use with the specific connection for this account.
151  *
152  * Returns: TRUE if it worked, FALSE otherwise
153  */                              
154 gboolean 
155 modest_account_mgr_remove_connection_specific_smtp (ModestAccountMgr *self, 
156                                                     const gchar* connection_id)
157 {
158         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
159         
160         gboolean result = TRUE;
161         GError *err = NULL;
162         GSList *list = modest_conf_get_list (priv->modest_conf, 
163                                              MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
164                                              MODEST_CONF_VALUE_STRING, &err);
165         if (err) {
166                 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
167                 g_error_free (err);
168                 err = NULL;
169                 result = FALSE;
170         }
171
172         if (!list)
173                 return FALSE;
174                 
175         /* The server account is in the item after the connection name: */
176         GSList *list_connection = g_slist_find_custom (list, connection_id, (GCompareFunc)strcmp);
177         if (list_connection) {
178                 GSList *account_node = g_slist_next (list_connection);
179                 /* remove both items: */
180                 list = g_slist_delete_link(list, list_connection);
181                 list = g_slist_delete_link(list, account_node);
182         }
183         
184         /* Reset the changed list: */
185         modest_conf_set_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST, list,
186                                                     MODEST_CONF_VALUE_STRING, &err);
187         if (err) {
188                 g_printerr ("modest: %s: error setting list: %s.\n", __FUNCTION__, err->message);
189                 g_error_free (err);
190                 result = FALSE;
191         }
192                                 
193         /* Free the list */
194         if (list) {
195                 g_slist_foreach (list, (GFunc) g_free, NULL);
196                 g_slist_free (list);
197         }
198         
199         return result;
200 }
201
202
203 gboolean modest_account_mgr_get_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name)
204 {
205         return modest_account_mgr_get_bool (self, account_name, 
206                 MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, FALSE);
207 }
208
209 gboolean modest_account_mgr_set_use_connection_specific_smtp (ModestAccountMgr *self, const gchar* account_name, 
210         gboolean new_value)
211 {
212         return modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USE_CONNECTION_SPECIFIC_SMTP, 
213                 new_value, FALSE);
214 }
215
216 /**
217  * modest_account_mgr_get_connection_specific_smtp
218  * @self: a ModestAccountMgr instance
219  * @connection_id: A libconic IAP connection id
220  * 
221  * Retrieve a server account to use with this specific connection for this account.
222  *
223  * Returns: a server account name to use for this connection, or NULL if none is specified.
224  */                      
225 gchar* modest_account_mgr_get_connection_specific_smtp (ModestAccountMgr *self,  const gchar* connection_id)
226 {
227         gchar *result = NULL;
228         
229         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
230         
231         GError *err = NULL;
232         GSList *list = modest_conf_get_list (priv->modest_conf, MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
233                                                     MODEST_CONF_VALUE_STRING, &err);
234         if (err) {
235                 g_printerr ("modest: %s: error getting list: %s.\n", __FUNCTION__, err->message);
236                 g_error_free (err);
237                 err = NULL;
238         }
239
240         if (!list)
241                 return NULL;
242
243         /* The server account is in the item after the connection name: */
244         GSList *iter = list;
245         while (iter) {
246                 const gchar* this_connection_id = (const gchar*)(iter->data);
247                 if (strcmp (this_connection_id, connection_id) == 0) {
248                         iter = g_slist_next (iter);
249                         
250                         if (iter) {
251                                 const gchar* account_name = (const gchar*)(iter->data);
252                                 if (account_name) {
253                                         result = g_strdup (account_name);
254                                         break;
255                                 }
256                         }
257                 }
258                 
259                 /* Skip 2 to go to the next connection in the list: */
260                 iter = g_slist_next (iter);
261                 if (iter)
262                         iter = g_slist_next (iter);
263         }
264                 
265         /* Free the list */
266         if (list) {
267                 g_slist_foreach (list, (GFunc) g_free, NULL);
268                 g_slist_free (list);
269         }
270         
271         return result;
272 }
273                                          
274 gchar*
275 modest_account_mgr_get_server_account_username (ModestAccountMgr *self, const gchar* account_name)
276 {
277         return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_USERNAME, 
278                 TRUE /* server account */);
279 }
280
281 void
282 modest_account_mgr_set_server_account_username (ModestAccountMgr *self, const gchar* account_name, 
283         const gchar* username)
284 {
285         /* Note that this won't work properly as long as the gconf cache is broken 
286          * in Maemo Bora: */
287         gchar *existing_username = modest_account_mgr_get_server_account_username(self, 
288                 account_name);
289         
290         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME, 
291                 username, TRUE /* server account */);
292                 
293         /* We don't know anything about new usernames: */
294         if (strcmp (existing_username, username) != 0)
295                 modest_account_mgr_set_server_account_username_has_succeeded (self, account_name, FALSE);
296                 
297         g_free (existing_username);
298 }
299
300 gboolean
301 modest_account_mgr_get_server_account_username_has_succeeded (ModestAccountMgr *self, const gchar* account_name)
302 {
303         return modest_account_mgr_get_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED, 
304                                             TRUE /* server account */);
305 }
306
307 void
308 modest_account_mgr_set_server_account_username_has_succeeded (ModestAccountMgr *self, 
309                                                   const gchar* account_name, 
310                                                   gboolean succeeded)
311 {
312         modest_account_mgr_set_bool (self, account_name, MODEST_ACCOUNT_USERNAME_HAS_SUCCEEDED, 
313                                      succeeded, TRUE /* server account */);
314 }
315
316 gchar*
317 modest_account_mgr_get_server_account_password (ModestAccountMgr *self, const gchar* account_name)
318 {
319         return modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD, 
320                 TRUE /* server account */);     
321 }
322
323 gboolean
324 modest_account_mgr_get_server_account_has_password (ModestAccountMgr *self, const gchar* account_name)
325 {
326         gboolean result = FALSE;
327         gchar *password = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_PASSWORD, 
328                 TRUE /* server account */);
329         if (password && strlen (password)) {
330                 result = TRUE;
331         
332                 /* Clean password */
333                 bzero (password, strlen (password));
334         }
335
336         g_free (password);
337         return result;
338 }
339                          
340         
341 gchar*
342 modest_account_mgr_get_server_account_hostname (ModestAccountMgr *self, 
343                                                 const gchar* account_name)
344 {
345         return modest_account_mgr_get_string (self, 
346                                               account_name, 
347                                               MODEST_ACCOUNT_HOSTNAME, 
348                                               TRUE /* server account */);
349 }
350  
351 void
352 modest_account_mgr_set_server_account_hostname (ModestAccountMgr *self, 
353                                                 const gchar *server_account_name,
354                                                 const gchar *hostname)
355 {
356         modest_account_mgr_set_string (self, 
357                                        server_account_name,
358                                        MODEST_ACCOUNT_HOSTNAME, 
359                                        hostname, 
360                                        TRUE /* server account */);
361 }
362
363
364
365 ModestProtocolType
366 modest_account_mgr_get_server_account_secure_auth (ModestAccountMgr *self, 
367         const gchar* account_name)
368 {
369         ModestProtocolRegistry *protocol_registry;
370         ModestProtocolType result = MODEST_PROTOCOLS_AUTH_NONE;
371         gchar* value;
372
373         protocol_registry = modest_runtime_get_protocol_registry ();
374         value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, 
375                                                TRUE /* server account */);
376         if (value) {
377                 ModestProtocol *protocol;
378
379                 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, value);
380                 g_free (value);
381
382                 if (protocol)
383                         result = modest_protocol_get_type_id (protocol);
384                         
385         }
386         
387         return result;
388 }
389
390
391 void
392 modest_account_mgr_set_server_account_secure_auth (ModestAccountMgr *self, 
393         const gchar* account_name, ModestProtocolType secure_auth)
394 {
395         const gchar* str_value;
396         ModestProtocolRegistry *protocol_registry;
397         ModestProtocol *protocol;
398
399         /* Get the conf string for the protocol: */
400         protocol_registry = modest_runtime_get_protocol_registry ();
401         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, secure_auth);
402         str_value = modest_protocol_get_name (protocol);
403         
404         /* Set it in the configuration: */
405         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
406 }
407
408 ModestProtocolType
409 modest_account_mgr_get_server_account_security (ModestAccountMgr *self, 
410         const gchar* account_name)
411 {
412         ModestProtocolType result = MODEST_PROTOCOLS_CONNECTION_NONE;
413         gchar* value;
414
415         value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY, 
416                                                TRUE /* server account */);
417         if (value) {
418                 ModestProtocolRegistry *protocol_registry;
419                 ModestProtocol *protocol;
420
421                 protocol_registry = modest_runtime_get_protocol_registry ();
422                 protocol = modest_protocol_registry_get_protocol_by_name (protocol_registry,
423                                                                           MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
424                                                                           value);
425                 g_free (value);
426
427                 if (protocol)
428                         result = modest_protocol_get_type_id (protocol);
429         }
430         
431         return result;
432 }
433
434 void
435 modest_account_mgr_set_server_account_security (ModestAccountMgr *self, 
436                                                 const gchar* account_name, 
437                                                 ModestProtocolType security)
438 {
439         const gchar* str_value;
440         ModestProtocolRegistry *protocol_registry;
441         ModestProtocol *protocol;
442
443         /* Get the conf string for the protocol type: */
444         protocol_registry = modest_runtime_get_protocol_registry ();
445         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, security);
446         str_value = modest_protocol_get_name (protocol);
447         
448         /* Set it in the configuration: */
449         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
450 }
451
452 ModestServerAccountSettings*
453 modest_account_mgr_load_server_settings (ModestAccountMgr *self, const gchar* name, gboolean is_transport_and_not_store)
454 {
455         ModestServerAccountSettings *settings;
456         ModestProtocol *protocol;
457         ModestProtocolRegistry *registry;
458         gchar *string;
459         
460         g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
461         registry = modest_runtime_get_protocol_registry ();
462         settings = modest_server_account_settings_new ();
463
464         modest_server_account_settings_set_account_name (settings, name);
465
466         string = modest_account_mgr_get_string (self, name, 
467                                                 MODEST_ACCOUNT_HOSTNAME,TRUE);
468         modest_server_account_settings_set_hostname (settings, string);
469         g_free (string);
470
471         string = modest_account_mgr_get_string (self, name, 
472                                                 MODEST_ACCOUNT_USERNAME,TRUE);
473         modest_server_account_settings_set_username (settings, string); 
474         g_free (string);
475
476         string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
477         if (is_transport_and_not_store) {
478                 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS, string);
479         } else {
480                 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS, string);
481         }
482         modest_server_account_settings_set_protocol (settings,
483                                                      modest_protocol_get_type_id (protocol));
484         g_free (string);
485
486         modest_server_account_settings_set_port (settings,
487                                                  modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE));
488
489         string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
490         if (string) {
491                 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS, string);
492                 modest_server_account_settings_set_auth_protocol (settings,
493                                                                   modest_protocol_get_type_id (protocol));
494                 g_free (string);
495         } else {
496                 modest_server_account_settings_set_auth_protocol (settings, MODEST_PROTOCOLS_AUTH_NONE);
497         }
498         
499         string = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
500         if (string) {
501                 protocol = modest_protocol_registry_get_protocol_by_name (registry, MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS, string);
502                 modest_server_account_settings_set_security_protocol (settings,
503                                                                       modest_protocol_get_type_id (protocol));
504                 g_free (string);
505         } else {
506                 modest_server_account_settings_set_security_protocol (settings,
507                                                                       MODEST_PROTOCOLS_CONNECTION_NONE);
508         }
509
510         string = modest_account_mgr_get_string (self, name, 
511                                                 MODEST_ACCOUNT_PASSWORD, TRUE);
512         modest_server_account_settings_set_password (settings, string);
513         g_free (string);
514         
515         string = modest_account_mgr_get_string (self, name, 
516                                                 MODEST_ACCOUNT_URI, TRUE);
517         modest_server_account_settings_set_uri (settings, string);
518         g_free (string);
519         
520         return settings;
521 }
522
523 gboolean 
524 modest_account_mgr_save_server_settings (ModestAccountMgr *self,
525                                          ModestServerAccountSettings *settings)
526 {
527         gboolean has_errors = FALSE;
528         const gchar *account_name;
529         const gchar *protocol_name;
530         const gchar *uri;
531         ModestProtocolRegistry *protocol_registry;
532         ModestProtocol *protocol;
533         
534         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), FALSE);
535         protocol_registry = modest_runtime_get_protocol_registry ();
536         account_name = modest_server_account_settings_get_account_name (settings);
537
538         /* if we don't have a valid account name we cannot save */
539         g_return_val_if_fail (account_name, FALSE);
540
541         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
542                                                                   modest_server_account_settings_get_protocol (settings));
543         protocol_name = modest_protocol_get_name (protocol);
544         uri = modest_server_account_settings_get_uri (settings);
545         if (!uri) {
546                 const gchar *hostname;
547                 const gchar *username;
548                 const gchar *password;
549                 gint port;
550                 const gchar *auth_protocol_name;
551                 const gchar *security_name;
552
553                 hostname = null_means_empty (modest_server_account_settings_get_hostname (settings));
554                 username = null_means_empty (modest_server_account_settings_get_username (settings));
555                 password = null_means_empty (modest_server_account_settings_get_password (settings));
556                 port = modest_server_account_settings_get_port (settings);
557                 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
558                                                                           modest_server_account_settings_get_auth_protocol (settings));
559                 auth_protocol_name = modest_protocol_get_name (protocol);
560                 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
561                                                                           modest_server_account_settings_get_security_protocol (settings));
562                 security_name = modest_protocol_get_name (protocol);
563
564                 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_HOSTNAME, 
565                                                             hostname, TRUE);
566                 if (!has_errors)
567                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_USERNAME,
568                                                                            username, TRUE));
569                 if (!has_errors)
570                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PASSWORD,
571                                                                            password, TRUE));
572                 if (!has_errors)
573                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
574                                                                            protocol_name, TRUE));
575                 if (!has_errors)
576                         (has_errors = !modest_account_mgr_set_int (self, account_name, MODEST_ACCOUNT_PORT,
577                                                                         port, TRUE));
578                 if (!has_errors)
579                         (has_errors = !modest_account_mgr_set_string (self, account_name, 
580                                                                            MODEST_ACCOUNT_AUTH_MECH,
581                                                                            auth_protocol_name, TRUE));          
582                 if (!has_errors)
583                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY,
584                                                                            security_name,
585                                                                            TRUE));
586         } else {
587                 const gchar *uri = modest_server_account_settings_get_uri (settings);
588                 has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_URI,
589                                                             uri, TRUE);
590                 if (!has_errors)
591                         (has_errors = !modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_PROTO,
592                                                                            protocol_name, TRUE));
593         }
594
595         return !has_errors;
596
597 }
598
599
600 ModestAccountSettings *
601 modest_account_mgr_load_account_settings (ModestAccountMgr *self, 
602                                           const gchar* name)
603 {
604         ModestAccountSettings *settings;
605         gchar *string;
606         gchar *server_account;
607         gchar *default_account;
608         gboolean use_signature = FALSE;
609         
610         g_return_val_if_fail (self, NULL);
611         g_return_val_if_fail (name, NULL);
612         
613         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
614                 /* For instance, maybe you are mistakenly checking for a server account name? */
615                 g_warning ("%s: Account %s does not exist.", __FUNCTION__, name);
616                 return NULL;
617         }
618         
619         settings = modest_account_settings_new ();
620
621         modest_account_settings_set_account_name (settings, name);
622
623         string = modest_account_mgr_get_string (self, name,
624                                                 MODEST_ACCOUNT_DISPLAY_NAME,
625                                                 FALSE);
626         modest_account_settings_set_display_name (settings, string);
627         g_free (string);
628
629         string = modest_account_mgr_get_string (self, name,
630                                                 MODEST_ACCOUNT_FULLNAME,
631                                                 FALSE);
632         modest_account_settings_set_fullname (settings, string);
633         g_free (string);
634
635         string = modest_account_mgr_get_string (self, name,
636                                                 MODEST_ACCOUNT_EMAIL,
637                                                 FALSE);
638         modest_account_settings_set_email_address (settings, string);
639         g_free (string);
640
641         modest_account_settings_set_enabled (settings, modest_account_mgr_get_enabled (self, name));
642         modest_account_settings_set_retrieve_type (settings, modest_account_mgr_get_retrieve_type (self, name));
643         modest_account_settings_set_retrieve_limit (settings, modest_account_mgr_get_retrieve_limit (self, name));
644
645         default_account    = modest_account_mgr_get_default_account (self);
646         modest_account_settings_set_is_default (settings,
647                                                 (default_account && strcmp (default_account, name) == 0));
648         g_free (default_account);
649
650         string = modest_account_mgr_get_signature (self, name, &use_signature);
651         modest_account_settings_set_use_signature (settings, use_signature);
652         modest_account_settings_set_signature (settings, string);
653         g_free (string);
654
655         modest_account_settings_set_leave_messages_on_server 
656                 (settings, modest_account_mgr_get_leave_on_server (self, name));
657         modest_account_settings_set_use_connection_specific_smtp 
658                 (settings, modest_account_mgr_get_use_connection_specific_smtp (self, name));
659
660         /* store */
661         server_account     = modest_account_mgr_get_string (self, name,
662                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
663                                                             FALSE);
664         if (server_account) {
665                 ModestServerAccountSettings *store_settings;
666                 store_settings = modest_account_mgr_load_server_settings (self, server_account, FALSE);
667                 modest_account_settings_set_store_settings (settings,
668                                                             store_settings);
669                 g_object_unref (store_settings);
670                 g_free (server_account);
671         }
672
673         /* transport */
674         server_account = modest_account_mgr_get_string (self, name,
675                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
676                                                         FALSE);
677         if (server_account) {
678                 ModestServerAccountSettings *transport_settings;
679                 transport_settings = modest_account_mgr_load_server_settings (self, server_account, TRUE);
680                 modest_account_settings_set_transport_settings (settings, transport_settings);
681                 g_object_unref (transport_settings);
682                 g_free (server_account);
683         }
684
685         return settings;
686 }
687
688 void
689 modest_account_mgr_save_account_settings (ModestAccountMgr *mgr,
690                                           ModestAccountSettings *settings)
691 {
692         g_return_if_fail (MODEST_IS_ACCOUNT_MGR (mgr));
693         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
694
695         const gchar *account_name;
696         const gchar *store_account_name;
697         const gchar *transport_account_name;
698         ModestServerAccountSettings *store_settings;
699         ModestServerAccountSettings *transport_settings;
700
701         account_name = modest_account_settings_get_account_name (settings);
702         g_return_if_fail (account_name != NULL);
703
704         modest_account_mgr_set_display_name (mgr, account_name,
705                                              modest_account_settings_get_display_name (settings));
706         modest_account_mgr_set_user_fullname (mgr, account_name,
707                                               modest_account_settings_get_fullname (settings));
708         modest_account_mgr_set_user_email (mgr, account_name,
709                                            modest_account_settings_get_email_address (settings));
710         modest_account_mgr_set_retrieve_type (mgr, account_name,
711                                               modest_account_settings_get_retrieve_type (settings));
712         modest_account_mgr_set_retrieve_limit (mgr, account_name,
713                                                modest_account_settings_get_retrieve_limit (settings));
714         modest_account_mgr_set_leave_on_server (mgr, account_name,
715                                                 modest_account_settings_get_leave_messages_on_server (settings));
716         modest_account_mgr_set_signature (mgr, account_name,
717                                           modest_account_settings_get_signature (settings),
718                                           modest_account_settings_get_use_signature (settings));
719         modest_account_mgr_set_use_connection_specific_smtp 
720                 (mgr, account_name,
721                  modest_account_settings_get_use_connection_specific_smtp (settings));
722
723         store_settings = modest_account_settings_get_store_settings (settings);
724         store_account_name = modest_server_account_settings_get_account_name (store_settings);
725         if (store_settings != NULL) {
726                 modest_account_mgr_save_server_settings (mgr, store_settings);
727         }
728         modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_STORE_ACCOUNT, store_account_name, FALSE);
729         g_object_unref (store_settings);
730
731         transport_settings = modest_account_settings_get_transport_settings (settings);
732         transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
733         if (transport_settings != NULL) {
734                 modest_account_mgr_save_server_settings (mgr, transport_settings);
735         }
736         modest_account_mgr_set_string (mgr, account_name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT, transport_account_name, FALSE);
737         g_object_unref (transport_settings);
738         modest_account_mgr_set_enabled (mgr, account_name, TRUE);
739 }
740
741
742 gint 
743 on_accounts_list_sort_by_title(gconstpointer a, gconstpointer b)
744 {
745         return g_utf8_collate((const gchar*)a, (const gchar*)b);
746 }
747
748 /** Get the first one, alphabetically, by title. */
749 gchar* 
750 modest_account_mgr_get_first_account_name (ModestAccountMgr *self)
751 {
752         const gchar* account_name = NULL;
753         GSList *account_names = modest_account_mgr_account_names (self, TRUE /* only enabled */);
754
755         /* Return TRUE if there is no account */
756         if (!account_names)
757                 return NULL;
758
759         /* Get the first one, alphabetically, by title: */
760         /* gchar *old_default = modest_account_mgr_get_default_account (self); */
761         GSList* list_sorted = g_slist_sort (account_names, on_accounts_list_sort_by_title);
762
763         GSList* iter = list_sorted;
764         gboolean found = FALSE;
765         while (iter && !found) {
766                 account_name = (const gchar*)list_sorted->data;
767
768                 if (account_name)
769                         found = TRUE;
770
771                 if (!found)
772                         iter = g_slist_next (iter);
773         }
774
775         gchar* result = NULL;
776         if (account_name)
777                 result = g_strdup (account_name);
778                 
779         modest_account_mgr_free_account_names (account_names);
780         account_names = NULL;
781
782         return result;
783 }
784
785 gboolean
786 modest_account_mgr_set_first_account_as_default (ModestAccountMgr *self)
787 {
788         gboolean result = FALSE;
789         
790         gchar* account_name = modest_account_mgr_get_first_account_name(self);
791         if (account_name) {
792                 result = modest_account_mgr_set_default_account (self, account_name);
793                 g_free (account_name);
794         }
795         else
796                 result = TRUE; /* If there are no accounts then it's not a failure. */
797
798         return result;
799 }
800
801 gchar*
802 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
803 {
804         gchar *fullname, *email, *from;
805         
806         g_return_val_if_fail (self, NULL);
807         g_return_val_if_fail (name, NULL);
808
809         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
810                                                        FALSE);
811         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
812                                                        FALSE);
813         from = g_strdup_printf ("%s <%s>",
814                                 fullname ? fullname : "",
815                                 email    ? email    : "");
816         g_free (fullname);
817         g_free (email);
818
819         return from;
820 }
821
822 /* Add a number to the end of the text, or increment a number that is already there.
823  */
824 static gchar*
825 util_increment_name (const gchar* text)
826 {
827         g_return_val_if_fail (text, NULL);
828
829         /* Get the end character,
830          * also doing a UTF-8 validation which is required for using g_utf8_prev_char().
831          */
832         const gchar* end = NULL;
833         if (!g_utf8_validate (text, -1, &end))
834                 return NULL;
835   
836         if (!end)
837                 return NULL;
838                 
839         --end; /* Go to before the null-termination. */
840                 
841         /* Look at each UTF-8 characer, starting at the end: */
842         const gchar* p = end;
843         const gchar* alpha_end = NULL;
844         while (p)
845         {       
846                 /* Stop when we reach the first character that is not a numeric digit: */
847                 const gunichar ch = g_utf8_get_char (p);
848                 if (!g_unichar_isdigit (ch)) {
849                         alpha_end = p;
850                         break;
851                 }
852                 
853                 p = g_utf8_find_prev_char (text, p);    
854         }
855         
856         if(!alpha_end) {
857                 /* The text must consist completely of numeric digits. */
858                 alpha_end = text;
859         }
860         else
861                 ++alpha_end;
862         
863         /* Intepret and increment the number, if any: */
864         gint num = atol (alpha_end);
865         ++num;
866         
867         /* Get the name part: */
868         gint name_len = alpha_end - text;
869         gchar *name_without_number = g_malloc(name_len + 1);
870         memcpy (name_without_number, text, name_len);
871         name_without_number[name_len] = 0;\
872         
873     /* Concatenate the text part and the new number: */ 
874         gchar *result = g_strdup_printf("%s%d", name_without_number, num);
875         g_free (name_without_number);
876         
877         return result;  
878 }
879
880 gchar*
881 modest_account_mgr_get_unused_account_name (ModestAccountMgr *self, const gchar* starting_name,
882         gboolean server_account)
883 {
884         gchar *account_name = g_strdup (starting_name);
885
886         while (modest_account_mgr_account_exists (self, 
887                 account_name, server_account /*  server_account */)) {
888                         
889                 gchar * account_name2 = util_increment_name (account_name);
890                 g_free (account_name);
891                 account_name = account_name2;
892         }
893         
894         return account_name;
895 }
896
897 gchar*
898 modest_account_mgr_get_unused_account_display_name (ModestAccountMgr *self, const gchar* starting_name)
899 {
900         gchar *account_name = g_strdup (starting_name);
901
902         while (modest_account_mgr_account_with_display_name_exists (self, account_name)) {
903                         
904                 gchar * account_name2 = util_increment_name (account_name);
905                 g_free (account_name);
906                 account_name = account_name2;
907         }
908         
909         return account_name;
910 }
911
912 void 
913 modest_account_mgr_set_leave_on_server (ModestAccountMgr *self, 
914                                         const gchar *account_name, 
915                                         gboolean leave_on_server)
916 {
917         modest_account_mgr_set_bool (self, 
918                                      account_name,
919                                      MODEST_ACCOUNT_LEAVE_ON_SERVER, 
920                                      leave_on_server, 
921                                      FALSE);
922 }
923
924 gboolean 
925 modest_account_mgr_get_leave_on_server (ModestAccountMgr *self, 
926                                         const gchar* account_name)
927 {
928         return modest_account_mgr_get_bool (self, 
929                                             account_name,
930                                             MODEST_ACCOUNT_LEAVE_ON_SERVER, 
931                                             FALSE);
932 }
933
934 gint 
935 modest_account_mgr_get_last_updated (ModestAccountMgr *self, 
936                                      const gchar* account_name)
937 {
938         return modest_account_mgr_get_int (modest_runtime_get_account_mgr (), 
939                                            account_name, 
940                                            MODEST_ACCOUNT_LAST_UPDATED, 
941                                            TRUE);
942 }
943
944 void 
945 modest_account_mgr_set_last_updated (ModestAccountMgr *self, 
946                                      const gchar* account_name,
947                                      gint time)
948 {
949         modest_account_mgr_set_int (self, 
950                                     account_name, 
951                                     MODEST_ACCOUNT_LAST_UPDATED, 
952                                     time, 
953                                     TRUE);
954
955         /* TODO: notify about changes */
956 }
957
958 gint  
959 modest_account_mgr_get_retrieve_limit (ModestAccountMgr *self, 
960                                        const gchar* account_name)
961 {
962         return modest_account_mgr_get_int (self, 
963                                            account_name,
964                                            MODEST_ACCOUNT_LIMIT_RETRIEVE, 
965                                            FALSE);
966 }
967
968 void  
969 modest_account_mgr_set_retrieve_limit (ModestAccountMgr *self, 
970                                        const gchar* account_name,
971                                        gint limit_retrieve)
972 {
973         modest_account_mgr_set_int (self, 
974                                     account_name,
975                                     MODEST_ACCOUNT_LIMIT_RETRIEVE, 
976                                     limit_retrieve, 
977                                     FALSE /* not server account */);
978 }
979
980 gint  
981 modest_account_mgr_get_server_account_port (ModestAccountMgr *self, 
982                                             const gchar* account_name)
983 {
984         return modest_account_mgr_get_int (self, 
985                                            account_name,
986                                            MODEST_ACCOUNT_PORT, 
987                                            TRUE);
988 }
989
990 void
991 modest_account_mgr_set_server_account_port (ModestAccountMgr *self, 
992                                             const gchar *account_name,
993                                             gint port_num)
994 {
995         modest_account_mgr_set_int (self, 
996                                     account_name,
997                                     MODEST_ACCOUNT_PORT, 
998                                     port_num, TRUE /* server account */);
999 }
1000
1001 gchar* 
1002 modest_account_mgr_get_server_account_name (ModestAccountMgr *self, 
1003                                             const gchar *account_name,
1004                                             TnyAccountType account_type)
1005 {
1006         return modest_account_mgr_get_string (self, 
1007                                               account_name,
1008                                               (account_type == TNY_ACCOUNT_TYPE_STORE) ?
1009                                               MODEST_ACCOUNT_STORE_ACCOUNT :
1010                                               MODEST_ACCOUNT_TRANSPORT_ACCOUNT, 
1011                                               FALSE);
1012 }
1013
1014 static const gchar *
1015 get_retrieve_type_name (ModestAccountRetrieveType retrieve_type)
1016 {
1017         switch(retrieve_type) {
1018         case MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY:
1019                 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1020                 break;
1021         case MODEST_ACCOUNT_RETRIEVE_MESSAGES:
1022                 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES;
1023                 break;
1024         case MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS:
1025                 return MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS;
1026                 break;
1027         default:
1028                 return MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1029         };
1030 }
1031
1032 static ModestAccountRetrieveType
1033 get_retrieve_type (const gchar *name)
1034 {
1035         if (!name || name[0] == 0)
1036                 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1037         if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES) == 0) {
1038                 return MODEST_ACCOUNT_RETRIEVE_MESSAGES;
1039         } else if (strcmp (name, MODEST_ACCOUNT_RETRIEVE_VALUE_MESSAGES_AND_ATTACHMENTS) == 0) {
1040                 return MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS;
1041         } else {
1042                 /* we fall back to headers only */
1043                 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
1044         }
1045 }
1046
1047 ModestAccountRetrieveType
1048 modest_account_mgr_get_retrieve_type (ModestAccountMgr *self, 
1049                                       const gchar *account_name)
1050 {
1051         gchar *string;
1052         ModestAccountRetrieveType result;
1053
1054         string =  modest_account_mgr_get_string (self, 
1055                                                  account_name,
1056                                                  MODEST_ACCOUNT_RETRIEVE, 
1057                                                  FALSE /* not server account */);
1058         result = get_retrieve_type (string);
1059         g_free (string);
1060
1061         return result;
1062 }
1063
1064 void 
1065 modest_account_mgr_set_retrieve_type (ModestAccountMgr *self, 
1066                                       const gchar *account_name,
1067                                       ModestAccountRetrieveType retrieve_type)
1068 {
1069         modest_account_mgr_set_string (self, 
1070                                        account_name,
1071                                        MODEST_ACCOUNT_RETRIEVE, 
1072                                        get_retrieve_type_name (retrieve_type), 
1073                                        FALSE /* not server account */);
1074 }
1075
1076
1077 void
1078 modest_account_mgr_set_user_fullname (ModestAccountMgr *self, 
1079                                       const gchar *account_name,
1080                                       const gchar *fullname)
1081 {
1082         modest_account_mgr_set_string (self, 
1083                                        account_name,
1084                                        MODEST_ACCOUNT_FULLNAME, 
1085                                        fullname, 
1086                                        FALSE /* not server account */);
1087 }
1088
1089 void
1090 modest_account_mgr_set_user_email (ModestAccountMgr *self, 
1091                                    const gchar *account_name,
1092                                    const gchar *email)
1093 {
1094         modest_account_mgr_set_string (self, 
1095                                        account_name,
1096                                        MODEST_ACCOUNT_EMAIL, 
1097                                        email, 
1098                                        FALSE /* not server account */);
1099 }