* Some aesthetic changes
[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         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
975
976         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key,
977                                                                   server_account);
978         
979         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
980         if (err) {
981                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
982                             err->message);
983                 g_error_free (err);
984                 retval = FALSE;
985         }
986         return retval;
987 }
988
989
990 gboolean
991 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
992                                const gchar * key, const gchar * val, gboolean server_account)
993 {
994         ModestAccountMgrPrivate *priv;
995
996         const gchar *keyname;
997         gboolean retval;
998         GError *err = NULL;
999
1000         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1001         g_return_val_if_fail (name, FALSE);
1002         g_return_val_if_fail (key, FALSE);
1003
1004         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1005
1006         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1007         
1008         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
1009         if (err) {
1010                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
1011                 g_error_free (err);
1012                 retval = FALSE;
1013         }
1014         return retval;
1015 }
1016
1017
1018 gboolean
1019 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
1020                                  const gchar * key, const gchar * val, gboolean server_account)
1021 {
1022         return modest_account_mgr_set_password (self, name, key, val, server_account);
1023 }
1024
1025
1026
1027 gboolean
1028 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
1029                             const gchar * key, int val, gboolean server_account)
1030 {
1031         ModestAccountMgrPrivate *priv;
1032
1033         const gchar *keyname;
1034         gboolean retval;
1035         GError *err = NULL;
1036         
1037         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1038         g_return_val_if_fail (name, FALSE);
1039         g_return_val_if_fail (key, FALSE);
1040
1041         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1042
1043         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1044         
1045         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
1046         if (err) {
1047                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
1048                 g_error_free (err);
1049                 retval = FALSE;
1050         }
1051         return retval;
1052 }
1053
1054
1055
1056 gboolean
1057 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
1058                              const gchar * key, gboolean val, gboolean server_account)
1059 {
1060         ModestAccountMgrPrivate *priv;
1061
1062         const gchar *keyname;
1063         gboolean retval;
1064         GError *err = NULL;
1065
1066         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1067         g_return_val_if_fail (name, FALSE);
1068         g_return_val_if_fail (key, FALSE);
1069
1070         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1071         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1072         
1073         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
1074         if (err) {
1075                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
1076                 g_error_free (err);
1077                 retval = FALSE;
1078         }
1079
1080         return retval;
1081 }
1082
1083
1084 gboolean
1085 modest_account_mgr_set_list (ModestAccountMgr *self,
1086                              const gchar *name,
1087                              const gchar *key,
1088                              GSList *val,
1089                              ModestConfValueType list_type,
1090                              gboolean server_account)
1091 {
1092         ModestAccountMgrPrivate *priv;
1093         const gchar *keyname;
1094         GError *err = NULL;
1095         gboolean retval;
1096         
1097         g_return_val_if_fail (self, FALSE);
1098         g_return_val_if_fail (name, FALSE);
1099         g_return_val_if_fail (key,  FALSE);
1100         g_return_val_if_fail (val,  FALSE);
1101
1102         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1103
1104         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1105         
1106         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
1107         if (err) {
1108                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
1109                 g_error_free (err);
1110                 retval = FALSE;
1111         }
1112
1113         return retval;
1114 }
1115
1116 gboolean
1117 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
1118                                    gboolean server_account)
1119 {
1120         ModestAccountMgrPrivate *priv;
1121
1122         const gchar *keyname;
1123         gboolean retval;
1124         GError *err = NULL;
1125
1126         g_return_val_if_fail (self, FALSE);
1127         g_return_val_if_fail (name, FALSE);
1128
1129         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1130         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, NULL, server_account);
1131         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1132         if (err) {
1133                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1134                             err->message);
1135                 g_error_free (err);
1136                 retval = FALSE;
1137         }
1138         return retval;
1139 }
1140
1141 gboolean
1142 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, const gchar *display_name)
1143 {
1144         GSList *account_names = NULL;
1145         GSList *cursor = NULL;
1146         
1147         cursor = account_names = modest_account_mgr_account_names (self, 
1148                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1149
1150         gboolean found = FALSE;
1151         
1152         /* Look at each non-server account to check their display names; */
1153         while (cursor) {
1154                 const gchar * account_name = (gchar*)cursor->data;
1155                 
1156                 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1157                 if (!account_data) {
1158                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1159                         continue;
1160                 }
1161
1162                 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1163                         found = TRUE;
1164                         break;
1165                 }
1166
1167                 modest_account_mgr_free_account_data (self, account_data);
1168                 cursor = cursor->next;
1169         }
1170         modest_account_mgr_free_account_names (account_names);
1171         account_names = NULL;
1172         
1173         return found;
1174 }
1175
1176
1177
1178
1179 gboolean 
1180 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1181                           const gchar *key, gboolean server_account)
1182 {
1183         ModestAccountMgrPrivate *priv;
1184         
1185         const gchar *keyname;
1186         gboolean retval;
1187         GError *err = NULL;
1188         
1189         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1190         g_return_val_if_fail (name, FALSE);
1191         g_return_val_if_fail (key, FALSE);
1192
1193         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1194         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1195
1196         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1197         if (err) {
1198                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1199                             err->message);
1200                 g_error_free (err);
1201                 retval = FALSE;
1202         }
1203         return retval;
1204 }
1205
1206 gchar*
1207 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1208 {
1209         /* Initialize input parameters: */
1210         if (is_account_key)
1211                 *is_account_key = FALSE;
1212
1213         if (is_server_account)
1214                 *is_server_account = FALSE;
1215
1216         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1217         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1218         gchar *cursor;
1219         gchar *account = NULL;
1220
1221         /* determine whether it's an account or a server account,
1222          * based on the prefix */
1223         if (g_str_has_prefix (key, account_ns)) {
1224
1225                 if (is_server_account)
1226                         *is_server_account = FALSE;
1227                 
1228                 account = g_strdup (key + strlen (account_ns));
1229
1230         } else if (g_str_has_prefix (key, server_account_ns)) {
1231
1232                 if (is_server_account)
1233                         *is_server_account = TRUE;
1234                 
1235                 account = g_strdup (key + strlen (server_account_ns));  
1236         } else
1237                 return NULL;
1238
1239         /* if there are any slashes left in the key, it's not
1240          * the toplevel entry for an account
1241          */
1242         cursor = strstr(account, "/");
1243         
1244         if (is_account_key && cursor)
1245                 *is_account_key = TRUE;
1246
1247         /* put a NULL where the first slash was */
1248         if (cursor)
1249                 *cursor = '\0';
1250
1251         if (account) {
1252                 /* The key is an escaped string, so unescape it to get the actual account name: */
1253                 gchar *unescaped_name = modest_conf_key_unescape (account);
1254                 g_free (account);
1255                 return unescaped_name;
1256         } else
1257                 return NULL;
1258 }
1259
1260
1261
1262
1263
1264 /* optimization: only with non-alphanum chars, escaping is needed */
1265 inline static gboolean
1266 is_alphanum (const gchar* str)
1267 {
1268         const gchar *cursor;
1269         for (cursor = str; cursor && *cursor; ++cursor) {
1270                 const char c = *cursor;
1271                 /* we cannot trust isalnum(3), because it might consider locales */
1272                 /*       numbers            ALPHA            alpha       */
1273                 if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
1274                         return FALSE;
1275         }
1276         return TRUE;
1277 }
1278                 
1279
1280
1281
1282 /* must be freed by caller */
1283 gchar *
1284 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar* name,
1285                                          gboolean server_account)
1286 {
1287         gchar *retval = NULL;   
1288         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1289         gchar *escaped_account_name, *escaped_name;
1290         
1291         if (!account_name)
1292                 return g_strdup (namespace);
1293
1294         /* optimization: only escape names when need to be escaped */
1295         if (is_alphanum (account_name))
1296                 escaped_account_name = (gchar*)account_name;
1297         else
1298                 escaped_account_name = modest_conf_key_escape (account_name);
1299         
1300         if (is_alphanum (name))
1301                 escaped_name = (gchar*)name;
1302         else
1303                 escaped_name = modest_conf_key_escape (name);
1304         //////////////////////////////////////////////////////////////
1305
1306         if (escaped_account_name && escaped_name)
1307                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1308         else if (escaped_account_name)
1309                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1310
1311         /* Sanity check: */
1312         if (!modest_conf_key_is_valid (retval)) {
1313                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1314                 g_free (retval);
1315                 retval = NULL;
1316         }
1317         
1318         /* g_free is only needed if we actually allocated anything */
1319         if (name != escaped_name)
1320                 g_free (escaped_name);
1321         if (account_name != escaped_account_name)
1322                 g_free (escaped_account_name);
1323
1324         return retval;
1325 }
1326
1327
1328 #if 0
1329 static void
1330 f2 (gchar*key, gchar* val, gpointer user_data)
1331 {
1332         g_debug (">>%s:%s", key, val);
1333 }
1334
1335
1336 static void
1337 f1 (gchar*key, GHashTable* h, gpointer user_data)
1338 {
1339         g_debug (">%s", key);
1340         g_hash_table_foreach (h, (GHFunc)f2, NULL);
1341 }
1342 #endif 
1343
1344
1345 static const gchar *
1346 _modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, const gchar* account_name,
1347                                                 const gchar *name, gboolean is_server)
1348 {
1349         //return _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1350         
1351         
1352         GHashTable *hash = is_server ? priv->server_account_key_hash : priv->account_key_hash;
1353         GHashTable *account_hash;
1354         gchar *key = NULL;
1355         const gchar *search_name;
1356         
1357         //g_hash_table_foreach (hash, (GHFunc)f1, NULL); 
1358
1359         if (!account_name)
1360                 return is_server ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1361
1362         search_name = name ? name : "<dummy>";
1363         
1364         account_hash = g_hash_table_lookup (hash, account_name);        
1365         if (!account_hash) { /* no hash for this account yet? create it */
1366                 account_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);         
1367                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1368                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1369                 g_hash_table_insert (hash, g_strdup(account_name), account_hash);
1370                 return key;
1371         }
1372         
1373         /* we have a hash for this account, but do we have the key? */  
1374         key = g_hash_table_lookup (account_hash, search_name);
1375         if (!key) {
1376                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1377                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1378         }
1379         
1380         return key;
1381 }
1382
1383
1384 gboolean
1385 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1386 {
1387         /* Check that at least one account exists: */
1388         GSList *account_names = modest_account_mgr_account_names (self,
1389                                                                   enabled);
1390         gboolean accounts_exist = account_names != NULL;
1391         
1392         modest_account_mgr_free_account_names (account_names);
1393         account_names = NULL;
1394         
1395         return accounts_exist;
1396 }
1397
1398 static int
1399 compare_account_name(gconstpointer a, gconstpointer b)
1400 {
1401         const gchar* account_name = (const gchar*) a;
1402         const gchar* account_name2 = (const gchar*) b;
1403         return strcmp(account_name, account_name2);
1404 }
1405
1406 void 
1407 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
1408                                     gboolean busy)
1409 {
1410         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1411         if (busy)
1412         {
1413                 GSList *account_names = modest_account_mgr_account_names (self, TRUE);
1414                 GSList* account = g_slist_find_custom(account_names, account_name, 
1415                                                       (GCompareFunc) compare_account_name);
1416
1417                 if (account && !modest_account_mgr_account_is_busy(self, account_name)) {
1418                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1419                         g_signal_emit (G_OBJECT(self), ACCOUNT_BUSY_SIGNAL, 0, 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
1427                 if (account) {
1428                         g_free(account->data);
1429                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1430                         g_signal_emit (G_OBJECT(self), ACCOUNT_BUSY_SIGNAL, 0, 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