+ retval = modest_conf_remove_key (priv->modest_conf, keyname, &err);
+ if (err) {
+ g_printerr ("modest: error unsetting'%s': %s\n", keyname,
+ err->message);
+ g_error_free (err);
+ retval = FALSE;
+ }
+ return retval;
+}
+
+gchar*
+_modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
+{
+ /* Initialize input parameters: */
+ if (is_account_key)
+ *is_account_key = FALSE;
+
+ if (is_server_account)
+ *is_server_account = FALSE;
+
+ 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';
+
+ if (account) {
+ /* The key is an escaped string, so unescape it to get the actual account name: */
+ gchar *unescaped_name = modest_conf_key_unescape (account);
+ g_free (account);
+ return unescaped_name;
+ } else
+ return NULL;
+}
+
+
+
+
+
+/* optimization: only with non-alphanum chars, escaping is needed */
+inline static gboolean
+is_alphanum (const gchar* str)
+{
+ const gchar *cursor;
+ for (cursor = str; cursor && *cursor; ++cursor) {
+ const char c = *cursor;
+ /* we cannot trust isalnum(3), because it might consider locales */
+ /* numbers ALPHA alpha */
+ if (!((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122)))
+ return FALSE;
+ }
+ return TRUE;
+}
+
+
+
+
+/* must be freed by caller */
+gchar *
+_modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar* name,
+ gboolean server_account)
+{
+ gchar *retval = NULL;
+ gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
+ gchar *escaped_account_name, *escaped_name;
+
+ if (!account_name)
+ return g_strdup (namespace);
+
+ /* optimization: only escape names when need to be escaped */
+ if (is_alphanum (account_name))
+ escaped_account_name = (gchar*)account_name;
+ else
+ escaped_account_name = modest_conf_key_escape (account_name);
+
+ if (is_alphanum (name))
+ escaped_name = (gchar*)name;
+ else
+ escaped_name = modest_conf_key_escape (name);
+ //////////////////////////////////////////////////////////////
+
+ if (escaped_account_name && escaped_name)
+ retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
+ else if (escaped_account_name)
+ retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
+
+ /* Sanity check: */
+ if (!retval || !modest_conf_key_is_valid (retval)) {
+ g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__,
+ retval ? retval: "<empty>");
+ g_free (retval);
+ retval = NULL;
+ }
+
+ /* g_free is only needed if we actually allocated anything */
+ if (name != escaped_name)
+ g_free (escaped_name);
+ if (account_name != escaped_account_name)
+ g_free (escaped_account_name);