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