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