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