Force to have at least first row selected in edit modes (fixes NB#98788).
[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         const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
750
751         g_return_val_if_fail (self, NULL);
752
753         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
754         accounts = modest_conf_list_subkeys (priv->modest_conf,
755                                              MODEST_ACCOUNT_NAMESPACE, &err);
756
757         if (err) {
758                 g_printerr ("modest: failed to get subkeys (%s): %s\n",
759                             MODEST_ACCOUNT_NAMESPACE, err->message);
760                 g_error_free (err);
761                 return NULL; /* assume accounts did not get value when err is set...*/
762         }
763         
764         strip_prefix_from_elements (accounts, prefix_len);
765                 
766         GSList *result = NULL;
767         
768         /* Unescape the keys to get the account names: */
769         GSList *iter = accounts;
770         while (iter) {
771                 if (!(iter->data))
772                         continue;
773                         
774                 const gchar* account_name_key = (const gchar*)iter->data;
775                 gchar* unescaped_name = account_name_key ? 
776                         modest_conf_key_unescape (account_name_key) 
777                         : NULL;
778                 
779                 gboolean add = TRUE;
780                 if (only_enabled) {
781                         if (unescaped_name && 
782                             !modest_account_mgr_get_bool (self, unescaped_name, 
783                                                           MODEST_ACCOUNT_ENABLED, FALSE))
784                                 add = FALSE;
785                 }
786                 
787                 /* Ignore modest accounts whose server accounts don't exist: 
788                  * (We could be getting this list while the account is being deleted, 
789                  * while the child server accounts have already been deleted, but the 
790                  * parent modest account already exists.
791                  */
792                 if (add) {
793                         gchar* server_account_name = modest_account_mgr_get_string
794                                 (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
795                                  FALSE);
796                         if (server_account_name) {
797                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
798                                         add = FALSE;
799                                 g_free (server_account_name);
800                         }
801                 }
802                 
803                 if (add) {
804                         gchar* server_account_name = modest_account_mgr_get_string
805                                 (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
806                                  FALSE);
807                         if (server_account_name) {
808                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
809                                         add = FALSE;
810                                 g_free (server_account_name);
811                         }
812                 }
813                 
814                 if (add)
815                         result = g_slist_append (result, unescaped_name);
816                 else 
817                         g_free (unescaped_name);
818
819                 g_free (iter->data);
820                 iter->data = NULL;
821                 
822                 iter = g_slist_next (iter);     
823         }
824         
825
826         /* we already freed the strings in the loop */
827         g_slist_free (accounts);
828         
829         accounts = NULL;
830         return result;
831 }
832
833
834 void
835 modest_account_mgr_free_account_names (GSList *account_names)
836 {
837         g_slist_foreach (account_names, (GFunc)g_free, NULL);
838         g_slist_free (account_names);
839 }
840
841
842
843 gchar *
844 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
845                                const gchar *key, gboolean server_account) {
846
847         ModestAccountMgrPrivate *priv;
848
849         const gchar *keyname;
850         gchar *retval;
851         GError *err = NULL;
852
853         g_return_val_if_fail (self, NULL);
854         g_return_val_if_fail (name, NULL);
855         g_return_val_if_fail (key, NULL);
856
857         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
858         
859         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
860         
861         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
862         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
863         if (err) {
864                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
865                 g_error_free (err);
866                 retval = NULL;
867         }
868
869         return retval;
870 }
871
872
873 gint
874 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
875                             gboolean server_account)
876 {
877         ModestAccountMgrPrivate *priv;
878
879         const gchar *keyname;
880         gint retval;
881         GError *err = NULL;
882         
883         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
884         g_return_val_if_fail (name, -1);
885         g_return_val_if_fail (key, -1);
886
887         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
888
889         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
890         
891         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
892         if (err) {
893                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
894                 g_error_free (err);
895                 retval = -1;
896         }
897
898         return retval;
899 }
900
901
902
903 gboolean
904 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
905                              const gchar * key, gboolean server_account)
906 {
907         ModestAccountMgrPrivate *priv;
908
909         const gchar *keyname;
910         gboolean retval;
911         GError *err = NULL;
912
913         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
914         g_return_val_if_fail (account, FALSE);
915         g_return_val_if_fail (key, FALSE);
916
917         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
918         ///keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
919
920         keyname = _modest_account_mgr_get_account_keyname_cached (priv, account, key, server_account);
921                 
922         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
923         if (err) {
924                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
925                 g_error_free (err);
926                 retval = FALSE;
927         }
928
929         return retval;
930 }
931
932
933
934 GSList * 
935 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
936                              const gchar *key, ModestConfValueType list_type,
937                              gboolean server_account)
938 {
939         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
940
941         const gchar *keyname;
942         GSList *retval;
943         GError *err = NULL;
944         
945         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
946         g_return_val_if_fail (name, NULL);
947         g_return_val_if_fail (key, NULL);
948         
949         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
950
951         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key,
952                                                                   server_account);
953         
954         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
955         if (err) {
956                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
957                             err->message);
958                 g_error_free (err);
959                 retval = NULL;
960         }
961         return retval;
962 }
963
964
965 gboolean
966 modest_account_mgr_set_string (ModestAccountMgr * self, 
967                                const gchar * name,
968                                const gchar * key, 
969                                const gchar * val, 
970                                gboolean server_account)
971 {
972         ModestAccountMgrPrivate *priv;
973
974         const gchar *keyname;
975         gboolean retval;
976         GError *err = NULL;
977
978         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
979         g_return_val_if_fail (name, FALSE);
980         g_return_val_if_fail (key, FALSE);
981
982         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
983
984         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
985         
986         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
987         if (err) {
988                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
989                 g_error_free (err);
990                 retval = FALSE;
991         }
992         return retval;
993 }
994
995 gboolean
996 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
997                             const gchar * key, int val, gboolean server_account)
998 {
999         ModestAccountMgrPrivate *priv;
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_int (priv->modest_conf, keyname, val, &err);
1013         if (err) {
1014                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
1015                 g_error_free (err);
1016                 retval = FALSE;
1017         } else {
1018                 /* check whether this field is one of those interesting for the 
1019                  * "account-updated" signal */
1020                 if (strcmp(key, MODEST_ACCOUNT_LAST_UPDATED) == 0) {
1021                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATED_SIGNAL], 
1022                                         0, name);
1023                 }
1024         }
1025         return retval;
1026 }
1027
1028
1029
1030 gboolean
1031 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
1032                              const gchar * key, gboolean val, gboolean server_account)
1033 {
1034         ModestAccountMgrPrivate *priv;
1035
1036         const gchar *keyname;
1037         gboolean retval;
1038         GError *err = NULL;
1039
1040         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1041         g_return_val_if_fail (name, FALSE);
1042         g_return_val_if_fail (key, FALSE);
1043
1044         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1045         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1046         
1047         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
1048         if (err) {
1049                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
1050                 g_error_free (err);
1051                 retval = FALSE;
1052         }
1053
1054         return retval;
1055 }
1056
1057
1058 gboolean
1059 modest_account_mgr_set_list (ModestAccountMgr *self,
1060                              const gchar *name,
1061                              const gchar *key,
1062                              GSList *val,
1063                              ModestConfValueType list_type,
1064                              gboolean server_account)
1065 {
1066         ModestAccountMgrPrivate *priv;
1067         const gchar *keyname;
1068         GError *err = NULL;
1069         gboolean retval;
1070         
1071         g_return_val_if_fail (self, FALSE);
1072         g_return_val_if_fail (name, FALSE);
1073         g_return_val_if_fail (key,  FALSE);
1074         g_return_val_if_fail (val,  FALSE);
1075
1076         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1077
1078         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1079         
1080         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
1081         if (err) {
1082                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
1083                 g_error_free (err);
1084                 retval = FALSE;
1085         }
1086
1087         return retval;
1088 }
1089
1090 gboolean
1091 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
1092                                    gboolean server_account)
1093 {
1094         ModestAccountMgrPrivate *priv;
1095
1096         const gchar *keyname;
1097         gboolean retval;
1098         GError *err = NULL;
1099
1100         g_return_val_if_fail (self, FALSE);
1101         g_return_val_if_fail (name, FALSE);
1102
1103         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1104         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, NULL, server_account);
1105         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1106         if (err) {
1107                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1108                             err->message);
1109                 g_error_free (err);
1110                 retval = FALSE;
1111         }
1112         return retval;
1113 }
1114
1115 gboolean
1116 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, 
1117                                                       const gchar *display_name)
1118 {
1119         GSList *account_names = NULL;
1120         GSList *cursor = NULL;
1121         
1122         cursor = account_names = modest_account_mgr_account_names (self, 
1123                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1124
1125         gboolean found = FALSE;
1126         
1127         /* Look at each non-server account to check their display names; */
1128         while (cursor) {
1129                 const gchar *account_name = (gchar*)cursor->data;
1130                 const gchar *cursor_display_name;
1131                 
1132                 ModestAccountSettings *settings = modest_account_mgr_load_account_settings (self, account_name);
1133                 if (!settings) {
1134                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1135                         continue;
1136                 }
1137
1138                 cursor_display_name = modest_account_settings_get_display_name (settings);
1139                 if(cursor_display_name && (strcmp (cursor_display_name, display_name) == 0)) {
1140                         found = TRUE;
1141                         g_object_unref (settings);
1142                         break;
1143                 }
1144
1145                 g_object_unref (settings);
1146                 cursor = cursor->next;
1147         }
1148         modest_account_mgr_free_account_names (account_names);
1149         account_names = NULL;
1150         
1151         return found;
1152 }
1153
1154 static gboolean
1155 server_accounts_equal (ModestServerAccountSettings *s1,
1156                        ModestServerAccountSettings *s2)
1157 {
1158         const gchar *str1, *str2;
1159
1160         if (modest_server_account_settings_get_protocol (s1) !=
1161             modest_server_account_settings_get_protocol (s2))
1162                 return FALSE;
1163
1164         str1 = modest_server_account_settings_get_username (s1);
1165         str2 = modest_server_account_settings_get_username (s2);
1166         if (str1 && str2 && (str1 != str2) &&
1167             strcmp (str1, str2) != 0)
1168                 return FALSE;
1169
1170         str1 = modest_server_account_settings_get_hostname (s1);
1171         str2 = modest_server_account_settings_get_hostname (s2);
1172         if (str1 && str2 && (str1 != str2) &&
1173             strcmp (str1, str2) != 0)
1174                 return FALSE;
1175
1176         if (modest_server_account_settings_get_port (s1) !=
1177             modest_server_account_settings_get_port (s2))
1178                 return FALSE;
1179
1180         return TRUE;
1181 }
1182
1183 gboolean
1184 modest_account_mgr_check_already_configured_account  (ModestAccountMgr *self, 
1185                                                       ModestAccountSettings *settings)
1186 {
1187         GSList *account_names = NULL;
1188         GSList *cursor = NULL;
1189         ModestServerAccountSettings *server_settings;
1190         
1191         cursor = account_names = modest_account_mgr_account_names (self, 
1192                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1193
1194         gboolean found = FALSE;
1195
1196         server_settings = modest_account_settings_get_store_settings (settings);
1197         if (!server_settings) {
1198                 g_printerr ("modest: couldn't get store settings from settings");
1199                 modest_account_mgr_free_account_names (account_names);
1200                 return FALSE;
1201         }
1202         
1203         /* Look at each non-server account to check their display names; */
1204         while (cursor && !found) {
1205                 const gchar *account_name;
1206                 ModestAccountSettings *from_mgr_settings;
1207                 ModestServerAccountSettings *from_mgr_server_settings;
1208
1209                 account_name = (gchar*)cursor->data;            
1210                 from_mgr_settings = modest_account_mgr_load_account_settings (self, account_name);
1211                 if (!settings) {
1212                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1213                         continue;
1214                 }
1215
1216                 from_mgr_server_settings = modest_account_settings_get_store_settings (from_mgr_settings);
1217                 if (server_settings) {
1218                         if (server_accounts_equal (from_mgr_server_settings, server_settings)) {
1219                                 found = TRUE;
1220                         }
1221                         g_object_unref (from_mgr_server_settings);
1222                 } else {
1223                         g_printerr ("modest: couldn't get store settings from account %s", account_name);
1224                 }
1225                 g_object_unref (from_mgr_settings);
1226                 cursor = cursor->next;
1227         }
1228
1229         g_object_unref (server_settings);
1230         modest_account_mgr_free_account_names (account_names);
1231         account_names = NULL;
1232         
1233         return found;
1234 }
1235
1236
1237
1238
1239 gboolean 
1240 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1241                           const gchar *key, gboolean server_account)
1242 {
1243         ModestAccountMgrPrivate *priv;
1244         
1245         const gchar *keyname;
1246         gboolean retval;
1247         GError *err = NULL;
1248         
1249         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1250         g_return_val_if_fail (name, FALSE);
1251         g_return_val_if_fail (key, FALSE);
1252
1253         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1254         keyname = _modest_account_mgr_get_account_keyname_cached (priv, name, key, server_account);
1255
1256         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1257         if (err) {
1258                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1259                             err->message);
1260                 g_error_free (err);
1261                 retval = FALSE;
1262         }
1263         return retval;
1264 }
1265
1266 gchar*
1267 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1268 {
1269         /* Initialize input parameters: */
1270         if (is_account_key)
1271                 *is_account_key = FALSE;
1272
1273         if (is_server_account)
1274                 *is_server_account = FALSE;
1275
1276         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1277         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1278         gchar *cursor;
1279         gchar *account = NULL;
1280
1281         /* determine whether it's an account or a server account,
1282          * based on the prefix */
1283         if (g_str_has_prefix (key, account_ns)) {
1284
1285                 if (is_server_account)
1286                         *is_server_account = FALSE;
1287                 
1288                 account = g_strdup (key + strlen (account_ns));
1289
1290         } else if (g_str_has_prefix (key, server_account_ns)) {
1291
1292                 if (is_server_account)
1293                         *is_server_account = TRUE;
1294                 
1295                 account = g_strdup (key + strlen (server_account_ns));  
1296         } else
1297                 return NULL;
1298
1299         /* if there are any slashes left in the key, it's not
1300          * the toplevel entry for an account
1301          */
1302         cursor = strstr(account, "/");
1303         
1304         if (is_account_key && cursor)
1305                 *is_account_key = TRUE;
1306
1307         /* put a NULL where the first slash was */
1308         if (cursor)
1309                 *cursor = '\0';
1310
1311         if (account) {
1312                 /* The key is an escaped string, so unescape it to get the actual account name: */
1313                 gchar *unescaped_name = modest_conf_key_unescape (account);
1314                 g_free (account);
1315                 return unescaped_name;
1316         } else
1317                 return NULL;
1318 }
1319
1320
1321
1322
1323
1324 /* optimization: only with non-alphanum chars, escaping is needed */
1325 inline static gboolean
1326 is_alphanum (const gchar* str)
1327 {
1328         const gchar *cursor;
1329         for (cursor = str; cursor && *cursor; ++cursor) {
1330                 const char c = *cursor;
1331                 /* we cannot trust isalnum(3), because it might consider locales */
1332                 /*       numbers            ALPHA            alpha       */
1333                 if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
1334                         return FALSE;
1335         }
1336         return TRUE;
1337 }
1338                 
1339
1340
1341
1342 /* must be freed by caller */
1343 gchar *
1344 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar* name,
1345                                          gboolean server_account)
1346 {
1347         gchar *retval = NULL;   
1348         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1349         gchar *escaped_account_name, *escaped_name;
1350         
1351         if (!account_name)
1352                 return g_strdup (namespace);
1353
1354         /* optimization: only escape names when need to be escaped */
1355         if (is_alphanum (account_name))
1356                 escaped_account_name = (gchar*)account_name;
1357         else
1358                 escaped_account_name = modest_conf_key_escape (account_name);
1359         
1360         if (is_alphanum (name))
1361                 escaped_name = (gchar*)name;
1362         else
1363                 escaped_name = modest_conf_key_escape (name);
1364         //////////////////////////////////////////////////////////////
1365
1366         if (escaped_account_name && escaped_name)
1367                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1368         else if (escaped_account_name)
1369                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1370
1371         /* Sanity check: */
1372         if (!retval || !modest_conf_key_is_valid (retval)) {
1373                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__,
1374                            retval ? retval: "<empty>");
1375                 g_free (retval);
1376                 retval = NULL;
1377         }
1378         
1379         /* g_free is only needed if we actually allocated anything */
1380         if (name != escaped_name)
1381                 g_free (escaped_name);
1382         if (account_name != escaped_account_name)
1383                 g_free (escaped_account_name);
1384
1385         return retval;
1386 }
1387
1388 static const gchar *
1389 _modest_account_mgr_get_account_keyname_cached (ModestAccountMgrPrivate *priv, 
1390                                                 const gchar* account_name,
1391                                                 const gchar *name, 
1392                                                 gboolean is_server)
1393 {
1394         GHashTable *hash = is_server ? priv->server_account_key_hash : priv->account_key_hash;
1395         GHashTable *account_hash;
1396         gchar *key = NULL;
1397         const gchar *search_name;
1398
1399         if (!account_name)
1400                 return is_server ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1401
1402         search_name = name ? name : "<dummy>";
1403         
1404         account_hash = g_hash_table_lookup (hash, account_name);        
1405         if (!account_hash) { /* no hash for this account yet? create it */
1406                 account_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);         
1407                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1408                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1409                 g_hash_table_insert (hash, g_strdup(account_name), account_hash);
1410                 return key;
1411         }
1412         
1413         /* we have a hash for this account, but do we have the key? */  
1414         key = g_hash_table_lookup (account_hash, search_name);
1415         if (!key) {
1416                 key = _modest_account_mgr_get_account_keyname (account_name, name, is_server);
1417                 g_hash_table_insert (account_hash, g_strdup(search_name), key);
1418         }
1419         
1420         return key;
1421 }
1422
1423
1424 gboolean
1425 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1426 {
1427         ModestAccountMgrPrivate* priv;
1428         GSList *account_names;
1429         gboolean accounts_exist;
1430
1431         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1432         
1433         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1434         
1435         if (enabled && priv->has_enabled_accounts)
1436                 return TRUE;
1437         else if (priv->has_accounts)
1438                 return TRUE;
1439                 
1440         /* Check that at least one account exists: */
1441         account_names = modest_account_mgr_account_names (self,enabled);
1442         accounts_exist = account_names != NULL;
1443         modest_account_mgr_free_account_names (account_names);
1444         account_names = NULL;
1445
1446         /* cache it. */
1447         if (enabled)
1448                 priv->has_enabled_accounts = accounts_exist;
1449         else
1450                 priv->has_accounts = accounts_exist;
1451         
1452         return accounts_exist;
1453 }
1454
1455 static int
1456 compare_account_name(gconstpointer a, gconstpointer b)
1457 {
1458         const gchar* account_name = (const gchar*) a;
1459         const gchar* account_name2 = (const gchar*) b;
1460         return strcmp(account_name, account_name2);
1461 }
1462
1463 void 
1464 modest_account_mgr_set_account_busy(ModestAccountMgr* self, 
1465                                     const gchar* account_name, 
1466                                     gboolean busy)
1467 {
1468         ModestAccountMgrPrivate* priv;
1469
1470         g_return_if_fail (MODEST_IS_ACCOUNT_MGR(self));
1471         g_return_if_fail (account_name);
1472
1473         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1474         if (busy) {
1475                 GSList *account_names = modest_account_mgr_account_names (self, TRUE);
1476                 GSList* account = g_slist_find_custom(account_names, account_name, 
1477                                                       (GCompareFunc) compare_account_name);
1478
1479                 if (account && !modest_account_mgr_account_is_busy(self, account_name)) {
1480                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1481                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
1482                                        0, account_name, TRUE);
1483                 }
1484                 modest_account_mgr_free_account_names (account_names);
1485                 account_names = NULL;
1486         } else {
1487                 GSList* account = 
1488                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1489
1490                 if (account) {
1491                         g_free(account->data);
1492                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1493                         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_BUSY_SIGNAL], 
1494                                        0, account_name, FALSE);
1495                 }
1496         }
1497 }
1498
1499 gboolean
1500 modest_account_mgr_account_is_busy (ModestAccountMgr* self, const gchar* account_name)
1501 {
1502         ModestAccountMgrPrivate* priv;
1503         
1504         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1505         g_return_val_if_fail (account_name, FALSE);
1506
1507         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1508                 
1509         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1510                 != NULL);
1511 }
1512
1513 void
1514 modest_account_mgr_notify_account_update (ModestAccountMgr* self, 
1515                                           const gchar *server_account_name)
1516 {
1517         ModestProtocolType proto;
1518         ModestAccountMgrPrivate* priv;
1519         ModestProtocolRegistry *protocol_registry;
1520         gchar *proto_name = NULL;
1521
1522         g_return_if_fail (self);
1523         g_return_if_fail (server_account_name);
1524         
1525         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1526         protocol_registry = modest_runtime_get_protocol_registry ();
1527         
1528         /* Get protocol */
1529         proto_name = modest_account_mgr_get_string (self, server_account_name, 
1530                                                     MODEST_ACCOUNT_PROTO, TRUE);
1531         if (!proto_name) {
1532                 g_return_if_reached ();
1533                 return;
1534         }
1535         proto = modest_protocol_get_type_id (modest_protocol_registry_get_protocol_by_name (protocol_registry,
1536                                                                                             MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS,
1537                                                                                             proto_name));
1538         g_free (proto_name);
1539
1540         /* there is some update in the account, so we can't
1541          * be sure about whether there are still enabled accounts...
1542          */
1543         priv->has_enabled_accounts = FALSE;
1544         priv->has_accounts         = FALSE;
1545         
1546         /* Emit "update-account" */
1547         g_signal_emit (G_OBJECT(self), 
1548                        signals[ACCOUNT_CHANGED_SIGNAL], 0, 
1549                        server_account_name, 
1550                        (modest_protocol_registry_protocol_type_has_tag (protocol_registry, proto, MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) ? 
1551                        TNY_ACCOUNT_TYPE_STORE : 
1552                        TNY_ACCOUNT_TYPE_TRANSPORT);
1553 }
1554
1555
1556 gboolean
1557 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
1558 {
1559         ModestConf *conf;
1560         gboolean retval;
1561         
1562         g_return_val_if_fail (self,    FALSE);
1563         g_return_val_if_fail (account, FALSE);
1564         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
1565                               FALSE);
1566         
1567         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1568
1569         /* Change the default account and notify */
1570         retval = modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, account, NULL);
1571         if (retval)
1572                 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
1573
1574         return retval;
1575 }
1576
1577
1578 gchar*
1579 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
1580 {
1581         gchar *account; 
1582         ModestConf *conf;
1583         GError *err = NULL;
1584         
1585         g_return_val_if_fail (self, NULL);
1586
1587         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1588         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
1589         
1590         if (err) {
1591                 g_printerr ("modest: failed to get '%s': %s\n",
1592                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
1593                 g_error_free (err);
1594                 return  NULL;
1595         }
1596         
1597         /* sanity check */
1598         if (account && !modest_account_mgr_account_exists (self, account, FALSE)) {
1599                 g_printerr ("modest: default account does not exist\n");
1600                 g_free (account);
1601                 return NULL;
1602         }
1603
1604         return account;
1605 }
1606
1607 static gboolean
1608 modest_account_mgr_unset_default_account (ModestAccountMgr *self)
1609 {
1610         ModestConf *conf;
1611         gboolean retval;
1612         
1613         g_return_val_if_fail (self,    FALSE);
1614
1615         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
1616                 
1617         retval = modest_conf_remove_key (conf, MODEST_CONF_DEFAULT_ACCOUNT, NULL /* err */);
1618
1619         if (retval)
1620                 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
1621
1622         return retval;
1623 }
1624
1625
1626 gchar* 
1627 modest_account_mgr_get_display_name (ModestAccountMgr *self, 
1628                                      const gchar* name)
1629 {
1630         return modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
1631 }
1632
1633 void 
1634 modest_account_mgr_set_display_name (ModestAccountMgr *self, 
1635                                      const gchar *account_name,
1636                                      const gchar *display_name)
1637 {
1638         gboolean notify = TRUE;
1639
1640         if (!modest_account_mgr_get_display_name (self, account_name))
1641                 notify = FALSE;
1642
1643         modest_account_mgr_set_string (self, 
1644                                        account_name,
1645                                        MODEST_ACCOUNT_DISPLAY_NAME, 
1646                                        display_name, 
1647                                        FALSE /* not server account */);
1648
1649         /* Notify about the change in the display name */
1650         if (notify)
1651                 g_signal_emit (self, signals[DISPLAY_NAME_CHANGED_SIGNAL], 0, account_name);
1652 }