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