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