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