2007-07-13 MMurray 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                 /* Ignore modest accounts whose server accounts don't exist: 
680                  * (We could be getting this list while the account is being deleted, 
681                  * while the child server accounts have already been deleted, but the 
682                  * parent modest account already exists.
683                  */
684                 if (add) {
685                         gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
686                                                                             FALSE);
687                         if (server_account_name) {
688                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
689                                         add = FALSE;
690                                 g_free (server_account_name);
691                         }
692                 }
693                 
694                 if (add) {
695                         gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
696                                                                             FALSE);
697                         if (server_account_name) {
698                                 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
699                                         add = FALSE;
700                                 g_free (server_account_name);
701                         }
702                 }
703                 
704                 if (add)        
705                         result = g_slist_append (result, unescaped_name);
706                 else 
707                         g_free (unescaped_name);
708
709                 g_free (iter->data);
710                 iter->data = NULL;
711                 
712                 iter = g_slist_next (iter);     
713         }
714         
715
716         /* we already freed the strings in the loop */
717         g_slist_free (accounts);
718         
719         accounts = NULL;
720         return result;
721 }
722
723
724 void
725 modest_account_mgr_free_account_names (GSList *account_names)
726 {
727         g_slist_foreach (account_names, (GFunc)g_free, NULL);
728         g_slist_free (account_names);
729 }
730
731
732
733 gchar *
734 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
735                                const gchar *key, gboolean server_account) {
736
737         ModestAccountMgrPrivate *priv;
738
739         gchar *keyname;
740         gchar *retval;
741         GError *err = NULL;
742
743         g_return_val_if_fail (self, NULL);
744         g_return_val_if_fail (name, NULL);
745         g_return_val_if_fail (key, NULL);
746
747         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
748         
749         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
750         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
751         if (err) {
752                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
753                 g_error_free (err);
754                 retval = NULL;
755         }
756         g_free (keyname);
757
758         return retval;
759 }
760
761
762 gchar *
763 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
764                                const gchar *key, gboolean server_account)
765 {
766         return modest_account_mgr_get_string (self, name, key, server_account);
767
768 }
769
770
771
772 gint
773 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
774                             gboolean server_account)
775 {
776         ModestAccountMgrPrivate *priv;
777
778         gchar *keyname;
779         gint retval;
780         GError *err = NULL;
781         
782         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
783         g_return_val_if_fail (name, -1);
784         g_return_val_if_fail (key, -1);
785
786         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
787
788         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
789         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
790         if (err) {
791                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
792                 g_error_free (err);
793                 retval = -1;
794         }
795         g_free (keyname);
796
797         return retval;
798 }
799
800
801
802 gboolean
803 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
804                              const gchar * key, gboolean server_account)
805 {
806         ModestAccountMgrPrivate *priv;
807
808         gchar *keyname;
809         gboolean retval;
810         GError *err = NULL;
811
812         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
813         g_return_val_if_fail (account, FALSE);
814         g_return_val_if_fail (key, FALSE);
815
816         keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
817         
818         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
819         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
820         if (err) {
821                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
822                 g_error_free (err);
823                 retval = FALSE;
824         }
825         g_free (keyname);
826
827         return retval;
828 }
829
830
831
832 GSList * 
833 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
834                              const gchar *key, ModestConfValueType list_type,
835                              gboolean server_account)
836 {
837         ModestAccountMgrPrivate *priv;
838
839         gchar *keyname;
840         GSList *retval;
841         GError *err = NULL;
842         
843         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
844         g_return_val_if_fail (name, NULL);
845         g_return_val_if_fail (key, NULL);
846
847         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
848         
849         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
850         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
851         if (err) {
852                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
853                             err->message);
854                 g_error_free (err);
855                 retval = FALSE;
856         }
857         g_free (keyname);
858
859         return retval;
860 }
861
862
863 gboolean
864 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
865                                const gchar * key, const gchar * val, gboolean server_account)
866 {
867         ModestAccountMgrPrivate *priv;
868
869         gchar *keyname;
870         gboolean retval;
871         GError *err = NULL;
872
873         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
874         g_return_val_if_fail (name, FALSE);
875         g_return_val_if_fail (key, FALSE);
876
877         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
878         
879         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
880
881         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
882         if (err) {
883                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
884                 g_error_free (err);
885                 retval = FALSE;
886         }
887         g_free (keyname);       
888         return retval;
889 }
890
891
892 gboolean
893 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
894                                  const gchar * key, const gchar * val, gboolean server_account)
895 {
896         return modest_account_mgr_set_password (self, name, key, val, server_account);
897 }
898
899
900
901 gboolean
902 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
903                             const gchar * key, int val, gboolean server_account)
904 {
905         ModestAccountMgrPrivate *priv;
906
907         gchar *keyname;
908         gboolean retval;
909         GError *err = NULL;
910         
911         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
912         g_return_val_if_fail (name, FALSE);
913         g_return_val_if_fail (key, FALSE);
914
915         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
916         
917         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
918
919         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
920         if (err) {
921                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
922                 g_error_free (err);
923                 retval = FALSE;
924         }
925         g_free (keyname);
926         return retval;
927 }
928
929
930
931 gboolean
932 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
933                              const gchar * key, gboolean val, gboolean server_account)
934 {
935         ModestAccountMgrPrivate *priv;
936
937         gchar *keyname;
938         gboolean retval;
939         GError *err = NULL;
940
941         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
942         g_return_val_if_fail (name, FALSE);
943         g_return_val_if_fail (key, FALSE);
944
945         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
946
947         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
948
949         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
950         if (err) {
951                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
952                 g_error_free (err);
953                 retval = FALSE;
954         }
955         g_free (keyname);
956         return retval;
957 }
958
959
960 gboolean
961 modest_account_mgr_set_list (ModestAccountMgr *self,
962                              const gchar *name,
963                              const gchar *key,
964                              GSList *val,
965                              ModestConfValueType list_type,
966                              gboolean server_account)
967 {
968         ModestAccountMgrPrivate *priv;
969         gchar *keyname;
970         GError *err = NULL;
971         gboolean retval;
972         
973         g_return_val_if_fail (self, FALSE);
974         g_return_val_if_fail (name, FALSE);
975         g_return_val_if_fail (key,  FALSE);
976         g_return_val_if_fail (val,  FALSE);
977
978         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
979         
980         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
981         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
982         if (err) {
983                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
984                 g_error_free (err);
985                 retval = FALSE;
986         }
987         g_free (keyname);
988
989         return retval;
990 }
991
992 gboolean
993 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
994                                    gboolean server_account)
995 {
996         ModestAccountMgrPrivate *priv;
997
998         gchar *keyname;
999         gboolean retval;
1000         GError *err = NULL;
1001
1002         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1003         g_return_val_if_fail (name, FALSE);
1004
1005         keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
1006         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1007         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1008         if (err) {
1009                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1010                             err->message);
1011                 g_error_free (err);
1012                 retval = FALSE;
1013         }
1014         g_free (keyname);
1015         return retval;
1016 }
1017
1018 gboolean
1019 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, const gchar *display_name)
1020 {
1021         GSList *account_names = NULL;
1022         GSList *cursor = NULL;
1023         
1024         cursor = account_names = modest_account_mgr_account_names (self, 
1025                                                                    TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1026
1027         gboolean found = FALSE;
1028         
1029         /* Look at each non-server account to check their display names; */
1030         while (cursor) {
1031                 const gchar * account_name = (gchar*)cursor->data;
1032                 
1033                 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1034                 if (!account_data) {
1035                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1036                         continue;
1037                 }
1038
1039                 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1040                         found = TRUE;
1041                         break;
1042                 }
1043
1044                 modest_account_mgr_free_account_data (self, account_data);
1045                 cursor = cursor->next;
1046         }
1047         modest_account_mgr_free_account_names (account_names);
1048         account_names = NULL;
1049         
1050         return found;
1051 }
1052
1053
1054
1055
1056 gboolean 
1057 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1058                           const gchar *key, gboolean server_account)
1059 {
1060         ModestAccountMgrPrivate *priv;
1061         
1062         gchar *keyname;
1063         gboolean retval;
1064         GError *err = NULL;
1065         
1066         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1067         g_return_val_if_fail (name, FALSE);
1068         g_return_val_if_fail (key, FALSE);
1069
1070         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1071
1072         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1073         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1074         if (err) {
1075                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1076                             err->message);
1077                 g_error_free (err);
1078                 retval = FALSE;
1079         }
1080         g_free (keyname);
1081         return retval;
1082 }
1083
1084 gchar*
1085 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1086 {
1087         /* Initialize input parameters: */
1088         if (is_account_key)
1089                 *is_account_key = FALSE;
1090
1091         if (is_server_account)
1092                 *is_server_account = FALSE;
1093
1094         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1095         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1096         gchar *cursor;
1097         gchar *account = NULL;
1098
1099         /* determine whether it's an account or a server account,
1100          * based on the prefix */
1101         if (g_str_has_prefix (key, account_ns)) {
1102
1103                 if (is_server_account)
1104                         *is_server_account = FALSE;
1105                 
1106                 account = g_strdup (key + strlen (account_ns));
1107
1108         } else if (g_str_has_prefix (key, server_account_ns)) {
1109
1110                 if (is_server_account)
1111                         *is_server_account = TRUE;
1112                 
1113                 account = g_strdup (key + strlen (server_account_ns));  
1114         } else
1115                 return NULL;
1116
1117         /* if there are any slashes left in the key, it's not
1118          * the toplevel entry for an account
1119          */
1120         cursor = strstr(account, "/");
1121         
1122         if (is_account_key && cursor)
1123                 *is_account_key = TRUE;
1124
1125         /* put a NULL where the first slash was */
1126         if (cursor)
1127                 *cursor = '\0';
1128
1129         if (account) {
1130                 /* The key is an escaped string, so unescape it to get the actual account name: */
1131                 gchar *unescaped_name = modest_conf_key_unescape (account);
1132                 g_free (account);
1133                 return unescaped_name;
1134         } else
1135                 return NULL;
1136 }
1137
1138
1139
1140 /* must be freed by caller */
1141 gchar *
1142 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
1143 {
1144         gchar *retval = NULL;
1145         
1146         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1147         
1148         if (!account_name)
1149                 return g_strdup (namespace);
1150         
1151         /* Always escape the conf keys, so that it is acceptable to gconf: */
1152         gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
1153         gchar *escaped_name =  name ? modest_conf_key_escape (name) : NULL;
1154
1155         if (escaped_account_name && escaped_name)
1156                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1157         else if (escaped_account_name)
1158                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1159
1160         /* Sanity check: */
1161         if (!modest_conf_key_is_valid (retval)) {
1162                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1163                 g_free (retval);
1164                 retval = NULL;
1165         }
1166
1167         g_free (escaped_name);
1168         g_free (escaped_account_name);
1169
1170         return retval;
1171 }
1172
1173 gboolean
1174 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1175 {
1176         /* Check that at least one account exists: */
1177         GSList *account_names = modest_account_mgr_account_names (self,
1178                                                                   enabled);
1179         gboolean accounts_exist = account_names != NULL;
1180         
1181         modest_account_mgr_free_account_names (account_names);
1182         account_names = NULL;
1183         
1184         return accounts_exist;
1185 }
1186
1187 static int
1188 compare_account_name(gconstpointer a, gconstpointer b)
1189 {
1190         const gchar* account_name = (const gchar*) a;
1191         const gchar* account_name2 = (const gchar*) b;
1192         return strcmp(account_name, account_name2);
1193 }
1194
1195 void 
1196 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
1197                                                                                                                                                 gboolean busy)
1198 {
1199         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1200         if (busy)
1201         {
1202                 GSList *account_names = modest_account_mgr_account_names (self,
1203                                 TRUE);
1204                 GSList* account = 
1205                         g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1206                 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1207                 {
1208                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1209                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1210                 }
1211                 modest_account_mgr_free_account_names (account_names);
1212                 account_names = NULL;
1213         } else {
1214                 GSList* account = 
1215                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1216                 if (account)
1217                 {
1218                         g_free(account->data);
1219                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1220                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1221                 }
1222         }
1223 }
1224
1225 gboolean
1226 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1227 {
1228         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1229         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1230                                         != NULL);
1231 }
1232