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