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