Now GetUnreadMessages dbus method tells also the number of unread messages
[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-defs.h>
34 #include <modest-account-mgr.h>
35 #include <modest-account-mgr-priv.h>
36 #include <modest-account-mgr-helpers.h>
37 #include <modest-platform.h>
38
39 /* 'private'/'protected' functions */
40 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
41 static void modest_account_mgr_init       (ModestAccountMgr * obj);
42 static void modest_account_mgr_finalize   (GObject * obj);
43 static void modest_account_mgr_base_init  (gpointer g_class);
44
45 static const gchar *_modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, 
46                                                                     const gchar* account_name,
47                                                                     const gchar *name, 
48                                                                     gboolean is_server);
49
50 static gboolean modest_account_mgr_unset_default_account (ModestAccountMgr *self);
51
52 /* list my signals */
53 enum {
54         ACCOUNT_INSERTED_SIGNAL,
55         ACCOUNT_CHANGED_SIGNAL,
56         ACCOUNT_REMOVED_SIGNAL,
57         ACCOUNT_BUSY_SIGNAL,
58         DEFAULT_ACCOUNT_CHANGED_SIGNAL,
59         DISPLAY_NAME_CHANGED_SIGNAL,
60         ACCOUNT_UPDATED_SIGNAL,
61         LAST_SIGNAL
62 };
63
64 /* globals */
65 static GObjectClass *parent_class = NULL;
66 static guint signals[LAST_SIGNAL] = {0};
67
68 GType
69 modest_account_mgr_get_type (void)
70 {
71         static GType my_type = 0;
72
73         if (!my_type) {
74                 static const GTypeInfo my_info = {
75                         sizeof (ModestAccountMgrClass),
76                         modest_account_mgr_base_init,   /* base init */
77                         NULL,   /* base finalize */
78                         (GClassInitFunc) modest_account_mgr_class_init,
79                         NULL,   /* class finalize */
80                         NULL,   /* class data */
81                         sizeof (ModestAccountMgr),
82                         1,      /* n_preallocs */
83                         (GInstanceInitFunc) modest_account_mgr_init,
84                         NULL
85                 };
86
87                 my_type = g_type_register_static (G_TYPE_OBJECT,
88                                                   "ModestAccountMgr",
89                                                   &my_info, 0);
90         }
91         return my_type;
92 }
93
94 static void 
95 modest_account_mgr_base_init (gpointer g_class)
96 {
97         static gboolean modest_account_mgr_initialized = FALSE;
98
99         if (!modest_account_mgr_initialized) {
100                 /* signal definitions */
101                 signals[ACCOUNT_INSERTED_SIGNAL] =
102                         g_signal_new ("account_inserted",
103                                       MODEST_TYPE_ACCOUNT_MGR,
104                                       G_SIGNAL_RUN_FIRST,
105                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_inserted),
106                                       NULL, NULL,
107                                       g_cclosure_marshal_VOID__STRING,
108                                       G_TYPE_NONE, 1, G_TYPE_STRING);
109
110                 signals[ACCOUNT_REMOVED_SIGNAL] =
111                         g_signal_new ("account_removed",
112                                       MODEST_TYPE_ACCOUNT_MGR,
113                                       G_SIGNAL_RUN_FIRST,
114                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_removed),
115                                       NULL, NULL,
116                                       g_cclosure_marshal_VOID__STRING,
117                                       G_TYPE_NONE, 1, G_TYPE_STRING);
118
119                 signals[ACCOUNT_CHANGED_SIGNAL] =
120                         g_signal_new ("account_changed",
121                                       MODEST_TYPE_ACCOUNT_MGR,
122                                       G_SIGNAL_RUN_FIRST,
123                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_changed),
124                                       NULL, NULL,
125                                       modest_marshal_VOID__STRING_INT,
126                                       G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT);
127
128                 signals[ACCOUNT_BUSY_SIGNAL] =
129                         g_signal_new ("account_busy_changed",
130                                       MODEST_TYPE_ACCOUNT_MGR,
131                                       G_SIGNAL_RUN_FIRST,
132                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_busy_changed),
133                                       NULL, NULL,
134                                       modest_marshal_VOID__STRING_BOOLEAN,
135                                       G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
136
137                 signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL] =
138                         g_signal_new ("default_account_changed",
139                                       MODEST_TYPE_ACCOUNT_MGR,
140                                       G_SIGNAL_RUN_FIRST,
141                                       G_STRUCT_OFFSET(ModestAccountMgrClass, default_account_changed),
142                                       NULL, NULL,
143                                       g_cclosure_marshal_VOID__VOID,
144                                       G_TYPE_NONE, 0);
145
146                 signals[DISPLAY_NAME_CHANGED_SIGNAL] =
147                         g_signal_new ("display_name_changed",
148                                       MODEST_TYPE_ACCOUNT_MGR,
149                                       G_SIGNAL_RUN_FIRST,
150                                       G_STRUCT_OFFSET(ModestAccountMgrClass, display_name_changed),
151                                       NULL, NULL,
152                                       g_cclosure_marshal_VOID__STRING,
153                                       G_TYPE_NONE, 1, G_TYPE_STRING);
154                 
155                 signals[ACCOUNT_UPDATED_SIGNAL] =
156                         g_signal_new ("account_updated",
157                                       MODEST_TYPE_ACCOUNT_MGR,
158                                       G_SIGNAL_RUN_FIRST,
159                                       G_STRUCT_OFFSET(ModestAccountMgrClass, account_updated),
160                                       NULL, NULL,
161                                       g_cclosure_marshal_VOID__STRING,
162                                       G_TYPE_NONE, 1, G_TYPE_STRING);
163
164
165                 modest_account_mgr_initialized = TRUE;
166         }
167 }
168
169 static void
170 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
171 {
172         GObjectClass *gobject_class;
173         gobject_class = (GObjectClass *) klass;
174
175         parent_class = g_type_class_peek_parent (klass);
176         gobject_class->finalize = modest_account_mgr_finalize;
177
178         g_type_class_add_private (gobject_class,
179                                   sizeof (ModestAccountMgrPrivate));
180 }
181
182
183 static void
184 modest_account_mgr_init (ModestAccountMgr * obj)
185 {
186         ModestAccountMgrPrivate *priv =
187                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
188
189         priv->modest_conf   = NULL;
190         priv->busy_accounts = NULL;
191         priv->timeout       = 0;
192         
193         priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
194
195         /* we maintain hashes for the modest-conf keys we build from account name
196          * + key. many seem to be used often, and generating them showed up high
197          * in oprofile */
198         /* both hashes are hashes to hashes;
199          * account-key => keyname ==> account-key-name
200          */     
201         priv->server_account_key_hash = g_hash_table_new_full (g_str_hash,
202                                                                g_str_equal,
203                                                                g_free,
204                                                                (GDestroyNotify)g_hash_table_destroy);
205         priv->account_key_hash        = g_hash_table_new_full (g_str_hash,
206                                                                g_str_equal,
207                                                                g_free,
208                                                                (GDestroyNotify)g_hash_table_destroy);
209
210         /* FALSE means: status is unknown */
211         priv->has_accounts = FALSE;
212         priv->has_enabled_accounts = FALSE;
213 }
214
215 static void
216 modest_account_mgr_finalize (GObject * obj)
217 {
218         ModestAccountMgrPrivate *priv = 
219                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
220
221         if (priv->notification_id_accounts) {
222                 g_hash_table_destroy (priv->notification_id_accounts);
223                 priv->notification_id_accounts = NULL;
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         if (priv->busy_accounts) {
246                 g_slist_foreach (priv->busy_accounts, (GFunc) g_free, NULL);
247                 g_slist_free (priv->busy_accounts);
248                 priv->busy_accounts = NULL;
249         }
250
251         G_OBJECT_CLASS(parent_class)->finalize (obj);
252 }
253
254
255 ModestAccountMgr *
256 modest_account_mgr_new (ModestConf *conf)
257 {
258         GObject *obj;
259         ModestAccountMgrPrivate *priv;
260
261         g_return_val_if_fail (conf, NULL);
262
263         obj = G_OBJECT (g_object_new (MODEST_TYPE_ACCOUNT_MGR, NULL));
264         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
265
266         g_object_ref (G_OBJECT(conf));
267         priv->modest_conf = conf;
268
269         return MODEST_ACCOUNT_MGR (obj);
270 }
271
272
273 static const gchar *
274 null_means_empty (const gchar * str)
275 {
276         return str ? str : "";
277 }
278
279 gboolean
280 modest_account_mgr_add_account_from_settings (ModestAccountMgr *self,
281                                               ModestAccountSettings *settings)
282 {
283         ModestAccountMgrPrivate *priv;
284         const gchar* display_name;
285         gchar *account_name_start, *account_name;
286         gchar *store_name_start, *store_name;
287         gchar *transport_name_start, *transport_name;
288         gchar *default_account;
289         ModestServerAccountSettings *store_settings, *transport_settings;
290
291         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR (self), FALSE);
292         g_return_val_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings), FALSE);
293
294         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
295         display_name = modest_account_settings_get_display_name (settings);
296
297         /* We should have checked for this already, and changed that name accordingly, 
298          * but let's check again just in case */
299         if (!display_name || 
300             modest_account_mgr_account_with_display_name_exists (self, display_name)) {
301                 display_name = _("mcen_ia_emailsetup_defaultname");
302         }
303
304         /* Increment the non-user visible name if necessary, 
305          * based on the display name: */
306         account_name_start = g_strdup_printf ("%sID", display_name);
307         account_name = modest_account_mgr_get_unused_account_name (self,
308                                                                    account_name_start, FALSE /* not a server account */);
309         g_free (account_name_start);
310         
311         /* Add a (incoming) server account, to be used by the account: */
312         store_name_start = g_strconcat (account_name, "_store", NULL);
313         store_name = modest_account_mgr_get_unused_account_name (self, 
314                                                                  store_name_start, TRUE /* server account */);
315         g_free (store_name_start);
316         
317         /* Add a (outgoing) server account to be used by the account: */
318         transport_name_start = g_strconcat (account_name, "_transport", NULL);
319         transport_name = modest_account_mgr_get_unused_account_name (self, 
320                                                                      transport_name_start, TRUE /* server account */);
321         g_free (transport_name_start);
322
323         modest_account_settings_set_account_name (settings, account_name);
324         store_settings = modest_account_settings_get_store_settings (settings);
325         modest_server_account_settings_set_account_name (store_settings, store_name);
326         transport_settings = modest_account_settings_get_transport_settings (settings);
327         modest_server_account_settings_set_account_name (transport_settings, transport_name);
328         g_object_unref (store_settings);
329         g_object_unref (transport_settings);
330
331         /* Create the account, which will contain the two "server accounts": */
332         modest_account_mgr_save_account_settings (self, settings);
333         g_free (store_name);
334         g_free (transport_name);
335         
336         /* Sanity check: */
337         /* There must be at least one account now: */
338         /* Note, when this fails is is caused by a Maemo gconf bug that has been 
339          * fixed in versions after 3.1. */
340         if(!modest_account_mgr_has_accounts (self, FALSE))
341                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
342                                 
343         /* Notify the observers */
344         g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, account_name);
345         modest_platform_emit_account_created_signal (account_name);
346
347         /* if no default account has been defined yet, do so now */
348         default_account = modest_account_mgr_get_default_account (self);
349         if (!default_account) {
350                 modest_account_mgr_set_default_account (self, account_name);
351                 modest_account_settings_set_is_default (settings, TRUE);
352         }
353         g_free (default_account);
354         g_free (account_name);
355
356         /* (re)set the automatic account update */
357         modest_platform_set_update_interval
358                 (modest_conf_get_int (priv->modest_conf, MODEST_CONF_UPDATE_INTERVAL, NULL));
359
360         return TRUE;
361 }
362
363
364 gboolean
365 modest_account_mgr_add_account (ModestAccountMgr *self,
366                                 const gchar *name,
367                                 const gchar *display_name,
368                                 const gchar *user_fullname,
369                                 const gchar *user_email,
370                                 ModestAccountRetrieveType retrieve_type,
371                                 const gchar *store_account,
372                                 const gchar *transport_account,
373                                 gboolean enabled)
374 {
375         ModestAccountMgrPrivate *priv;
376         const gchar *key;
377         gboolean ok;
378         gchar *default_account;
379         GError *err = NULL;
380         
381         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
382         g_return_val_if_fail (name, FALSE);
383         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
384         
385         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
386
387         /*
388          * we create the account by adding an account 'dir', with the name <name>,
389          * and in that the 'display_name' string key
390          */
391         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_DISPLAY_NAME,
392                                                               FALSE);
393         if (modest_account_mgr_account_exists (self, key, FALSE)) {
394                 g_printerr ("modest: account already exists\n");
395                 return FALSE;
396         }
397         
398         ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
399         if (!ok) {
400                 g_printerr ("modest: cannot set display name\n");
401                 if (err) {
402                         g_printerr ("modest: Error adding account conf: %s\n", err->message);
403                         g_error_free (err);
404                 }
405                 return FALSE;
406         }
407         
408         if (store_account) {
409                 key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_STORE_ACCOUNT,
410                                                                       FALSE);
411                 ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
412                 if (!ok) {
413                         g_printerr ("modest: failed to set store account '%s'\n",
414                                     store_account);
415                         if (err) {
416                                 g_printerr ("modest: Error adding store account conf: %s\n", err->message);
417                                 g_error_free (err);
418                         }
419                         return FALSE;
420                 }
421         }
422         
423         if (transport_account) {
424                 key = _modest_account_mgr_get_account_keyname_cached (priv, name,
425                                                                       MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
426                                                                       FALSE);
427                 ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
428                 if (!ok) {
429                         g_printerr ("modest: failed to set transport account '%s'\n",
430                                     transport_account);
431                         if (err) {
432                                 g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
433                                 g_error_free (err);
434                         }       
435                         return FALSE;
436                 }
437         }
438
439         /* Make sure that leave-messages-on-server is enabled by default, 
440          * as per the UI spec, though it is only meaningful for accounts using POP.
441          * (possibly this gconf key should be under the server account): */
442         modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE);
443         modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
444
445         /* Fill other data */
446         modest_account_mgr_set_string (self, name,
447                                        MODEST_ACCOUNT_DISPLAY_NAME, 
448                                        display_name, FALSE);
449         modest_account_mgr_set_string (self, name,
450                                        MODEST_ACCOUNT_FULLNAME, 
451                                        user_fullname, FALSE);
452         modest_account_mgr_set_string (self, name,
453                                        MODEST_ACCOUNT_EMAIL, 
454                                        user_email, FALSE);
455         modest_account_mgr_set_retrieve_type (self, name,
456                                               retrieve_type);
457
458         /* Notify the observers */
459         g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
460         modest_platform_emit_account_created_signal (name);
461
462         /* if no default account has been defined yet, do so now */
463         default_account = modest_account_mgr_get_default_account (self);
464         if (!default_account)
465                 modest_account_mgr_set_default_account (self, name);
466         g_free (default_account);
467         
468         /* (re)set the automatic account update */
469         modest_platform_set_update_interval
470                 (modest_conf_get_int (priv->modest_conf, MODEST_CONF_UPDATE_INTERVAL, NULL));
471         
472         return TRUE;
473 }
474
475
476 gboolean
477 modest_account_mgr_add_server_account (ModestAccountMgr * self,
478                                        const gchar *name, 
479                                        const gchar *hostname,
480                                        guint portnumber,
481                                        const gchar *username, 
482                                        const gchar *password,
483                                        ModestProtocolType proto,
484                                        ModestProtocolType security,
485                                        ModestProtocolType auth)
486 {
487         ModestAccountMgrPrivate *priv;
488         const gchar *key;
489         gboolean ok = TRUE;
490         GError *err = NULL;
491         ModestProtocolRegistry *protocol_registry;
492
493         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
494         g_return_val_if_fail (name, FALSE);
495         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
496
497         protocol_registry = modest_runtime_get_protocol_registry ();
498         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
499
500         /* hostname */
501         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_HOSTNAME, TRUE);
502         if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
503                 g_printerr ("modest: server account '%s' already exists\n", name);
504                 ok =  FALSE;
505         }
506         if (!ok)
507                 goto cleanup;
508         
509         modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &err);
510         if (err) {
511                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
512                 g_error_free (err);
513                 ok = FALSE;
514         }
515         if (!ok)
516                 goto cleanup;
517         
518         /* username */
519         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_USERNAME, TRUE);
520         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
521         if (err) {
522                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
523                 g_error_free (err);
524                 ok = FALSE;
525         }
526         if (!ok)
527                 goto cleanup;
528         
529         
530         /* password */
531         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PASSWORD, TRUE);
532         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
533         if (err) {
534                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
535                 g_error_free (err);
536                 ok = FALSE;
537         }
538         if (!ok)
539                 goto cleanup;
540
541         /* proto */
542         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
543         ok = modest_conf_set_string (priv->modest_conf, key,
544                                      modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
545                                      &err);
546         if (err) {
547                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
548                 g_error_free (err);
549                 ok = FALSE;
550         }
551         if (!ok)
552                 goto cleanup;
553
554
555         /* portnumber */
556         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PORT, TRUE);
557         ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
558         if (err) {
559                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
560                 g_error_free (err);
561                 ok = FALSE;
562         }
563         if (!ok)
564                 goto cleanup;
565
566         
567         /* auth mechanism */
568         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
569         ok = modest_conf_set_string (priv->modest_conf, key,
570                                      modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, auth)),
571                                      &err);
572         if (err) {
573                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
574                 g_error_free (err);
575                 ok = FALSE;
576         }
577         if (!ok)
578                 goto cleanup;
579         
580         /* Add the security settings: */
581         modest_account_mgr_set_server_account_security (self, name, security);
582
583 cleanup:
584         if (!ok) {
585                 g_printerr ("modest: failed to add server account\n");
586                 return FALSE;
587         }
588
589         return TRUE;
590 }
591
592 /** modest_account_mgr_add_server_account_uri:
593  * Only used for mbox and maildir accounts.
594  */
595 gboolean
596 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
597                                            const gchar *name, 
598                                            ModestProtocolType proto,
599                                            const gchar *uri)
600 {
601         ModestAccountMgrPrivate *priv;
602         const gchar *key;
603         gboolean ok;
604         ModestProtocolRegistry *protocol_registry;
605         
606         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
607         g_return_val_if_fail (name, FALSE);
608         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
609         g_return_val_if_fail (uri, FALSE);
610         
611         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
612         protocol_registry = modest_runtime_get_protocol_registry ();
613         
614         /* proto */
615         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_PROTO, TRUE);
616         ok = modest_conf_set_string (priv->modest_conf, key,
617                                      modest_protocol_get_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, proto)),
618                                      NULL);
619
620         if (!ok) {
621                 g_printerr ("modest: failed to set proto\n");
622                 return FALSE;
623         }
624         
625         /* uri */
626         key = _modest_account_mgr_get_account_keyname_cached (priv, name, MODEST_ACCOUNT_URI, TRUE);
627         ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
628
629         if (!ok) {
630                 g_printerr ("modest: failed to set uri\n");
631                 return FALSE;
632         }
633         return TRUE;
634 }
635
636 /* 
637  * Utility function used by modest_account_mgr_remove_account
638  */
639 static void
640 real_remove_account (ModestConf *conf,
641                      const gchar *acc_name,
642                      gboolean server_account)
643 {
644         GError *err = NULL;
645         gchar *key;
646         
647         key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
648         modest_conf_remove_key (conf, key, &err);
649
650         if (err) {
651                 g_printerr ("modest: error removing key: %s\n", err->message);
652                 g_error_free (err);
653         }
654         g_free (key);
655 }
656
657 gboolean
658 modest_account_mgr_remove_account (ModestAccountMgr * self,
659                                    const gchar* name)
660 {
661         ModestAccountMgrPrivate *priv;
662         gchar *default_account_name, *store_acc_name, *transport_acc_name;
663         gboolean default_account_deleted;
664
665         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
666         g_return_val_if_fail (name, FALSE);
667
668         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
669                 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
670                 return FALSE;
671         }
672
673         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
674         default_account_deleted = FALSE;
675
676         /* If this was the default, then remove that setting: */
677         default_account_name = modest_account_mgr_get_default_account (self);
678         if (default_account_name && (strcmp (default_account_name, name) == 0)) {
679                 modest_account_mgr_unset_default_account (self);
680                 default_account_deleted = TRUE;
681         }
682         g_free (default_account_name);
683
684         /* Delete transport and store accounts */
685         store_acc_name = modest_account_mgr_get_string (self, name, 
686                                                         MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
687         if (store_acc_name)
688                 real_remove_account (priv->modest_conf, store_acc_name, TRUE);
689
690         transport_acc_name = modest_account_mgr_get_string (self, name, 
691                                                             MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
692         if (transport_acc_name)
693                 real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
694                         
695         /* Remove the modest account */
696         real_remove_account (priv->modest_conf, name, FALSE);
697
698         if (default_account_deleted) {  
699                 /* pick another one as the new default account. We do
700                    this *after* deleting the keys, because otherwise a
701                    call to account_names will retrieve also the
702                    deleted account */
703                 modest_account_mgr_set_first_account_as_default (self);
704         }
705
706         /* if this was the last account, stop any auto-updating */
707         /* (re)set the automatic account update */
708         GSList *acc_names = modest_account_mgr_account_names (self, TRUE);
709         if (!acc_names) {
710                 modest_platform_set_update_interval (0);
711                 /* it was the last account, the has_account / has_enabled_account
712                  * changes
713                  */
714                 priv->has_accounts = priv->has_enabled_accounts = FALSE; 
715         } else
716                 modest_account_mgr_free_account_names (acc_names);
717         
718         /* Notify the observers. We do this *after* deleting
719            the keys, because otherwise a call to account_names
720            will retrieve also the deleted account */
721         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
722         modest_platform_emit_account_removed_signal (name);
723         
724         return TRUE;
725 }
726
727 gboolean
728 modest_account_mgr_remove_server_account (ModestAccountMgr * self,
729                                           const gchar* name)
730 {
731         ModestAccountMgrPrivate *priv;
732
733         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
734         g_return_val_if_fail (name, FALSE);
735
736         if (!modest_account_mgr_account_exists (self, name, TRUE)) {
737                 g_printerr ("modest: %s: server account '%s' does not exist\n", __FUNCTION__, name);
738                 return FALSE;
739         }
740
741         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
742         real_remove_account (priv->modest_conf, name, TRUE);
743
744         return TRUE;
745 }
746
747
748
749 /* strip the first /n/ character from each element
750  * caller must make sure all elements are strings with
751  * length >= n, and also that data can be freed.
752  * change is in-place
753  */
754 static void
755 strip_prefix_from_elements (GSList * lst, guint n)
756 {
757         while (lst) {
758                 memmove (lst->data, lst->data + n,
759                          strlen(lst->data) - n + 1);
760                 lst = lst->next;
761         }
762 }
763
764
765 GSList*
766 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
767 {
768         GSList *accounts;
769         ModestAccountMgrPrivate *priv;
770         GError *err = NULL;
771         
772         /* we add 1 for the trailing "/" */
773         const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE) + 1;
774
775         g_return_val_if_fail (self, NULL);
776
777         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
778         accounts = modest_conf_list_subkeys (priv->modest_conf,
779                                              MODEST_ACCOUNT_NAMESPACE, &err);
780
781         if (err) {
782                 g_printerr ("modest: failed to get subkeys (%s): %s\n",
783                             MODEST_ACCOUNT_NAMESPACE, err->message);
784                 g_error_free (err);
785                 return NULL; /* assume accounts did not get value when err is set...*/
786         }
787         
788         strip_prefix_from_elements (accounts, prefix_len);
789                 
790         GSList *result = NULL;
791         
792         /* Unescape the keys to get the account names: */
793         GSList *iter = accounts;
794         while (iter) {
795                 if (!(iter->data)) {
796                         iter = iter->next;
797                         continue;
798                 }
799
800                 const gchar* account_name_key = (const gchar*)iter->data;
801                 gchar* unescaped_name = account_name_key ? 
802                         modest_conf_key_unescape (account_name_key) 
803                         : NULL;
804                 
805                 gboolean add = TRUE;
806                 if (only_enabled) {
807                         if (unescaped_name && 
808                             !modest_account_mgr_get_bool (self, unescaped_name, 
809                                                           MODEST_ACCOUNT_ENABLED, FALSE))
810                                 add = FALSE;
811                 }
812                 
813                 /* Ignore modest accounts whose server accounts don't exist: 
814                  * (We could be getting this list while the account is being deleted, 
815                  * while the child server accounts have already been deleted, but the 
816                  * parent modest account already exists.
817                  */
818                 if (add) {
819                         gchar* server_account_name = modest_account_mgr_get_string
820                                 (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
821                                  FALSE);
822                         if (server_account_name) {
823                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
824                                         add = FALSE;
825                                 g_free (server_account_name);
826                         }
827                 }
828                 
829                 if (add) {
830                         gchar* server_account_name = modest_account_mgr_get_string
831                                 (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
832                                  FALSE);
833                         if (server_account_name) {
834                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
835                                         add = FALSE;
836                                 g_free (server_account_name);
837                         }
838                 }
839                 
840                 if (add)
841                         result = g_slist_append (result, unescaped_name);
842                 else 
843                         g_free (unescaped_name);
844
845                 g_free (iter->data);
846                 iter->data = NULL;
847                 
848                 iter = g_slist_next (iter);     
849         }
850         
851
852         /* we already freed the strings in the loop */
853         g_slist_free (accounts);
854         
855         accounts = NULL;
856         return result;
857 }
858
859
860 void
861 modest_account_mgr_free_account_names (GSList *account_names)
862 {
863         g_slist_foreach (account_names, (GFunc)g_free, NULL);
864         g_slist_free (account_names);
865 }
866
867
868
869 gchar *
870 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
871                                const gchar *key, gboolean server_account) {
872
873         ModestAccountMgrPrivate *priv;
874
875         const gchar *keyname;
876         gchar *retval;
877         GError *err = NULL;
878
879         g_return_val_if_fail (self, NULL);
880         g_return_val_if_fail (name, NULL);
881         g_return_val_if_fail (key, NULL);
882
883         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
884         
885         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
886         
887         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
888         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
889         if (err) {
890                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
891                 g_error_free (err);
892                 retval = NULL;
893         }
894
895         return retval;
896 }
897
898
899 gint
900 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
901                             gboolean server_account)
902 {
903         ModestAccountMgrPrivate *priv;
904
905         const gchar *keyname;
906         gint retval;
907         GError *err = NULL;
908         
909         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
910         g_return_val_if_fail (name, -1);
911         g_return_val_if_fail (key, -1);
912
913         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
914
915         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
916         
917         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
918         if (err) {
919                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
920                 g_error_free (err);
921                 retval = -1;
922         }
923
924         return retval;
925 }
926
927
928
929 gboolean
930 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
931                              const gchar * key, gboolean server_account)
932 {
933         ModestAccountMgrPrivate *priv;
934
935         const gchar *keyname;
936         gboolean retval;
937         GError *err = NULL;
938
939         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
940         g_return_val_if_fail (account, FALSE);
941         g_return_val_if_fail (key, FALSE);
942
943         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
944         ///keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
945
946         keyname = _modest_account_mgr_get_account_keyname_cached (priv, account, key, server_account);
947                 
948         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
949         if (err) {
950                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
951                 g_error_free (err);
952                 retval = FALSE;
953         }
954
955         return retval;
956 }
957
958
959
960 GSList * 
961 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
962                              const gchar *key, ModestConfValueType list_type,
963                              gboolean server_account)
964 {
965         ModestAccountMgrPrivate *priv = NULL;
966
967         const gchar *keyname;
968         GSList *retval;
969         GError *err = NULL;
970         
971         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
972         g_return_val_if_fail (name, NULL);
973         g_return_val_if_fail (key, NULL);
974         
975         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
976
977         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key,
978                                                                   server_account);
979         
980         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
981         if (err) {
982                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
983                             err->message);
984                 g_error_free (err);
985                 retval = NULL;
986         }
987         return retval;
988 }
989
990
991 gboolean
992 modest_account_mgr_set_string (ModestAccountMgr * self, 
993                                const gchar * name,
994                                const gchar * key, 
995                                const gchar * val, 
996                                gboolean server_account)
997 {
998         ModestAccountMgrPrivate *priv;
999
1000         const gchar *keyname;
1001         gboolean retval;
1002         GError *err = NULL;
1003
1004         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1005         g_return_val_if_fail (name, FALSE);
1006         g_return_val_if_fail (key, FALSE);
1007
1008         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1009
1010         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1011         
1012         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
1013         if (err) {
1014                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
1015                 g_error_free (err);
1016                 retval = FALSE;
1017         }
1018         return retval;
1019 }
1020
1021 gboolean
1022 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
1023                             const gchar * key, int val, gboolean server_account)
1024 {
1025         ModestAccountMgrPrivate *priv;
1026         const gchar *keyname;
1027         gboolean retval;
1028         GError *err = NULL;
1029         
1030         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1031         g_return_val_if_fail (name, FALSE);
1032         g_return_val_if_fail (key, FALSE);
1033
1034         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1035
1036         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1037         
1038         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
1039         if (err) {
1040                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
1041                 g_error_free (err);
1042                 retval = FALSE;
1043         } else {
1044                 /* check whether this field is one of those interesting for the 
1045                  * "account-updated" signal */
1046                 if (strcmp(key, MODEST_ACCOUNT_LAST_UPDATED) == 0) {
1047                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATED_SIGNAL], 
1048                                         0, name);
1049                 }
1050         }
1051         return retval;
1052 }
1053
1054
1055
1056 gboolean
1057 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
1058                              const gchar * key, gboolean val, gboolean server_account)
1059 {
1060         ModestAccountMgrPrivate *priv;
1061
1062         const gchar *keyname;
1063         gboolean retval;
1064         GError *err = NULL;
1065
1066         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1067         g_return_val_if_fail (name, FALSE);
1068         g_return_val_if_fail (key, FALSE);
1069
1070         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1071         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1072         
1073         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
1074         if (err) {
1075                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
1076                 g_error_free (err);
1077                 retval = FALSE;
1078         }
1079
1080         return retval;
1081 }
1082
1083
1084 gboolean
1085 modest_account_mgr_set_list (ModestAccountMgr *self,
1086                              const gchar *name,
1087                              const gchar *key,
1088                              GSList *val,
1089                              ModestConfValueType list_type,
1090                              gboolean server_account)
1091 {
1092         ModestAccountMgrPrivate *priv;
1093         const gchar *keyname;
1094         GError *err = NULL;
1095         gboolean retval;
1096         
1097         g_return_val_if_fail (self, FALSE);
1098         g_return_val_if_fail (name, FALSE);
1099         g_return_val_if_fail (key,  FALSE);
1100         g_return_val_if_fail (val,  FALSE);
1101
1102         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1103
1104         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1105         
1106         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
1107         if (err) {
1108                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
1109                 g_error_free (err);
1110                 retval = FALSE;
1111         }
1112
1113         return retval;
1114 }
1115
1116 gboolean
1117 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
1118                                    gboolean server_account)
1119 {
1120         ModestAccountMgrPrivate *priv;
1121
1122         const gchar *keyname;
1123         gboolean retval;
1124         GError *err = NULL;
1125
1126         g_return_val_if_fail (self, FALSE);
1127         g_return_val_if_fail (name, FALSE);
1128
1129         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1130         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, NULL, server_account);
1131         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1132         if (err) {
1133                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1134                             err->message);
1135                 g_error_free (err);
1136                 retval = FALSE;
1137         }
1138         return retval;
1139 }
1140
1141 gboolean
1142 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, 
1143                                                       const gchar *display_name)
1144 {
1145         GSList *account_names = NULL;
1146         GSList *cursor = NULL;
1147         
1148         cursor = account_names = modest_account_mgr_account_names (self, 
1149                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1150
1151         gboolean found = FALSE;
1152         
1153         /* Look at each non-server account to check their display names; */
1154         while (cursor) {
1155                 const gchar *account_name = (gchar*)cursor->data;
1156                 const gchar *cursor_display_name;
1157                 
1158                 ModestAccountSettings *settings = modest_account_mgr_load_account_settings (self, account_name);
1159                 if (!settings) {
1160                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1161                         cursor = cursor->next;
1162                         continue;
1163                 }
1164
1165                 cursor_display_name = modest_account_settings_get_display_name (settings);
1166                 if(cursor_display_name && (strcmp (cursor_display_name, display_name) == 0)) {
1167                         found = TRUE;
1168                         g_object_unref (settings);
1169                         break;
1170                 }
1171
1172                 g_object_unref (settings);
1173                 cursor = cursor->next;
1174         }
1175         modest_account_mgr_free_account_names (account_names);
1176         account_names = NULL;
1177         
1178         return found;
1179 }
1180
1181 static gboolean
1182 server_accounts_equal (ModestServerAccountSettings *s1,
1183                        ModestServerAccountSettings *s2)
1184 {
1185         const gchar *str1, *str2;
1186
1187         if (modest_server_account_settings_get_protocol (s1) !=
1188             modest_server_account_settings_get_protocol (s2))
1189                 return FALSE;
1190
1191         str1 = modest_server_account_settings_get_username (s1);
1192         str2 = modest_server_account_settings_get_username (s2);
1193         if (str1 && str2 && (str1 != str2) &&
1194             strcmp (str1, str2) != 0)
1195                 return FALSE;
1196
1197         str1 = modest_server_account_settings_get_hostname (s1);
1198         str2 = modest_server_account_settings_get_hostname (s2);
1199         if (str1 && str2 && (str1 != str2) &&
1200             strcmp (str1, str2) != 0)
1201                 return FALSE;
1202
1203         if (modest_server_account_settings_get_port (s1) !=
1204             modest_server_account_settings_get_port (s2))
1205                 return FALSE;
1206
1207         return TRUE;
1208 }
1209
1210 gboolean
1211 modest_account_mgr_check_already_configured_account  (ModestAccountMgr *self, 
1212                                                       ModestAccountSettings *settings)
1213 {
1214         GSList *account_names = NULL;
1215         GSList *cursor = NULL;
1216         ModestServerAccountSettings *server_settings;
1217         
1218         cursor = account_names = modest_account_mgr_account_names (self, 
1219                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1220
1221         gboolean found = FALSE;
1222
1223         server_settings = modest_account_settings_get_store_settings (settings);
1224         if (!server_settings) {
1225                 g_printerr ("modest: couldn't get store settings from settings");
1226                 modest_account_mgr_free_account_names (account_names);
1227                 return FALSE;
1228         }
1229         
1230         /* Look at each non-server account to check their display names; */
1231         while (cursor && !found) {
1232                 const gchar *account_name;
1233                 ModestAccountSettings *from_mgr_settings;
1234                 ModestServerAccountSettings *from_mgr_server_settings;
1235
1236                 account_name = (gchar*)cursor->data;            
1237                 from_mgr_settings = modest_account_mgr_load_account_settings (self, account_name);
1238                 if (!settings) {
1239                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1240                         cursor = cursor->next;
1241                         continue;
1242                 }
1243
1244                 from_mgr_server_settings = modest_account_settings_get_store_settings (from_mgr_settings);
1245                 if (server_settings) {
1246                         if (server_accounts_equal (from_mgr_server_settings, server_settings)) {
1247                                 found = TRUE;
1248                         }
1249                         g_object_unref (from_mgr_server_settings);
1250                 } else {
1251                         g_printerr ("modest: couldn't get store settings from account %s", account_name);
1252                 }
1253                 g_object_unref (from_mgr_settings);
1254                 cursor = cursor->next;
1255         }
1256
1257         g_object_unref (server_settings);
1258         modest_account_mgr_free_account_names (account_names);
1259         account_names = NULL;
1260         
1261         return found;
1262 }
1263
1264
1265
1266
1267 gboolean 
1268 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1269                           const gchar *key, gboolean server_account)
1270 {
1271         ModestAccountMgrPrivate *priv;
1272         
1273         const gchar *keyname;
1274         gboolean retval;
1275         GError *err = NULL;
1276         
1277         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1278         g_return_val_if_fail (name, FALSE);
1279         g_return_val_if_fail (key, FALSE);
1280
1281         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1282         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1283
1284         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1285         if (err) {
1286                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1287                             err->message);
1288                 g_error_free (err);
1289                 retval = FALSE;
1290         }
1291         return retval;
1292 }
1293
1294 gchar*
1295 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1296 {
1297         const gchar* account_ns;
1298         const gchar* server_account_ns;
1299         gchar *account = NULL;
1300         gchar *unescaped_name = NULL;
1301
1302         /* Initialize input parameters: */
1303         if (is_account_key)
1304                 *is_account_key = FALSE;
1305
1306         if (is_server_account)
1307                 *is_server_account = FALSE;
1308
1309         account_ns        = modest_defs_namespace (MODEST_ACCOUNT_SUBNAMESPACE "/");
1310         server_account_ns = modest_defs_namespace (MODEST_SERVER_ACCOUNT_SUBNAMESPACE "/");
1311
1312         /* determine whether it's an account or a server account,
1313          * based on the prefix */
1314         if (g_str_has_prefix (key, account_ns)) {
1315
1316                 if (is_server_account)
1317                         *is_server_account = FALSE;
1318
1319                 account = g_strdup (key + strlen (account_ns));
1320
1321         } else if (g_str_has_prefix (key, server_account_ns)) {
1322
1323                 if (is_server_account)
1324                         *is_server_account = TRUE;
1325
1326                 account = g_strdup (key + strlen (server_account_ns));
1327         } else
1328                 return NULL;
1329
1330         if (account) {
1331                 gchar *cursor;
1332
1333                 /* if there are any slashes left in the key, it's not
1334                  * the toplevel entry for an account
1335                  */
1336                 cursor = strstr(account, "/");
1337
1338                 if (cursor) {
1339                         if (is_account_key)
1340                                 *is_account_key = TRUE;
1341
1342                         /* put a NULL where the first slash was */
1343                         *cursor = '\0';
1344                 }
1345
1346                 /* The key is an escaped string, so unescape it to get the actual account name */
1347                 unescaped_name = modest_conf_key_unescape (account);
1348                 g_free (account);
1349         }
1350
1351         return unescaped_name;
1352 }
1353
1354
1355
1356
1357
1358 /* optimization: only with non-alphanum chars, escaping is needed */
1359 inline static gboolean
1360 is_alphanum (const gchar* str)
1361 {
1362         const gchar *cursor;
1363         for (cursor = str; cursor && *cursor; ++cursor) {
1364                 const char c = *cursor;
1365                 /* we cannot trust isalnum(3), because it might consider locales */
1366                 /*       numbers            ALPHA            alpha       */
1367                 if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
1368                         return FALSE;
1369         }
1370         return TRUE;
1371 }
1372                 
1373
1374
1375
1376 /* must be freed by caller */
1377 gchar *
1378 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar* name,
1379                                          gboolean server_account)
1380 {
1381         gchar *retval = NULL;   
1382         gchar *namespace = server_account ? (gchar *) MODEST_SERVER_ACCOUNT_NAMESPACE : (gchar *) MODEST_ACCOUNT_NAMESPACE;
1383         gchar *escaped_account_name, *escaped_name;
1384         
1385         if (!account_name)
1386                 return g_strdup (namespace);
1387
1388         /* optimization: only escape names when need to be escaped */
1389         if (is_alphanum (account_name))
1390                 escaped_account_name = (gchar*)account_name;
1391         else
1392                 escaped_account_name = modest_conf_key_escape (account_name);
1393         
1394         if (is_alphanum (name))
1395                 escaped_name = (gchar*)name;
1396         else
1397                 escaped_name = modest_conf_key_escape (name);
1398         //////////////////////////////////////////////////////////////
1399
1400         if (escaped_account_name && escaped_name)
1401                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1402         else if (escaped_account_name)
1403                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1404
1405         /* Sanity check: */
1406         if (!retval || !modest_conf_key_is_valid (retval)) {
1407                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__,
1408                            retval ? retval: "<empty>");
1409                 g_free (retval);
1410                 retval = NULL;
1411         }
1412         
1413         /* g_free is only needed if we actually allocated anything */
1414         if (name != escaped_name)
1415                 g_free (escaped_name);
1416         if (account_name != escaped_account_name)
1417                 g_free (escaped_account_name);
1418
1419         return retval;
1420 }
1421
1422 static const gchar *
1423 _modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, 
1424                                                 const gchar* account_name,
1425                                                 const gchar *name, 
1426                                                 gboolean is_server)
1427 {
1428         GHashTable *hash = is_server ? priv->server_account_key_hash : priv->account_key_hash;
1429         GHashTable *account_hash;
1430         gchar *key = NULL;
1431         const gchar *search_name;
1432
1433         if (!account_name)
1434                 return is_server ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1435
1436         search_name = name ? name : "<dummy>";
1437         
1438         account_hash = g_hash_table_lookup (hash, account_name);        
1439         if (!account_hash) { /* no hash for this account yet? create it */
1440                 account_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);         
1441                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1442                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1443                 g_hash_table_insert (hash, g_strdup(account_name), account_hash);
1444                 return key;
1445         }
1446         
1447         /* we have a hash for this account, but do we have the key? */  
1448         key = g_hash_table_lookup (account_hash, search_name);
1449         if (!key) {
1450                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1451                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1452         }
1453         
1454         return key;
1455 }
1456
1457
1458 gboolean
1459 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1460 {
1461         ModestAccountMgrPrivate* priv;
1462         GSList *account_names;
1463         gboolean accounts_exist;
1464
1465         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1466         
1467         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1468         
1469         if (enabled && priv->has_enabled_accounts)
1470                 return TRUE;
1471         else if (priv->has_accounts)
1472                 return TRUE;
1473                 
1474         /* Check that at least one account exists: */
1475         account_names = modest_account_mgr_account_names (self,enabled);
1476         accounts_exist = account_names != NULL;
1477         modest_account_mgr_free_account_names (account_names);
1478         account_names = NULL;
1479
1480         /* cache it. */
1481         if (enabled)
1482                 priv->has_enabled_accounts = accounts_exist;
1483         else
1484                 priv->has_accounts = accounts_exist;
1485         
1486         return accounts_exist;
1487 }
1488
1489 static int
1490 compare_account_name(gconstpointer a, gconstpointer b)
1491 {
1492         const gchar* account_name = (const gchar*) a;
1493         const gchar* account_name2 = (const gchar*) b;
1494         return strcmp(account_name, account_name2);
1495 }
1496
1497 void 
1498 modest_account_mgr_set_account_busy(ModestAccountMgr* self, 
1499                                     const gchar* account_name, 
1500                                     gboolean busy)
1501 {
1502         ModestAccountMgrPrivate* priv;
1503
1504         g_return_if_fail (MODEST_IS_ACCOUNT_MGR(self));
1505         g_return_if_fail (account_name);
1506
1507         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1508         if (busy) {
1509                 GSList *account_names = modest_account_mgr_account_names (self, TRUE);
1510                 GSList* account = g_slist_find_custom(account_names, account_name, 
1511                                                       (GCompareFunc) compare_account_name);
1512
1513                 if (account && !modest_account_mgr_account_is_busy(self, account_name)) {
1514                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1515                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
1516                                        0, account_name, TRUE);
1517                 }
1518                 modest_account_mgr_free_account_names (account_names);
1519                 account_names = NULL;
1520         } else {
1521                 GSList* account = 
1522                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1523
1524                 if (account) {
1525                         g_free(account->data);
1526                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1527                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
1528                                        0, account_name, FALSE);
1529                 }
1530         }
1531 }
1532
1533 gboolean
1534 modest_account_mgr_account_is_busy (ModestAccountMgr* self, const gchar* account_name)
1535 {
1536         ModestAccountMgrPrivate* priv;
1537         
1538         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1539         g_return_val_if_fail (account_name, FALSE);
1540
1541         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1542                 
1543         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1544                 != NULL);
1545 }
1546
1547 void
1548 modest_account_mgr_notify_account_update (ModestAccountMgr* self, 
1549                                           const gchar *server_account_name)
1550 {
1551         ModestProtocolType proto;
1552         ModestAccountMgrPrivate* priv;
1553         ModestProtocolRegistry *protocol_registry;
1554         gchar *proto_name = NULL;
1555
1556         g_return_if_fail (self);
1557         g_return_if_fail (server_account_name);
1558         
1559         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1560         protocol_registry = modest_runtime_get_protocol_registry ();
1561         
1562         /* Get protocol */
1563         proto_name = modest_account_mgr_get_string (self, server_account_name, 
1564                                                     MODEST_ACCOUNT_PROTO, TRUE);
1565         if (!proto_name) {
1566                 g_return_if_reached ();
1567                 return;
1568         }
1569         proto = modest_protocol_get_type_id (modest_protocol_registry_get_protocol_by_name (protocol_registry,
1570                                                                                             MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
1571                                                                                             proto_name));
1572         g_free (proto_name);
1573
1574         /* there is some update in the account, so we can't
1575          * be sure about whether there are still enabled accounts...
1576          */
1577         priv->has_enabled_accounts = FALSE;
1578         priv->has_accounts         = FALSE;
1579         
1580         /* Emit "update-account" */
1581         g_signal_emit (G_OBJECT(self), 
1582                        signals[ACCOUNT_CHANGED_SIGNAL], 0, 
1583                        server_account_name, 
1584                        (modest_protocol_registry_protocol_type_has_tag (protocol_registry, proto, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) ? 
1585                        TNY_ACCOUNT_TYPE_STORE : 
1586                        TNY_ACCOUNT_TYPE_TRANSPORT);
1587 }
1588
1589
1590 gboolean
1591 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
1592 {
1593         ModestConf *conf;
1594         gboolean retval;
1595         
1596         g_return_val_if_fail (self,    FALSE);
1597         g_return_val_if_fail (account, FALSE);
1598         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
1599                               FALSE);
1600         
1601         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1602
1603         /* Change the default account and notify */
1604         retval = modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, account, NULL);
1605         if (retval)
1606                 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
1607
1608         return retval;
1609 }
1610
1611
1612 gchar*
1613 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
1614 {
1615         gchar *account; 
1616         ModestConf *conf;
1617         GError *err = NULL;
1618         
1619         g_return_val_if_fail (self, NULL);
1620
1621         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1622         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
1623         
1624         if (err) {
1625                 g_printerr ("modest: failed to get '%s': %s\n",
1626                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
1627                 g_error_free (err);
1628                 return  NULL;
1629         }
1630         
1631         /* sanity check */
1632         if (account && !modest_account_mgr_account_exists (self, account, FALSE)) {
1633                 g_printerr ("modest: default account does not exist\n");
1634                 g_free (account);
1635                 return NULL;
1636         }
1637
1638         return account;
1639 }
1640
1641 static gboolean
1642 modest_account_mgr_unset_default_account (ModestAccountMgr *self)
1643 {
1644         ModestConf *conf;
1645         gboolean retval;
1646         
1647         g_return_val_if_fail (self,    FALSE);
1648
1649         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1650                 
1651         retval = modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
1652
1653         if (retval)
1654                 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
1655
1656         return retval;
1657 }
1658
1659
1660 gchar* 
1661 modest_account_mgr_get_display_name (ModestAccountMgr *self, 
1662                                      const gchar* name)
1663 {
1664         return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
1665 }
1666
1667 void 
1668 modest_account_mgr_set_display_name (ModestAccountMgr *self, 
1669                                      const gchar *account_name,
1670                                      const gchar *display_name)
1671 {
1672         gboolean notify = TRUE;
1673
1674         if (!modest_account_mgr_get_display_name (self, account_name))
1675                 notify = FALSE;
1676
1677         modest_account_mgr_set_string (self, 
1678                                        account_name,
1679                                        MODEST_ACCOUNT_DISPLAY_NAME, 
1680                                        display_name, 
1681                                        FALSE /* not server account */);
1682
1683         /* Notify about the change in the display name */
1684         if (notify)
1685                 g_signal_emit (self, signals[DISPLAY_NAME_CHANGED_SIGNAL], 0, account_name);
1686 }
1687
1688 gboolean 
1689 modest_account_mgr_singleton_protocol_exists (ModestAccountMgr *mgr,
1690                                               ModestProtocolType protocol_type)
1691 {
1692         GSList *account_names, *node;
1693         gboolean found = FALSE;
1694
1695         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR (mgr), FALSE);
1696         account_names = modest_account_mgr_account_names (mgr, FALSE);
1697
1698         for (node = account_names; node != NULL; node = g_slist_next (node)) {
1699                 ModestProtocolType current_protocol;
1700
1701                 current_protocol = modest_account_mgr_get_store_protocol (mgr, (gchar *) node->data);
1702                 if (current_protocol == protocol_type) {
1703                         found = TRUE;
1704                         break;
1705                 }
1706         }
1707
1708         modest_account_mgr_free_account_names (account_names);
1709
1710         return found;
1711 }