2007-06-17 Johannes Schmid <johannes.schmid@openismus.com>
[modest] / src / modest-account-mgr.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <string.h>
31 #include <modest-marshal.h>
32 #include <modest-account-mgr.h>
33 #include <modest-account-mgr-priv.h>
34 #include <modest-account-mgr-helpers.h>
35
36 /* 'private'/'protected' functions */
37 static void modest_account_mgr_class_init (ModestAccountMgrClass * klass);
38 static void modest_account_mgr_init       (ModestAccountMgr * obj);
39 static void modest_account_mgr_finalize   (GObject * obj);
40
41 /* list my signals */
42 enum {
43         ACCOUNT_CHANGED_SIGNAL,
44         ACCOUNT_REMOVED_SIGNAL,
45         ACCOUNT_BUSY_SIGNAL,
46         LAST_SIGNAL
47 };
48
49
50 /* globals */
51 static GObjectClass *parent_class = NULL;
52 static guint signals[LAST_SIGNAL] = {0};
53
54 /* We signal key changes in batches, every X seconds: */
55 static gboolean
56 on_timeout_notify_changes (gpointer data)
57 {
58         ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
59         ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
60                 
61         /* TODO: Also store the account names, and notify one list for each account,
62          * if anything uses the account names. */
63         
64         if (priv->changed_conf_keys) {
65                 gchar *default_account = 
66                                 modest_account_mgr_get_default_account (self);
67                 
68                 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 #if 0
622 /* Not used. */
623 GSList*
624 modest_account_mgr_search_server_accounts (ModestAccountMgr * self,
625                                            const gchar * account_name,
626                                            ModestTransportStoreProtocol proto)
627 {
628         GSList *accounts;
629         GSList *cursor;
630         ModestAccountMgrPrivate *priv;
631         gchar *key;
632         GError *err = NULL;
633         
634         g_return_val_if_fail (self, NULL);
635         
636         key      = _modest_account_mgr_get_account_keyname (account_name, NULL, TRUE);
637         priv     = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
638         
639         /* get the list of all server accounts */
640         accounts = modest_conf_list_subkeys (priv->modest_conf, key, &err);
641         if (err) {
642                 g_printerr ("modest: failed to get subkeys for '%s' (%s)\n", key,
643                         err->message);
644                 g_error_free(err);
645                 return NULL;
646         }
647         
648         /* filter out the ones with the wrong protocol */
649         /* we could optimize for unknown proto / unknown type, but it will only
650          * make the code more complex */
651         cursor = accounts;
652         while (cursor) { 
653                 gchar *account   = _modest_account_mgr_account_from_key ((gchar*)cursor->data, NULL, NULL);
654                 gchar *acc_proto = modest_account_mgr_get_string (self, account, MODEST_ACCOUNT_PROTO,TRUE);
655                 ModestTransportStoreProtocol this_proto = 
656                         modest_protocol_info_get_transport_store_protocol (acc_proto);
657                 if (this_proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN && this_proto != proto) {
658                         GSList *nxt = cursor->next;
659                         accounts = g_slist_delete_link (accounts, cursor);
660                         cursor = nxt;
661                 } else
662                         cursor = cursor->next;
663                 
664                 g_free (account);
665                 g_free (acc_proto);
666         }
667         
668         /* +1 because we must remove the ending '/' as well */
669         strip_prefix_from_elements (accounts, strlen(key)+1);
670         return accounts;        
671 }
672 #endif
673
674 GSList*
675 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
676 {
677         GSList *accounts;
678         ModestAccountMgrPrivate *priv;
679         GError *err = NULL;
680         
681         const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
682
683         g_return_val_if_fail (self, NULL);
684
685         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
686         accounts = modest_conf_list_subkeys (priv->modest_conf,
687                                              MODEST_ACCOUNT_NAMESPACE, &err);
688
689         if (err) {
690                 g_printerr ("modest: failed to get subkeys (%s): %s\n",
691                             MODEST_ACCOUNT_NAMESPACE, err->message);
692                 g_error_free (err);
693                 return NULL; /* assume accounts did not get value when err is set...*/
694         }
695         
696         strip_prefix_from_elements (accounts, prefix_len);
697                 
698         GSList *result = NULL;
699         
700         /* Unescape the keys to get the account names: */
701         GSList *iter = accounts;
702         while (iter) {
703                 if (!(iter->data))
704                         continue;
705                         
706                 const gchar* account_name_key = (const gchar*)iter->data;
707                 /* printf ("DEBUG: %s: account_name_key=%s\n", __FUNCTION__, account_name_key); */
708                 gchar* unescaped_name = account_name_key ? 
709                         modest_conf_key_unescape (account_name_key) 
710                         : NULL;
711                 /* printf ("  DEBUG: %s: unescaped name=%s\n", __FUNCTION__, unescaped_name); */
712                 
713                 gboolean add = TRUE;
714                 if (only_enabled) {
715                         if (unescaped_name && 
716                                 !modest_account_mgr_get_enabled (self, unescaped_name)) {
717                                 add = FALSE;
718                         }
719                 }
720                 
721                 if (add) {      
722                         result = g_slist_append (result, unescaped_name);
723                 }
724                 else {
725                         g_free (unescaped_name);
726                 }
727                         
728                 iter = g_slist_next (iter);     
729         }
730         
731         /* TODO: Free the strings too? */
732         g_slist_free (accounts);
733         accounts = NULL;
734
735         return result;
736 }
737
738
739 gchar *
740 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
741                                const gchar *key, gboolean server_account) {
742
743         ModestAccountMgrPrivate *priv;
744
745         gchar *keyname;
746         gchar *retval;
747         GError *err = NULL;
748
749         g_return_val_if_fail (self, NULL);
750         g_return_val_if_fail (name, NULL);
751         g_return_val_if_fail (key, NULL);
752
753         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
754         
755         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
756         retval = modest_conf_get_string (priv->modest_conf, keyname, &err);     
757         if (err) {
758                 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
759                 g_error_free (err);
760                 retval = NULL;
761         }
762         g_free (keyname);
763
764         return retval;
765 }
766
767
768 gchar *
769 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
770                                const gchar *key, gboolean server_account)
771 {
772         return modest_account_mgr_get_string (self, name, key, server_account);
773
774 }
775
776
777
778 gint
779 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
780                             gboolean server_account)
781 {
782         ModestAccountMgrPrivate *priv;
783
784         gchar *keyname;
785         gint retval;
786         GError *err = NULL;
787         
788         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
789         g_return_val_if_fail (name, -1);
790         g_return_val_if_fail (key, -1);
791
792         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
793
794         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
795         retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
796         if (err) {
797                 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
798                 g_error_free (err);
799                 retval = -1;
800         }
801         g_free (keyname);
802
803         return retval;
804 }
805
806
807
808 gboolean
809 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
810                              const gchar * key, gboolean server_account)
811 {
812         ModestAccountMgrPrivate *priv;
813
814         gchar *keyname;
815         gboolean retval;
816         GError *err = NULL;
817
818         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
819         g_return_val_if_fail (account, FALSE);
820         g_return_val_if_fail (key, FALSE);
821
822         keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
823         
824         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
825         retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);               
826         if (err) {
827                 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
828                 g_error_free (err);
829                 retval = FALSE;
830         }
831         g_free (keyname);
832
833         return retval;
834 }
835
836
837
838 GSList * 
839 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
840                              const gchar *key, ModestConfValueType list_type,
841                              gboolean server_account)
842 {
843         ModestAccountMgrPrivate *priv;
844
845         gchar *keyname;
846         GSList *retval;
847         GError *err = NULL;
848         
849         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
850         g_return_val_if_fail (name, NULL);
851         g_return_val_if_fail (key, NULL);
852
853         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
854         
855         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
856         retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
857         if (err) {
858                 g_printerr ("modest: error getting list '%s': %s\n", keyname,
859                             err->message);
860                 g_error_free (err);
861                 retval = FALSE;
862         }
863         g_free (keyname);
864
865         return retval;
866 }
867
868
869 gboolean
870 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
871                                const gchar * key, const gchar * val, gboolean server_account)
872 {
873         ModestAccountMgrPrivate *priv;
874
875         gchar *keyname;
876         gboolean retval;
877         GError *err = NULL;
878
879         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
880         g_return_val_if_fail (name, FALSE);
881         g_return_val_if_fail (key, FALSE);
882
883         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
884         
885         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
886
887         retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
888         if (err) {
889                 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
890                 g_error_free (err);
891                 retval = FALSE;
892         }
893         g_free (keyname);       
894         return retval;
895 }
896
897
898 gboolean
899 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
900                                  const gchar * key, const gchar * val, gboolean server_account)
901 {
902         return modest_account_mgr_set_password (self, name, key, val, server_account);
903 }
904
905
906
907 gboolean
908 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
909                             const gchar * key, int val, gboolean server_account)
910 {
911         ModestAccountMgrPrivate *priv;
912
913         gchar *keyname;
914         gboolean retval;
915         GError *err = NULL;
916         
917         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
918         g_return_val_if_fail (name, FALSE);
919         g_return_val_if_fail (key, FALSE);
920
921         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
922         
923         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
924
925         retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
926         if (err) {
927                 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
928                 g_error_free (err);
929                 retval = FALSE;
930         }
931         g_free (keyname);
932         return retval;
933 }
934
935
936
937 gboolean
938 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
939                              const gchar * key, gboolean val, gboolean server_account)
940 {
941         ModestAccountMgrPrivate *priv;
942
943         gchar *keyname;
944         gboolean retval;
945         GError *err = NULL;
946
947         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
948         g_return_val_if_fail (name, FALSE);
949         g_return_val_if_fail (key, FALSE);
950
951         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
952
953         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
954
955         retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
956         if (err) {
957                 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
958                 g_error_free (err);
959                 retval = FALSE;
960         }
961         g_free (keyname);
962         return retval;
963 }
964
965
966 gboolean
967 modest_account_mgr_set_list (ModestAccountMgr *self,
968                              const gchar *name,
969                              const gchar *key,
970                              GSList *val,
971                              ModestConfValueType list_type,
972                              gboolean server_account)
973 {
974         ModestAccountMgrPrivate *priv;
975         gchar *keyname;
976         GError *err = NULL;
977         gboolean retval;
978         
979         g_return_val_if_fail (self, FALSE);
980         g_return_val_if_fail (name, FALSE);
981         g_return_val_if_fail (key,  FALSE);
982         g_return_val_if_fail (val,  FALSE);
983
984         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
985         
986         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
987         retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
988         if (err) {
989                 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
990                 g_error_free (err);
991                 retval = FALSE;
992         }
993         g_free (keyname);
994
995         return retval;
996 }
997
998 gboolean
999 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
1000                                    gboolean server_account)
1001 {
1002         ModestAccountMgrPrivate *priv;
1003
1004         gchar *keyname;
1005         gboolean retval;
1006         GError *err = NULL;
1007
1008         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1009         g_return_val_if_fail (name, FALSE);
1010
1011         keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
1012         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1013         retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1014         if (err) {
1015                 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1016                             err->message);
1017                 g_error_free (err);
1018                 retval = FALSE;
1019         }
1020         g_free (keyname);
1021         return retval;
1022 }
1023
1024 gboolean
1025 modest_account_mgr_account_with_display_name_exists  (ModestAccountMgr *self, const gchar *display_name)
1026 {
1027         GSList *account_names = NULL;
1028         GSList *cursor = NULL;
1029         
1030         cursor = account_names = modest_account_mgr_account_names (self, 
1031                 TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1032
1033         gboolean found = FALSE;
1034         
1035         /* Look at each non-server account to check their display names; */
1036         while (cursor) {
1037                 const gchar * account_name = (gchar*)cursor->data;
1038                 
1039                 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1040                 if (!account_data) {
1041                         g_printerr ("modest: failed to get account data for %s\n", account_name);
1042                         continue;
1043                 }
1044
1045                 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1046                         found = TRUE;
1047                         break;
1048                 }
1049
1050                 modest_account_mgr_free_account_data (self, account_data);
1051                 cursor = cursor->next;
1052         }
1053         g_slist_free (account_names);
1054         
1055         return found;
1056 }
1057
1058
1059
1060
1061 gboolean 
1062 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1063                           const gchar *key, gboolean server_account)
1064 {
1065         ModestAccountMgrPrivate *priv;
1066         
1067         gchar *keyname;
1068         gboolean retval;
1069         GError *err = NULL;
1070         
1071         g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1072         g_return_val_if_fail (name, FALSE);
1073         g_return_val_if_fail (key, FALSE);
1074
1075         keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1076
1077         priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1078         retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1079         if (err) {
1080                 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1081                             err->message);
1082                 g_error_free (err);
1083                 retval = FALSE;
1084         }
1085         g_free (keyname);
1086         return retval;
1087 }
1088
1089 gchar*
1090 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1091 {
1092         /* Initialize input parameters: */
1093         if (is_account_key)
1094                 *is_account_key = FALSE;
1095
1096         if (is_server_account)
1097                 *is_server_account = FALSE;
1098
1099         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
1100         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1101         gchar *cursor;
1102         gchar *account = NULL;
1103
1104         /* determine whether it's an account or a server account,
1105          * based on the prefix */
1106         if (g_str_has_prefix (key, account_ns)) {
1107
1108                 if (is_server_account)
1109                         *is_server_account = FALSE;
1110                 
1111                 account = g_strdup (key + strlen (account_ns));
1112
1113         } else if (g_str_has_prefix (key, server_account_ns)) {
1114
1115                 if (is_server_account)
1116                         *is_server_account = TRUE;
1117                 
1118                 account = g_strdup (key + strlen (server_account_ns));  
1119         } else
1120                 return NULL;
1121
1122         /* if there are any slashes left in the key, it's not
1123          * the toplevel entry for an account
1124          */
1125         cursor = strstr(account, "/");
1126         
1127         if (is_account_key && cursor)
1128                 *is_account_key = TRUE;
1129
1130         /* put a NULL where the first slash was */
1131         if (cursor)
1132                 *cursor = '\0';
1133
1134         if (account) {
1135                 /* The key is an escaped string, so unescape it to get the actual account name: */
1136                 gchar *unescaped_name = modest_conf_key_unescape (account);
1137                 g_free (account);
1138                 return unescaped_name;
1139         } else
1140                 return NULL;
1141 }
1142
1143
1144
1145 /* must be freed by caller */
1146 gchar *
1147 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
1148 {
1149         gchar *retval = NULL;
1150         
1151         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1152         
1153         if (!account_name)
1154                 return g_strdup (namespace);
1155         
1156         /* Always escape the conf keys, so that it is acceptable to gconf: */
1157         gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
1158         gchar *escaped_name =  name ? modest_conf_key_escape (name) : NULL;
1159
1160         if (escaped_account_name && escaped_name)
1161                 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1162         else if (escaped_account_name)
1163                 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1164
1165         /* Sanity check: */
1166         if (!modest_conf_key_is_valid (retval)) {
1167                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1168                 g_free (retval);
1169                 retval = NULL;
1170         }
1171
1172         g_free (escaped_name);
1173         g_free (escaped_account_name);
1174
1175         return retval;
1176 }
1177
1178 gboolean
1179 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1180 {
1181         /* Check that at least one account exists: */
1182         GSList *account_names = modest_account_mgr_account_names (self,
1183                                 enabled);
1184         gboolean accounts_exist = account_names != NULL;
1185         g_slist_free (account_names);
1186         
1187         return accounts_exist;
1188 }
1189
1190 static int
1191 compare_account_name(gconstpointer a, gconstpointer b)
1192 {
1193         const gchar* account_name = (const gchar*) a;
1194         const gchar* account_name2 = (const gchar*) b;
1195         return strcmp(account_name, account_name2);
1196 }
1197
1198 void 
1199 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name, 
1200                                                                                                                                                 gboolean busy)
1201 {
1202         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1203         if (busy)
1204         {
1205                 GSList *account_names = modest_account_mgr_account_names (self,
1206                                 TRUE);
1207                 GSList* account = 
1208                         g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1209                 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1210                 {
1211                         priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1212                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1213                 }
1214                 g_slist_free(account_names);
1215         }
1216         else
1217         {
1218                 GSList* account = 
1219                         g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1220                 if (account)
1221                 {
1222                         g_free(account->data);
1223                         priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1224                         g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1225                 }
1226         }
1227 }
1228
1229 gboolean
1230 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1231 {
1232         ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1233         return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)
1234                                         != NULL);
1235 }
1236