* cache the modest-conf keynames, so we don't have to re-calculate
[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-account-mgr.h>
33 #include <modest-account-mgr-priv.h>
34 #include <modest-account-mgr-helpers.h>
35
36 /* 'private'/'protected' functions */
37 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
38 static void modest_account_mgr_init       (ModestAccountMgr * obj);
39 static void modest_account_mgr_finalize   (GObject * obj);
40 static void modest_account_mgr_base_init  (gpointer g_class);
41
42 static const gchar *
43 _modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, const gchar* account_name,
44                                                 const gchar *name, gboolean is_server);
45
46 /* list my signals */
47 enum {
48         ACCOUNT_INSERTED_SIGNAL,
49         ACCOUNT_CHANGED_SIGNAL,
50         ACCOUNT_REMOVED_SIGNAL,
51         ACCOUNT_BUSY_SIGNAL,
52         DEFAULT_ACCOUNT_CHANGED_SIGNAL,
53         LAST_SIGNAL
54 };
55
56 /* globals */
57 static GObjectClass *parent_class = NULL;
58 static guint signals[LAST_SIGNAL] = {0};
59
60 /* is the account already in the queue? */
61 static gboolean
62 in_change_queue (GSList *change_queue, const gchar *account)
63 {
64         GSList *cursor = change_queue;
65         while (cursor) {
66                 const gchar *acc = cursor->data;
67                 if (acc && strcmp (acc, account) == 0)
68                         return TRUE;
69                 cursor = g_slist_next (cursor);
70         }
71         return FALSE;
72 }
73
74 static GSList*
75 add_to_change_queue (GSList *change_queue, const gchar *account_name)
76 {
77         g_return_val_if_fail (account_name, change_queue);      
78         return g_slist_prepend (change_queue, g_strdup (account_name));
79 }
80
81
82 /* we don't need to track specific accounts, as in our UI case
83  * it's impossible to change two accounts within .5 seconds.
84  * still, we might want to allow that later, and then this func
85  * will come in handy */
86 #if 0
87 static GSList*
88 remove_from_queue (GSList *change_queue, const gchar *account)
89 {
90         GSList *cursor = change_queue;
91         while (cursor) {
92                 const gchar *acc = cursor->data;
93                 if (acc && strcmp (acc, account) == 0) {
94                         g_free (acc);
95                         return g_slist_delete_link (change_queue, cursor);
96                 }
97                 cursor = g_slist_next (cursor);
98         }
99         return change_queue;
100 }
101 #endif
102
103 static gboolean
104 on_timeout_notify_changes (gpointer data)
105 {
106         ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
107         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
108                 
109         GSList *cursor = priv->change_queue;
110         while (cursor) {
111                 const gchar *account = cursor->data;
112                 if (account)
113                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
114                                        account);
115                 cursor = g_slist_next (cursor);
116         }
117         
118         /* free our queue */
119         g_slist_foreach (priv->change_queue, (GFunc)g_free, NULL);
120         g_slist_free (priv->change_queue);
121         priv->change_queue = NULL;
122         priv->timeout = 0; /* hmmm */
123         
124         return FALSE; /* don't call me again */
125 }
126
127
128 /* little hack to retrieve the account name from a server account name,
129  * by relying on the convention for that. Note: this changes the
130  * string in-place
131  *
132  * server accounts look like fooID_transport or fooID_store
133  * FIXME: make the suffixes more explicit in the account setup
134  */
135 static void
136 get_account_name_from_server_account (gchar *server_account_name)
137 {
138         static const gchar *t = "ID_transport";
139         static const gchar *s = "ID_store";
140         const guint len_t = strlen (t);
141         const guint len_s = strlen (s);
142         
143         guint len_a = strlen (server_account_name);
144                 
145         if (g_str_has_suffix (server_account_name, t)) 
146                 server_account_name [len_a - len_t] = '\0';
147         else if (g_str_has_suffix (server_account_name, s)) 
148                 server_account_name [len_a - len_s] = '\0';
149 }
150
151
152
153 static void
154 on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event,
155                ModestConfNotificationId id, gpointer user_data)
156 {
157         ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data);
158         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
159
160         gboolean is_account_key;
161         gboolean is_server_account;
162         gchar* account_name = NULL;
163
164         /* Notify that the default account has changed */
165         if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
166                 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
167                 return;
168         }
169         
170         is_account_key = FALSE;
171         is_server_account = FALSE;
172         account_name = _modest_account_mgr_account_from_key (key, &is_account_key,
173                                                              &is_server_account);
174
175         /* if this is not an account-related key change, ignore */
176         if (!account_name)
177                 return;
178
179         /* account was removed. Do not emit an account removed signal
180            because it was already being done in the remove_account
181            method. Do not notify also the removal of the server
182            account keys for the same reason */
183         if ((is_account_key || is_server_account) &&
184             event == MODEST_CONF_EVENT_KEY_UNSET) {
185                 g_free (account_name);
186                 return;
187         }
188
189         /* ignore server account changes */
190         if (is_server_account)
191                 /* change in place: retrieve the parent account name */
192                 get_account_name_from_server_account (account_name);
193
194         /* is this account enabled? */
195         gboolean enabled = FALSE;
196         if (is_server_account)
197                 enabled = TRUE;
198         else
199                 enabled = modest_account_mgr_get_enabled (self, account_name);
200
201         /* Notify is server account was changed, default account was changed
202          * or when enabled/disabled changes:
203          */
204         if (!is_server_account)
205         if (enabled || g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) ||
206             strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
207                 if (!in_change_queue (priv->change_queue, account_name)) {
208                         priv->change_queue = add_to_change_queue (priv->change_queue,
209                                                                   account_name);
210                         /* hmm, small race when this object is destroyed within
211                          * 500ms of the last change, and there are multiple timeouts... */
212                         priv->timeout = g_timeout_add (500, (GSourceFunc) on_timeout_notify_changes,
213                                                        self);
214                 }
215         }
216         g_free (account_name);
217 }
218
219
220 GType
221 modest_account_mgr_get_type (void)
222 {
223         static GType my_type = 0;
224
225         if (!my_type) {
226                 static const GTypeInfo my_info = {
227                         sizeof (ModestAccountMgrClass),
228                         modest_account_mgr_base_init,   /* base init */
229                         NULL,   /* base finalize */
230                         (GClassInitFunc) modest_account_mgr_class_init,
231                         NULL,   /* class finalize */
232                         NULL,   /* class data */
233                         sizeof (ModestAccountMgr),
234                         1,      /* n_preallocs */
235                         (GInstanceInitFunc) modest_account_mgr_init,
236                         NULL
237                 };
238
239                 my_type = g_type_register_static (G_TYPE_OBJECT,
240                                                   "ModestAccountMgr",
241                                                   &my_info, 0);
242         }
243         return my_type;
244 }
245
246 static void 
247 modest_account_mgr_base_init (gpointer g_class)
248 {
249         static gboolean modest_account_mgr_initialized = FALSE;
250
251         if (!modest_account_mgr_initialized) {
252                 /* signal definitions */
253                 signals[ACCOUNT_INSERTED_SIGNAL] =
254                         g_signal_new ("account_inserted",
255                                       MODEST_TYPE_ACCOUNT_MGR,
256                                       G_SIGNAL_RUN_FIRST,
257                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_inserted),
258                                       NULL, NULL,
259                                       g_cclosure_marshal_VOID__STRING,
260                                       G_TYPE_NONE, 1, G_TYPE_STRING);
261
262                 signals[ACCOUNT_REMOVED_SIGNAL] =
263                         g_signal_new ("account_removed",
264                                       MODEST_TYPE_ACCOUNT_MGR,
265                                       G_SIGNAL_RUN_FIRST,
266                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_removed),
267                                       NULL, NULL,
268                                       g_cclosure_marshal_VOID__STRING,
269                                       G_TYPE_NONE, 1, G_TYPE_STRING);
270
271                 signals[ACCOUNT_CHANGED_SIGNAL] =
272                         g_signal_new ("account_changed",
273                                       MODEST_TYPE_ACCOUNT_MGR,
274                                       G_SIGNAL_RUN_FIRST,
275                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_changed),
276                                       NULL, NULL,
277                                       g_cclosure_marshal_VOID__STRING,
278                                       G_TYPE_NONE, 1, G_TYPE_STRING);
279
280                 signals[ACCOUNT_BUSY_SIGNAL] =
281                         g_signal_new ("account_busy_changed",
282                                       MODEST_TYPE_ACCOUNT_MGR,
283                                       G_SIGNAL_RUN_FIRST,
284                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_busy_changed),
285                                       NULL, NULL,
286                                       modest_marshal_VOID__STRING_BOOLEAN,
287                                       G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
288
289                 signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL] =
290                         g_signal_new ("default_account_changed",
291                                       MODEST_TYPE_ACCOUNT_MGR,
292                                       G_SIGNAL_RUN_FIRST,
293                                       G_STRUCT_OFFSET(ModestAccountMgrClass, default_account_changed),
294                                       NULL, NULL,
295                                       g_cclosure_marshal_VOID__VOID,
296                                       G_TYPE_NONE, 0);
297
298                 modest_account_mgr_initialized = TRUE;
299         }
300 }
301
302 static void
303 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
304 {
305         GObjectClass *gobject_class;
306         gobject_class = (GObjectClass *) klass;
307
308         parent_class = g_type_class_peek_parent (klass);
309         gobject_class->finalize = modest_account_mgr_finalize;
310
311         g_type_class_add_private (gobject_class,
312                                   sizeof (ModestAccountMgrPrivate));
313 }
314
315
316 static void
317 modest_account_mgr_init (ModestAccountMgr * obj)
318 {
319         ModestAccountMgrPrivate *priv =
320                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
321
322         priv->modest_conf   = NULL;
323         priv->busy_accounts = NULL;
324         priv->change_queue  = NULL;
325         priv->timeout       = 0;
326         
327         priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
328
329         /* we maintain hashes for the modest-conf keys we build from account name
330          * + key. many seem to be used often, and generating them showed up high
331          * in oprofile */
332         /* both hashes are hashes to hashes;
333          * account-key => keyname ==> account-key-name
334          */     
335         priv->server_account_key_hash = g_hash_table_new_full (g_str_hash,
336                                                                g_str_equal,
337                                                                g_free,
338                                                                (GDestroyNotify)g_hash_table_destroy);
339         priv->account_key_hash        = g_hash_table_new_full (g_str_hash,
340                                                                g_str_equal,
341                                                                g_free,
342                                                                (GDestroyNotify)g_hash_table_destroy);
343 }
344
345 static void
346 modest_account_mgr_finalize (GObject * obj)
347 {
348         ModestAccountMgrPrivate *priv = 
349                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
350
351         if (priv->notification_id_accounts) {
352                 /* TODO: forget dirs */
353
354                 g_hash_table_destroy (priv->notification_id_accounts);
355         }
356
357         if (priv->key_changed_handler_uid) {
358                 g_signal_handler_disconnect (priv->modest_conf, 
359                                              priv->key_changed_handler_uid);
360                 priv->key_changed_handler_uid = 0;
361         }
362
363         if (priv->modest_conf) {
364                 g_object_unref (G_OBJECT(priv->modest_conf));
365                 priv->modest_conf = NULL;
366         }
367
368         if (priv->timeout)
369                 g_source_remove (priv->timeout);
370         priv->timeout = 0;
371
372         if (priv->server_account_key_hash) {
373                 g_hash_table_destroy (priv->server_account_key_hash);
374                 priv->server_account_key_hash = NULL;
375         }
376         
377         if (priv->account_key_hash) {
378                 g_hash_table_destroy (priv->account_key_hash);
379                 priv->account_key_hash = NULL;
380         }
381
382         G_OBJECT_CLASS(parent_class)->finalize (obj);
383 }
384
385
386 ModestAccountMgr *
387 modest_account_mgr_new (ModestConf *conf)
388 {
389         GObject *obj;
390         ModestAccountMgrPrivate *priv;
391
392         g_return_val_if_fail (conf, NULL);
393
394         obj = G_OBJECT (g_object_new (MODEST_TYPE_ACCOUNT_MGR, NULL));
395         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
396
397         g_object_ref (G_OBJECT(conf));
398         priv->modest_conf = conf;
399
400         priv->key_changed_handler_uid = 
401                 g_signal_connect (G_OBJECT (conf), "key_changed",
402                                   G_CALLBACK (on_key_change),
403                                   obj);
404         
405         return MODEST_ACCOUNT_MGR (obj);
406 }
407
408
409 static const gchar *
410 null_means_empty (const gchar * str)
411 {
412         return str ? str : "";
413 }
414
415
416 gboolean
417 modest_account_mgr_add_account (ModestAccountMgr *self,
418                                 const gchar *name,
419                                 const gchar *store_account,
420                                 const gchar *transport_account,
421                                 gboolean enabled)
422 {
423         ModestAccountMgrPrivate *priv;
424         const gchar *key;
425         gboolean ok;
426         gchar *default_account;
427         GError *err = NULL;
428         
429         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
430         g_return_val_if_fail (name, FALSE);
431         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
432         
433         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
434
435         /*
436          * we create the account by adding an account 'dir', with the name <name>,
437          * and in that the 'display_name' string key
438          */
439         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_DISPLAY_NAME,
440                                                               FALSE);
441         if (modest_account_mgr_account_exists (self, key, FALSE)) {
442                 g_printerr ("modest: account already exists\n");
443                 return FALSE;
444         }
445         
446         ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
447         if (!ok) {
448                 g_printerr ("modest: cannot set display name\n");
449                 if (err) {
450                         g_printerr ("modest: Error adding account conf: %s\n", err->message);
451                         g_error_free (err);
452                 }
453                 return FALSE;
454         }
455         
456         if (store_account) {
457                 key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_STORE_ACCOUNT,
458                                                                       FALSE);
459                 ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
460                 if (!ok) {
461                         g_printerr ("modest: failed to set store account '%s'\n",
462                                     store_account);
463                         if (err) {
464                                 g_printerr ("modest: Error adding store account conf: %s\n", err->message);
465                                 g_error_free (err);
466                         }
467                         return FALSE;
468                 }
469         }
470         
471         if (transport_account) {
472                 key = _modest_account_mgr_get_account_keyname_cached (priv, name,
473                                                                       MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
474                                                                       FALSE);
475                 ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
476                 if (!ok) {
477                         g_printerr ("modest: failed to set transport account '%s'\n",
478                                     transport_account);
479                         if (err) {
480                                 g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
481                                 g_error_free (err);
482                         }       
483                         return FALSE;
484                 }
485         }
486
487         /* Make sure that leave-messages-on-server is enabled by default, 
488          * as per the UI spec, though it is only meaningful for accounts using POP.
489          * (possibly this gconf key should be under the server account): */
490         modest_account_mgr_set_bool (self, name,
491                 MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE /* not server account */);
492
493         modest_account_mgr_set_enabled (self, name, enabled);
494
495         /* Notify the observers */
496         g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
497
498         /* if no default account has been defined yet, do so now */
499         default_account = modest_account_mgr_get_default_account (self);
500         if (!default_account)
501                 modest_account_mgr_set_default_account (self, name);
502         g_free (default_account);
503
504         return TRUE;
505 }
506
507
508 gboolean
509 modest_account_mgr_add_server_account (ModestAccountMgr * self,
510                                        const gchar * name, const gchar *hostname,
511                                        guint portnumber,
512                                        const gchar * username, const gchar * password,
513                                        ModestTransportStoreProtocol proto,
514                                        ModestConnectionProtocol security,
515                                        ModestAuthProtocol auth)
516 {
517         ModestAccountMgrPrivate *priv;
518         const gchar *key;
519         gboolean ok = TRUE;
520         GError *err = NULL;
521
522         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
523         g_return_val_if_fail (name, FALSE);
524         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
525                               
526         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
527
528         /* hostname */
529         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_HOSTNAME, TRUE);
530         if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
531                 g_printerr ("modest: server account '%s' already exists\n", name);
532                 ok =  FALSE;
533         }
534         if (!ok)
535                 goto cleanup;
536         
537         modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &err);
538         if (err) {
539                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
540                 g_error_free (err);
541                 ok = FALSE;
542         }
543         if (!ok)
544                 goto cleanup;
545         
546         /* username */
547         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_USERNAME, TRUE);
548         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
549         if (err) {
550                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
551                 g_error_free (err);
552                 ok = FALSE;
553         }
554         if (!ok)
555                 goto cleanup;
556         
557         
558         /* password */
559         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PASSWORD, TRUE);
560         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
561         if (err) {
562                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
563                 g_error_free (err);
564                 ok = FALSE;
565         }
566         if (!ok)
567                 goto cleanup;
568
569         /* proto */
570         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
571         ok = modest_conf_set_string (priv->modest_conf, key,
572                                      modest_protocol_info_get_transport_store_protocol_name(proto),
573                                      &err);
574         if (err) {
575                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
576                 g_error_free (err);
577                 ok = FALSE;
578         }
579         if (!ok)
580                 goto cleanup;
581
582
583         /* portnumber */
584         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PORT, TRUE);
585         ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
586         if (err) {
587                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
588                 g_error_free (err);
589                 ok = FALSE;
590         }
591         if (!ok)
592                 goto cleanup;
593
594         
595         /* auth mechanism */
596         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
597         ok = modest_conf_set_string (priv->modest_conf, key,
598                                      modest_protocol_info_get_auth_protocol_name (auth),
599                                      &err);
600         if (err) {
601                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
602                 g_error_free (err);
603                 ok = FALSE;
604         }
605         if (!ok)
606                 goto cleanup;
607         
608         /* Add the security settings: */
609         modest_server_account_set_security (self, name, security);
610         
611 cleanup:
612         if (!ok) {
613                 g_printerr ("modest: failed to add server account\n");
614                 return FALSE;
615         }
616
617         return TRUE;
618 }
619
620 /** modest_account_mgr_add_server_account_uri:
621  * Only used for mbox and maildir accounts.
622  */
623 gboolean
624 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
625                                            const gchar *name, ModestTransportStoreProtocol proto,
626                                            const gchar *uri)
627 {
628         ModestAccountMgrPrivate *priv;
629         const gchar *key;
630         gboolean ok;
631         
632         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
633         g_return_val_if_fail (name, FALSE);
634         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
635         g_return_val_if_fail (uri, FALSE);
636         
637         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
638         
639         /* proto */
640         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
641         ok = modest_conf_set_string (priv->modest_conf, key,
642                                      modest_protocol_info_get_transport_store_protocol_name(proto),
643                                      NULL);
644
645         if (!ok) {
646                 g_printerr ("modest: failed to set proto\n");
647                 return FALSE;
648         }
649         
650         /* uri */
651         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_URI, TRUE);
652         ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
653
654         if (!ok) {
655                 g_printerr ("modest: failed to set uri\n");
656                 return FALSE;
657         }
658         return TRUE;
659 }
660
661 /* 
662  * Utility function used by modest_account_mgr_remove_account
663  */
664 static void
665 real_remove_account (ModestConf *conf,
666                      const gchar *acc_name,
667                      gboolean server_account)
668 {
669         GError *err = NULL;
670         gchar *key;
671         
672         key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
673         modest_conf_remove_key (conf, key, &err);
674
675         if (err) {
676                 g_printerr ("modest: error removing key: %s\n", err->message);
677                 g_error_free (err);
678         }
679         g_free (key);
680 }
681
682 gboolean
683 modest_account_mgr_remove_account (ModestAccountMgr * self,
684                                    const gchar* name)
685 {
686         ModestAccountMgrPrivate *priv;
687         gchar *default_account_name, *store_acc_name, *transport_acc_name;
688         gboolean default_account_deleted;
689
690         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
691         g_return_val_if_fail (name, FALSE);
692
693         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
694                 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
695                 return FALSE;
696         }
697
698         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
699         default_account_deleted = FALSE;
700
701         /* If this was the default, then remove that setting: */
702         default_account_name = modest_account_mgr_get_default_account (self);
703         if (default_account_name && (strcmp (default_account_name, name) == 0)) {
704                 modest_account_mgr_unset_default_account (self);
705                 default_account_deleted = TRUE;
706         }
707         g_free (default_account_name);
708
709         /* Delete transport and store accounts */
710         store_acc_name = modest_account_mgr_get_string (self, name, 
711                                                         MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
712         if (store_acc_name)
713                 real_remove_account (priv->modest_conf, store_acc_name, TRUE);
714
715         transport_acc_name = modest_account_mgr_get_string (self, name, 
716                                                             MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
717         if (transport_acc_name)
718                 real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
719                         
720         /* Remove the modest account */
721         real_remove_account (priv->modest_conf, name, FALSE);
722
723         if (default_account_deleted) {  
724                 /* pick another one as the new default account. We do
725                    this *after* deleting the keys, because otherwise a
726                    call to account_names will retrieve also the
727                    deleted account */
728                 modest_account_mgr_set_first_account_as_default (self);
729         }
730         
731         /* Notify the observers. We do this *after* deleting
732            the keys, because otherwise a call to account_names
733            will retrieve also the deleted account */
734         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
735
736         return TRUE;
737 }
738
739
740
741 /* strip the first /n/ character from each element
742  * caller must make sure all elements are strings with
743  * length >= n, and also that data can be freed.
744  * change is in-place
745  */
746 static void
747 strip_prefix_from_elements (GSList * lst, guint n)
748 {
749         while (lst) {
750                 memmove (lst->data, lst->data + n,
751                          strlen(lst->data) - n + 1);
752                 lst = lst->next;
753         }
754 }
755
756
757 GSList*
758 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
759 {
760         GSList *accounts;
761         ModestAccountMgrPrivate *priv;
762         GError *err = NULL;
763         
764         const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
765
766         g_return_val_if_fail (self, NULL);
767
768         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
769         accounts = modest_conf_list_subkeys (priv->modest_conf,
770                                              MODEST_ACCOUNT_NAMESPACE, &err);
771
772         if (err) {
773                 g_printerr ("modest: failed to get subkeys (%s): %s\n",
774                             MODEST_ACCOUNT_NAMESPACE, err->message);
775                 g_error_free (err);
776                 return NULL; /* assume accounts did not get value when err is set...*/
777         }
778         
779         strip_prefix_from_elements (accounts, prefix_len);
780                 
781         GSList *result = NULL;
782         
783         /* Unescape the keys to get the account names: */
784         GSList *iter = accounts;
785         while (iter) {
786                 if (!(iter->data))
787                         continue;
788                         
789                 const gchar* account_name_key = (const gchar*)iter->data;
790                 gchar* unescaped_name = account_name_key ? 
791                         modest_conf_key_unescape (account_name_key) 
792                         : NULL;
793                 
794                 gboolean add = TRUE;
795                 if (only_enabled) {
796                         if (unescaped_name && 
797                                 !modest_account_mgr_get_enabled (self, unescaped_name)) {
798                                 add = FALSE;
799                         }
800                 }
801                 
802                 /* Ignore modest accounts whose server accounts don't exist: 
803                  * (We could be getting this list while the account is being deleted, 
804                  * while the child server accounts have already been deleted, but the 
805                  * parent modest account already exists.
806                  */
807                 if (add) {
808                         gchar* server_account_name = modest_account_mgr_get_string
809                                 (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
810                                  FALSE);
811                         if (server_account_name) {
812                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
813                                         add = FALSE;
814                                 g_free (server_account_name);
815                         }
816                 }
817                 
818                 if (add) {
819                         gchar* server_account_name = modest_account_mgr_get_string
820                                 (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
821                                  FALSE);
822                         if (server_account_name) {
823                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
824                                         add = FALSE;
825                                 g_free (server_account_name);
826                         }
827                 }
828                 
829                 if (add)        
830                         result = g_slist_append (result, unescaped_name);
831                 else 
832                         g_free (unescaped_name);
833
834                 g_free (iter->data);
835                 iter->data = NULL;
836                 
837                 iter = g_slist_next (iter);     
838         }
839         
840
841         /* we already freed the strings in the loop */
842         g_slist_free (accounts);
843         
844         accounts = NULL;
845         return result;
846 }
847
848
849 void
850 modest_account_mgr_free_account_names (GSList *account_names)
851 {
852         g_slist_foreach (account_names, (GFunc)g_free, NULL);
853         g_slist_free (account_names);
854 }
855
856
857
858 gchar *
859 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
860                                const gchar *key, gboolean server_account) {
861
862         ModestAccountMgrPrivate *priv;
863
864         const gchar *keyname;
865         gchar *retval;
866         GError *err = NULL;
867
868         g_return_val_if_fail (self, NULL);
869         g_return_val_if_fail (name, NULL);
870         g_return_val_if_fail (key, NULL);
871
872         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
873         
874         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
875         
876         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
877         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
878         if (err) {
879                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
880                 g_error_free (err);
881                 retval = NULL;
882         }
883
884         return retval;
885 }
886
887
888 gchar *
889 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
890                                const gchar *key, gboolean server_account)
891 {
892         return modest_account_mgr_get_string (self, name, key, server_account);
893
894 }
895
896
897
898 gint
899 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
900                             gboolean server_account)
901 {
902         ModestAccountMgrPrivate *priv;
903
904         const gchar *keyname;
905         gint retval;
906         GError *err = NULL;
907         
908         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
909         g_return_val_if_fail (name, -1);
910         g_return_val_if_fail (key, -1);
911
912         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
913
914         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
915         
916         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
917         if (err) {
918                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
919                 g_error_free (err);
920                 retval = -1;
921         }
922
923         return retval;
924 }
925
926
927
928 gboolean
929 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
930                              const gchar * key, gboolean server_account)
931 {
932         ModestAccountMgrPrivate *priv;
933
934         const gchar *keyname;
935         gboolean retval;
936         GError *err = NULL;
937
938         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
939         g_return_val_if_fail (account, FALSE);
940         g_return_val_if_fail (key, FALSE);
941
942         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
943         ///keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
944
945         keyname = _modest_account_mgr_get_account_keyname_cached (priv, account, key, server_account);
946                 
947         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
948         if (err) {
949                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
950                 g_error_free (err);
951                 retval = FALSE;
952         }
953
954         return retval;
955 }
956
957
958
959 GSList * 
960 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
961                              const gchar *key, ModestConfValueType list_type,
962                              gboolean server_account)
963 {
964         ModestAccountMgrPrivate *priv;
965
966         const gchar *keyname;
967         GSList *retval;
968         GError *err = NULL;
969         
970         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
971         g_return_val_if_fail (name, NULL);
972         g_return_val_if_fail (key, NULL);
973         
974         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key,
975                                                                   server_account);
976         
977         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
978         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
979         if (err) {
980                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
981                             err->message);
982                 g_error_free (err);
983                 retval = FALSE;
984         }
985         return retval;
986 }
987
988
989 gboolean
990 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
991                                const gchar * key, const gchar * val, gboolean server_account)
992 {
993         ModestAccountMgrPrivate *priv;
994
995         const gchar *keyname;
996         gboolean retval;
997         GError *err = NULL;
998
999         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1000         g_return_val_if_fail (name, FALSE);
1001         g_return_val_if_fail (key, FALSE);
1002
1003         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1004
1005         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1006         
1007         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
1008         if (err) {
1009                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
1010                 g_error_free (err);
1011                 retval = FALSE;
1012         }
1013         return retval;
1014 }
1015
1016
1017 gboolean
1018 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
1019                                  const gchar * key, const gchar * val, gboolean server_account)
1020 {
1021         return modest_account_mgr_set_password (self, name, key, val, server_account);
1022 }
1023
1024
1025
1026 gboolean
1027 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
1028                             const gchar * key, int val, gboolean server_account)
1029 {
1030         ModestAccountMgrPrivate *priv;
1031
1032         const gchar *keyname;
1033         gboolean retval;
1034         GError *err = NULL;
1035         
1036         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1037         g_return_val_if_fail (name, FALSE);
1038         g_return_val_if_fail (key, FALSE);
1039
1040         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1041
1042         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1043         
1044         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
1045         if (err) {
1046                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
1047                 g_error_free (err);
1048                 retval = FALSE;
1049         }
1050         return retval;
1051 }
1052
1053
1054
1055 gboolean
1056 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
1057                              const gchar * key, gboolean val, gboolean server_account)
1058 {
1059         ModestAccountMgrPrivate *priv;
1060
1061         const gchar *keyname;
1062         gboolean retval;
1063         GError *err = NULL;
1064
1065         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1066         g_return_val_if_fail (name, FALSE);
1067         g_return_val_if_fail (key, FALSE);
1068
1069         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1070         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1071         
1072         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
1073         if (err) {
1074                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
1075                 g_error_free (err);
1076                 retval = FALSE;
1077         }
1078
1079         return retval;
1080 }
1081
1082
1083 gboolean
1084 modest_account_mgr_set_list (ModestAccountMgr *self,
1085                              const gchar *name,
1086                              const gchar *key,
1087                              GSList *val,
1088                              ModestConfValueType list_type,
1089                              gboolean server_account)
1090 {
1091         ModestAccountMgrPrivate *priv;
1092         const gchar *keyname;
1093         GError *err = NULL;
1094         gboolean retval;
1095         
1096         g_return_val_if_fail (self, FALSE);
1097         g_return_val_if_fail (name, FALSE);
1098         g_return_val_if_fail (key,  FALSE);
1099         g_return_val_if_fail (val,  FALSE);
1100
1101         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1102
1103         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1104         
1105         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
1106         if (err) {
1107                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
1108                 g_error_free (err);
1109                 retval = FALSE;
1110         }
1111
1112         return retval;
1113 }
1114
1115 gboolean
1116 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
1117                                    gboolean server_account)
1118 {
1119         ModestAccountMgrPrivate *priv;
1120
1121         const gchar *keyname;
1122         gboolean retval;
1123         GError *err = NULL;
1124
1125         g_return_val_if_fail (self, FALSE);
1126         g_return_val_if_fail (name, FALSE);
1127
1128         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1129         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, NULL, server_account);
1130         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1131         if (err) {
1132                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1133                             err->message);
1134                 g_error_free (err);
1135                 retval = FALSE;
1136         }
1137         return retval;
1138 }
1139
1140 gboolean
1141 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, const gchar *display_name)
1142 {
1143         GSList *account_names = NULL;
1144         GSList *cursor = NULL;
1145         
1146         cursor = account_names = modest_account_mgr_account_names (self, 
1147                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1148
1149         gboolean found = FALSE;
1150         
1151         /* Look at each non-server account to check their display names; */
1152         while (cursor) {
1153                 const gchar * account_name = (gchar*)cursor->data;
1154                 
1155                 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1156                 if (!account_data) {
1157                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1158                         continue;
1159                 }
1160
1161                 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1162                         found = TRUE;
1163                         break;
1164                 }
1165
1166                 modest_account_mgr_free_account_data (self, account_data);
1167                 cursor = cursor->next;
1168         }
1169         modest_account_mgr_free_account_names (account_names);
1170         account_names = NULL;
1171         
1172         return found;
1173 }
1174
1175
1176
1177
1178 gboolean 
1179 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1180                           const gchar *key, gboolean server_account)
1181 {
1182         ModestAccountMgrPrivate *priv;
1183         
1184         const gchar *keyname;
1185         gboolean retval;
1186         GError *err = NULL;
1187         
1188         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1189         g_return_val_if_fail (name, FALSE);
1190         g_return_val_if_fail (key, FALSE);
1191
1192         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1193         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1194
1195         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1196         if (err) {
1197                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1198                             err->message);
1199                 g_error_free (err);
1200                 retval = FALSE;
1201         }
1202         return retval;
1203 }
1204
1205 gchar*
1206 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1207 {
1208         /* Initialize input parameters: */
1209         if (is_account_key)
1210                 *is_account_key = FALSE;
1211
1212         if (is_server_account)
1213                 *is_server_account = FALSE;
1214
1215         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1216         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1217         gchar *cursor;
1218         gchar *account = NULL;
1219
1220         /* determine whether it's an account or a server account,
1221          * based on the prefix */
1222         if (g_str_has_prefix (key, account_ns)) {
1223
1224                 if (is_server_account)
1225                         *is_server_account = FALSE;
1226                 
1227                 account = g_strdup (key + strlen (account_ns));
1228
1229         } else if (g_str_has_prefix (key, server_account_ns)) {
1230
1231                 if (is_server_account)
1232                         *is_server_account = TRUE;
1233                 
1234                 account = g_strdup (key + strlen (server_account_ns));  
1235         } else
1236                 return NULL;
1237
1238         /* if there are any slashes left in the key, it's not
1239          * the toplevel entry for an account
1240          */
1241         cursor = strstr(account, "/");
1242         
1243         if (is_account_key && cursor)
1244                 *is_account_key = TRUE;
1245
1246         /* put a NULL where the first slash was */
1247         if (cursor)
1248                 *cursor = '\0';
1249
1250         if (account) {
1251                 /* The key is an escaped string, so unescape it to get the actual account name: */
1252                 gchar *unescaped_name = modest_conf_key_unescape (account);
1253                 g_free (account);
1254                 return unescaped_name;
1255         } else
1256                 return NULL;
1257 }
1258
1259
1260
1261
1262
1263 /* optimization: only with non-alphanum chars, escaping is needed */
1264 inline static gboolean
1265 is_alphanum (const gchar* str)
1266 {
1267         const gchar *cursor;
1268         for (cursor = str; cursor && *cursor; ++cursor) {
1269                 const char c = *cursor;
1270                 /* we cannot trust isalnum(3), because it might consider locales */
1271                 /*       numbers            ALPHA            alpha       */
1272                 if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
1273                         return FALSE;
1274         }
1275         return TRUE;
1276 }
1277                 
1278
1279
1280
1281 /* must be freed by caller */
1282 gchar *
1283 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar* name,
1284                                          gboolean server_account)
1285 {
1286         gchar *retval = NULL;   
1287         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1288         gchar *escaped_account_name, *escaped_name;
1289         
1290         if (!account_name)
1291                 return g_strdup (namespace);
1292
1293         /* optimization: only escape names when need to be escaped */
1294         if (is_alphanum (account_name))
1295                 escaped_account_name = (gchar*)account_name;
1296         else
1297                 escaped_account_name = modest_conf_key_escape (account_name);
1298         
1299         if (is_alphanum (name))
1300                 escaped_name = (gchar*)name;
1301         else
1302                 escaped_name = modest_conf_key_escape (name);
1303         //////////////////////////////////////////////////////////////
1304
1305         if (escaped_account_name && escaped_name)
1306                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1307         else if (escaped_account_name)
1308                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1309
1310         /* Sanity check: */
1311         if (!modest_conf_key_is_valid (retval)) {
1312                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1313                 g_free (retval);
1314                 retval = NULL;
1315         }
1316         
1317         /* g_free is only needed if we actually allocated anything */
1318         if (name != escaped_name)
1319                 g_free (escaped_name);
1320         if (account_name != escaped_account_name)
1321                 g_free (escaped_account_name);
1322
1323         return retval;
1324 }
1325
1326
1327 #if 0
1328 static void
1329 f2 (gchar*key, gchar* val, gpointer user_data)
1330 {
1331         g_debug (">>%s:%s", key, val);
1332 }
1333
1334
1335 static void
1336 f1 (gchar*key, GHashTable* h, gpointer user_data)
1337 {
1338         g_debug (">%s", key);
1339         g_hash_table_foreach (h, (GHFunc)f2, NULL);
1340 }
1341 #endif 
1342
1343
1344 static const gchar *
1345 _modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, const gchar* account_name,
1346                                                 const gchar *name, gboolean is_server)
1347 {
1348         //return _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1349         
1350         
1351         GHashTable *hash = is_server ? priv->server_account_key_hash : priv->account_key_hash;
1352         GHashTable *account_hash;
1353         gchar *key = NULL;
1354         const gchar *search_name;
1355         
1356         //g_hash_table_foreach (hash, (GHFunc)f1, NULL); 
1357
1358         if (!account_name)
1359                 return is_server ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1360
1361         search_name = name ? name : "<dummy>";
1362         
1363         account_hash = g_hash_table_lookup (hash, account_name);        
1364         if (!account_hash) { /* no hash for this account yet? create it */
1365                 account_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);         
1366                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1367                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1368                 g_hash_table_insert (hash, g_strdup(account_name), account_hash);
1369                 return key;
1370         }
1371         
1372         /* we have a hash for this account, but do we have the key? */  
1373         key = g_hash_table_lookup (account_hash, search_name);
1374         if (!key) {
1375                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1376                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1377         }
1378         
1379         return key;
1380 }
1381
1382
1383 gboolean
1384 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1385 {
1386         /* Check that at least one account exists: */
1387         GSList *account_names = modest_account_mgr_account_names (self,
1388                                                                   enabled);
1389         gboolean accounts_exist = account_names != NULL;
1390         
1391         modest_account_mgr_free_account_names (account_names);
1392         account_names = NULL;
1393         
1394         return accounts_exist;
1395 }
1396
1397 static int
1398 compare_account_name(gconstpointer a, gconstpointer b)
1399 {
1400         const gchar* account_name = (const gchar*) a;
1401         const gchar* account_name2 = (const gchar*) b;
1402         return strcmp(account_name, account_name2);
1403 }
1404
1405 void 
1406 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
1407                                     gboolean busy)
1408 {
1409         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1410         if (busy)
1411         {
1412                 GSList *account_names = modest_account_mgr_account_names (self,
1413                                 TRUE);
1414                 GSList* account = 
1415                         g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1416                 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1417                 {
1418                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1419                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1420                 }
1421                 modest_account_mgr_free_account_names (account_names);
1422                 account_names = NULL;
1423         } else {
1424                 GSList* account = 
1425                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1426                 if (account)
1427                 {
1428                         g_free(account->data);
1429                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1430                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1431                 }
1432         }
1433 }
1434
1435 gboolean
1436 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1437 {
1438         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1439         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1440                                         != NULL);
1441 }
1442