* Fixes NB#57580
[modest] / src / modest-account-mgr-priv.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30
31 #include <modest-account-mgr-priv.h>
32 #include <modest-defs.h>
33 #include <string.h>
34 #include <modest-conf.h>
35 #include <stdio.h>
36
37 gchar*
38 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
39 {
40         /* Initialize input parameters: */
41         if (is_account_key)
42                 *is_account_key = FALSE;
43
44         if (is_server_account)
45                 *is_server_account = FALSE;
46
47         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
48         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
49         gchar *cursor;
50         gchar *account = NULL;
51
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)) {
55
56                 if (is_server_account)
57                         *is_server_account = FALSE;
58                 
59                 account = g_strdup (key + strlen (account_ns));
60
61         } else if (g_str_has_prefix (key, server_account_ns)) {
62
63                 if (is_server_account)
64                         *is_server_account = TRUE;
65                 
66                 account = g_strdup (key + strlen (server_account_ns));  
67         } else
68                 return NULL;
69
70         /* if there are any slashes left in the key, it's not
71          * the toplevel entry for an account
72          */
73         cursor = strstr(account, "/");
74         
75         if (is_account_key && cursor)
76                 *is_account_key = TRUE;
77
78         /* put a NULL where the first slash was */
79         if (cursor)
80                 *cursor = '\0';
81
82         if (account) {
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);
85                 g_free (account);
86                 return unescaped_name;
87         } else
88                 return NULL;
89 }
90
91
92
93 /* must be freed by caller */
94 gchar *
95 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
96 {
97         gchar *retval = NULL;
98         
99         gchar *namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
100         
101         if (!account_name)
102                 return g_strdup (namespace);
103         
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;
107
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);
112
113         /* Sanity check: */
114         if (!modest_conf_key_is_valid (retval)) {
115                 g_warning ("%s: Generated conf key was invalid: %s", __FUNCTION__, retval);
116                 g_free (retval);
117                 retval = NULL;
118         }
119
120         g_free (escaped_name);
121         g_free (escaped_account_name);
122
123         return retval;
124 }