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