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