2007-05-23 Murray Cumming <murrayc@murrayc.com>
[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
36 gchar*
37 _modest_account_mgr_account_from_key (const gchar *key, gboolean *is_account_key, gboolean *is_server_account)
38 {
39         /* Initialize input parameters: */
40         if (is_account_key)
41                 *is_account_key = FALSE;
42
43         if (is_server_account)
44                 *is_server_account = FALSE;
45
46         const gchar* account_ns        = MODEST_ACCOUNT_NAMESPACE "/";
47         const gchar* server_account_ns = MODEST_SERVER_ACCOUNT_NAMESPACE "/";
48         gchar *cursor;
49         gchar *account = NULL;
50
51         /* determine whether it's an account or a server account,
52          * based on the prefix */
53         if (g_str_has_prefix (key, account_ns)) {
54
55                 if (is_server_account)
56                         *is_server_account = FALSE;
57                 
58                 account = g_strdup (key + strlen (account_ns));
59
60         } else if (g_str_has_prefix (key, server_account_ns)) {
61
62                 if (is_server_account)
63                         *is_server_account = TRUE;
64                 
65                 account = g_strdup (key + strlen (server_account_ns));  
66         } else
67                 return NULL;
68
69         /* if there are any slashes left in the key, it's not
70          * the toplevel entry for an account
71          */
72         cursor = strstr(account, "/");
73         
74         if (is_account_key && cursor)
75                 *is_account_key = TRUE;
76
77         /* put a NULL where the first slash was */
78         if (cursor)
79                 *cursor = '\0';
80
81         return account;
82 }
83
84
85
86 /* must be freed by caller */
87 gchar *
88 _modest_account_mgr_get_account_keyname (const gchar *account_name, const gchar * name, gboolean server_account)
89 {
90         gchar *namespace;
91         gchar *retval;
92         
93         namespace = server_account ? MODEST_SERVER_ACCOUNT_NAMESPACE : MODEST_ACCOUNT_NAMESPACE;
94         
95         if (!account_name)
96                 return g_strdup (namespace);
97         
98         if (name)
99                 retval = g_strconcat (namespace, "/", account_name, "/", name, NULL);
100         else
101                 retval = g_strconcat (namespace, "/", account_name, NULL);
102
103         /* special case: the key has some weird characters */
104         if (!modest_conf_key_is_valid (retval)) {
105
106                 gchar *account_name_esc, *name_esc;
107                 g_free (retval);
108                 
109                 account_name_esc = account_name ? modest_conf_key_escape (account_name) : NULL;
110                 name_esc         = name ? modest_conf_key_escape (name) : NULL;
111                 
112                 retval =  _modest_account_mgr_get_account_keyname (account_name_esc, name_esc, server_account);
113
114                 g_free (account_name_esc);
115                 g_free (name_esc);
116         }
117         return retval;
118 }