+static guint signals[LAST_SIGNAL] = {0};
+
+static gchar*
+account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
+{
+ const gchar* account_ns = MODEST_ACCOUNT_NAMESPACE "/";
+ const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
+ gchar *cursor;
+ gchar *account = NULL;
+
+ /* determine whether it's an account or a server account,
+ * based on the prefix */
+ if (g_str_has_prefix (key, account_ns)) {
+
+ if (is_server_account)
+ *is_server_account = FALSE;
+
+ account = g_strdup (key + strlen (account_ns));
+
+ } else if (g_str_has_prefix (key, server_account_ns)) {
+
+ if (is_server_account)
+ *is_server_account = TRUE;
+
+ account = g_strdup (key + strlen (server_account_ns));
+ } else
+ return NULL;
+
+ /* if there are any slashes left in the key, it's not
+ * the toplevel entry for an account
+ */
+ cursor = strstr(account, "/");
+
+ if (is_account_key && cursor)
+ *is_account_key = TRUE;
+
+ /* put a NULL where the first slash was */
+ if (cursor)
+ *cursor = '\0';
+
+ return account;
+}
+
+
+
+
+static void
+on_key_change (ModestConf *conf, const gchar *key, ModestConfEvent event, gpointer user_data)
+{
+ ModestAccountMgr *self;
+ ModestAccountMgrPrivate *priv;
+
+ gchar *account;
+ gboolean is_account_key, is_server_account;
+ gboolean enabled;
+
+ self = MODEST_ACCOUNT_MGR (user_data);
+ priv = MODEST_ACCOUNT_MGR_GET_PRIVATE (self);
+
+ account = account_from_key (key, &is_account_key, &is_server_account);
+
+ /* if this is not an account-related key change, ignore */
+ if (!account)
+ return;
+
+ /* account was removed -- emit this, even if the account was disabled */
+ if (is_account_key && event == MODEST_CONF_EVENT_KEY_UNSET) {
+ g_signal_emit (G_OBJECT(self), signals[ACCOUNT_REMOVED_SIGNAL], 0,
+ account, is_server_account);
+ g_free (account);
+ return;
+ }
+
+ /* is this account enabled? */
+ if (is_server_account)
+ enabled = TRUE;
+ else
+ enabled = modest_account_mgr_account_get_enabled (self, account);
+
+ /* account was changed.
+ * and always notify when enabled/disabled changes
+ */
+ if (enabled || g_str_has_suffix (key, MODEST_ACCOUNT_ENABLED))
+ g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0,
+ account, key, is_server_account);
+
+ g_free (account);
+}