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