1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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>
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);
43 ACCOUNT_CHANGED_SIGNAL,
44 ACCOUNT_REMOVED_SIGNAL,
51 static GObjectClass *parent_class = NULL;
52 static guint signals[LAST_SIGNAL] = {0};
54 /* We signal key changes in batches, every X seconds: */
56 on_timeout_notify_changes (gpointer data)
58 ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
59 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
61 /* TODO: Also store the account names, and notify one list for each account,
62 * if anything uses the account names. */
64 if (priv->changed_conf_keys) {
65 gchar *default_account =
66 modest_account_mgr_get_default_account (self);
68 /* printf ("DEBUG: %s: priv->changed_conf_key length=%d\n",
69 __FUNCTION__, g_slist_length (priv->changed_conf_keys)); */
70 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
71 default_account, priv->changed_conf_keys, FALSE);
73 g_free (default_account);
75 g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
76 g_slist_free (priv->changed_conf_keys);
77 priv->changed_conf_keys = NULL;
80 return TRUE; /* Call this again later. */
84 on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpointer user_data)
86 ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data);
87 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
88 gboolean is_account_key;
89 gboolean is_server_account;
90 gchar* account = NULL;
92 /* there is only one not-really-account key which will still emit
93 * a signal: a change in MODEST_CONF_DEFAULT_ACCOUNT */
94 if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
95 /* Get the default account instead. */
97 /* Store the key for later notification in our timeout callback.
98 * Notifying for every key change would cause unnecessary work: */
99 priv->changed_conf_keys = g_slist_append (priv->changed_conf_keys,
100 (gpointer) g_strdup (key));
103 is_account_key = FALSE;
104 is_server_account = FALSE;
105 account = _modest_account_mgr_account_from_key (key, &is_account_key,
108 /* if this is not an account-related key change, ignore */
112 /* account was removed. Do not emit an account removed signal
113 because it was already being done in the remove_account
114 method. Do not notify also the removal of the server
115 account keys for the same reason */
116 if ((is_account_key || is_server_account) &&
117 event == MODEST_CONF_EVENT_KEY_UNSET) {
122 /* is this account enabled? */
123 gboolean enabled = FALSE;
124 if (is_server_account)
127 enabled = modest_account_mgr_get_enabled (self, account);
129 /* Notify is server account was changed, default account was changed
130 * or when enabled/disabled changes:
133 g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) ||
134 strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
135 /* Store the key for later notification in our timeout callback.
136 * Notifying for every key change would cause unnecessary work: */
137 priv->changed_conf_keys = g_slist_append (NULL,
138 (gpointer) g_strdup (key));
145 modest_account_mgr_get_type (void)
147 static GType my_type = 0;
150 static const GTypeInfo my_info = {
151 sizeof (ModestAccountMgrClass),
152 NULL, /* base init */
153 NULL, /* base finalize */
154 (GClassInitFunc) modest_account_mgr_class_init,
155 NULL, /* class finalize */
156 NULL, /* class data */
157 sizeof (ModestAccountMgr),
159 (GInstanceInitFunc) modest_account_mgr_init,
163 my_type = g_type_register_static (G_TYPE_OBJECT,
171 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
173 GObjectClass *gobject_class;
174 gobject_class = (GObjectClass *) klass;
176 parent_class = g_type_class_peek_parent (klass);
177 gobject_class->finalize = modest_account_mgr_finalize;
179 g_type_class_add_private (gobject_class,
180 sizeof (ModestAccountMgrPrivate));
182 /* signal definitions */
183 signals[ACCOUNT_REMOVED_SIGNAL] =
184 g_signal_new ("account_removed",
185 G_TYPE_FROM_CLASS (klass),
187 G_STRUCT_OFFSET(ModestAccountMgrClass,account_removed),
189 modest_marshal_VOID__STRING_BOOLEAN,
190 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
191 signals[ACCOUNT_CHANGED_SIGNAL] =
192 g_signal_new ("account_changed",
193 G_TYPE_FROM_CLASS (klass),
195 G_STRUCT_OFFSET(ModestAccountMgrClass,account_changed),
197 modest_marshal_VOID__STRING_POINTER_BOOLEAN,
198 G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
199 signals[ACCOUNT_BUSY_SIGNAL] =
200 g_signal_new ("account_busy_changed",
201 G_TYPE_FROM_CLASS (klass),
203 G_STRUCT_OFFSET(ModestAccountMgrClass,account_busy_changed),
205 modest_marshal_VOID__STRING_BOOLEAN,
206 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
211 modest_account_mgr_init (ModestAccountMgr * obj)
213 ModestAccountMgrPrivate *priv =
214 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
216 priv->modest_conf = NULL;
217 priv->busy_accounts = NULL;
218 priv->timeout = g_timeout_add (1000 /* milliseconds */, on_timeout_notify_changes, obj);
222 modest_account_mgr_finalize (GObject * obj)
224 ModestAccountMgrPrivate *priv =
225 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
227 if (priv->key_changed_handler_uid) {
228 g_signal_handler_disconnect (priv->modest_conf,
229 priv->key_changed_handler_uid);
230 priv->key_changed_handler_uid = 0;
233 if (priv->modest_conf) {
234 g_object_unref (G_OBJECT(priv->modest_conf));
235 priv->modest_conf = NULL;
239 g_source_remove (priv->timeout);
241 if (priv->changed_conf_keys) {
242 g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL);
243 g_slist_free (priv->changed_conf_keys);
246 G_OBJECT_CLASS(parent_class)->finalize (obj);
251 modest_account_mgr_new (ModestConf *conf)
254 ModestAccountMgrPrivate *priv;
256 g_return_val_if_fail (conf, NULL);
258 obj = G_OBJECT (g_object_new (MODEST_TYPE_ACCOUNT_MGR, NULL));
259 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
261 g_object_ref (G_OBJECT(conf));
262 priv->modest_conf = conf;
264 priv->key_changed_handler_uid =
265 g_signal_connect (G_OBJECT (conf), "key_changed",
266 G_CALLBACK (on_key_change),
269 return MODEST_ACCOUNT_MGR (obj);
274 null_means_empty (const gchar * str)
276 return str ? str : "";
281 modest_account_mgr_add_account (ModestAccountMgr *self,
283 const gchar *store_account,
284 const gchar *transport_account,
287 ModestAccountMgrPrivate *priv;
290 gchar *default_account;
293 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
294 g_return_val_if_fail (name, FALSE);
295 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
297 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
300 * we create the account by adding an account 'dir', with the name <name>,
301 * and in that the 'display_name' string key
303 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
304 if (modest_account_mgr_account_exists (self, key, FALSE)) {
305 g_printerr ("modest: account already exists\n");
310 ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
313 g_printerr ("modest: cannot set display name\n");
315 g_printerr ("modest: Error adding account conf: %s\n", err->message);
322 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
323 ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
326 g_printerr ("modest: failed to set store account '%s'\n",
329 g_printerr ("modest: Error adding store account conf: %s\n", err->message);
336 if (transport_account) {
337 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
339 ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
342 g_printerr ("modest: failed to set transport account '%s'\n",
345 g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
352 /* Make sure that leave-messages-on-server is enabled by default,
353 * as per the UI spec, though it is only meaningful for accounts using POP.
354 * (possibly this gconf key should be under the server account): */
355 modest_account_mgr_set_bool (self, name,
356 MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE /* not server account */);
359 modest_account_mgr_set_enabled (self, name, enabled);
361 /* if no default account has been defined yet, do so now */
362 default_account = modest_account_mgr_get_default_account (self);
363 if (!default_account)
364 modest_account_mgr_set_default_account (self, name);
365 g_free (default_account);
372 modest_account_mgr_add_server_account (ModestAccountMgr * self,
373 const gchar * name, const gchar *hostname,
375 const gchar * username, const gchar * password,
376 ModestTransportStoreProtocol proto,
377 ModestConnectionProtocol security,
378 ModestAuthProtocol auth)
380 ModestAccountMgrPrivate *priv;
385 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
386 g_return_val_if_fail (name, FALSE);
387 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
389 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
392 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
393 if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
394 g_printerr ("modest: server account '%s' already exists\n", name);
401 modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &err);
403 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
412 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_USERNAME, TRUE);
413 ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
415 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
425 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PASSWORD, TRUE);
426 ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
428 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
437 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
438 ok = modest_conf_set_string (priv->modest_conf, key,
439 modest_protocol_info_get_transport_store_protocol_name(proto),
442 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
452 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PORT, TRUE);
453 ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
455 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
465 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
466 ok = modest_conf_set_string (priv->modest_conf, key,
467 modest_protocol_info_get_auth_protocol_name (auth),
470 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
478 /* Add the security settings: */
479 modest_server_account_set_security (self, name, security);
483 g_printerr ("modest: failed to add server account\n");
490 /** modest_account_mgr_add_server_account_uri:
491 * Only used for mbox and maildir accounts.
494 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
495 const gchar *name, ModestTransportStoreProtocol proto,
498 ModestAccountMgrPrivate *priv;
502 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
503 g_return_val_if_fail (name, FALSE);
504 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
505 g_return_val_if_fail (uri, FALSE);
507 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
511 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
512 ok = modest_conf_set_string (priv->modest_conf, key,
513 modest_protocol_info_get_transport_store_protocol_name(proto),
518 g_printerr ("modest: failed to set proto\n");
523 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_URI, TRUE);
524 ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
528 g_printerr ("modest: failed to set uri\n");
535 modest_account_mgr_remove_account (ModestAccountMgr * self,
536 const gchar* name, gboolean server_account)
538 ModestAccountMgrPrivate *priv;
540 gboolean retval, default_account_deleted;
543 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
544 g_return_val_if_fail (name, FALSE);
546 if (!modest_account_mgr_account_exists (self, name, server_account)) {
547 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
551 default_account_deleted = FALSE;
553 if (!server_account) {
554 gchar *server_account_name, *default_account_name;
556 /* If this was the default, then remove that setting: */
557 default_account_name = modest_account_mgr_get_default_account (self);
558 if (default_account_name && (strcmp (default_account_name, name) == 0)) {
559 modest_account_mgr_unset_default_account (self);
560 default_account_deleted = TRUE;
562 g_free (default_account_name);
564 /* in case we're deleting an account, also delete the dependent store and transport account */
565 server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_STORE_ACCOUNT,
567 if (server_account_name) {
568 if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
569 g_printerr ("modest: failed to remove store account '%s' (%s)\n",
570 server_account_name, name);
571 g_free (server_account_name);
573 g_printerr ("modest: could not find the store account for %s\n", name);
575 server_account_name = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
577 if (server_account_name) {
578 if (!modest_account_mgr_remove_account (self, server_account_name, TRUE))
579 g_printerr ("modest: failed to remove transport account '%s' (%s)\n",
580 server_account_name, name);
581 g_free (server_account_name);
583 g_printerr ("modest: could not find the transport account for %s\n", name);
586 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
587 key = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
589 retval = modest_conf_remove_key (priv->modest_conf, key, &err);
593 g_printerr ("modest: error removing key: %s\n", err->message);
597 if (default_account_deleted) {
598 /* pick another one as the new default account. We do
599 this *after* deleting the keys, because otherwise a
600 call to account_names will retrieve also the
602 modest_account_mgr_set_first_account_as_default (self);
605 if (server_account) {
606 /* Notify the observers. We do this *after* deleting
607 the keys, because otherwise a call to account_names
608 will retrieve also the deleted account */
609 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0,
610 name, server_account);
618 /* strip the first /n/ character from each element
619 * caller must make sure all elements are strings with
620 * length >= n, and also that data can be freed.
624 strip_prefix_from_elements (GSList * lst, guint n)
627 memmove (lst->data, lst->data + n,
628 strlen(lst->data) - n + 1);
635 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
638 ModestAccountMgrPrivate *priv;
641 const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
643 g_return_val_if_fail (self, NULL);
645 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
646 accounts = modest_conf_list_subkeys (priv->modest_conf,
647 MODEST_ACCOUNT_NAMESPACE, &err);
650 g_printerr ("modest: failed to get subkeys (%s): %s\n",
651 MODEST_ACCOUNT_NAMESPACE, err->message);
653 return NULL; /* assume accounts did not get value when err is set...*/
656 strip_prefix_from_elements (accounts, prefix_len);
658 GSList *result = NULL;
660 /* Unescape the keys to get the account names: */
661 GSList *iter = accounts;
666 const gchar* account_name_key = (const gchar*)iter->data;
667 gchar* unescaped_name = account_name_key ?
668 modest_conf_key_unescape (account_name_key)
673 if (unescaped_name &&
674 !modest_account_mgr_get_enabled (self, unescaped_name)) {
679 /* Ignore modest accounts whose server accounts don't exist:
680 * (We could be getting this list while the account is being deleted,
681 * while the child server accounts have already been deleted, but the
682 * parent modest account already exists.
685 gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
687 if (server_account_name) {
688 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
690 g_free (server_account_name);
695 gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
697 if (server_account_name) {
698 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
700 g_free (server_account_name);
705 result = g_slist_append (result, unescaped_name);
707 g_free (unescaped_name);
712 iter = g_slist_next (iter);
716 /* we already freed the strings in the loop */
717 g_slist_free (accounts);
725 modest_account_mgr_free_account_names (GSList *account_names)
727 g_slist_foreach (account_names, (GFunc)g_free, NULL);
728 g_slist_free (account_names);
734 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
735 const gchar *key, gboolean server_account) {
737 ModestAccountMgrPrivate *priv;
743 g_return_val_if_fail (self, NULL);
744 g_return_val_if_fail (name, NULL);
745 g_return_val_if_fail (key, NULL);
747 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
749 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
750 retval = modest_conf_get_string (priv->modest_conf, keyname, &err);
752 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
763 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
764 const gchar *key, gboolean server_account)
766 return modest_account_mgr_get_string (self, name, key, server_account);
773 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
774 gboolean server_account)
776 ModestAccountMgrPrivate *priv;
782 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
783 g_return_val_if_fail (name, -1);
784 g_return_val_if_fail (key, -1);
786 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
788 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
789 retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
791 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
803 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
804 const gchar * key, gboolean server_account)
806 ModestAccountMgrPrivate *priv;
812 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
813 g_return_val_if_fail (account, FALSE);
814 g_return_val_if_fail (key, FALSE);
816 keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
818 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
819 retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);
821 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
833 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
834 const gchar *key, ModestConfValueType list_type,
835 gboolean server_account)
837 ModestAccountMgrPrivate *priv;
843 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
844 g_return_val_if_fail (name, NULL);
845 g_return_val_if_fail (key, NULL);
847 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
849 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
850 retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
852 g_printerr ("modest: error getting list '%s': %s\n", keyname,
864 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
865 const gchar * key, const gchar * val, gboolean server_account)
867 ModestAccountMgrPrivate *priv;
873 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
874 g_return_val_if_fail (name, FALSE);
875 g_return_val_if_fail (key, FALSE);
877 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
879 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
881 retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
883 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
893 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
894 const gchar * key, const gchar * val, gboolean server_account)
896 return modest_account_mgr_set_password (self, name, key, val, server_account);
902 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
903 const gchar * key, int val, gboolean server_account)
905 ModestAccountMgrPrivate *priv;
911 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
912 g_return_val_if_fail (name, FALSE);
913 g_return_val_if_fail (key, FALSE);
915 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
917 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
919 retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
921 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
932 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
933 const gchar * key, gboolean val, gboolean server_account)
935 ModestAccountMgrPrivate *priv;
941 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
942 g_return_val_if_fail (name, FALSE);
943 g_return_val_if_fail (key, FALSE);
945 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
947 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
949 retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
951 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
961 modest_account_mgr_set_list (ModestAccountMgr *self,
965 ModestConfValueType list_type,
966 gboolean server_account)
968 ModestAccountMgrPrivate *priv;
973 g_return_val_if_fail (self, FALSE);
974 g_return_val_if_fail (name, FALSE);
975 g_return_val_if_fail (key, FALSE);
976 g_return_val_if_fail (val, FALSE);
978 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
980 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
981 retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
983 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
993 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
994 gboolean server_account)
996 ModestAccountMgrPrivate *priv;
1002 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1003 g_return_val_if_fail (name, FALSE);
1005 keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
1006 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1007 retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1009 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1019 modest_account_mgr_account_with_display_name_exists (ModestAccountMgr *self, const gchar *display_name)
1021 GSList *account_names = NULL;
1022 GSList *cursor = NULL;
1024 cursor = account_names = modest_account_mgr_account_names (self,
1025 TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1027 gboolean found = FALSE;
1029 /* Look at each non-server account to check their display names; */
1031 const gchar * account_name = (gchar*)cursor->data;
1033 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1034 if (!account_data) {
1035 g_printerr ("modest: failed to get account data for %s\n", account_name);
1039 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1044 modest_account_mgr_free_account_data (self, account_data);
1045 cursor = cursor->next;
1047 modest_account_mgr_free_account_names (account_names);
1048 account_names = NULL;
1057 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1058 const gchar *key, gboolean server_account)
1060 ModestAccountMgrPrivate *priv;
1066 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1067 g_return_val_if_fail (name, FALSE);
1068 g_return_val_if_fail (key, FALSE);
1070 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1072 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1073 retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1075 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1085 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1087 /* Initialize input parameters: */
1089 *is_account_key = FALSE;
1091 if (is_server_account)
1092 *is_server_account = FALSE;
1094 const gchar* account_ns = MODEST_ACCOUNT_NAMESPACE "/";
1095 const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1097 gchar *account = NULL;
1099 /* determine whether it's an account or a server account,
1100 * based on the prefix */
1101 if (g_str_has_prefix (key, account_ns)) {
1103 if (is_server_account)
1104 *is_server_account = FALSE;
1106 account = g_strdup (key + strlen (account_ns));
1108 } else if (g_str_has_prefix (key, server_account_ns)) {
1110 if (is_server_account)
1111 *is_server_account = TRUE;
1113 account = g_strdup (key + strlen (server_account_ns));
1117 /* if there are any slashes left in the key, it's not
1118 * the toplevel entry for an account
1120 cursor = strstr(account, "/");
1122 if (is_account_key && cursor)
1123 *is_account_key = TRUE;
1125 /* put a NULL where the first slash was */
1130 /* The key is an escaped string, so unescape it to get the actual account name: */
1131 gchar *unescaped_name = modest_conf_key_unescape (account);
1133 return unescaped_name;
1140 /* must be freed by caller */
1142 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
1144 gchar *retval = NULL;
1146 gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1149 return g_strdup (namespace);
1151 /* Always escape the conf keys, so that it is acceptable to gconf: */
1152 gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
1153 gchar *escaped_name = name ? modest_conf_key_escape (name) : NULL;
1155 if (escaped_account_name && escaped_name)
1156 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1157 else if (escaped_account_name)
1158 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1161 if (!modest_conf_key_is_valid (retval)) {
1162 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1167 g_free (escaped_name);
1168 g_free (escaped_account_name);
1174 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1176 /* Check that at least one account exists: */
1177 GSList *account_names = modest_account_mgr_account_names (self,
1179 gboolean accounts_exist = account_names != NULL;
1181 modest_account_mgr_free_account_names (account_names);
1182 account_names = NULL;
1184 return accounts_exist;
1188 compare_account_name(gconstpointer a, gconstpointer b)
1190 const gchar* account_name = (const gchar*) a;
1191 const gchar* account_name2 = (const gchar*) b;
1192 return strcmp(account_name, account_name2);
1196 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name,
1199 ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1202 GSList *account_names = modest_account_mgr_account_names (self,
1205 g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1206 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1208 priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1209 g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1211 modest_account_mgr_free_account_names (account_names);
1212 account_names = NULL;
1215 g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1218 g_free(account->data);
1219 priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1220 g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1226 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1228 ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1229 return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)