2007-07-12 Murray Cumming <murrayc@murrayc.com>
[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                               modest_marshal_VOID__STRING_BOOLEAN,
190                               G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
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 gboolean
535 modest_account_mgr_remove_account (ModestAccountMgr * self,
536                                    const gchar* name,  gboolean server_account)
537 {
538         ModestAccountMgrPrivate *priv;
539         gchar *key;
540         gboolean retval, default_account_deleted;
541         GError *err = NULL;
542
543         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
544         g_return_val_if_fail (name, FALSE);
545
546         if (!modest_account_mgr_account_exists (self, name, server_account)) {
547                 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
548                 return FALSE;
549         }
550
551         default_account_deleted = FALSE;
552
553         if (!server_account) {
554                 gchar *server_account_name, *default_account_name;
555
556                 /* If this was the default, then remove that setting: */
557                 default_account_name = modest_account_mgr_get_default_account (self);
558                 if (default_account_name && (strcmp (default_account_name, name) == 0)) {
559                         modest_account_mgr_unset_default_account (self);
560                         default_account_deleted = TRUE;
561                 }
562                 g_free (default_account_name);
563
564                 /* in case we're deleting an account, also delete the dependent store and transport account */
565                 server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_STORE_ACCOUNT,
566                                                                     FALSE);
567                 if (server_account_name) {
568                         if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
569                                 g_printerr ("modest: failed to remove store account '%s' (%s)\n",
570                                             server_account_name, name);
571                         g_free (server_account_name);
572                 } else
573                         g_printerr ("modest: could not find the store account for %s\n", name);
574                 
575                 server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
576                                                                     FALSE);
577                 if (server_account_name) {
578                         if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
579                                 g_printerr ("modest: failed to remove transport account '%s' (%s)\n",
580                                             server_account_name, name);
581                         g_free (server_account_name);
582                 } else
583                         g_printerr ("modest: could not find the transport account for %s\n", name);
584         }
585                         
586         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
587         key = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
588         
589         retval = modest_conf_remove_key (priv->modest_conf, key, &err);
590         g_free (key);
591
592         if (err) {
593                 g_printerr ("modest: error removing key: %s\n", err->message);
594                 g_error_free (err);
595         }
596
597         if (default_account_deleted) {  
598                 /* pick another one as the new default account. We do
599                    this *after* deleting the keys, because otherwise a
600                    call to account_names will retrieve also the
601                    deleted account */
602                 modest_account_mgr_set_first_account_as_default (self);
603         }
604
605         if (server_account) {
606                 /* Notify the observers. We do this *after* deleting
607                    the keys, because otherwise a call to account_names
608                    will retrieve also the deleted account */
609                 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0,
610                                name, server_account);
611         }
612
613         return retval;
614 }
615
616
617
618 /* strip the first /n/ character from each element
619  * caller must make sure all elements are strings with
620  * length >= n, and also that data can be freed.
621  * change is in-place
622  */
623 static void
624 strip_prefix_from_elements (GSList * lst, guint n)
625 {
626         while (lst) {
627                 memmove (lst->data, lst->data + n,
628                          strlen(lst->data) - n + 1);
629                 lst = lst->next;
630         }
631 }
632
633
634 GSList*
635 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
636 {
637         GSList *accounts;
638         ModestAccountMgrPrivate *priv;
639         GError *err = NULL;
640         
641         const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
642
643         g_return_val_if_fail (self, NULL);
644
645         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
646         accounts = modest_conf_list_subkeys (priv->modest_conf,
647                                              MODEST_ACCOUNT_NAMESPACE, &err);
648
649         if (err) {
650                 g_printerr ("modest: failed to get subkeys (%s): %s\n",
651                             MODEST_ACCOUNT_NAMESPACE, err->message);
652                 g_error_free (err);
653                 return NULL; /* assume accounts did not get value when err is set...*/
654         }
655         
656         strip_prefix_from_elements (accounts, prefix_len);
657                 
658         GSList *result = NULL;
659         
660         /* Unescape the keys to get the account names: */
661         GSList *iter = accounts;
662         while (iter) {
663                 if (!(iter->data))
664                         continue;
665                         
666                 const gchar* account_name_key = (const gchar*)iter->data;
667                 gchar* unescaped_name = account_name_key ? 
668                         modest_conf_key_unescape (account_name_key) 
669                         : NULL;
670                 
671                 gboolean add = TRUE;
672                 if (only_enabled) {
673                         if (unescaped_name && 
674                                 !modest_account_mgr_get_enabled (self, unescaped_name)) {
675                                 add = FALSE;
676                         }
677                 }
678                 
679                 if (add)        
680                         result = g_slist_append (result, unescaped_name);
681                 else 
682                         g_free (unescaped_name);
683
684                 g_free (iter->data);
685                 iter->data = NULL;
686                 
687                 iter = g_slist_next (iter);     
688         }
689         
690
691         /* we already freed the strings in the loop */
692         g_slist_free (accounts);
693         
694         accounts = NULL;
695         return result;
696 }
697
698
699 void
700 modest_account_mgr_free_account_names (GSList *account_names)
701 {
702         g_slist_foreach (account_names, (GFunc)g_free, NULL);
703         g_slist_free (account_names);
704 }
705
706
707
708 gchar *
709 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
710                                const gchar *key, gboolean server_account) {
711
712         ModestAccountMgrPrivate *priv;
713
714         gchar *keyname;
715         gchar *retval;
716         GError *err = NULL;
717
718         g_return_val_if_fail (self, NULL);
719         g_return_val_if_fail (name, NULL);
720         g_return_val_if_fail (key, NULL);
721
722         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
723         
724         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
725         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
726         if (err) {
727                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
728                 g_error_free (err);
729                 retval = NULL;
730         }
731         g_free (keyname);
732
733         return retval;
734 }
735
736
737 gchar *
738 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
739                                const gchar *key, gboolean server_account)
740 {
741         return modest_account_mgr_get_string (self, name, key, server_account);
742
743 }
744
745
746
747 gint
748 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
749                             gboolean server_account)
750 {
751         ModestAccountMgrPrivate *priv;
752
753         gchar *keyname;
754         gint retval;
755         GError *err = NULL;
756         
757         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
758         g_return_val_if_fail (name, -1);
759         g_return_val_if_fail (key, -1);
760
761         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
762
763         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
764         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
765         if (err) {
766                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
767                 g_error_free (err);
768                 retval = -1;
769         }
770         g_free (keyname);
771
772         return retval;
773 }
774
775
776
777 gboolean
778 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
779                              const gchar * key, gboolean server_account)
780 {
781         ModestAccountMgrPrivate *priv;
782
783         gchar *keyname;
784         gboolean retval;
785         GError *err = NULL;
786
787         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
788         g_return_val_if_fail (account, FALSE);
789         g_return_val_if_fail (key, FALSE);
790
791         keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
792         
793         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
794         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
795         if (err) {
796                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
797                 g_error_free (err);
798                 retval = FALSE;
799         }
800         g_free (keyname);
801
802         return retval;
803 }
804
805
806
807 GSList * 
808 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
809                              const gchar *key, ModestConfValueType list_type,
810                              gboolean server_account)
811 {
812         ModestAccountMgrPrivate *priv;
813
814         gchar *keyname;
815         GSList *retval;
816         GError *err = NULL;
817         
818         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
819         g_return_val_if_fail (name, NULL);
820         g_return_val_if_fail (key, NULL);
821
822         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
823         
824         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
825         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
826         if (err) {
827                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
828                             err->message);
829                 g_error_free (err);
830                 retval = FALSE;
831         }
832         g_free (keyname);
833
834         return retval;
835 }
836
837
838 gboolean
839 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
840                                const gchar * key, const gchar * val, gboolean server_account)
841 {
842         ModestAccountMgrPrivate *priv;
843
844         gchar *keyname;
845         gboolean retval;
846         GError *err = NULL;
847
848         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
849         g_return_val_if_fail (name, FALSE);
850         g_return_val_if_fail (key, FALSE);
851
852         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
853         
854         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
855
856         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
857         if (err) {
858                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
859                 g_error_free (err);
860                 retval = FALSE;
861         }
862         g_free (keyname);       
863         return retval;
864 }
865
866
867 gboolean
868 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
869                                  const gchar * key, const gchar * val, gboolean server_account)
870 {
871         return modest_account_mgr_set_password (self, name, key, val, server_account);
872 }
873
874
875
876 gboolean
877 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
878                             const gchar * key, int val, gboolean server_account)
879 {
880         ModestAccountMgrPrivate *priv;
881
882         gchar *keyname;
883         gboolean retval;
884         GError *err = NULL;
885         
886         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
887         g_return_val_if_fail (name, FALSE);
888         g_return_val_if_fail (key, FALSE);
889
890         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
891         
892         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
893
894         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
895         if (err) {
896                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
897                 g_error_free (err);
898                 retval = FALSE;
899         }
900         g_free (keyname);
901         return retval;
902 }
903
904
905
906 gboolean
907 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
908                              const gchar * key, gboolean val, gboolean server_account)
909 {
910         ModestAccountMgrPrivate *priv;
911
912         gchar *keyname;
913         gboolean retval;
914         GError *err = NULL;
915
916         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
917         g_return_val_if_fail (name, FALSE);
918         g_return_val_if_fail (key, FALSE);
919
920         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
921
922         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
923
924         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
925         if (err) {
926                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
927                 g_error_free (err);
928                 retval = FALSE;
929         }
930         g_free (keyname);
931         return retval;
932 }
933
934
935 gboolean
936 modest_account_mgr_set_list (ModestAccountMgr *self,
937                              const gchar *name,
938                              const gchar *key,
939                              GSList *val,
940                              ModestConfValueType list_type,
941                              gboolean server_account)
942 {
943         ModestAccountMgrPrivate *priv;
944         gchar *keyname;
945         GError *err = NULL;
946         gboolean retval;
947         
948         g_return_val_if_fail (self, FALSE);
949         g_return_val_if_fail (name, FALSE);
950         g_return_val_if_fail (key,  FALSE);
951         g_return_val_if_fail (val,  FALSE);
952
953         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
954         
955         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
956         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
957         if (err) {
958                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
959                 g_error_free (err);
960                 retval = FALSE;
961         }
962         g_free (keyname);
963
964         return retval;
965 }
966
967 gboolean
968 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
969                                    gboolean server_account)
970 {
971         ModestAccountMgrPrivate *priv;
972
973         gchar *keyname;
974         gboolean retval;
975         GError *err = NULL;
976
977         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
978         g_return_val_if_fail (name, FALSE);
979
980         keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
981         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
982         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
983         if (err) {
984                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
985                             err->message);
986                 g_error_free (err);
987                 retval = FALSE;
988         }
989         g_free (keyname);
990         return retval;
991 }
992
993 gboolean
994 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, const gchar *display_name)
995 {
996         GSList *account_names = NULL;
997         GSList *cursor = NULL;
998         
999         cursor = account_names = modest_account_mgr_account_names (self, 
1000                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1001
1002         gboolean found = FALSE;
1003         
1004         /* Look at each non-server account to check their display names; */
1005         while (cursor) {
1006                 const gchar * account_name = (gchar*)cursor->data;
1007                 
1008                 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1009                 if (!account_data) {
1010                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1011                         continue;
1012                 }
1013
1014                 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1015                         found = TRUE;
1016                         break;
1017                 }
1018
1019                 modest_account_mgr_free_account_data (self, account_data);
1020                 cursor = cursor->next;
1021         }
1022         modest_account_mgr_free_account_names (account_names);
1023         account_names = NULL;
1024         
1025         return found;
1026 }
1027
1028
1029
1030
1031 gboolean 
1032 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1033                           const gchar *key, gboolean server_account)
1034 {
1035         ModestAccountMgrPrivate *priv;
1036         
1037         gchar *keyname;
1038         gboolean retval;
1039         GError *err = NULL;
1040         
1041         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1042         g_return_val_if_fail (name, FALSE);
1043         g_return_val_if_fail (key, FALSE);
1044
1045         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1046
1047         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1048         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1049         if (err) {
1050                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1051                             err->message);
1052                 g_error_free (err);
1053                 retval = FALSE;
1054         }
1055         g_free (keyname);
1056         return retval;
1057 }
1058
1059 gchar*
1060 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1061 {
1062         /* Initialize input parameters: */
1063         if (is_account_key)
1064                 *is_account_key = FALSE;
1065
1066         if (is_server_account)
1067                 *is_server_account = FALSE;
1068
1069         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1070         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1071         gchar *cursor;
1072         gchar *account = NULL;
1073
1074         /* determine whether it's an account or a server account,
1075          * based on the prefix */
1076         if (g_str_has_prefix (key, account_ns)) {
1077
1078                 if (is_server_account)
1079                         *is_server_account = FALSE;
1080                 
1081                 account = g_strdup (key + strlen (account_ns));
1082
1083         } else if (g_str_has_prefix (key, server_account_ns)) {
1084
1085                 if (is_server_account)
1086                         *is_server_account = TRUE;
1087                 
1088                 account = g_strdup (key + strlen (server_account_ns));  
1089         } else
1090                 return NULL;
1091
1092         /* if there are any slashes left in the key, it's not
1093          * the toplevel entry for an account
1094          */
1095         cursor = strstr(account, "/");
1096         
1097         if (is_account_key && cursor)
1098                 *is_account_key = TRUE;
1099
1100         /* put a NULL where the first slash was */
1101         if (cursor)
1102                 *cursor = '\0';
1103
1104         if (account) {
1105                 /* The key is an escaped string, so unescape it to get the actual account name: */
1106                 gchar *unescaped_name = modest_conf_key_unescape (account);
1107                 g_free (account);
1108                 return unescaped_name;
1109         } else
1110                 return NULL;
1111 }
1112
1113
1114
1115 /* must be freed by caller */
1116 gchar *
1117 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
1118 {
1119         gchar *retval = NULL;
1120         
1121         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1122         
1123         if (!account_name)
1124                 return g_strdup (namespace);
1125         
1126         /* Always escape the conf keys, so that it is acceptable to gconf: */
1127         gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
1128         gchar *escaped_name =  name ? modest_conf_key_escape (name) : NULL;
1129
1130         if (escaped_account_name && escaped_name)
1131                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1132         else if (escaped_account_name)
1133                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1134
1135         /* Sanity check: */
1136         if (!modest_conf_key_is_valid (retval)) {
1137                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1138                 g_free (retval);
1139                 retval = NULL;
1140         }
1141
1142         g_free (escaped_name);
1143         g_free (escaped_account_name);
1144
1145         return retval;
1146 }
1147
1148 gboolean
1149 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1150 {
1151         /* Check that at least one account exists: */
1152         GSList *account_names = modest_account_mgr_account_names (self,
1153                                                                   enabled);
1154         gboolean accounts_exist = account_names != NULL;
1155         
1156         modest_account_mgr_free_account_names (account_names);
1157         account_names = NULL;
1158         
1159         return accounts_exist;
1160 }
1161
1162 static int
1163 compare_account_name(gconstpointer a, gconstpointer b)
1164 {
1165         const gchar* account_name = (const gchar*) a;
1166         const gchar* account_name2 = (const gchar*) b;
1167         return strcmp(account_name, account_name2);
1168 }
1169
1170 void 
1171 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
1172                                                                                                                                                 gboolean busy)
1173 {
1174         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1175         if (busy)
1176         {
1177                 GSList *account_names = modest_account_mgr_account_names (self,
1178                                 TRUE);
1179                 GSList* account = 
1180                         g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1181                 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1182                 {
1183                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1184                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1185                 }
1186                 modest_account_mgr_free_account_names (account_names);
1187                 account_names = NULL;
1188         } else {
1189                 GSList* account = 
1190                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1191                 if (account)
1192                 {
1193                         g_free(account->data);
1194                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1195                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1196                 }
1197         }
1198 }
1199
1200 gboolean
1201 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1202 {
1203         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1204         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1205                                         != NULL);
1206 }
1207