539a0d6630de803142a7d61712cd9602fa848628
[modest] / src / modest-account-mgr-helpers.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 #include <modest-account-mgr-helpers.h>
31 #include <modest-account-mgr-priv.h>
32
33
34 gboolean
35 modest_account_mgr_account_set_enabled (ModestAccountMgr *self, const gchar* name,
36                                         gboolean enabled)
37 {
38         return modest_account_mgr_set_bool (self, name,
39                                             MODEST_ACCOUNT_ENABLED, enabled,
40                                             FALSE, NULL);
41 }
42
43
44 gboolean
45 modest_account_mgr_account_get_enabled (ModestAccountMgr *self, const gchar* name)
46 {
47         return modest_account_mgr_get_bool (self, name,
48                                             MODEST_ACCOUNT_ENABLED, FALSE,
49                                             NULL);
50 }
51
52
53 static ModestServerAccountData*
54 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
55 {
56         ModestServerAccountData *data;
57         gchar *proto;
58         
59         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
60                                                                  TRUE, NULL), NULL);    
61         data = g_new0 (ModestServerAccountData, 1);
62
63         data->account_name = g_strdup (name);
64         data->hostname     = modest_account_mgr_get_string (self, name,
65                                                             MODEST_ACCOUNT_HOSTNAME,
66                                                             TRUE, NULL);
67         data->username     = modest_account_mgr_get_string (self, name,
68                                                             MODEST_ACCOUNT_USERNAME,
69                                                             TRUE, NULL);
70         
71         proto        = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO,
72                                                       TRUE, NULL);
73         data->proto  = modest_protocol_info_get_protocol (proto);
74         g_free (proto);
75         
76         data->password     = modest_account_mgr_get_string (self, name,
77                                                             MODEST_ACCOUNT_PASSWORD,
78                                                             TRUE, NULL);
79
80         data->options = modest_account_mgr_get_list (self, name,
81                                                      MODEST_ACCOUNT_OPTIONS,
82                                                      MODEST_CONF_VALUE_STRING,
83                                                      TRUE, NULL);
84         return data;
85 }
86
87
88 static void
89 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
90                                              ModestServerAccountData* data)
91 {
92         g_return_if_fail (self);
93
94         if (!data)
95                 return; /* not an error */
96
97         g_free (data->account_name);
98         data->account_name = NULL;
99         
100         g_free (data->hostname);
101         data->hostname = NULL;
102         
103         g_free (data->username);
104         data->username = NULL;
105
106         g_free (data->password);
107         data->password = NULL;
108         
109         if (data->options) {
110                 GSList *tmp = data->options;
111                 while (tmp) {
112                         g_free (tmp->data);
113                         tmp = g_slist_next (tmp);
114                 }
115                 g_slist_free (data->options);
116         }
117
118         g_free (data);
119 }
120
121 ModestAccountData*
122 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
123 {
124         ModestAccountData *data;
125         gchar *server_account;
126         
127         g_return_val_if_fail (self, NULL);
128         g_return_val_if_fail (name, NULL);
129         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
130                                                                  FALSE, NULL), NULL);   
131         data = g_new0 (ModestAccountData, 1);
132
133         data->account_name = g_strdup (name);
134
135         data->display_name = modest_account_mgr_get_string (self, name,
136                                                             MODEST_ACCOUNT_DISPLAY_NAME,
137                                                             FALSE, NULL);
138         data->fullname      = modest_account_mgr_get_string (self, name,
139                                                               MODEST_ACCOUNT_FULLNAME,
140                                                                FALSE, NULL);
141         data->email        = modest_account_mgr_get_string (self, name,
142                                                             MODEST_ACCOUNT_EMAIL,
143                                                             FALSE, NULL);
144         data->enabled      = modest_account_mgr_account_get_enabled (self, name);
145
146         /* store */
147         server_account     = modest_account_mgr_get_string (self, name,
148                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
149                                                             FALSE, NULL);
150         if (server_account) {
151                 data->store_account =
152                         modest_account_mgr_get_server_account_data (self,
153                                                                     server_account);
154                 g_free (server_account);
155         }
156
157         /* transport */
158         server_account = modest_account_mgr_get_string (self, name,
159                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
160                                                         FALSE, NULL);
161         if (server_account) {
162                 data->transport_account =
163                         modest_account_mgr_get_server_account_data (self,
164                                                                     server_account);
165                 g_free (server_account);
166         }
167
168         return data;
169 }
170
171
172 void
173 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
174 {
175         g_return_if_fail (self);
176
177         if (!data) /* not an error */ 
178                 return;
179
180         g_free (data->account_name);
181         g_free (data->display_name);
182         g_free (data->fullname);
183         g_free (data->email);
184
185         modest_account_mgr_free_server_account_data (self, data->store_account);
186         modest_account_mgr_free_server_account_data (self, data->transport_account);
187         
188         g_free (data);
189 }
190
191
192 gchar*
193 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
194 {
195         gchar *account; 
196         ModestConf *conf;
197         
198         g_return_val_if_fail (self, NULL);
199
200         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
201         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
202                                           NULL);
203         
204         /* it's not really an error if there is no default account */
205         if (!account) 
206                 return NULL;
207
208         /* sanity check */
209         if (!modest_account_mgr_account_exists (self, account, FALSE, NULL)) {
210                 g_printerr ("modest: default account does not exist\n");
211                 g_free (account);
212                 return NULL;
213         }
214
215         return account;
216 }
217
218
219 gboolean
220 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
221 {
222         ModestConf *conf;
223         
224         g_return_val_if_fail (self,    FALSE);
225         g_return_val_if_fail (account, FALSE);
226         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE, NULL),
227                               FALSE);
228         
229         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
230                 
231         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
232                                        account, NULL);
233
234 }
235
236
237