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-account-mgr-priv.h>
32 #include <modest-defs.h>
34 #include <modest-conf.h>
38 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
40 /* Initialize input parameters: */
42 *is_account_key = FALSE;
44 if (is_server_account)
45 *is_server_account = FALSE;
47 const gchar* account_ns = MODEST_ACCOUNT_NAMESPACE "/";
48 const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
50 gchar *account = NULL;
52 /* determine whether it's an account or a server account,
53 * based on the prefix */
54 if (g_str_has_prefix (key, account_ns)) {
56 if (is_server_account)
57 *is_server_account = FALSE;
59 account = g_strdup (key + strlen (account_ns));
61 } else if (g_str_has_prefix (key, server_account_ns)) {
63 if (is_server_account)
64 *is_server_account = TRUE;
66 account = g_strdup (key + strlen (server_account_ns));
70 /* if there are any slashes left in the key, it's not
71 * the toplevel entry for an account
73 cursor = strstr(account, "/");
75 if (is_account_key && cursor)
76 *is_account_key = TRUE;
78 /* put a NULL where the first slash was */
83 /* The key is an escaped string, so unescape it to get the actual account name: */
84 gchar *unescaped_name = modest_conf_key_unescape (account);
86 return unescaped_name;
93 /* must be freed by caller */
95 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
99 gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
102 return g_strdup (namespace);
104 /* Always escape the conf keys, so that it is acceptable to gconf: */
105 gchar *escaped_account_name = account_name ? modest_conf_key_escape (account_name) : NULL;
106 gchar *escaped_name = name ? modest_conf_key_escape (name) : NULL;
108 if (escaped_account_name && escaped_name)
109 retval = g_strconcat (namespace, "/", escaped_account_name, "/", escaped_name, NULL);
110 else if (escaped_account_name)
111 retval = g_strconcat (namespace, "/", escaped_account_name, NULL);
114 if (!modest_conf_key_is_valid (retval)) {
115 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
120 g_free (escaped_name);
121 g_free (escaped_account_name);