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,
48 DEFAULT_ACCOUNT_CHANGED_SIGNAL,
53 static GObjectClass *parent_class = NULL;
54 static guint signals[LAST_SIGNAL] = {0};
56 /* is the account already in the queue? */
58 in_change_queue (GSList *change_queue, const gchar *account)
60 GSList *cursor = change_queue;
62 const gchar *acc = cursor->data;
63 if (acc && strcmp (acc, account) == 0)
65 cursor = g_slist_next (cursor);
71 add_to_change_queue (GSList *change_queue, const gchar *account_name)
73 g_return_val_if_fail (account_name, change_queue);
74 return g_slist_prepend (change_queue, g_strdup (account_name));
78 /* we don't need to track specific accounts, as in our UI case
79 * it's impossible to change two accounts within .5 seconds.
80 * still, we might want to allow that later, and then this func
81 * will come in handy */
84 remove_from_queue (GSList *change_queue, const gchar *account)
86 GSList *cursor = change_queue;
88 const gchar *acc = cursor->data;
89 if (acc && strcmp (acc, account) == 0) {
91 return g_slist_delete_link (change_queue, cursor);
93 cursor = g_slist_next (cursor);
100 on_timeout_notify_changes (gpointer data)
102 ModestAccountMgr *self = MODEST_ACCOUNT_MGR (data);
103 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
105 GSList *cursor = priv->change_queue;
107 const gchar *account = cursor->data;
109 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
111 cursor = g_slist_next (cursor);
115 g_slist_foreach (priv->change_queue, (GFunc)g_free, NULL);
116 g_slist_free (priv->change_queue);
117 priv->change_queue = NULL;
118 priv->timeout = 0; /* hmmm */
120 return FALSE; /* don't call me again */
124 /* little hack to retrieve the account name from a server account name,
125 * by relying on the convention for that. Note: this changes the
128 * server accounts look like fooID_transport or fooID_store
129 * FIXME: make the suffixes more explicit in the account setup
132 get_account_name_from_server_account (gchar *server_account_name)
134 static const gchar *t = "ID_transport";
135 static const gchar *s = "ID_store";
136 const guint len_t = strlen (t);
137 const guint len_s = strlen (s);
139 guint len_a = strlen (server_account_name);
141 if (g_str_has_suffix (server_account_name, t))
142 server_account_name [len_a - len_t] = '\0';
143 else if (g_str_has_suffix (server_account_name, s))
144 server_account_name [len_a - len_s] = '\0';
150 on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event,
151 ModestConfNotificationId id, gpointer user_data)
153 ModestAccountMgr *self = MODEST_ACCOUNT_MGR (user_data);
154 ModestAccountMgrPrivate *priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
156 gboolean is_account_key;
157 gboolean is_server_account;
158 gchar* account_name = NULL;
160 /* Notify that the default account has changed */
161 if (key && strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
162 g_signal_emit (G_OBJECT(self), signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL], 0);
166 is_account_key = FALSE;
167 is_server_account = FALSE;
168 account_name = _modest_account_mgr_account_from_key (key, &is_account_key,
171 /* if this is not an account-related key change, ignore */
175 /* account was removed. Do not emit an account removed signal
176 because it was already being done in the remove_account
177 method. Do not notify also the removal of the server
178 account keys for the same reason */
179 if ((is_account_key || is_server_account) &&
180 event == MODEST_CONF_EVENT_KEY_UNSET) {
181 g_free (account_name);
185 /* ignore server account changes */
186 if (is_server_account)
187 /* change in place: retrieve the parent account name */
188 get_account_name_from_server_account (account_name);
190 /* is this account enabled? */
191 gboolean enabled = FALSE;
192 if (is_server_account)
195 enabled = modest_account_mgr_get_enabled (self, account_name);
197 /* Notify is server account was changed, default account was changed
198 * or when enabled/disabled changes:
200 if (!is_server_account)
201 if (enabled || g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED) ||
202 strcmp (key, MODEST_CONF_DEFAULT_ACCOUNT) == 0) {
203 if (!in_change_queue (priv->change_queue, account_name)) {
204 priv->change_queue = add_to_change_queue (priv->change_queue,
206 /* hmm, small race when this object is destroyed within
207 * 500ms of the last change, and there are multiple timeouts... */
208 priv->timeout = g_timeout_add (500, (GSourceFunc) on_timeout_notify_changes,
212 g_free (account_name);
217 modest_account_mgr_get_type (void)
219 static GType my_type = 0;
222 static const GTypeInfo my_info = {
223 sizeof (ModestAccountMgrClass),
224 modest_account_mgr_base_init, /* base init */
225 NULL, /* base finalize */
226 (GClassInitFunc) modest_account_mgr_class_init,
227 NULL, /* class finalize */
228 NULL, /* class data */
229 sizeof (ModestAccountMgr),
231 (GInstanceInitFunc) modest_account_mgr_init,
235 my_type = g_type_register_static (G_TYPE_OBJECT,
243 modest_account_mgr_base_init (gpointer g_class)
245 static gboolean modest_account_mgr_initialized = FALSE;
247 if (!modest_account_mgr_initialized) {
248 /* signal definitions */
249 signals[ACCOUNT_INSERTED_SIGNAL] =
250 g_signal_new ("account_inserted",
251 MODEST_TYPE_ACCOUNT_MGR,
253 G_STRUCT_OFFSET(ModestAccountMgrClass, account_inserted),
255 g_cclosure_marshal_VOID__STRING,
256 G_TYPE_NONE, 1, G_TYPE_STRING);
258 signals[ACCOUNT_REMOVED_SIGNAL] =
259 g_signal_new ("account_removed",
260 MODEST_TYPE_ACCOUNT_MGR,
262 G_STRUCT_OFFSET(ModestAccountMgrClass, account_removed),
264 g_cclosure_marshal_VOID__STRING,
265 G_TYPE_NONE, 1, G_TYPE_STRING);
267 signals[ACCOUNT_CHANGED_SIGNAL] =
268 g_signal_new ("account_changed",
269 MODEST_TYPE_ACCOUNT_MGR,
271 G_STRUCT_OFFSET(ModestAccountMgrClass, account_changed),
273 g_cclosure_marshal_VOID__STRING,
274 G_TYPE_NONE, 1, G_TYPE_STRING);
276 signals[ACCOUNT_BUSY_SIGNAL] =
277 g_signal_new ("account_busy_changed",
278 MODEST_TYPE_ACCOUNT_MGR,
280 G_STRUCT_OFFSET(ModestAccountMgrClass, account_busy_changed),
282 modest_marshal_VOID__STRING_BOOLEAN,
283 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);
285 signals[DEFAULT_ACCOUNT_CHANGED_SIGNAL] =
286 g_signal_new ("default_account_changed",
287 MODEST_TYPE_ACCOUNT_MGR,
289 G_STRUCT_OFFSET(ModestAccountMgrClass, default_account_changed),
291 g_cclosure_marshal_VOID__VOID,
294 modest_account_mgr_initialized = TRUE;
299 modest_account_mgr_class_init (ModestAccountMgrClass * klass)
301 GObjectClass *gobject_class;
302 gobject_class = (GObjectClass *) klass;
304 parent_class = g_type_class_peek_parent (klass);
305 gobject_class->finalize = modest_account_mgr_finalize;
307 g_type_class_add_private (gobject_class,
308 sizeof (ModestAccountMgrPrivate));
313 modest_account_mgr_init (ModestAccountMgr * obj)
315 ModestAccountMgrPrivate *priv =
316 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
318 priv->modest_conf = NULL;
319 priv->busy_accounts = NULL;
320 priv->change_queue = NULL;
323 priv->notification_id_accounts = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
327 modest_account_mgr_finalize (GObject * obj)
329 ModestAccountMgrPrivate *priv =
330 MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
332 if (priv->notification_id_accounts) {
333 /* TODO: forget dirs */
335 g_hash_table_destroy (priv->notification_id_accounts);
338 if (priv->key_changed_handler_uid) {
339 g_signal_handler_disconnect (priv->modest_conf,
340 priv->key_changed_handler_uid);
341 priv->key_changed_handler_uid = 0;
344 if (priv->modest_conf) {
345 g_object_unref (G_OBJECT(priv->modest_conf));
346 priv->modest_conf = NULL;
350 g_source_remove (priv->timeout);
354 G_OBJECT_CLASS(parent_class)->finalize (obj);
359 modest_account_mgr_new (ModestConf *conf)
362 ModestAccountMgrPrivate *priv;
364 g_return_val_if_fail (conf, NULL);
366 obj = G_OBJECT (g_object_new (MODEST_TYPE_ACCOUNT_MGR, NULL));
367 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (obj);
369 g_object_ref (G_OBJECT(conf));
370 priv->modest_conf = conf;
372 priv->key_changed_handler_uid =
373 g_signal_connect (G_OBJECT (conf), "key_changed",
374 G_CALLBACK (on_key_change),
377 return MODEST_ACCOUNT_MGR (obj);
382 null_means_empty (const gchar * str)
384 return str ? str : "";
389 modest_account_mgr_add_account (ModestAccountMgr *self,
391 const gchar *store_account,
392 const gchar *transport_account,
395 ModestAccountMgrPrivate *priv;
398 gchar *default_account;
401 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
402 g_return_val_if_fail (name, FALSE);
403 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
405 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
408 * we create the account by adding an account 'dir', with the name <name>,
409 * and in that the 'display_name' string key
411 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_DISPLAY_NAME, FALSE);
412 if (modest_account_mgr_account_exists (self, key, FALSE)) {
413 g_printerr ("modest: account already exists\n");
418 ok = modest_conf_set_string (priv->modest_conf, key, name, &err);
421 g_printerr ("modest: cannot set display name\n");
423 g_printerr ("modest: Error adding account conf: %s\n", err->message);
430 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
431 ok = modest_conf_set_string (priv->modest_conf, key, store_account, &err);
434 g_printerr ("modest: failed to set store account '%s'\n",
437 g_printerr ("modest: Error adding store account conf: %s\n", err->message);
444 if (transport_account) {
445 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
447 ok = modest_conf_set_string (priv->modest_conf, key, transport_account, &err);
450 g_printerr ("modest: failed to set transport account '%s'\n",
453 g_printerr ("modest: Error adding transport account conf: %s\n", err->message);
460 /* Make sure that leave-messages-on-server is enabled by default,
461 * as per the UI spec, though it is only meaningful for accounts using POP.
462 * (possibly this gconf key should be under the server account): */
463 modest_account_mgr_set_bool (self, name,
464 MODEST_ACCOUNT_LEAVE_ON_SERVER, TRUE, FALSE /* not server account */);
467 modest_account_mgr_set_enabled (self, name, enabled);
469 /* Notify the observers */
470 g_signal_emit (self, signals[ACCOUNT_INSERTED_SIGNAL], 0, name);
472 /* if no default account has been defined yet, do so now */
473 default_account = modest_account_mgr_get_default_account (self);
474 if (!default_account)
475 modest_account_mgr_set_default_account (self, name);
476 g_free (default_account);
483 modest_account_mgr_add_server_account (ModestAccountMgr * self,
484 const gchar * name, const gchar *hostname,
486 const gchar * username, const gchar * password,
487 ModestTransportStoreProtocol proto,
488 ModestConnectionProtocol security,
489 ModestAuthProtocol auth)
491 ModestAccountMgrPrivate *priv;
496 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
497 g_return_val_if_fail (name, FALSE);
498 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
500 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
503 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_HOSTNAME, TRUE);
504 if (modest_conf_key_exists (priv->modest_conf, key, &err)) {
505 g_printerr ("modest: server account '%s' already exists\n", name);
512 modest_conf_set_string (priv->modest_conf, key, null_means_empty(hostname), &err);
514 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
523 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_USERNAME, TRUE);
524 ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (username), &err);
526 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
536 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PASSWORD, TRUE);
537 ok = modest_conf_set_string (priv->modest_conf, key, null_means_empty (password), &err);
539 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
548 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
549 ok = modest_conf_set_string (priv->modest_conf, key,
550 modest_protocol_info_get_transport_store_protocol_name(proto),
553 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
563 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PORT, TRUE);
564 ok = modest_conf_set_int (priv->modest_conf, key, portnumber, &err);
566 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
576 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
577 ok = modest_conf_set_string (priv->modest_conf, key,
578 modest_protocol_info_get_auth_protocol_name (auth),
581 g_printerr ("modest: failed to set %s: %s\n", key, err->message);
589 /* Add the security settings: */
590 modest_server_account_set_security (self, name, security);
594 g_printerr ("modest: failed to add server account\n");
601 /** modest_account_mgr_add_server_account_uri:
602 * Only used for mbox and maildir accounts.
605 modest_account_mgr_add_server_account_uri (ModestAccountMgr * self,
606 const gchar *name, ModestTransportStoreProtocol proto,
609 ModestAccountMgrPrivate *priv;
613 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
614 g_return_val_if_fail (name, FALSE);
615 g_return_val_if_fail (strchr(name, '/') == NULL, FALSE);
616 g_return_val_if_fail (uri, FALSE);
618 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
622 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_PROTO, TRUE);
623 ok = modest_conf_set_string (priv->modest_conf, key,
624 modest_protocol_info_get_transport_store_protocol_name(proto),
629 g_printerr ("modest: failed to set proto\n");
634 key = _modest_account_mgr_get_account_keyname (name, MODEST_ACCOUNT_URI, TRUE);
635 ok = modest_conf_set_string (priv->modest_conf, key, uri, NULL);
639 g_printerr ("modest: failed to set uri\n");
646 * Utility function used by modest_account_mgr_remove_account
649 real_remove_account (ModestConf *conf,
650 const gchar *acc_name,
651 gboolean server_account)
656 key = _modest_account_mgr_get_account_keyname (acc_name, NULL, server_account);
657 modest_conf_remove_key (conf, key, &err);
661 g_printerr ("modest: error removing key: %s\n", err->message);
667 modest_account_mgr_remove_account (ModestAccountMgr * self,
670 ModestAccountMgrPrivate *priv;
671 gchar *default_account_name, *store_acc_name, *transport_acc_name;
672 gboolean default_account_deleted;
674 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
675 g_return_val_if_fail (name, FALSE);
677 if (!modest_account_mgr_account_exists (self, name, FALSE)) {
678 g_printerr ("modest: %s: account '%s' does not exist\n", __FUNCTION__, name);
682 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
683 default_account_deleted = FALSE;
685 /* If this was the default, then remove that setting: */
686 default_account_name = modest_account_mgr_get_default_account (self);
687 if (default_account_name && (strcmp (default_account_name, name) == 0)) {
688 modest_account_mgr_unset_default_account (self);
689 default_account_deleted = TRUE;
691 g_free (default_account_name);
693 /* Delete transport and store accounts */
694 store_acc_name = modest_account_mgr_get_string (self, name,
695 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE);
697 real_remove_account (priv->modest_conf, store_acc_name, TRUE);
699 transport_acc_name = modest_account_mgr_get_string (self, name,
700 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE);
701 if (transport_acc_name)
702 real_remove_account (priv->modest_conf, transport_acc_name, TRUE);
704 /* Remove the modest account */
705 real_remove_account (priv->modest_conf, name, FALSE);
707 if (default_account_deleted) {
708 /* pick another one as the new default account. We do
709 this *after* deleting the keys, because otherwise a
710 call to account_names will retrieve also the
712 modest_account_mgr_set_first_account_as_default (self);
715 /* Notify the observers. We do this *after* deleting
716 the keys, because otherwise a call to account_names
717 will retrieve also the deleted account */
718 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0, name);
725 /* strip the first /n/ character from each element
726 * caller must make sure all elements are strings with
727 * length >= n, and also that data can be freed.
731 strip_prefix_from_elements (GSList * lst, guint n)
734 memmove (lst->data, lst->data + n,
735 strlen(lst->data) - n + 1);
742 modest_account_mgr_account_names (ModestAccountMgr * self, gboolean only_enabled)
745 ModestAccountMgrPrivate *priv;
748 const size_t prefix_len = strlen (MODEST_ACCOUNT_NAMESPACE "/");
750 g_return_val_if_fail (self, NULL);
752 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
753 accounts = modest_conf_list_subkeys (priv->modest_conf,
754 MODEST_ACCOUNT_NAMESPACE, &err);
757 g_printerr ("modest: failed to get subkeys (%s): %s\n",
758 MODEST_ACCOUNT_NAMESPACE, err->message);
760 return NULL; /* assume accounts did not get value when err is set...*/
763 strip_prefix_from_elements (accounts, prefix_len);
765 GSList *result = NULL;
767 /* Unescape the keys to get the account names: */
768 GSList *iter = accounts;
773 const gchar* account_name_key = (const gchar*)iter->data;
774 gchar* unescaped_name = account_name_key ?
775 modest_conf_key_unescape (account_name_key)
780 if (unescaped_name &&
781 !modest_account_mgr_get_enabled (self, unescaped_name)) {
786 /* Ignore modest accounts whose server accounts don't exist:
787 * (We could be getting this list while the account is being deleted,
788 * while the child server accounts have already been deleted, but the
789 * parent modest account already exists.
792 gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_STORE_ACCOUNT,
794 if (server_account_name) {
795 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
797 g_free (server_account_name);
802 gchar* server_account_name = modest_account_mgr_get_string (self, account_name_key, MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
804 if (server_account_name) {
805 if (!modest_account_mgr_account_exists (self, server_account_name, TRUE))
807 g_free (server_account_name);
812 result = g_slist_append (result, unescaped_name);
814 g_free (unescaped_name);
819 iter = g_slist_next (iter);
823 /* we already freed the strings in the loop */
824 g_slist_free (accounts);
832 modest_account_mgr_free_account_names (GSList *account_names)
834 g_slist_foreach (account_names, (GFunc)g_free, NULL);
835 g_slist_free (account_names);
841 modest_account_mgr_get_string (ModestAccountMgr *self, const gchar *name,
842 const gchar *key, gboolean server_account) {
844 ModestAccountMgrPrivate *priv;
850 g_return_val_if_fail (self, NULL);
851 g_return_val_if_fail (name, NULL);
852 g_return_val_if_fail (key, NULL);
854 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
856 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
857 retval = modest_conf_get_string (priv->modest_conf, keyname, &err);
859 g_printerr ("modest: error getting string '%s': %s\n", keyname, err->message);
870 modest_account_mgr_get_password (ModestAccountMgr *self, const gchar *name,
871 const gchar *key, gboolean server_account)
873 return modest_account_mgr_get_string (self, name, key, server_account);
880 modest_account_mgr_get_int (ModestAccountMgr *self, const gchar *name, const gchar *key,
881 gboolean server_account)
883 ModestAccountMgrPrivate *priv;
889 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), -1);
890 g_return_val_if_fail (name, -1);
891 g_return_val_if_fail (key, -1);
893 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
895 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
896 retval = modest_conf_get_int (priv->modest_conf, keyname, &err);
898 g_printerr ("modest: error getting int '%s': %s\n", keyname, err->message);
910 modest_account_mgr_get_bool (ModestAccountMgr * self, const gchar *account,
911 const gchar * key, gboolean server_account)
913 ModestAccountMgrPrivate *priv;
919 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
920 g_return_val_if_fail (account, FALSE);
921 g_return_val_if_fail (key, FALSE);
923 keyname = _modest_account_mgr_get_account_keyname (account, key, server_account);
925 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
926 retval = modest_conf_get_bool (priv->modest_conf, keyname, &err);
928 g_printerr ("modest: error getting bool '%s': %s\n", keyname, err->message);
940 modest_account_mgr_get_list (ModestAccountMgr *self, const gchar *name,
941 const gchar *key, ModestConfValueType list_type,
942 gboolean server_account)
944 ModestAccountMgrPrivate *priv;
950 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), NULL);
951 g_return_val_if_fail (name, NULL);
952 g_return_val_if_fail (key, NULL);
954 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
956 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
957 retval = modest_conf_get_list (priv->modest_conf, keyname, list_type, &err);
959 g_printerr ("modest: error getting list '%s': %s\n", keyname,
971 modest_account_mgr_set_string (ModestAccountMgr * self, const gchar * name,
972 const gchar * key, const gchar * val, gboolean server_account)
974 ModestAccountMgrPrivate *priv;
980 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
981 g_return_val_if_fail (name, FALSE);
982 g_return_val_if_fail (key, FALSE);
984 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
986 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
988 retval = modest_conf_set_string (priv->modest_conf, keyname, val, &err);
990 g_printerr ("modest: error setting string '%s': %s\n", keyname, err->message);
1000 modest_account_mgr_set_password (ModestAccountMgr * self, const gchar * name,
1001 const gchar * key, const gchar * val, gboolean server_account)
1003 return modest_account_mgr_set_password (self, name, key, val, server_account);
1009 modest_account_mgr_set_int (ModestAccountMgr * self, const gchar * name,
1010 const gchar * key, int val, gboolean server_account)
1012 ModestAccountMgrPrivate *priv;
1018 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1019 g_return_val_if_fail (name, FALSE);
1020 g_return_val_if_fail (key, FALSE);
1022 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1024 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1026 retval = modest_conf_set_int (priv->modest_conf, keyname, val, &err);
1028 g_printerr ("modest: error setting int '%s': %s\n", keyname, err->message);
1039 modest_account_mgr_set_bool (ModestAccountMgr * self, const gchar * name,
1040 const gchar * key, gboolean val, gboolean server_account)
1042 ModestAccountMgrPrivate *priv;
1048 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1049 g_return_val_if_fail (name, FALSE);
1050 g_return_val_if_fail (key, FALSE);
1052 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1054 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1056 retval = modest_conf_set_bool (priv->modest_conf, keyname, val, &err);
1058 g_printerr ("modest: error setting bool '%s': %s\n", keyname, err->message);
1068 modest_account_mgr_set_list (ModestAccountMgr *self,
1072 ModestConfValueType list_type,
1073 gboolean server_account)
1075 ModestAccountMgrPrivate *priv;
1080 g_return_val_if_fail (self, FALSE);
1081 g_return_val_if_fail (name, FALSE);
1082 g_return_val_if_fail (key, FALSE);
1083 g_return_val_if_fail (val, FALSE);
1085 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1087 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1088 retval = modest_conf_set_list (priv->modest_conf, keyname, val, list_type, &err);
1090 g_printerr ("modest: error setting list '%s': %s\n", keyname, err->message);
1100 modest_account_mgr_account_exists (ModestAccountMgr * self, const gchar* name,
1101 gboolean server_account)
1103 ModestAccountMgrPrivate *priv;
1109 g_return_val_if_fail (self, FALSE);
1110 g_return_val_if_fail (name, FALSE);
1112 keyname = _modest_account_mgr_get_account_keyname (name, NULL, server_account);
1113 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1114 retval = modest_conf_key_exists (priv->modest_conf, keyname, &err);
1116 g_printerr ("modest: error determining existance of '%s': %s\n", keyname,
1126 modest_account_mgr_account_with_display_name_exists (ModestAccountMgr *self, const gchar *display_name)
1128 GSList *account_names = NULL;
1129 GSList *cursor = NULL;
1131 cursor = account_names = modest_account_mgr_account_names (self,
1132 TRUE /* enabled accounts, because disabled accounts are not user visible. */);
1134 gboolean found = FALSE;
1136 /* Look at each non-server account to check their display names; */
1138 const gchar * account_name = (gchar*)cursor->data;
1140 ModestAccountData *account_data = modest_account_mgr_get_account_data (self, account_name);
1141 if (!account_data) {
1142 g_printerr ("modest: failed to get account data for %s\n", account_name);
1146 if(account_data->display_name && (strcmp (account_data->display_name, display_name) == 0)) {
1151 modest_account_mgr_free_account_data (self, account_data);
1152 cursor = cursor->next;
1154 modest_account_mgr_free_account_names (account_names);
1155 account_names = NULL;
1164 modest_account_mgr_unset (ModestAccountMgr *self, const gchar *name,
1165 const gchar *key, gboolean server_account)
1167 ModestAccountMgrPrivate *priv;
1173 g_return_val_if_fail (MODEST_IS_ACCOUNT_MGR(self), FALSE);
1174 g_return_val_if_fail (name, FALSE);
1175 g_return_val_if_fail (key, FALSE);
1177 keyname = _modest_account_mgr_get_account_keyname (name, key, server_account);
1179 priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1180 retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
1182 g_printerr ("modest: error unsetting'%s': %s\n", keyname,
1192 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
1194 /* Initialize input parameters: */
1196 *is_account_key = FALSE;
1198 if (is_server_account)
1199 *is_server_account = FALSE;
1201 const gchar* account_ns = MODEST_ACCOUNT_NAMESPACE "/";
1202 const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
1204 gchar *account = NULL;
1206 /* determine whether it's an account or a server account,
1207 * based on the prefix */
1208 if (g_str_has_prefix (key, account_ns)) {
1210 if (is_server_account)
1211 *is_server_account = FALSE;
1213 account = g_strdup (key + strlen (account_ns));
1215 } else if (g_str_has_prefix (key, server_account_ns)) {
1217 if (is_server_account)
1218 *is_server_account = TRUE;
1220 account = g_strdup (key + strlen (server_account_ns));
1224 /* if there are any slashes left in the key, it's not
1225 * the toplevel entry for an account
1227 cursor = strstr(account, "/");
1229 if (is_account_key && cursor)
1230 *is_account_key = TRUE;
1232 /* put a NULL where the first slash was */
1237 /* The key is an escaped string, so unescape it to get the actual account name: */
1238 gchar *unescaped_name = modest_conf_key_unescape (account);
1240 return unescaped_name;
1248 /* optimization: only with non-alphanum chars, escaping is needed */
1249 inline static gboolean
1250 is_alphanum (const gchar* str)
1252 const gchar *cursor;
1253 for (cursor = str; cursor && *cursor; ++cursor) {
1254 const char c = *cursor;
1255 /* we cannot trust isalnum(3), because it might consider locales */
1256 /* numbers ALPHA alpha */
1257 if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
1264 /* must be freed by caller */
1266 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
1268 gchar *retval = NULL;
1269 gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
1270 gchar *escaped_account_name, *escaped_name;
1273 return g_strdup (namespace);
1275 /* optimization: only escape names when need to be escaped */
1276 if (is_alphanum (account_name))
1277 escaped_account_name = (gchar*)account_name;
1279 escaped_account_name = modest_conf_key_escape (account_name);
1281 if (is_alphanum (name))
1282 escaped_name = (gchar*)name;
1284 escaped_name = modest_conf_key_escape (name);
1285 //////////////////////////////////////////////////////////////
1287 if (escaped_account_name && escaped_name)
1288 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
1289 else if (escaped_account_name)
1290 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
1293 if (!modest_conf_key_is_valid (retval)) {
1294 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
1299 /* g_free is only needed if we actually allocated anything */
1300 if (name != escaped_name)
1301 g_free (escaped_name);
1302 if (account_name != escaped_account_name)
1303 g_free (escaped_account_name);
1309 modest_account_mgr_has_accounts (ModestAccountMgr* self, gboolean enabled)
1311 /* Check that at least one account exists: */
1312 GSList *account_names = modest_account_mgr_account_names (self,
1314 gboolean accounts_exist = account_names != NULL;
1316 modest_account_mgr_free_account_names (account_names);
1317 account_names = NULL;
1319 return accounts_exist;
1323 compare_account_name(gconstpointer a, gconstpointer b)
1325 const gchar* account_name = (const gchar*) a;
1326 const gchar* account_name2 = (const gchar*) b;
1327 return strcmp(account_name, account_name2);
1331 modest_account_mgr_set_account_busy(ModestAccountMgr* self, const gchar* account_name,
1334 ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1337 GSList *account_names = modest_account_mgr_account_names (self,
1340 g_slist_find_custom(account_names, account_name, (GCompareFunc) compare_account_name);
1341 if (account && !modest_account_mgr_account_is_busy(self, account_name))
1343 priv->busy_accounts = g_slist_append(priv->busy_accounts, g_strdup(account_name));
1344 g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, TRUE);
1346 modest_account_mgr_free_account_names (account_names);
1347 account_names = NULL;
1350 g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name);
1353 g_free(account->data);
1354 priv->busy_accounts = g_slist_delete_link(priv->busy_accounts, account);
1355 g_signal_emit_by_name(G_OBJECT(self), "account-busy-changed", account_name, FALSE);
1361 modest_account_mgr_account_is_busy(ModestAccountMgr* self, const gchar* account_name)
1363 ModestAccountMgrPrivate* priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
1364 return (g_slist_find_custom(priv->busy_accounts, account_name, (GCompareFunc) compare_account_name)