* ModestAccountMgr: the account manager is just a facade to the configuration...
[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-account-mgr.h>
33 #include <modest-account-mgr-priv.h>
34 #include <modest-account-mgr-helpers.h>
35
36 /* 'private'/'protected' functions */
37 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
38 static void modest_account_mgr_init       (ModestAccountMgr * obj);
39 static void modest_account_mgr_finalize   (GObject * obj);
40
41 /* list my signals */
42 enum {
43         ACCOUNT_CHANGED_SIGNAL,
44         ACCOUNT_REMOVED_SIGNAL,
45         ACCOUNT_BUSY_SIGNAL,
46         LAST_SIGNAL
47 };
48
49
50 /* globals */
51 static GObjectClass *parent_class = NULL;
52 static guint signals[LAST_SIGNAL] = {0};
53
54 /* We signal key changes in batches, every X seconds: */
55 static gboolean
56 on_timeout_notify_changes (gpointer data)
57 {
58         ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
59         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
60                 
61         /* TODO: Also store the account names, and notify one list for each account,
62          * if anything uses the account names. */
63         
64         if (priv->changed_conf_keys) {
65                 gchar *default_account = 
66                                 modest_account_mgr_get_default_account (self);
67                 
68                 /* printf ("DEBUG: %s: priv->changed_conf_key length=%d\n", 
69                         __FUNCTION__, g_slist_length (priv->changed_conf_keys)); */
70                 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
71                                  default_account, priv->changed_conf_keys, FALSE);
72                         
73                 g_free (default_account);
74                 
75                 g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
76                 g_slist_free (priv->changed_conf_keys);
77                 priv->changed_conf_keys = NULL;
78         }
79         
80         return TRUE; /* Call this again later. */
81 }
82
83 static void
84 on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpointer user_data)
85 {
86         ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data);
87         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
88         gboolean is_account_key;
89         gboolean is_server_account;
90         gchar* account = NULL;
91
92         /* there is only one not-really-account key which will still emit
93          * a signal: a change in MODEST_CONF_DEFAULT_ACCOUNT */
94         if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
95                 /* Get the default account instead. */
96                 
97                 /* Store the key for later notification in our timeout callback.
98                  * Notifying for every key change would cause unnecessary work: */
99                 priv->changed_conf_keys = g_slist_append (priv->changed_conf_keys,
100                         (gpointer) g_strdup (key));
101         }
102         
103         is_account_key = FALSE;
104         is_server_account = FALSE;
105         account = _modest_account_mgr_account_from_key (key, &is_account_key, 
106                                                         &is_server_account);
107
108         /* if this is not an account-related key change, ignore */
109         if (!account)
110                 return;
111
112         /* account was removed. Do not emit an account removed signal
113            because it was already being done in the remove_account
114            method. Do not notify also the removal of the server
115            account keys for the same reason */
116         if ((is_account_key || is_server_account) && 
117             event == MODEST_CONF_EVENT_KEY_UNSET) {
118                 g_free (account);
119                 return;
120         }
121
122         /* is this account enabled? */
123         gboolean enabled = FALSE;
124         if (is_server_account)
125                 enabled = TRUE;
126         else
127                 enabled = modest_account_mgr_get_enabled (self, account);
128
129         /* Notify is server account was changed, default account was changed
130          * or when enabled/disabled changes:
131          */
132         if (enabled ||
133             g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) ||
134             strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
135                 /* Store the key for later notification in our timeout callback.
136                  * Notifying for every key change would cause unnecessary work: */
137                 priv->changed_conf_keys = g_slist_append (NULL,
138                         (gpointer) g_strdup (key));
139         }
140         g_free (account);
141 }
142
143
144 GType
145 modest_account_mgr_get_type (void)
146 {
147         static GType my_type = 0;
148
149         if (!my_type) {
150                 static const GTypeInfo my_info = {
151                         sizeof (ModestAccountMgrClass),
152                         NULL,   /* base init */
153                         NULL,   /* base finalize */
154                         (GClassInitFunc) modest_account_mgr_class_init,
155                         NULL,   /* class finalize */
156                         NULL,   /* class data */
157                         sizeof (ModestAccountMgr),
158                         1,      /* n_preallocs */
159                         (GInstanceInitFunc) modest_account_mgr_init,
160                         NULL
161                 };
162
163                 my_type = g_type_register_static (G_TYPE_OBJECT,
164                                                   "ModestAccountMgr",
165                                                   &my_info, 0);
166         }
167         return my_type;
168 }
169
170 static void
171 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
172 {
173         GObjectClass *gobject_class;
174         gobject_class = (GObjectClass *) klass;
175
176         parent_class = g_type_class_peek_parent (klass);
177         gobject_class->finalize = modest_account_mgr_finalize;
178
179         g_type_class_add_private (gobject_class,
180                                   sizeof (ModestAccountMgrPrivate));
181
182         /* signal definitions */
183         signals[ACCOUNT_REMOVED_SIGNAL] =
184                 g_signal_new ("account_removed",
185                               G_TYPE_FROM_CLASS (klass),
186                               G_SIGNAL_RUN_FIRST,
187                               G_STRUCT_OFFSET(ModestAccountMgrClass,account_removed),
188                               NULL, NULL,
189                               g_cclosure_marshal_VOID__STRING,
190                               G_TYPE_NONE, 1, G_TYPE_STRING);
191         signals[ACCOUNT_CHANGED_SIGNAL] =
192                 g_signal_new ("account_changed",
193                                G_TYPE_FROM_CLASS (klass),
194                               G_SIGNAL_RUN_FIRST,
195                               G_STRUCT_OFFSET(ModestAccountMgrClass,account_changed),
196                               NULL, NULL,
197                               modest_marshal_VOID__STRING_POINTER_BOOLEAN,
198                               G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
199         signals[ACCOUNT_BUSY_SIGNAL] =
200                 g_signal_new ("account_busy_changed",
201                               G_TYPE_FROM_CLASS (klass),
202                               G_SIGNAL_RUN_FIRST,
203                               G_STRUCT_OFFSET(ModestAccountMgrClass,account_busy_changed),
204                               NULL, NULL,
205                               modest_marshal_VOID__STRING_BOOLEAN,
206                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
207 }
208
209
210 static void
211 modest_account_mgr_init (ModestAccountMgr * obj)
212 {
213         ModestAccountMgrPrivate *priv =
214                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
215
216         priv->modest_conf = NULL;
217         priv->busy_accounts = NULL;
218         priv->timeout = g_timeout_add (1000 /* milliseconds */, on_timeout_notify_changes, obj);
219 }
220
221 static void
222 modest_account_mgr_finalize (GObject * obj)
223 {
224         ModestAccountMgrPrivate *priv = 
225                 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
226
227         if (priv->key_changed_handler_uid) {
228                 g_signal_handler_disconnect (priv->modest_conf, 
229                                              priv->key_changed_handler_uid);
230                 priv->key_changed_handler_uid = 0;
231         }
232
233         if (priv->modest_conf) {
234                 g_object_unref (G_OBJECT(priv->modest_conf));
235                 priv->modest_conf = NULL;
236         }
237         
238         if (priv->timeout)
239                 g_source_remove (priv->timeout);
240                 
241         if (priv->changed_conf_keys) {
242                 g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
243                 g_slist_free (priv->changed_conf_keys);
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         priv->key_changed_handler_uid =
265                 g_signal_connect (G_OBJECT (conf), "key_changed",
266                                   G_CALLBACK (on_key_change),
267                                   obj);
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
280 gboolean
281 modest_account_mgr_add_account (ModestAccountMgr *self,
282                                 const gchar *name,
283                                 const gchar *store_account,
284                                 const gchar *transport_account,
285                                 gboolean enabled)
286 {
287         ModestAccountMgrPrivate *priv;
288         gchar *key;
289         gboolean ok;
290         gchar *default_account;
291         GError *err = NULL;
292         
293         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
294         g_return_val_if_fail (name, FALSE);
295         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
296         
297         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
298
299         /*
300          * we create the account by adding an account 'dir', with the name <name>,
301          * and in that the 'display_name' string key
302          */
303         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
304         if (modest_account_mgr_account_exists (self, key, FALSE)) {
305                 g_printerr ("modest: account already exists\n");
306                 g_free (key);
307                 return FALSE;
308         }
309         
310         ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
311         g_free (key);
312         if (!ok) {
313                 g_printerr ("modest: cannot set display name\n");
314                 if (err) {
315                         g_printerr ("modest: Error adding account conf: %s\n", err->message);
316                         g_error_free (err);
317                 }
318                 return FALSE;
319         }
320         
321         if (store_account) {
322                 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
323                 ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
324                 g_free (key);
325                 if (!ok) {
326                         g_printerr ("modest: failed to set store account '%s'\n",
327                                 store_account);
328                         if (err) {
329                                 g_printerr ("modest: Error adding store account conf: %s\n", err->message);
330                                 g_error_free (err);
331                         }
332                         return FALSE;
333                 }
334         }
335         
336         if (transport_account) {
337                 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
338                                                                FALSE);
339                 ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
340                 g_free (key);
341                 if (!ok) {
342                         g_printerr ("modest: failed to set transport account '%s'\n",
343                                     transport_account);
344                         if (err) {
345                                 g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
346                                 g_error_free (err);
347                         }       
348                         return FALSE;
349                 }
350         }
351
352         /* Make sure that leave-messages-on-server is enabled by default, 
353          * as per the UI spec, though it is only meaningful for accounts using POP.
354          * (possibly this gconf key should be under the server account): */
355         modest_account_mgr_set_bool (self, name,
356                 MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE /* not server account */);
357
358
359         modest_account_mgr_set_enabled (self, name, enabled);
360
361         /* if no default account has been defined yet, do so now */
362         default_account = modest_account_mgr_get_default_account (self);
363         if (!default_account)
364                 modest_account_mgr_set_default_account (self, name);
365         g_free (default_account);
366
367         return TRUE;
368 }
369
370
371 gboolean
372 modest_account_mgr_add_server_account (ModestAccountMgr * self,
373                                        const gchar * name, const gchar *hostname,
374                                        guint portnumber,
375                                        const gchar * username, const gchar * password,
376                                        ModestTransportStoreProtocol proto,
377                                        ModestConnectionProtocol security,
378                                        ModestAuthProtocol auth)
379 {
380         ModestAccountMgrPrivate *priv;
381         gchar *key;
382         gboolean ok = TRUE;
383         GError *err = NULL;
384
385         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
386         g_return_val_if_fail (name, FALSE);
387         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
388                               
389         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
390
391         /* hostname */
392         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
393         if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
394                 g_printerr ("modest: server account '%s' already exists\n", name);
395                 g_free (key);
396                 ok =  FALSE;
397         }
398         if (!ok)
399                 goto cleanup;
400         
401         modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &err);
402         if (err) {
403                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
404                 g_error_free (err);
405                 ok = FALSE;
406         }
407         g_free (key);
408         if (!ok)
409                 goto cleanup;
410         
411         /* username */
412         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_USERNAME, TRUE);
413         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
414         if (err) {
415                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
416                 g_error_free (err);
417                 ok = FALSE;
418         }
419         g_free (key);
420         if (!ok)
421                 goto cleanup;
422         
423         
424         /* password */
425         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PASSWORD, TRUE);
426         ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
427         if (err) {
428                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
429                 g_error_free (err);
430                 ok = FALSE;
431         }
432         g_free (key);
433         if (!ok)
434                 goto cleanup;
435
436         /* proto */
437         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
438         ok = modest_conf_set_string (priv->modest_conf, key,
439                                      modest_protocol_info_get_transport_store_protocol_name(proto),
440                                      &err);
441         if (err) {
442                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
443                 g_error_free (err);
444                 ok = FALSE;
445         }
446         g_free (key);
447         if (!ok)
448                 goto cleanup;
449
450
451         /* portnumber */
452         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PORT, TRUE);
453         ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
454         if (err) {
455                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
456                 g_error_free (err);
457                 ok = FALSE;
458         }
459         g_free (key);
460         if (!ok)
461                 goto cleanup;
462
463         
464         /* auth mechanism */
465         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
466         ok = modest_conf_set_string (priv->modest_conf, key,
467                                      modest_protocol_info_get_auth_protocol_name (auth),
468                                      &err);
469         if (err) {
470                 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
471                 g_error_free (err);
472                 ok = FALSE;
473         }
474         g_free (key);
475         if (!ok)
476                 goto cleanup;
477         
478         /* Add the security settings: */
479         modest_server_account_set_security (self, name, security);
480         
481 cleanup:
482         if (!ok) {
483                 g_printerr ("modest: failed to add server account\n");
484                 return FALSE;
485         }
486
487         return TRUE;
488 }
489
490 /** modest_account_mgr_add_server_account_uri:
491  * Only used for mbox and maildir accounts.
492  */
493 gboolean
494 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
495                                            const gchar *name, ModestTransportStoreProtocol proto,
496                                            const gchar *uri)
497 {
498         ModestAccountMgrPrivate *priv;
499         gchar *key;
500         gboolean ok;
501         
502         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
503         g_return_val_if_fail (name, FALSE);
504         g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
505         g_return_val_if_fail (uri, FALSE);
506         
507         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
508         
509         
510         /* proto */
511         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
512         ok = modest_conf_set_string (priv->modest_conf, key,
513                                      modest_protocol_info_get_transport_store_protocol_name(proto),
514                                      NULL);
515         g_free (key);
516
517         if (!ok) {
518                 g_printerr ("modest: failed to set proto\n");
519                 return FALSE;
520         }
521         
522         /* uri */
523         key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_URI, TRUE);
524         ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
525         g_free (key);
526
527         if (!ok) {
528                 g_printerr ("modest: failed to set uri\n");
529                 return FALSE;
530         }
531         return TRUE;
532 }
533
534 /* 
535  * Utility function used by modest_account_mgr_remove_account
536  */
537 static void
538 real_remove_account (ModestConf *conf,
539                      const gchar *acc_name,
540                      gboolean server_account)
541 {
542         GError *err = NULL;
543         gchar *key = NULL;
544
545         key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
546         modest_conf_remove_key (conf, key, &err);
547         g_free (key);       
548
549         if (err) {
550                 g_printerr ("modest: error removing key: %s\n", err->message);
551                 g_error_free (err);
552         }
553 }
554
555 gboolean
556 modest_account_mgr_remove_account (ModestAccountMgr * self,
557                                    const gchar* name)
558 {
559         ModestAccountMgrPrivate *priv;
560         gchar *default_account_name, *store_acc_name, *transport_acc_name;
561         gboolean default_account_deleted;
562
563         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
564         g_return_val_if_fail (name, FALSE);
565
566         if (!modest_account_mgr_account_exists (self, name, FALSE)) {
567                 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
568                 return FALSE;
569         }
570
571         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
572         default_account_deleted = FALSE;
573
574         /* If this was the default, then remove that setting: */
575         default_account_name = modest_account_mgr_get_default_account (self);
576         if (default_account_name && (strcmp (default_account_name, name) == 0)) {
577                 modest_account_mgr_unset_default_account (self);
578                 default_account_deleted = TRUE;
579         }
580         g_free (default_account_name);
581
582         /* Delete transport and store accounts */
583         store_acc_name = modest_account_mgr_get_string (self, name, 
584                                                         MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
585         if (store_acc_name)
586                 real_remove_account (priv->modest_conf, store_acc_name, TRUE);
587
588         transport_acc_name = modest_account_mgr_get_string (self, name, 
589                                                             MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
590         if (transport_acc_name)
591                 real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
592                         
593         /* Remove the modest account */
594         real_remove_account (priv->modest_conf, name, FALSE);
595
596         if (default_account_deleted) {  
597                 /* pick another one as the new default account. We do
598                    this *after* deleting the keys, because otherwise a
599                    call to account_names will retrieve also the
600                    deleted account */
601                 modest_account_mgr_set_first_account_as_default (self);
602         }
603         
604         /* Notify the observers. We do this *after* deleting
605            the keys, because otherwise a call to account_names
606            will retrieve also the deleted account */
607         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
608
609         return TRUE;
610 }
611
612
613
614 /* strip the first /n/ character from each element
615  * caller must make sure all elements are strings with
616  * length >= n, and also that data can be freed.
617  * change is in-place
618  */
619 static void
620 strip_prefix_from_elements (GSList * lst, guint n)
621 {
622         while (lst) {
623                 memmove (lst->data, lst->data + n,
624                          strlen(lst->data) - n + 1);
625                 lst = lst->next;
626         }
627 }
628
629
630 GSList*
631 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
632 {
633         GSList *accounts;
634         ModestAccountMgrPrivate *priv;
635         GError *err = NULL;
636         
637         const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
638
639         g_return_val_if_fail (self, NULL);
640
641         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
642         accounts = modest_conf_list_subkeys (priv->modest_conf,
643                                              MODEST_ACCOUNT_NAMESPACE, &err);
644
645         if (err) {
646                 g_printerr ("modest: failed to get subkeys (%s): %s\n",
647                             MODEST_ACCOUNT_NAMESPACE, err->message);
648                 g_error_free (err);
649                 return NULL; /* assume accounts did not get value when err is set...*/
650         }
651         
652         strip_prefix_from_elements (accounts, prefix_len);
653                 
654         GSList *result = NULL;
655         
656         /* Unescape the keys to get the account names: */
657         GSList *iter = accounts;
658         while (iter) {
659                 if (!(iter->data))
660                         continue;
661                         
662                 const gchar* account_name_key = (const gchar*)iter->data;
663                 gchar* unescaped_name = account_name_key ? 
664                         modest_conf_key_unescape (account_name_key) 
665                         : NULL;
666                 
667                 gboolean add = TRUE;
668                 if (only_enabled) {
669                         if (unescaped_name && 
670                                 !modest_account_mgr_get_enabled (self, unescaped_name)) {
671                                 add = FALSE;
672                         }
673                 }
674                 
675                 /* Ignore modest accounts whose server accounts don't exist: 
676                  * (We could be getting this list while the account is being deleted, 
677                  * while the child server accounts have already been deleted, but the 
678                  * parent modest account already exists.
679                  */
680                 if (add) {
681                         gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
682                                                                             FALSE);
683                         if (server_account_name) {
684                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
685                                         add = FALSE;
686                                 g_free (server_account_name);
687                         }
688                 }
689                 
690                 if (add) {
691                         gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
692                                                                             FALSE);
693                         if (server_account_name) {
694                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
695                                         add = FALSE;
696                                 g_free (server_account_name);
697                         }
698                 }
699                 
700                 if (add)        
701                         result = g_slist_append (result, unescaped_name);
702                 else 
703                         g_free (unescaped_name);
704
705                 g_free (iter->data);
706                 iter->data = NULL;
707                 
708                 iter = g_slist_next (iter);     
709         }
710         
711
712         /* we already freed the strings in the loop */
713         g_slist_free (accounts);
714         
715         accounts = NULL;
716         return result;
717 }
718
719
720 void
721 modest_account_mgr_free_account_names (GSList *account_names)
722 {
723         g_slist_foreach (account_names, (GFunc)g_free, NULL);
724         g_slist_free (account_names);
725 }
726
727
728
729 gchar *
730 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
731                                const gchar *key, gboolean server_account) {
732
733         ModestAccountMgrPrivate *priv;
734
735         gchar *keyname;
736         gchar *retval;
737         GError *err = NULL;
738
739         g_return_val_if_fail (self, NULL);
740         g_return_val_if_fail (name, NULL);
741         g_return_val_if_fail (key, NULL);
742
743         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
744         
745         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
746         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
747         if (err) {
748                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
749                 g_error_free (err);
750                 retval = NULL;
751         }
752         g_free (keyname);
753
754         return retval;
755 }
756
757
758 gchar *
759 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
760                                const gchar *key, gboolean server_account)
761 {
762         return modest_account_mgr_get_string (self, name, key, server_account);
763
764 }
765
766
767
768 gint
769 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
770                             gboolean server_account)
771 {
772         ModestAccountMgrPrivate *priv;
773
774         gchar *keyname;
775         gint retval;
776         GError *err = NULL;
777         
778         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
779         g_return_val_if_fail (name, -1);
780         g_return_val_if_fail (key, -1);
781
782         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
783
784         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
785         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
786         if (err) {
787                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
788                 g_error_free (err);
789                 retval = -1;
790         }
791         g_free (keyname);
792
793         return retval;
794 }
795
796
797
798 gboolean
799 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
800                              const gchar * key, gboolean server_account)
801 {
802         ModestAccountMgrPrivate *priv;
803
804         gchar *keyname;
805         gboolean retval;
806         GError *err = NULL;
807
808         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
809         g_return_val_if_fail (account, FALSE);
810         g_return_val_if_fail (key, FALSE);
811
812         keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
813         
814         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
815         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
816         if (err) {
817                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
818                 g_error_free (err);
819                 retval = FALSE;
820         }
821         g_free (keyname);
822
823         return retval;
824 }
825
826
827
828 GSList * 
829 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
830                              const gchar *key, ModestConfValueType list_type,
831                              gboolean server_account)
832 {
833         ModestAccountMgrPrivate *priv;
834
835         gchar *keyname;
836         GSList *retval;
837         GError *err = NULL;
838         
839         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
840         g_return_val_if_fail (name, NULL);
841         g_return_val_if_fail (key, NULL);
842
843         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
844         
845         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
846         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
847         if (err) {
848                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
849                             err->message);
850                 g_error_free (err);
851                 retval = FALSE;
852         }
853         g_free (keyname);
854
855         return retval;
856 }
857
858
859 gboolean
860 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
861                                const gchar * key, const gchar * val, gboolean server_account)
862 {
863         ModestAccountMgrPrivate *priv;
864
865         gchar *keyname;
866         gboolean retval;
867         GError *err = NULL;
868
869         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
870         g_return_val_if_fail (name, FALSE);
871         g_return_val_if_fail (key, FALSE);
872
873         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
874         
875         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
876
877         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
878         if (err) {
879                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
880                 g_error_free (err);
881                 retval = FALSE;
882         }
883         g_free (keyname);       
884         return retval;
885 }
886
887
888 gboolean
889 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
890                                  const gchar * key, const gchar * val, gboolean server_account)
891 {
892         return modest_account_mgr_set_password (self, name, key, val, server_account);
893 }
894
895
896
897 gboolean
898 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
899                             const gchar * key, int val, gboolean server_account)
900 {
901         ModestAccountMgrPrivate *priv;
902
903         gchar *keyname;
904         gboolean retval;
905         GError *err = NULL;
906         
907         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
908         g_return_val_if_fail (name, FALSE);
909         g_return_val_if_fail (key, FALSE);
910
911         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
912         
913         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
914
915         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
916         if (err) {
917                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
918                 g_error_free (err);
919                 retval = FALSE;
920         }
921         g_free (keyname);
922         return retval;
923 }
924
925
926
927 gboolean
928 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
929                              const gchar * key, gboolean val, gboolean server_account)
930 {
931         ModestAccountMgrPrivate *priv;
932
933         gchar *keyname;
934         gboolean retval;
935         GError *err = NULL;
936
937         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
938         g_return_val_if_fail (name, FALSE);
939         g_return_val_if_fail (key, FALSE);
940
941         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
942
943         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
944
945         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
946         if (err) {
947                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
948                 g_error_free (err);
949                 retval = FALSE;
950         }
951         g_free (keyname);
952         return retval;
953 }
954
955
956 gboolean
957 modest_account_mgr_set_list (ModestAccountMgr *self,
958                              const gchar *name,
959                              const gchar *key,
960                              GSList *val,
961                              ModestConfValueType list_type,
962                              gboolean server_account)
963 {
964         ModestAccountMgrPrivate *priv;
965         gchar *keyname;
966         GError *err = NULL;
967         gboolean retval;
968         
969         g_return_val_if_fail (self, FALSE);
970         g_return_val_if_fail (name, FALSE);
971         g_return_val_if_fail (key,  FALSE);
972         g_return_val_if_fail (val,  FALSE);
973
974         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
975         
976         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
977         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
978         if (err) {
979                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
980                 g_error_free (err);
981                 retval = FALSE;
982         }
983         g_free (keyname);
984
985         return retval;
986 }
987
988 gboolean
989 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
990                                    gboolean server_account)
991 {
992         ModestAccountMgrPrivate *priv;
993
994         gchar *keyname;
995         gboolean retval;
996         GError *err = NULL;
997
998         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
999         g_return_val_if_fail (name, FALSE);
1000
1001         keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
1002         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1003         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1004         if (err) {
1005                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1006                             err->message);
1007                 g_error_free (err);
1008                 retval = FALSE;
1009         }
1010         g_free (keyname);
1011         return retval;
1012 }
1013
1014 gboolean
1015 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, const gchar *display_name)
1016 {
1017         GSList *account_names = NULL;
1018         GSList *cursor = NULL;
1019         
1020         cursor = account_names = modest_account_mgr_account_names (self, 
1021                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1022
1023         gboolean found = FALSE;
1024         
1025         /* Look at each non-server account to check their display names; */
1026         while (cursor) {
1027                 const gchar * account_name = (gchar*)cursor->data;
1028                 
1029                 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1030                 if (!account_data) {
1031                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1032                         continue;
1033                 }
1034
1035                 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1036                         found = TRUE;
1037                         break;
1038                 }
1039
1040                 modest_account_mgr_free_account_data (self, account_data);
1041                 cursor = cursor->next;
1042         }
1043         modest_account_mgr_free_account_names (account_names);
1044         account_names = NULL;
1045         
1046         return found;
1047 }
1048
1049
1050
1051
1052 gboolean 
1053 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1054                           const gchar *key, gboolean server_account)
1055 {
1056         ModestAccountMgrPrivate *priv;
1057         
1058         gchar *keyname;
1059         gboolean retval;
1060         GError *err = NULL;
1061         
1062         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1063         g_return_val_if_fail (name, FALSE);
1064         g_return_val_if_fail (key, FALSE);
1065
1066         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1067
1068         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1069         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1070         if (err) {
1071                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1072                             err->message);
1073                 g_error_free (err);
1074                 retval = FALSE;
1075         }
1076         g_free (keyname);
1077         return retval;
1078 }
1079
1080 gchar*
1081 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1082 {
1083         /* Initialize input parameters: */
1084         if (is_account_key)
1085                 *is_account_key = FALSE;
1086
1087         if (is_server_account)
1088                 *is_server_account = FALSE;
1089
1090         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1091         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1092         gchar *cursor;
1093         gchar *account = NULL;
1094
1095         /* determine whether it's an account or a server account,
1096          * based on the prefix */
1097         if (g_str_has_prefix (key, account_ns)) {
1098
1099                 if (is_server_account)
1100                         *is_server_account = FALSE;
1101                 
1102                 account = g_strdup (key + strlen (account_ns));
1103
1104         } else if (g_str_has_prefix (key, server_account_ns)) {
1105
1106                 if (is_server_account)
1107                         *is_server_account = TRUE;
1108                 
1109                 account = g_strdup (key + strlen (server_account_ns));  
1110         } else
1111                 return NULL;
1112
1113         /* if there are any slashes left in the key, it's not
1114          * the toplevel entry for an account
1115          */
1116         cursor = strstr(account, "/");
1117         
1118         if (is_account_key && cursor)
1119                 *is_account_key = TRUE;
1120
1121         /* put a NULL where the first slash was */
1122         if (cursor)
1123                 *cursor = '\0';
1124
1125         if (account) {
1126                 /* The key is an escaped string, so unescape it to get the actual account name: */
1127                 gchar *unescaped_name = modest_conf_key_unescape (account);
1128                 g_free (account);
1129                 return unescaped_name;
1130         } else
1131                 return NULL;
1132 }
1133
1134
1135
1136 /* must be freed by caller */
1137 gchar *
1138 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
1139 {
1140         gchar *retval = NULL;
1141         
1142         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1143         
1144         if (!account_name)
1145                 return g_strdup (namespace);
1146         
1147         /* Always escape the conf keys, so that it is acceptable to gconf: */
1148         gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
1149         gchar *escaped_name =  name ? modest_conf_key_escape (name) : NULL;
1150
1151         if (escaped_account_name && escaped_name)
1152                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1153         else if (escaped_account_name)
1154                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1155
1156         /* Sanity check: */
1157         if (!modest_conf_key_is_valid (retval)) {
1158                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1159                 g_free (retval);
1160                 retval = NULL;
1161         }
1162
1163         g_free (escaped_name);
1164         g_free (escaped_account_name);
1165
1166         return retval;
1167 }
1168
1169 gboolean
1170 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1171 {
1172         /* Check that at least one account exists: */
1173         GSList *account_names = modest_account_mgr_account_names (self,
1174                                                                   enabled);
1175         gboolean accounts_exist = account_names != NULL;
1176         
1177         modest_account_mgr_free_account_names (account_names);
1178         account_names = NULL;
1179         
1180         return accounts_exist;
1181 }
1182
1183 static int
1184 compare_account_name(gconstpointer a, gconstpointer b)
1185 {
1186         const gchar* account_name = (const gchar*) a;
1187         const gchar* account_name2 = (const gchar*) b;
1188         return strcmp(account_name, account_name2);
1189 }
1190
1191 void 
1192 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
1193                                                                                                                                                 gboolean busy)
1194 {
1195         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1196         if (busy)
1197         {
1198                 GSList *account_names = modest_account_mgr_account_names (self,
1199                                 TRUE);
1200                 GSList* account = 
1201                         g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1202                 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1203                 {
1204                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1205                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1206                 }
1207                 modest_account_mgr_free_account_names (account_names);
1208                 account_names = NULL;
1209         } else {
1210                 GSList* account = 
1211                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1212                 if (account)
1213                 {
1214                         g_free(account->data);
1215                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1216                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1217                 }
1218         }
1219 }
1220
1221 gboolean
1222 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1223 {
1224         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1225         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1226                                         != NULL);
1227 }
1228