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);
40 static void modest_account_mgr_base_init (gpointer g_class);
44 ACCOUNT_INSERTED_SIGNAL,
45 ACCOUNT_CHANGED_SIGNAL,
46 ACCOUNT_REMOVED_SIGNAL,
52 static GObjectClass *parent_class = NULL;
53 static guint signals[LAST_SIGNAL] = {0};
55 /* /\* We signal key changes in batches, every X seconds: *\/ */
57 /* on_timeout_notify_changes (gpointer data) */
59 /* ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data); */
60 /* ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self); */
62 /* /\* TODO: Also store the account names, and notify one list for each account, */
63 /* * if anything uses the account names. *\/ */
65 /* if (priv->changed_conf_keys) { */
66 /* gchar *default_account = */
67 /* modest_account_mgr_get_default_account (self); */
69 /* /\* printf ("DEBUG: %s: priv->changed_conf_key length=%d\n", */
70 /* __FUNCTION__, g_slist_length (priv->changed_conf_keys)); *\/ */
71 /* g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, */
72 /* default_account, priv->changed_conf_keys, FALSE); */
74 /* g_free (default_account); */
76 /* g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL); */
77 /* g_slist_free (priv->changed_conf_keys); */
78 /* priv->changed_conf_keys = NULL; */
81 /* return TRUE; /\* Call this again later. *\/ */
85 /* on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpointer user_data) */
87 /* ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data); */
88 /* ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self); */
89 /* gboolean is_account_key; */
90 /* gboolean is_server_account; */
91 /* gchar* account = NULL; */
93 /* /\* there is only one not-really-account key which will still emit */
94 /* * a signal: a change in MODEST_CONF_DEFAULT_ACCOUNT *\/ */
95 /* if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) { */
96 /* /\* Get the default account instead. *\/ */
98 /* /\* Store the key for later notification in our timeout callback. */
99 /* * Notifying for every key change would cause unnecessary work: *\/ */
100 /* priv->changed_conf_keys = g_slist_append (priv->changed_conf_keys, */
101 /* (gpointer) g_strdup (key)); */
104 /* is_account_key = FALSE; */
105 /* is_server_account = FALSE; */
106 /* account = _modest_account_mgr_account_from_key (key, &is_account_key, */
107 /* &is_server_account); */
109 /* /\* if this is not an account-related key change, ignore *\/ */
113 /* /\* account was removed. Do not emit an account removed signal */
114 /* because it was already being done in the remove_account */
115 /* method. Do not notify also the removal of the server */
116 /* account keys for the same reason *\/ */
117 /* if ((is_account_key || is_server_account) && */
118 /* event == MODEST_CONF_EVENT_KEY_UNSET) { */
119 /* g_free (account); */
123 /* /\* is this account enabled? *\/ */
124 /* gboolean enabled = FALSE; */
125 /* if (is_server_account) */
126 /* enabled = TRUE; */
128 /* enabled = modest_account_mgr_get_enabled (self, account); */
130 /* /\* Notify is server account was changed, default account was changed */
131 /* * or when enabled/disabled changes: */
134 /* g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) || */
135 /* strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) { */
136 /* /\* Store the key for later notification in our timeout callback. */
137 /* * Notifying for every key change would cause unnecessary work: *\/ */
138 /* priv->changed_conf_keys = g_slist_append (NULL, */
139 /* (gpointer) g_strdup (key)); */
141 /* g_free (account); */
146 modest_account_mgr_get_type (void)
148 static GType my_type = 0;
151 static const GTypeInfo my_info = {
152 sizeof (ModestAccountMgrClass),
153 modest_account_mgr_base_init, /* base init */
154 NULL, /* base finalize */
155 (GClassInitFunc) modest_account_mgr_class_init,
156 NULL, /* class finalize */
157 NULL, /* class data */
158 sizeof (ModestAccountMgr),
160 (GInstanceInitFunc) modest_account_mgr_init,
164 my_type = g_type_register_static (G_TYPE_OBJECT,
172 modest_account_mgr_base_init (gpointer g_class)
174 static gboolean modest_account_mgr_initialized = FALSE;
176 if (!modest_account_mgr_initialized) {
177 /* signal definitions */
178 signals[ACCOUNT_INSERTED_SIGNAL] =
179 g_signal_new ("account_inserted",
180 MODEST_TYPE_ACCOUNT_MGR,
182 G_STRUCT_OFFSET(ModestAccountMgrClass,account_inserted),
184 g_cclosure_marshal_VOID__STRING,
185 G_TYPE_NONE, 1, G_TYPE_STRING);
187 signals[ACCOUNT_REMOVED_SIGNAL] =
188 g_signal_new ("account_removed",
189 MODEST_TYPE_ACCOUNT_MGR,
191 G_STRUCT_OFFSET(ModestAccountMgrClass,account_removed),
193 g_cclosure_marshal_VOID__STRING,
194 G_TYPE_NONE, 1, G_TYPE_STRING);
196 signals[ACCOUNT_CHANGED_SIGNAL] =
197 g_signal_new ("account_changed",
198 MODEST_TYPE_ACCOUNT_MGR,
200 G_STRUCT_OFFSET(ModestAccountMgrClass,account_changed),
202 modest_marshal_VOID__STRING_POINTER_BOOLEAN,
203 G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
205 signals[ACCOUNT_BUSY_SIGNAL] =
206 g_signal_new ("account_busy_changed",
207 MODEST_TYPE_ACCOUNT_MGR,
209 G_STRUCT_OFFSET(ModestAccountMgrClass,account_busy_changed),
211 modest_marshal_VOID__STRING_BOOLEAN,
212 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
214 modest_account_mgr_initialized = TRUE;
219 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
221 GObjectClass *gobject_class;
222 gobject_class = (GObjectClass *) klass;
224 parent_class = g_type_class_peek_parent (klass);
225 gobject_class->finalize = modest_account_mgr_finalize;
227 g_type_class_add_private (gobject_class,
228 sizeof (ModestAccountMgrPrivate));
233 modest_account_mgr_init (ModestAccountMgr * obj)
235 ModestAccountMgrPrivate *priv =
236 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
238 priv->modest_conf = NULL;
239 priv->busy_accounts = NULL;
241 priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
245 modest_account_mgr_finalize (GObject * obj)
247 ModestAccountMgrPrivate *priv =
248 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
250 if (priv->notification_id_accounts) {
251 /* TODO: forget dirs */
253 g_hash_table_destroy (priv->notification_id_accounts);
256 if (priv->key_changed_handler_uid) {
257 g_signal_handler_disconnect (priv->modest_conf,
258 priv->key_changed_handler_uid);
259 priv->key_changed_handler_uid = 0;
262 if (priv->modest_conf) {
263 g_object_unref (G_OBJECT(priv->modest_conf));
264 priv->modest_conf = NULL;
267 /* if (priv->timeout) */
268 /* g_source_remove (priv->timeout); */
270 /* if (priv->changed_conf_keys) { */
271 /* g_slist_foreach (priv->changed_conf_keys, (GFunc) g_free, NULL); */
272 /* g_slist_free (priv->changed_conf_keys); */
275 G_OBJECT_CLASS(parent_class)->finalize (obj);
280 modest_account_mgr_new (ModestConf *conf)
283 ModestAccountMgrPrivate *priv;
285 g_return_val_if_fail (conf, NULL);
287 obj = G_OBJECT (g_object_new (MODEST_TYPE_ACCOUNT_MGR, NULL));
288 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
290 g_object_ref (G_OBJECT(conf));
291 priv->modest_conf = conf;
293 /* priv->key_changed_handler_uid = */
294 /* g_signal_connect (G_OBJECT (conf), "key_changed", */
295 /* G_CALLBACK (on_key_change), */
298 return MODEST_ACCOUNT_MGR (obj);
303 null_means_empty (const gchar * str)
305 return str ? str : "";
310 modest_account_mgr_add_account (ModestAccountMgr *self,
312 const gchar *store_account,
313 const gchar *transport_account,
316 ModestAccountMgrPrivate *priv;
319 gchar *default_account;
322 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
323 g_return_val_if_fail (name, FALSE);
324 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
326 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
329 * we create the account by adding an account 'dir', with the name <name>,
330 * and in that the 'display_name' string key
332 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
333 if (modest_account_mgr_account_exists (self, key, FALSE)) {
334 g_printerr ("modest: account already exists\n");
339 ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
342 g_printerr ("modest: cannot set display name\n");
344 g_printerr ("modest: Error adding account conf: %s\n", err->message);
351 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
352 ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
355 g_printerr ("modest: failed to set store account '%s'\n",
358 g_printerr ("modest: Error adding store account conf: %s\n", err->message);
365 if (transport_account) {
366 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
368 ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
371 g_printerr ("modest: failed to set transport account '%s'\n",
374 g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
381 /* Make sure that leave-messages-on-server is enabled by default,
382 * as per the UI spec, though it is only meaningful for accounts using POP.
383 * (possibly this gconf key should be under the server account): */
384 modest_account_mgr_set_bool (self, name,
385 MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE /* not server account */);
388 modest_account_mgr_set_enabled (self, name, enabled);
390 /* Notify the observers */
391 g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
393 /* if no default account has been defined yet, do so now */
394 default_account = modest_account_mgr_get_default_account (self);
395 if (!default_account)
396 modest_account_mgr_set_default_account (self, name);
397 g_free (default_account);
404 modest_account_mgr_add_server_account (ModestAccountMgr * self,
405 const gchar * name, const gchar *hostname,
407 const gchar * username, const gchar * password,
408 ModestTransportStoreProtocol proto,
409 ModestConnectionProtocol security,
410 ModestAuthProtocol auth)
412 ModestAccountMgrPrivate *priv;
417 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
418 g_return_val_if_fail (name, FALSE);
419 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
421 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
424 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
425 if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
426 g_printerr ("modest: server account '%s' already exists\n", name);
433 modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &err);
435 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
444 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_USERNAME, TRUE);
445 ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
447 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
457 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PASSWORD, TRUE);
458 ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
460 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
469 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
470 ok = modest_conf_set_string (priv->modest_conf, key,
471 modest_protocol_info_get_transport_store_protocol_name(proto),
474 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
484 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PORT, TRUE);
485 ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
487 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
497 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
498 ok = modest_conf_set_string (priv->modest_conf, key,
499 modest_protocol_info_get_auth_protocol_name (auth),
502 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
510 /* Add the security settings: */
511 modest_server_account_set_security (self, name, security);
515 g_printerr ("modest: failed to add server account\n");
522 /** modest_account_mgr_add_server_account_uri:
523 * Only used for mbox and maildir accounts.
526 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
527 const gchar *name, ModestTransportStoreProtocol proto,
530 ModestAccountMgrPrivate *priv;
534 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
535 g_return_val_if_fail (name, FALSE);
536 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
537 g_return_val_if_fail (uri, FALSE);
539 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
543 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
544 ok = modest_conf_set_string (priv->modest_conf, key,
545 modest_protocol_info_get_transport_store_protocol_name(proto),
550 g_printerr ("modest: failed to set proto\n");
555 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_URI, TRUE);
556 ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
560 g_printerr ("modest: failed to set uri\n");
567 * Utility function used by modest_account_mgr_remove_account
570 real_remove_account (ModestConf *conf,
571 const gchar *acc_name,
572 gboolean server_account)
577 key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
578 modest_conf_remove_key (conf, key, &err);
582 g_printerr ("modest: error removing key: %s\n", err->message);
588 modest_account_mgr_remove_account (ModestAccountMgr * self,
591 ModestAccountMgrPrivate *priv;
592 gchar *default_account_name, *store_acc_name, *transport_acc_name;
593 gboolean default_account_deleted;
595 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
596 g_return_val_if_fail (name, FALSE);
598 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
599 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
603 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
604 default_account_deleted = FALSE;
606 /* If this was the default, then remove that setting: */
607 default_account_name = modest_account_mgr_get_default_account (self);
608 if (default_account_name && (strcmp (default_account_name, name) == 0)) {
609 modest_account_mgr_unset_default_account (self);
610 default_account_deleted = TRUE;
612 g_free (default_account_name);
614 /* Delete transport and store accounts */
615 store_acc_name = modest_account_mgr_get_string (self, name,
616 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
618 real_remove_account (priv->modest_conf, store_acc_name, TRUE);
620 transport_acc_name = modest_account_mgr_get_string (self, name,
621 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
622 if (transport_acc_name)
623 real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
625 /* Remove the modest account */
626 real_remove_account (priv->modest_conf, name, FALSE);
628 if (default_account_deleted) {
629 /* pick another one as the new default account. We do
630 this *after* deleting the keys, because otherwise a
631 call to account_names will retrieve also the
633 modest_account_mgr_set_first_account_as_default (self);
636 /* Notify the observers. We do this *after* deleting
637 the keys, because otherwise a call to account_names
638 will retrieve also the deleted account */
639 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
646 /* strip the first /n/ character from each element
647 * caller must make sure all elements are strings with
648 * length >= n, and also that data can be freed.
652 strip_prefix_from_elements (GSList * lst, guint n)
655 memmove (lst->data, lst->data + n,
656 strlen(lst->data) - n + 1);
663 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
666 ModestAccountMgrPrivate *priv;
669 const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
671 g_return_val_if_fail (self, NULL);
673 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
674 accounts = modest_conf_list_subkeys (priv->modest_conf,
675 MODEST_ACCOUNT_NAMESPACE, &err);
678 g_printerr ("modest: failed to get subkeys (%s): %s\n",
679 MODEST_ACCOUNT_NAMESPACE, err->message);
681 return NULL; /* assume accounts did not get value when err is set...*/
684 strip_prefix_from_elements (accounts, prefix_len);
686 GSList *result = NULL;
688 /* Unescape the keys to get the account names: */
689 GSList *iter = accounts;
694 const gchar* account_name_key = (const gchar*)iter->data;
695 gchar* unescaped_name = account_name_key ?
696 modest_conf_key_unescape (account_name_key)
701 if (unescaped_name &&
702 !modest_account_mgr_get_enabled (self, unescaped_name)) {
707 /* Ignore modest accounts whose server accounts don't exist:
708 * (We could be getting this list while the account is being deleted,
709 * while the child server accounts have already been deleted, but the
710 * parent modest account already exists.
713 gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
715 if (server_account_name) {
716 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
718 g_free (server_account_name);
723 gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
725 if (server_account_name) {
726 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
728 g_free (server_account_name);
733 result = g_slist_append (result, unescaped_name);
735 g_free (unescaped_name);
740 iter = g_slist_next (iter);
744 /* we already freed the strings in the loop */
745 g_slist_free (accounts);
753 modest_account_mgr_free_account_names (GSList *account_names)
755 g_slist_foreach (account_names, (GFunc)g_free, NULL);
756 g_slist_free (account_names);
762 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
763 const gchar *key, gboolean server_account) {
765 ModestAccountMgrPrivate *priv;
771 g_return_val_if_fail (self, NULL);
772 g_return_val_if_fail (name, NULL);
773 g_return_val_if_fail (key, NULL);
775 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
777 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
778 retval = modest_conf_get_string (priv->modest_conf, keyname, &err);
780 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
791 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
792 const gchar *key, gboolean server_account)
794 return modest_account_mgr_get_string (self, name, key, server_account);
801 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
802 gboolean server_account)
804 ModestAccountMgrPrivate *priv;
810 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
811 g_return_val_if_fail (name, -1);
812 g_return_val_if_fail (key, -1);
814 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
816 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
817 retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
819 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
831 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
832 const gchar * key, gboolean server_account)
834 ModestAccountMgrPrivate *priv;
840 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
841 g_return_val_if_fail (account, FALSE);
842 g_return_val_if_fail (key, FALSE);
844 keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
846 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
847 retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);
849 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
861 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
862 const gchar *key, ModestConfValueType list_type,
863 gboolean server_account)
865 ModestAccountMgrPrivate *priv;
871 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
872 g_return_val_if_fail (name, NULL);
873 g_return_val_if_fail (key, NULL);
875 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
877 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
878 retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
880 g_printerr ("modest: error getting list '%s': %s\n", keyname,
892 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
893 const gchar * key, const gchar * val, gboolean server_account)
895 ModestAccountMgrPrivate *priv;
901 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
902 g_return_val_if_fail (name, FALSE);
903 g_return_val_if_fail (key, FALSE);
905 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
907 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
909 retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
911 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
921 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
922 const gchar * key, const gchar * val, gboolean server_account)
924 return modest_account_mgr_set_password (self, name, key, val, server_account);
930 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
931 const gchar * key, int val, gboolean server_account)
933 ModestAccountMgrPrivate *priv;
939 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
940 g_return_val_if_fail (name, FALSE);
941 g_return_val_if_fail (key, FALSE);
943 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
945 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
947 retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
949 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
960 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
961 const gchar * key, gboolean val, gboolean server_account)
963 ModestAccountMgrPrivate *priv;
969 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
970 g_return_val_if_fail (name, FALSE);
971 g_return_val_if_fail (key, FALSE);
973 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
975 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
977 retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
979 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
989 modest_account_mgr_set_list (ModestAccountMgr *self,
993 ModestConfValueType list_type,
994 gboolean server_account)
996 ModestAccountMgrPrivate *priv;
1001 g_return_val_if_fail (self, FALSE);
1002 g_return_val_if_fail (name, FALSE);
1003 g_return_val_if_fail (key, FALSE);
1004 g_return_val_if_fail (val, FALSE);
1006 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1008 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1009 retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
1011 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
1021 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar * name,
1022 gboolean server_account)
1024 ModestAccountMgrPrivate *priv;
1030 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1031 g_return_val_if_fail (name, FALSE);
1033 keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
1034 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1035 retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1037 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1047 modest_account_mgr_account_with_display_name_exists (ModestAccountMgr *self, const gchar *display_name)
1049 GSList *account_names = NULL;
1050 GSList *cursor = NULL;
1052 cursor = account_names = modest_account_mgr_account_names (self,
1053 TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1055 gboolean found = FALSE;
1057 /* Look at each non-server account to check their display names; */
1059 const gchar * account_name = (gchar*)cursor->data;
1061 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1062 if (!account_data) {
1063 g_printerr ("modest: failed to get account data for %s\n", account_name);
1067 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1072 modest_account_mgr_free_account_data (self, account_data);
1073 cursor = cursor->next;
1075 modest_account_mgr_free_account_names (account_names);
1076 account_names = NULL;
1085 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1086 const gchar *key, gboolean server_account)
1088 ModestAccountMgrPrivate *priv;
1094 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1095 g_return_val_if_fail (name, FALSE);
1096 g_return_val_if_fail (key, FALSE);
1098 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1100 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1101 retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1103 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1113 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1115 /* Initialize input parameters: */
1117 *is_account_key = FALSE;
1119 if (is_server_account)
1120 *is_server_account = FALSE;
1122 const gchar* account_ns = MODEST_ACCOUNT_NAMESPACE "/";
1123 const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1125 gchar *account = NULL;
1127 /* determine whether it's an account or a server account,
1128 * based on the prefix */
1129 if (g_str_has_prefix (key, account_ns)) {
1131 if (is_server_account)
1132 *is_server_account = FALSE;
1134 account = g_strdup (key + strlen (account_ns));
1136 } else if (g_str_has_prefix (key, server_account_ns)) {
1138 if (is_server_account)
1139 *is_server_account = TRUE;
1141 account = g_strdup (key + strlen (server_account_ns));
1145 /* if there are any slashes left in the key, it's not
1146 * the toplevel entry for an account
1148 cursor = strstr(account, "/");
1150 if (is_account_key && cursor)
1151 *is_account_key = TRUE;
1153 /* put a NULL where the first slash was */
1158 /* The key is an escaped string, so unescape it to get the actual account name: */
1159 gchar *unescaped_name = modest_conf_key_unescape (account);
1161 return unescaped_name;
1168 /* must be freed by caller */
1170 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
1172 gchar *retval = NULL;
1174 gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1177 return g_strdup (namespace);
1179 /* Always escape the conf keys, so that it is acceptable to gconf: */
1180 gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
1181 gchar *escaped_name = name ? modest_conf_key_escape (name) : NULL;
1183 if (escaped_account_name && escaped_name)
1184 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1185 else if (escaped_account_name)
1186 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1189 if (!modest_conf_key_is_valid (retval)) {
1190 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1195 g_free (escaped_name);
1196 g_free (escaped_account_name);
1202 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1204 /* Check that at least one account exists: */
1205 GSList *account_names = modest_account_mgr_account_names (self,
1207 gboolean accounts_exist = account_names != NULL;
1209 modest_account_mgr_free_account_names (account_names);
1210 account_names = NULL;
1212 return accounts_exist;
1216 compare_account_name(gconstpointer a, gconstpointer b)
1218 const gchar* account_name = (const gchar*) a;
1219 const gchar* account_name2 = (const gchar*) b;
1220 return strcmp(account_name, account_name2);
1224 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name,
1227 ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1230 GSList *account_names = modest_account_mgr_account_names (self,
1233 g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1234 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1236 priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1237 g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1239 modest_account_mgr_free_account_names (account_names);
1240 account_names = NULL;
1243 g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1246 g_free(account->data);
1247 priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1248 g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1254 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1256 ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1257 return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)