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