updated bug fix info and version for week 06, 2009 first build
[modest] / src / modest-account-mgr.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 <string.h>
31 #include <modest-marshal.h>
32 #include <modest-runtime.h>
33 #include <modest-defs.h>
34 #include <modest-account-mgr.h>
35 #include <modest-account-mgr-priv.h>
36 #include <modest-account-mgr-helpers.h>
37 #include <modest-platform.h>
38
39 /* 'private'/'protected' functions */
40 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
41 static void modest_account_mgr_init       (ModestAccountMgr * obj);
42 static void modest_account_mgr_finalize   (GObject * obj);
43 static void modest_account_mgr_base_init  (gpointer g_class);
44
45 static const gchar *_modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, 
46                                                                     const gchar* account_name,
47                                                                     const gchar *name, 
48                                                                     gboolean is_server);
49
50 static gboolean modest_account_mgr_unset_default_account (ModestAccountMgr *self);
51
52 /* list my signals */
53 enum {
54         ACCOUNT_INSERTED_SIGNAL,
55         ACCOUNT_CHANGED_SIGNAL,
56         ACCOUNT_REMOVED_SIGNAL,
57         ACCOUNT_BUSY_SIGNAL,
58         DEFAULT_ACCOUNT_CHANGED_SIGNAL,
59         DISPLAY_NAME_CHANGED_SIGNAL,
60         ACCOUNT_UPDATED_SIGNAL,
61         LAST_SIGNAL
62 };
63
64 /* globals */
65 static GObjectClass *parent_class = NULL;
66 static guint signals[LAST_SIGNAL] = {0};
67
68 GType
69 modest_account_mgr_get_type (void)
70 {
71         static GType my_type = 0;
72
73         if (!my_type) {
74                 static const GTypeInfo my_info = {
75                         sizeof (ModestAccountMgrClass),
76                         modest_account_mgr_base_init,   /* base init */
77                         NULL,   /* base finalize */
78                         (GClassInitFunc) modest_account_mgr_class_init,
79                         NULL,   /* class finalize */
80                         NULL,   /* class data */
81                         sizeof (ModestAccountMgr),
82                         1,      /* n_preallocs */
83                         (GInstanceInitFunc) modest_account_mgr_init,
84                         NULL
85                 };
86
87                 my_type = g_type_register_static (G_TYPE_OBJECT,
88                                                   "ModestAccountMgr",
89                                                   &my_info, 0);
90         }
91         return my_type;
92 }
93
94 static void 
95 modest_account_mgr_base_init (gpointer g_class)
96 {
97         static gboolean modest_account_mgr_initialized = FALSE;
98
99         if (!modest_account_mgr_initialized) {
100                 /* signal definitions */
101                 signals[ACCOUNT_INSERTED_SIGNAL] =
102                         g_signal_new ("account_inserted",
103                                       MODEST_TYPE_ACCOUNT_MGR,
104                                       G_SIGNAL_RUN_FIRST,
105                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_inserted),
106                                       NULL, NULL,
107                                       g_cclosure_marshal_VOID__STRING,
108                                       G_TYPE_NONE, 1, G_TYPE_STRING);
109
110                 signals[ACCOUNT_REMOVED_SIGNAL] =
111                         g_signal_new ("account_removed",
112                                       MODEST_TYPE_ACCOUNT_MGR,
113                                       G_SIGNAL_RUN_FIRST,
114                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_removed),
115                                       NULL, NULL,
116                                       g_cclosure_marshal_VOID__STRING,
117                                       G_TYPE_NONE, 1, G_TYPE_STRING);
118
119                 signals[ACCOUNT_CHANGED_SIGNAL] =
120                         g_signal_new ("account_changed",
121                                       MODEST_TYPE_ACCOUNT_MGR,
122                                       G_SIGNAL_RUN_FIRST,
123                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_changed),
124                                       NULL, NULL,
125                                       modest_marshal_VOID__STRING_INT,
126                                       G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
127
128                 signals[ACCOUNT_BUSY_SIGNAL] =
129                         g_signal_new ("account_busy_changed",
130                                       MODEST_TYPE_ACCOUNT_MGR,
131                                       G_SIGNAL_RUN_FIRST,
132                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_busy_changed),
133                                       NULL, NULL,
134                                       modest_marshal_VOID__STRING_BOOLEAN,
135                                       G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
136
137                 signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL] =
138                         g_signal_new ("default_account_changed",
139                                       MODEST_TYPE_ACCOUNT_MGR,
140                                       G_SIGNAL_RUN_FIRST,
141                                       G_STRUCT_OFFSET(ModestAccountMgrClass, default_account_changed),
142                                       NULL, NULL,
143                                       g_cclosure_marshal_VOID__VOID,
144                                       G_TYPE_NONE, 0);
145
146                 signals[DISPLAY_NAME_CHANGED_SIGNAL] =
147                         g_signal_new ("display_name_changed",
148                                       MODEST_TYPE_ACCOUNT_MGR,
149                                       G_SIGNAL_RUN_FIRST,
150                                       G_STRUCT_OFFSET(ModestAccountMgrClass, display_name_changed),
151                                       NULL, NULL,
152                                       g_cclosure_marshal_VOID__STRING,
153                                       G_TYPE_NONE, 1, G_TYPE_STRING);
154                 
155                 signals[ACCOUNT_UPDATED_SIGNAL] =
156                         g_signal_new ("account_updated",
157                                       MODEST_TYPE_ACCOUNT_MGR,
158                                       G_SIGNAL_RUN_FIRST,
159                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_updated),
160                                       NULL, NULL,
161                                       g_cclosure_marshal_VOID__STRING,
162                                       G_TYPE_NONE, 1, G_TYPE_STRING);
163
164
165                 modest_account_mgr_initialized = TRUE;
166         }
167 }
168
169 static void
170 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
171 {
172         GObjectClass *gobject_class;
173         gobject_class = (GObjectClass *) klass;
174
175         parent_class = g_type_class_peek_parent (klass);
176         gobject_class->finalize = modest_account_mgr_finalize;
177
178         g_type_class_add_private (gobject_class,
179                                   sizeof (ModestAccountMgrPrivate));
180 }
181
182
183 static void
184 modest_account_mgr_init (ModestAccountMgr * obj)
185 {
186         ModestAccountMgrPrivate *priv =
187                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
188
189         priv->modest_conf   = NULL;
190         priv->busy_accounts = NULL;
191         priv->timeout       = 0;
192         
193         priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
194
195         /* we maintain hashes for the modest-conf keys we build from account name
196          * + key. many seem to be used often, and generating them showed up high
197          * in oprofile */
198         /* both hashes are hashes to hashes;
199          * account-key => keyname ==> account-key-name
200          */     
201         priv->server_account_key_hash = g_hash_table_new_full (g_str_hash,
202                                                                g_str_equal,
203                                                                g_free,
204                                                                (GDestroyNotify)g_hash_table_destroy);
205         priv->account_key_hash        = g_hash_table_new_full (g_str_hash,
206                                                                g_str_equal,
207                                                                g_free,
208                                                                (GDestroyNotify)g_hash_table_destroy);
209
210         /* FALSE means: status is unknown */
211         priv->has_accounts = FALSE;
212         priv->has_enabled_accounts = FALSE;
213 }
214
215 static void
216 modest_account_mgr_finalize (GObject * obj)
217 {
218         ModestAccountMgrPrivate *priv = 
219                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
220
221         if (priv->notification_id_accounts) {
222                 g_hash_table_destroy (priv->notification_id_accounts);
223                 priv->notification_id_accounts = NULL;
224         }
225
226         if (priv->modest_conf) {
227                 g_object_unref (G_OBJECT(priv->modest_conf));
228                 priv->modest_conf = NULL;
229         }
230
231         if (priv->timeout)
232                 g_source_remove (priv->timeout);
233         priv->timeout = 0;
234
235         if (priv->server_account_key_hash) {
236                 g_hash_table_destroy (priv->server_account_key_hash);
237                 priv->server_account_key_hash = NULL;
238         }
239         
240         if (priv->account_key_hash) {
241                 g_hash_table_destroy (priv->account_key_hash);
242                 priv->account_key_hash = NULL;
243         }
244
245         if (priv->busy_accounts) {
246                 g_slist_foreach (priv->busy_accounts, (GFunc) g_free, NULL);
247                 g_slist_free (priv->busy_accounts);
248                 priv->busy_accounts = NULL;
249         }
250
251         G_OBJECT_CLASS(parent_class)->finalize (obj);
252 }
253
254
255 ModestAccountMgr *
256 modest_account_mgr_new (ModestConf *conf)
257 {
258         GObject *obj;
259         ModestAccountMgrPrivate *priv;
260
261         g_return_val_if_fail (conf, NULL);
262
263         obj = G_OBJECT (g_object_new (MODEST_TYPE_ACCOUNT_MGR, NULL));
264         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
265
266         g_object_ref (G_OBJECT(conf));
267         priv->modest_conf = conf;
268
269         return MODEST_ACCOUNT_MGR (obj);
270 }
271
272
273 static const gchar *
274 null_means_empty (const gchar * str)
275 {
276         return str ? str : "";
277 }
278
279 gboolean
280 modest_account_mgr_add_account_from_settings (ModestAccountMgr *self,
281                                               ModestAccountSettings *settings)
282 {
283         ModestAccountMgrPrivate *priv;
284         const gchar* display_name;
285         gchar *account_name_start, *account_name;
286         gchar *store_name_start, *store_name;
287         gchar *transport_name_start, *transport_name;
288         gchar *default_account;
289         ModestServerAccountSettings *store_settings, *transport_settings;
290
291         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR (self), FALSE);
292         g_return_val_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings), FALSE);
293
294         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
295         display_name = modest_account_settings_get_display_name (settings);
296
297         /* We should have checked for this already, and changed that name accordingly, 
298          * but let's check again just in case */
299         if (!display_name || 
300             modest_account_mgr_account_with_display_name_exists (self, display_name)) {
301                 display_name = _("mcen_ia_emailsetup_defaultname");
302         }
303
304         /* Increment the non-user visible name if necessary, 
305          * based on the display name: */
306         account_name_start = g_strdup_printf ("%sID", display_name);
307         account_name = modest_account_mgr_get_unused_account_name (self,
308                                                                    account_name_start, FALSE /* not a server account */);
309         g_free (account_name_start);
310         
311         /* Add a (incoming) server account, to be used by the account: */
312         store_name_start = g_strconcat (account_name, "_store", NULL);
313         store_name = modest_account_mgr_get_unused_account_name (self, 
314                                                                  store_name_start, TRUE /* server account */);
315         g_free (store_name_start);
316         
317         /* Add a (outgoing) server account to be used by the account: */
318         transport_name_start = g_strconcat (account_name, "_transport", NULL);
319         transport_name = modest_account_mgr_get_unused_account_name (self, 
320                                                                      transport_name_start, TRUE /* server account */);
321         g_free (transport_name_start);
322
323         modest_account_settings_set_account_name (settings, account_name);
324         store_settings = modest_account_settings_get_store_settings (settings);
325         modest_server_account_settings_set_account_name (store_settings, store_name);
326         transport_settings = modest_account_settings_get_transport_settings (settings);
327         modest_server_account_settings_set_account_name (transport_settings, transport_name);
328         g_object_unref (store_settings);
329         g_object_unref (transport_settings);
330
331         /* Create the account, which will contain the two "server accounts": */
332         modest_account_mgr_save_account_settings (self, settings);
333         g_free (store_name);
334         g_free (transport_name);
335         
336         /* Sanity check: */
337         /* There must be at least one account now: */
338         /* Note, when this fails is is caused by a Maemo gconf bug that has been 
339          * fixed in versions after 3.1. */
340         if(!modest_account_mgr_has_accounts (self, FALSE))
341                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
342                                 
343         /* Notify the observers */
344         g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, account_name);
345
346         /* if no default account has been defined yet, do so now */
347         default_account = modest_account_mgr_get_default_account (self);
348         if (!default_account) {
349                 modest_account_mgr_set_default_account (self, account_name);
350                 modest_account_settings_set_is_default (settings, TRUE);
351         }
352         g_free (default_account);
353         g_free (account_name);
354
355         /* (re)set the automatic account update */
356         modest_platform_set_update_interval
357                 (modest_conf_get_int (priv->modest_conf, MODEST_CONF_UPDATE_INTERVAL, NULL));
358
359         return TRUE;
360 }
361
362
363 gboolean
364 modest_account_mgr_add_account (ModestAccountMgr *self,
365                                 const gchar *name,
366                                 const gchar *display_name,
367                                 const gchar *user_fullname,
368                                 const gchar *user_email,
369                                 ModestAccountRetrieveType retrieve_type,
370                                 const gchar *store_account,
371                                 const gchar *transport_account,
372                                 gboolean enabled)
373 {
374         ModestAccountMgrPrivate *priv;
375         const gchar *key;
376         gboolean ok;
377         gchar *default_account;
378         GError *err = NULL;
379         
380         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
381         g_return_val_if_fail (name, FALSE);
382         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
383         
384         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
385
386         /*
387          * we create the account by adding an account 'dir', with the name <name>,
388          * and in that the 'display_name' string key
389          */
390         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_DISPLAY_NAME,
391                                                               FALSE);
392         if (modest_account_mgr_account_exists (self, key, FALSE)) {
393                 g_printerr ("modest: account already exists\n");
394                 return FALSE;
395         }
396         
397         ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
398         if (!ok) {
399                 g_printerr ("modest: cannot set display name\n");
400                 if (err) {
401                         g_printerr ("modest: Error adding account conf: %s\n", err->message);
402                         g_error_free (err);
403                 }
404                 return FALSE;
405         }
406         
407         if (store_account) {
408                 key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_STORE_ACCOUNT,
409                                                                       FALSE);
410                 ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
411                 if (!ok) {
412                         g_printerr ("modest: failed to set store account '%s'\n",
413                                     store_account);
414                         if (err) {
415                                 g_printerr ("modest: Error adding store account conf: %s\n", err->message);
416                                 g_error_free (err);
417                         }
418                         return FALSE;
419                 }
420         }
421         
422         if (transport_account) {
423                 key = _modest_account_mgr_get_account_keyname_cached (priv, name,
424                                                                       MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
425                                                                       FALSE);
426                 ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
427                 if (!ok) {
428                         g_printerr ("modest: failed to set transport account '%s'\n",
429                                     transport_account);
430                         if (err) {
431                                 g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
432                                 g_error_free (err);
433                         }       
434                         return FALSE;
435                 }
436         }
437
438         /* Make sure that leave-messages-on-server is enabled by default, 
439          * as per the UI spec, though it is only meaningful for accounts using POP.
440          * (possibly this gconf key should be under the server account): */
441         modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE);
442         modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
443
444         /* Fill other data */
445         modest_account_mgr_set_string (self, name,
446                                        MODEST_ACCOUNT_DISPLAY_NAME, 
447                                        display_name, FALSE);
448         modest_account_mgr_set_string (self, name,
449                                        MODEST_ACCOUNT_FULLNAME, 
450                                        user_fullname, FALSE);
451         modest_account_mgr_set_string (self, name,
452                                        MODEST_ACCOUNT_EMAIL, 
453                                        user_email, FALSE);
454         modest_account_mgr_set_retrieve_type (self, name,
455                                               retrieve_type);
456
457         /* Notify the observers */
458         g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
459
460         /* if no default account has been defined yet, do so now */
461         default_account = modest_account_mgr_get_default_account (self);
462         if (!default_account)
463                 modest_account_mgr_set_default_account (self, name);
464         g_free (default_account);
465         
466         /* (re)set the automatic account update */
467         modest_platform_set_update_interval
468                 (modest_conf_get_int (priv->modest_conf, MODEST_CONF_UPDATE_INTERVAL, NULL));
469         
470         return TRUE;
471 }
472
473
474 gboolean
475 modest_account_mgr_add_server_account (ModestAccountMgr * self,
476                                        const gchar *name, 
477                                        const gchar *hostname,
478                                        guint portnumber,
479                                        const gchar *username, 
480                                        const gchar *password,
481                                        ModestProtocolType proto,
482                                        ModestProtocolType security,
483                                        ModestProtocolType auth)
484 {
485         ModestAccountMgrPrivate *priv;
486         const gchar *key;
487         gboolean ok = TRUE;
488         GError *err = NULL;
489         ModestProtocolRegistry *protocol_registry;
490
491         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
492         g_return_val_if_fail (name, FALSE);
493         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
494
495         protocol_registry = modest_runtime_get_protocol_registry ();
496         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
497
498         /* hostname */
499         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_HOSTNAME, TRUE);
500         if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
501                 g_printerr ("modest: server account '%s' already exists\n", name);
502                 ok =  FALSE;
503         }
504         if (!ok)
505                 goto cleanup;
506         
507         modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &err);
508         if (err) {
509                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
510                 g_error_free (err);
511                 ok = FALSE;
512         }
513         if (!ok)
514                 goto cleanup;
515         
516         /* username */
517         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_USERNAME, TRUE);
518         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
519         if (err) {
520                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
521                 g_error_free (err);
522                 ok = FALSE;
523         }
524         if (!ok)
525                 goto cleanup;
526         
527         
528         /* password */
529         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PASSWORD, TRUE);
530         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
531         if (err) {
532                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
533                 g_error_free (err);
534                 ok = FALSE;
535         }
536         if (!ok)
537                 goto cleanup;
538
539         /* proto */
540         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
541         ok = modest_conf_set_string (priv->modest_conf, key,
542                                      modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
543                                      &err);
544         if (err) {
545                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
546                 g_error_free (err);
547                 ok = FALSE;
548         }
549         if (!ok)
550                 goto cleanup;
551
552
553         /* portnumber */
554         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PORT, TRUE);
555         ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
556         if (err) {
557                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
558                 g_error_free (err);
559                 ok = FALSE;
560         }
561         if (!ok)
562                 goto cleanup;
563
564         
565         /* auth mechanism */
566         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
567         ok = modest_conf_set_string (priv->modest_conf, key,
568                                      modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, auth)),
569                                      &err);
570         if (err) {
571                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
572                 g_error_free (err);
573                 ok = FALSE;
574         }
575         if (!ok)
576                 goto cleanup;
577         
578         /* Add the security settings: */
579         modest_account_mgr_set_server_account_security (self, name, security);
580
581 cleanup:
582         if (!ok) {
583                 g_printerr ("modest: failed to add server account\n");
584                 return FALSE;
585         }
586
587         return TRUE;
588 }
589
590 /** modest_account_mgr_add_server_account_uri:
591  * Only used for mbox and maildir accounts.
592  */
593 gboolean
594 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
595                                            const gchar *name, 
596                                            ModestProtocolType proto,
597                                            const gchar *uri)
598 {
599         ModestAccountMgrPrivate *priv;
600         const gchar *key;
601         gboolean ok;
602         ModestProtocolRegistry *protocol_registry;
603         
604         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
605         g_return_val_if_fail (name, FALSE);
606         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
607         g_return_val_if_fail (uri, FALSE);
608         
609         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
610         protocol_registry = modest_runtime_get_protocol_registry ();
611         
612         /* proto */
613         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
614         ok = modest_conf_set_string (priv->modest_conf, key,
615                                      modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
616                                      NULL);
617
618         if (!ok) {
619                 g_printerr ("modest: failed to set proto\n");
620                 return FALSE;
621         }
622         
623         /* uri */
624         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_URI, TRUE);
625         ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
626
627         if (!ok) {
628                 g_printerr ("modest: failed to set uri\n");
629                 return FALSE;
630         }
631         return TRUE;
632 }
633
634 /* 
635  * Utility function used by modest_account_mgr_remove_account
636  */
637 static void
638 real_remove_account (ModestConf *conf,
639                      const gchar *acc_name,
640                      gboolean server_account)
641 {
642         GError *err = NULL;
643         gchar *key;
644         
645         key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
646         modest_conf_remove_key (conf, key, &err);
647
648         if (err) {
649                 g_printerr ("modest: error removing key: %s\n", err->message);
650                 g_error_free (err);
651         }
652         g_free (key);
653 }
654
655 gboolean
656 modest_account_mgr_remove_account (ModestAccountMgr * self,
657                                    const gchar* name)
658 {
659         ModestAccountMgrPrivate *priv;
660         gchar *default_account_name, *store_acc_name, *transport_acc_name;
661         gboolean default_account_deleted;
662
663         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
664         g_return_val_if_fail (name, FALSE);
665
666         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
667                 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
668                 return FALSE;
669         }
670
671         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
672         default_account_deleted = FALSE;
673
674         /* If this was the default, then remove that setting: */
675         default_account_name = modest_account_mgr_get_default_account (self);
676         if (default_account_name && (strcmp (default_account_name, name) == 0)) {
677                 modest_account_mgr_unset_default_account (self);
678                 default_account_deleted = TRUE;
679         }
680         g_free (default_account_name);
681
682         /* Delete transport and store accounts */
683         store_acc_name = modest_account_mgr_get_string (self, name, 
684                                                         MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
685         if (store_acc_name)
686                 real_remove_account (priv->modest_conf, store_acc_name, TRUE);
687
688         transport_acc_name = modest_account_mgr_get_string (self, name, 
689                                                             MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
690         if (transport_acc_name)
691                 real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
692                         
693         /* Remove the modest account */
694         real_remove_account (priv->modest_conf, name, FALSE);
695
696         if (default_account_deleted) {  
697                 /* pick another one as the new default account. We do
698                    this *after* deleting the keys, because otherwise a
699                    call to account_names will retrieve also the
700                    deleted account */
701                 modest_account_mgr_set_first_account_as_default (self);
702         }
703
704         /* if this was the last account, stop any auto-updating */
705         /* (re)set the automatic account update */
706         GSList *acc_names = modest_account_mgr_account_names (self, TRUE);
707         if (!acc_names) {
708                 modest_platform_set_update_interval (0);
709                 /* it was the last account, the has_account / has_enabled_account
710                  * changes
711                  */
712                 priv->has_accounts = priv->has_enabled_accounts = FALSE; 
713         } else
714                 modest_account_mgr_free_account_names (acc_names);
715         
716         /* Notify the observers. We do this *after* deleting
717            the keys, because otherwise a call to account_names
718            will retrieve also the deleted account */
719         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
720         
721         return TRUE;
722 }
723
724
725
726 /* strip the first /n/ character from each element
727  * caller must make sure all elements are strings with
728  * length >= n, and also that data can be freed.
729  * change is in-place
730  */
731 static void
732 strip_prefix_from_elements (GSList * lst, guint n)
733 {
734         while (lst) {
735                 memmove (lst->data, lst->data + n,
736                          strlen(lst->data) - n + 1);
737                 lst = lst->next;
738         }
739 }
740
741
742 GSList*
743 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
744 {
745         GSList *accounts;
746         ModestAccountMgrPrivate *priv;
747         GError *err = NULL;
748         
749         const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
750
751         g_return_val_if_fail (self, NULL);
752
753         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
754         accounts = modest_conf_list_subkeys (priv->modest_conf,
755                                              MODEST_ACCOUNT_NAMESPACE, &err);
756
757         if (err) {
758                 g_printerr ("modest: failed to get subkeys (%s): %s\n",
759                             MODEST_ACCOUNT_NAMESPACE, err->message);
760                 g_error_free (err);
761                 return NULL; /* assume accounts did not get value when err is set...*/
762         }
763         
764         strip_prefix_from_elements (accounts, prefix_len);
765                 
766         GSList *result = NULL;
767         
768         /* Unescape the keys to get the account names: */
769         GSList *iter = accounts;
770         while (iter) {
771                 if (!(iter->data)) {
772                         iter = iter->next;
773                         continue;
774                 }
775
776                 const gchar* account_name_key = (const gchar*)iter->data;
777                 gchar* unescaped_name = account_name_key ? 
778                         modest_conf_key_unescape (account_name_key) 
779                         : NULL;
780                 
781                 gboolean add = TRUE;
782                 if (only_enabled) {
783                         if (unescaped_name && 
784                             !modest_account_mgr_get_bool (self, unescaped_name, 
785                                                           MODEST_ACCOUNT_ENABLED, FALSE))
786                                 add = FALSE;
787                 }
788                 
789                 /* Ignore modest accounts whose server accounts don't exist: 
790                  * (We could be getting this list while the account is being deleted, 
791                  * while the child server accounts have already been deleted, but the 
792                  * parent modest account already exists.
793                  */
794                 if (add) {
795                         gchar* server_account_name = modest_account_mgr_get_string
796                                 (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
797                                  FALSE);
798                         if (server_account_name) {
799                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
800                                         add = FALSE;
801                                 g_free (server_account_name);
802                         }
803                 }
804                 
805                 if (add) {
806                         gchar* server_account_name = modest_account_mgr_get_string
807                                 (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
808                                  FALSE);
809                         if (server_account_name) {
810                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
811                                         add = FALSE;
812                                 g_free (server_account_name);
813                         }
814                 }
815                 
816                 if (add)
817                         result = g_slist_append (result, unescaped_name);
818                 else 
819                         g_free (unescaped_name);
820
821                 g_free (iter->data);
822                 iter->data = NULL;
823                 
824                 iter = g_slist_next (iter);     
825         }
826         
827
828         /* we already freed the strings in the loop */
829         g_slist_free (accounts);
830         
831         accounts = NULL;
832         return result;
833 }
834
835
836 void
837 modest_account_mgr_free_account_names (GSList *account_names)
838 {
839         g_slist_foreach (account_names, (GFunc)g_free, NULL);
840         g_slist_free (account_names);
841 }
842
843
844
845 gchar *
846 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
847                                const gchar *key, gboolean server_account) {
848
849         ModestAccountMgrPrivate *priv;
850
851         const gchar *keyname;
852         gchar *retval;
853         GError *err = NULL;
854
855         g_return_val_if_fail (self, NULL);
856         g_return_val_if_fail (name, NULL);
857         g_return_val_if_fail (key, NULL);
858
859         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
860         
861         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
862         
863         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
864         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
865         if (err) {
866                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
867                 g_error_free (err);
868                 retval = NULL;
869         }
870
871         return retval;
872 }
873
874
875 gint
876 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
877                             gboolean server_account)
878 {
879         ModestAccountMgrPrivate *priv;
880
881         const gchar *keyname;
882         gint retval;
883         GError *err = NULL;
884         
885         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
886         g_return_val_if_fail (name, -1);
887         g_return_val_if_fail (key, -1);
888
889         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
890
891         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
892         
893         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
894         if (err) {
895                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
896                 g_error_free (err);
897                 retval = -1;
898         }
899
900         return retval;
901 }
902
903
904
905 gboolean
906 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
907                              const gchar * key, gboolean server_account)
908 {
909         ModestAccountMgrPrivate *priv;
910
911         const gchar *keyname;
912         gboolean retval;
913         GError *err = NULL;
914
915         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
916         g_return_val_if_fail (account, FALSE);
917         g_return_val_if_fail (key, FALSE);
918
919         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
920         ///keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
921
922         keyname = _modest_account_mgr_get_account_keyname_cached (priv, account, key, server_account);
923                 
924         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
925         if (err) {
926                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
927                 g_error_free (err);
928                 retval = FALSE;
929         }
930
931         return retval;
932 }
933
934
935
936 GSList * 
937 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
938                              const gchar *key, ModestConfValueType list_type,
939                              gboolean server_account)
940 {
941         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
942
943         const gchar *keyname;
944         GSList *retval;
945         GError *err = NULL;
946         
947         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
948         g_return_val_if_fail (name, NULL);
949         g_return_val_if_fail (key, NULL);
950         
951         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
952
953         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key,
954                                                                   server_account);
955         
956         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
957         if (err) {
958                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
959                             err->message);
960                 g_error_free (err);
961                 retval = NULL;
962         }
963         return retval;
964 }
965
966
967 gboolean
968 modest_account_mgr_set_string (ModestAccountMgr * self, 
969                                const gchar * name,
970                                const gchar * key, 
971                                const gchar * val, 
972                                gboolean server_account)
973 {
974         ModestAccountMgrPrivate *priv;
975
976         const gchar *keyname;
977         gboolean retval;
978         GError *err = NULL;
979
980         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
981         g_return_val_if_fail (name, FALSE);
982         g_return_val_if_fail (key, FALSE);
983
984         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
985
986         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
987         
988         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
989         if (err) {
990                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
991                 g_error_free (err);
992                 retval = FALSE;
993         }
994         return retval;
995 }
996
997 gboolean
998 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
999                             const gchar * key, int val, gboolean server_account)
1000 {
1001         ModestAccountMgrPrivate *priv;
1002         const gchar *keyname;
1003         gboolean retval;
1004         GError *err = NULL;
1005         
1006         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1007         g_return_val_if_fail (name, FALSE);
1008         g_return_val_if_fail (key, FALSE);
1009
1010         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1011
1012         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1013         
1014         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
1015         if (err) {
1016                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
1017                 g_error_free (err);
1018                 retval = FALSE;
1019         } else {
1020                 /* check whether this field is one of those interesting for the 
1021                  * "account-updated" signal */
1022                 if (strcmp(key, MODEST_ACCOUNT_LAST_UPDATED) == 0) {
1023                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATED_SIGNAL], 
1024                                         0, name);
1025                 }
1026         }
1027         return retval;
1028 }
1029
1030
1031
1032 gboolean
1033 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
1034                              const gchar * key, gboolean val, gboolean server_account)
1035 {
1036         ModestAccountMgrPrivate *priv;
1037
1038         const gchar *keyname;
1039         gboolean retval;
1040         GError *err = NULL;
1041
1042         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1043         g_return_val_if_fail (name, FALSE);
1044         g_return_val_if_fail (key, FALSE);
1045
1046         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1047         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1048         
1049         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
1050         if (err) {
1051                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
1052                 g_error_free (err);
1053                 retval = FALSE;
1054         }
1055
1056         return retval;
1057 }
1058
1059
1060 gboolean
1061 modest_account_mgr_set_list (ModestAccountMgr *self,
1062                              const gchar *name,
1063                              const gchar *key,
1064                              GSList *val,
1065                              ModestConfValueType list_type,
1066                              gboolean server_account)
1067 {
1068         ModestAccountMgrPrivate *priv;
1069         const gchar *keyname;
1070         GError *err = NULL;
1071         gboolean retval;
1072         
1073         g_return_val_if_fail (self, FALSE);
1074         g_return_val_if_fail (name, FALSE);
1075         g_return_val_if_fail (key,  FALSE);
1076         g_return_val_if_fail (val,  FALSE);
1077
1078         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1079
1080         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1081         
1082         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
1083         if (err) {
1084                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
1085                 g_error_free (err);
1086                 retval = FALSE;
1087         }
1088
1089         return retval;
1090 }
1091
1092 gboolean
1093 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
1094                                    gboolean server_account)
1095 {
1096         ModestAccountMgrPrivate *priv;
1097
1098         const gchar *keyname;
1099         gboolean retval;
1100         GError *err = NULL;
1101
1102         g_return_val_if_fail (self, FALSE);
1103         g_return_val_if_fail (name, FALSE);
1104
1105         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1106         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, NULL, server_account);
1107         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1108         if (err) {
1109                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1110                             err->message);
1111                 g_error_free (err);
1112                 retval = FALSE;
1113         }
1114         return retval;
1115 }
1116
1117 gboolean
1118 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, 
1119                                                       const gchar *display_name)
1120 {
1121         GSList *account_names = NULL;
1122         GSList *cursor = NULL;
1123         
1124         cursor = account_names = modest_account_mgr_account_names (self, 
1125                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1126
1127         gboolean found = FALSE;
1128         
1129         /* Look at each non-server account to check their display names; */
1130         while (cursor) {
1131                 const gchar *account_name = (gchar*)cursor->data;
1132                 const gchar *cursor_display_name;
1133                 
1134                 ModestAccountSettings *settings = modest_account_mgr_load_account_settings (self, account_name);
1135                 if (!settings) {
1136                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1137                         cursor = cursor->next;
1138                         continue;
1139                 }
1140
1141                 cursor_display_name = modest_account_settings_get_display_name (settings);
1142                 if(cursor_display_name && (strcmp (cursor_display_name, display_name) == 0)) {
1143                         found = TRUE;
1144                         g_object_unref (settings);
1145                         break;
1146                 }
1147
1148                 g_object_unref (settings);
1149                 cursor = cursor->next;
1150         }
1151         modest_account_mgr_free_account_names (account_names);
1152         account_names = NULL;
1153         
1154         return found;
1155 }
1156
1157 static gboolean
1158 server_accounts_equal (ModestServerAccountSettings *s1,
1159                        ModestServerAccountSettings *s2)
1160 {
1161         const gchar *str1, *str2;
1162
1163         if (modest_server_account_settings_get_protocol (s1) !=
1164             modest_server_account_settings_get_protocol (s2))
1165                 return FALSE;
1166
1167         str1 = modest_server_account_settings_get_username (s1);
1168         str2 = modest_server_account_settings_get_username (s2);
1169         if (str1 && str2 && (str1 != str2) &&
1170             strcmp (str1, str2) != 0)
1171                 return FALSE;
1172
1173         str1 = modest_server_account_settings_get_hostname (s1);
1174         str2 = modest_server_account_settings_get_hostname (s2);
1175         if (str1 && str2 && (str1 != str2) &&
1176             strcmp (str1, str2) != 0)
1177                 return FALSE;
1178
1179         if (modest_server_account_settings_get_port (s1) !=
1180             modest_server_account_settings_get_port (s2))
1181                 return FALSE;
1182
1183         return TRUE;
1184 }
1185
1186 gboolean
1187 modest_account_mgr_check_already_configured_account  (ModestAccountMgr *self, 
1188                                                       ModestAccountSettings *settings)
1189 {
1190         GSList *account_names = NULL;
1191         GSList *cursor = NULL;
1192         ModestServerAccountSettings *server_settings;
1193         
1194         cursor = account_names = modest_account_mgr_account_names (self, 
1195                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1196
1197         gboolean found = FALSE;
1198
1199         server_settings = modest_account_settings_get_store_settings (settings);
1200         if (!server_settings) {
1201                 g_printerr ("modest: couldn't get store settings from settings");
1202                 modest_account_mgr_free_account_names (account_names);
1203                 return FALSE;
1204         }
1205         
1206         /* Look at each non-server account to check their display names; */
1207         while (cursor && !found) {
1208                 const gchar *account_name;
1209                 ModestAccountSettings *from_mgr_settings;
1210                 ModestServerAccountSettings *from_mgr_server_settings;
1211
1212                 account_name = (gchar*)cursor->data;            
1213                 from_mgr_settings = modest_account_mgr_load_account_settings (self, account_name);
1214                 if (!settings) {
1215                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1216                         cursor = cursor->next;
1217                         continue;
1218                 }
1219
1220                 from_mgr_server_settings = modest_account_settings_get_store_settings (from_mgr_settings);
1221                 if (server_settings) {
1222                         if (server_accounts_equal (from_mgr_server_settings, server_settings)) {
1223                                 found = TRUE;
1224                         }
1225                         g_object_unref (from_mgr_server_settings);
1226                 } else {
1227                         g_printerr ("modest: couldn't get store settings from account %s", account_name);
1228                 }
1229                 g_object_unref (from_mgr_settings);
1230                 cursor = cursor->next;
1231         }
1232
1233         g_object_unref (server_settings);
1234         modest_account_mgr_free_account_names (account_names);
1235         account_names = NULL;
1236         
1237         return found;
1238 }
1239
1240
1241
1242
1243 gboolean 
1244 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1245                           const gchar *key, gboolean server_account)
1246 {
1247         ModestAccountMgrPrivate *priv;
1248         
1249         const gchar *keyname;
1250         gboolean retval;
1251         GError *err = NULL;
1252         
1253         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1254         g_return_val_if_fail (name, FALSE);
1255         g_return_val_if_fail (key, FALSE);
1256
1257         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1258         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1259
1260         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1261         if (err) {
1262                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1263                             err->message);
1264                 g_error_free (err);
1265                 retval = FALSE;
1266         }
1267         return retval;
1268 }
1269
1270 gchar*
1271 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1272 {
1273         /* Initialize input parameters: */
1274         if (is_account_key)
1275                 *is_account_key = FALSE;
1276
1277         if (is_server_account)
1278                 *is_server_account = FALSE;
1279
1280         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1281         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1282         gchar *cursor;
1283         gchar *account = NULL;
1284
1285         /* determine whether it's an account or a server account,
1286          * based on the prefix */
1287         if (g_str_has_prefix (key, account_ns)) {
1288
1289                 if (is_server_account)
1290                         *is_server_account = FALSE;
1291                 
1292                 account = g_strdup (key + strlen (account_ns));
1293
1294         } else if (g_str_has_prefix (key, server_account_ns)) {
1295
1296                 if (is_server_account)
1297                         *is_server_account = TRUE;
1298                 
1299                 account = g_strdup (key + strlen (server_account_ns));  
1300         } else
1301                 return NULL;
1302
1303         /* if there are any slashes left in the key, it's not
1304          * the toplevel entry for an account
1305          */
1306         cursor = strstr(account, "/");
1307         
1308         if (is_account_key && cursor)
1309                 *is_account_key = TRUE;
1310
1311         /* put a NULL where the first slash was */
1312         if (cursor)
1313                 *cursor = '\0';
1314
1315         if (account) {
1316                 /* The key is an escaped string, so unescape it to get the actual account name: */
1317                 gchar *unescaped_name = modest_conf_key_unescape (account);
1318                 g_free (account);
1319                 return unescaped_name;
1320         } else
1321                 return NULL;
1322 }
1323
1324
1325
1326
1327
1328 /* optimization: only with non-alphanum chars, escaping is needed */
1329 inline static gboolean
1330 is_alphanum (const gchar* str)
1331 {
1332         const gchar *cursor;
1333         for (cursor = str; cursor && *cursor; ++cursor) {
1334                 const char c = *cursor;
1335                 /* we cannot trust isalnum(3), because it might consider locales */
1336                 /*       numbers            ALPHA            alpha       */
1337                 if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
1338                         return FALSE;
1339         }
1340         return TRUE;
1341 }
1342                 
1343
1344
1345
1346 /* must be freed by caller */
1347 gchar *
1348 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar* name,
1349                                          gboolean server_account)
1350 {
1351         gchar *retval = NULL;   
1352         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1353         gchar *escaped_account_name, *escaped_name;
1354         
1355         if (!account_name)
1356                 return g_strdup (namespace);
1357
1358         /* optimization: only escape names when need to be escaped */
1359         if (is_alphanum (account_name))
1360                 escaped_account_name = (gchar*)account_name;
1361         else
1362                 escaped_account_name = modest_conf_key_escape (account_name);
1363         
1364         if (is_alphanum (name))
1365                 escaped_name = (gchar*)name;
1366         else
1367                 escaped_name = modest_conf_key_escape (name);
1368         //////////////////////////////////////////////////////////////
1369
1370         if (escaped_account_name && escaped_name)
1371                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1372         else if (escaped_account_name)
1373                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1374
1375         /* Sanity check: */
1376         if (!retval || !modest_conf_key_is_valid (retval)) {
1377                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__,
1378                            retval ? retval: "<empty>");
1379                 g_free (retval);
1380                 retval = NULL;
1381         }
1382         
1383         /* g_free is only needed if we actually allocated anything */
1384         if (name != escaped_name)
1385                 g_free (escaped_name);
1386         if (account_name != escaped_account_name)
1387                 g_free (escaped_account_name);
1388
1389         return retval;
1390 }
1391
1392 static const gchar *
1393 _modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, 
1394                                                 const gchar* account_name,
1395                                                 const gchar *name, 
1396                                                 gboolean is_server)
1397 {
1398         GHashTable *hash = is_server ? priv->server_account_key_hash : priv->account_key_hash;
1399         GHashTable *account_hash;
1400         gchar *key = NULL;
1401         const gchar *search_name;
1402
1403         if (!account_name)
1404                 return is_server ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1405
1406         search_name = name ? name : "<dummy>";
1407         
1408         account_hash = g_hash_table_lookup (hash, account_name);        
1409         if (!account_hash) { /* no hash for this account yet? create it */
1410                 account_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);         
1411                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1412                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1413                 g_hash_table_insert (hash, g_strdup(account_name), account_hash);
1414                 return key;
1415         }
1416         
1417         /* we have a hash for this account, but do we have the key? */  
1418         key = g_hash_table_lookup (account_hash, search_name);
1419         if (!key) {
1420                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1421                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1422         }
1423         
1424         return key;
1425 }
1426
1427
1428 gboolean
1429 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1430 {
1431         ModestAccountMgrPrivate* priv;
1432         GSList *account_names;
1433         gboolean accounts_exist;
1434
1435         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1436         
1437         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1438         
1439         if (enabled && priv->has_enabled_accounts)
1440                 return TRUE;
1441         else if (priv->has_accounts)
1442                 return TRUE;
1443                 
1444         /* Check that at least one account exists: */
1445         account_names = modest_account_mgr_account_names (self,enabled);
1446         accounts_exist = account_names != NULL;
1447         modest_account_mgr_free_account_names (account_names);
1448         account_names = NULL;
1449
1450         /* cache it. */
1451         if (enabled)
1452                 priv->has_enabled_accounts = accounts_exist;
1453         else
1454                 priv->has_accounts = accounts_exist;
1455         
1456         return accounts_exist;
1457 }
1458
1459 static int
1460 compare_account_name(gconstpointer a, gconstpointer b)
1461 {
1462         const gchar* account_name = (const gchar*) a;
1463         const gchar* account_name2 = (const gchar*) b;
1464         return strcmp(account_name, account_name2);
1465 }
1466
1467 void 
1468 modest_account_mgr_set_account_busy(ModestAccountMgr* self, 
1469                                     const gchar* account_name, 
1470                                     gboolean busy)
1471 {
1472         ModestAccountMgrPrivate* priv;
1473
1474         g_return_if_fail (MODEST_IS_ACCOUNT_MGR(self));
1475         g_return_if_fail (account_name);
1476
1477         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1478         if (busy) {
1479                 GSList *account_names = modest_account_mgr_account_names (self, TRUE);
1480                 GSList* account = g_slist_find_custom(account_names, account_name, 
1481                                                       (GCompareFunc) compare_account_name);
1482
1483                 if (account && !modest_account_mgr_account_is_busy(self, account_name)) {
1484                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1485                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
1486                                        0, account_name, TRUE);
1487                 }
1488                 modest_account_mgr_free_account_names (account_names);
1489                 account_names = NULL;
1490         } else {
1491                 GSList* account = 
1492                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1493
1494                 if (account) {
1495                         g_free(account->data);
1496                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1497                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
1498                                        0, account_name, FALSE);
1499                 }
1500         }
1501 }
1502
1503 gboolean
1504 modest_account_mgr_account_is_busy (ModestAccountMgr* self, const gchar* account_name)
1505 {
1506         ModestAccountMgrPrivate* priv;
1507         
1508         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1509         g_return_val_if_fail (account_name, FALSE);
1510
1511         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1512                 
1513         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1514                 != NULL);
1515 }
1516
1517 void
1518 modest_account_mgr_notify_account_update (ModestAccountMgr* self, 
1519                                           const gchar *server_account_name)
1520 {
1521         ModestProtocolType proto;
1522         ModestAccountMgrPrivate* priv;
1523         ModestProtocolRegistry *protocol_registry;
1524         gchar *proto_name = NULL;
1525
1526         g_return_if_fail (self);
1527         g_return_if_fail (server_account_name);
1528         
1529         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1530         protocol_registry = modest_runtime_get_protocol_registry ();
1531         
1532         /* Get protocol */
1533         proto_name = modest_account_mgr_get_string (self, server_account_name, 
1534                                                     MODEST_ACCOUNT_PROTO, TRUE);
1535         if (!proto_name) {
1536                 g_return_if_reached ();
1537                 return;
1538         }
1539         proto = modest_protocol_get_type_id (modest_protocol_registry_get_protocol_by_name (protocol_registry,
1540                                                                                             MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
1541                                                                                             proto_name));
1542         g_free (proto_name);
1543
1544         /* there is some update in the account, so we can't
1545          * be sure about whether there are still enabled accounts...
1546          */
1547         priv->has_enabled_accounts = FALSE;
1548         priv->has_accounts         = FALSE;
1549         
1550         /* Emit "update-account" */
1551         g_signal_emit (G_OBJECT(self), 
1552                        signals[ACCOUNT_CHANGED_SIGNAL], 0, 
1553                        server_account_name, 
1554                        (modest_protocol_registry_protocol_type_has_tag (protocol_registry, proto, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) ? 
1555                        TNY_ACCOUNT_TYPE_STORE : 
1556                        TNY_ACCOUNT_TYPE_TRANSPORT);
1557 }
1558
1559
1560 gboolean
1561 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
1562 {
1563         ModestConf *conf;
1564         gboolean retval;
1565         
1566         g_return_val_if_fail (self,    FALSE);
1567         g_return_val_if_fail (account, FALSE);
1568         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
1569                               FALSE);
1570         
1571         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1572
1573         /* Change the default account and notify */
1574         retval = modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, account, NULL);
1575         if (retval)
1576                 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
1577
1578         return retval;
1579 }
1580
1581
1582 gchar*
1583 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
1584 {
1585         gchar *account; 
1586         ModestConf *conf;
1587         GError *err = NULL;
1588         
1589         g_return_val_if_fail (self, NULL);
1590
1591         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1592         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
1593         
1594         if (err) {
1595                 g_printerr ("modest: failed to get '%s': %s\n",
1596                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
1597                 g_error_free (err);
1598                 return  NULL;
1599         }
1600         
1601         /* sanity check */
1602         if (account && !modest_account_mgr_account_exists (self, account, FALSE)) {
1603                 g_printerr ("modest: default account does not exist\n");
1604                 g_free (account);
1605                 return NULL;
1606         }
1607
1608         return account;
1609 }
1610
1611 static gboolean
1612 modest_account_mgr_unset_default_account (ModestAccountMgr *self)
1613 {
1614         ModestConf *conf;
1615         gboolean retval;
1616         
1617         g_return_val_if_fail (self,    FALSE);
1618
1619         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1620                 
1621         retval = modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
1622
1623         if (retval)
1624                 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
1625
1626         return retval;
1627 }
1628
1629
1630 gchar* 
1631 modest_account_mgr_get_display_name (ModestAccountMgr *self, 
1632                                      const gchar* name)
1633 {
1634         return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
1635 }
1636
1637 void 
1638 modest_account_mgr_set_display_name (ModestAccountMgr *self, 
1639                                      const gchar *account_name,
1640                                      const gchar *display_name)
1641 {
1642         gboolean notify = TRUE;
1643
1644         if (!modest_account_mgr_get_display_name (self, account_name))
1645                 notify = FALSE;
1646
1647         modest_account_mgr_set_string (self, 
1648                                        account_name,
1649                                        MODEST_ACCOUNT_DISPLAY_NAME, 
1650                                        display_name, 
1651                                        FALSE /* not server account */);
1652
1653         /* Notify about the change in the display name */
1654         if (notify)
1655                 g_signal_emit (self, signals[DISPLAY_NAME_CHANGED_SIGNAL], 0, account_name);
1656 }
1657
1658 gboolean 
1659 modest_account_mgr_singleton_protocol_exists (ModestAccountMgr *mgr,
1660                                               ModestProtocolType protocol_type)
1661 {
1662         GSList *account_names, *node;
1663         gboolean found = FALSE;
1664
1665         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR (mgr), FALSE);
1666         account_names = modest_account_mgr_account_names (mgr, TRUE);
1667
1668         for (node = account_names; node != NULL; node = g_slist_next (node)) {
1669                 ModestProtocolType current_protocol;
1670
1671                 current_protocol = modest_account_mgr_get_store_protocol (mgr, (gchar *) node->data);
1672                 if (current_protocol == protocol_type) {
1673                         found = TRUE;
1674                         break;
1675                 }
1676         }
1677
1678         modest_account_mgr_free_account_names (account_names);
1679
1680         return found;
1681 }