2007-04-17 Murray Cumming <murrayc@murrayc.com>
[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 #include <tny-simple-list.h>
33 #include <modest-runtime.h>
34 #include <string.h>
35
36 gboolean
37 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
38                                         gboolean enabled)
39 {
40         return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
41 }
42
43
44 gboolean
45 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
46 {
47         return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
48 }
49
50 static gint
51 compare_option_strings_for_name (const gchar* a, const gchar* b)
52 {
53         printf("  debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b);
54         const gchar* sep = strchr(a, '=');
55         if (!sep)
56                 return -1;
57                 
58         gint len = sep - a;
59         if(len <= 0)
60                 return -1;
61                 
62         /* Get the part of the string before the =.
63          * Note that this allocation is inefficient just so we can do a strcmp. */
64         gchar* name = g_malloc (len+1);
65         memcpy(name, a, len);
66         name[len] = 0; /* Null-termination. */
67         
68         printf("    debug: name=%s\n", name);
69
70         gint result = strcmp (name, b);
71         
72         g_free (name);
73         
74         return result;
75 }
76            
77 gchar*
78 modest_server_account_data_get_option_value (GSList* options_list, const gchar* option_name)
79 {
80         if (!options_list)
81                 return NULL;
82         
83         gchar *result = NULL;
84         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
85         if(option) {
86                 /* Get the value part of the key=value pair: */
87                 const gchar* pair = (const gchar*)option->data;
88                 
89                 const gchar* sep = strchr(pair, '=');
90                 if (sep) {
91                         gint len = sep - pair;
92                         if(len > 0) {
93                                 result = g_strdup(sep+1);
94                                 
95                                 /* Avoid returning an empty string instead of NULL. */
96                                 if(result && strlen(result) == 0) {
97                                         g_free(result);
98                                         result = NULL;
99                                 }
100                         }
101                 }
102         }
103                 
104         return result;
105 }
106
107 gboolean
108 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
109 {
110         if (!options_list)
111                 return FALSE;
112         
113         gboolean result = FALSE;
114         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
115         if(option) {
116                 return TRUE;
117         }
118                 
119         return result;
120 }
121                                           
122 gchar*
123 modest_account_mgr_get_server_account_option (ModestAccountMgr *self, 
124         const gchar* account_name, const gchar* option_name)
125 {
126         GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
127                                                      MODEST_CONF_VALUE_STRING, TRUE);
128         if (!option_list)
129                 return NULL;
130                 
131         gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
132         
133         /* TODO: Should we free the items too, or just the list? */
134         g_slist_free (option_list);
135                 
136         return result;
137 }
138
139 static ModestServerAccountData*
140 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
141 {
142         ModestServerAccountData *data;
143         gchar *proto;
144         
145         g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);      
146         data = g_slice_new0 (ModestServerAccountData);
147         
148         data->account_name = g_strdup (name);
149         data->hostname     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
150         data->username     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);  
151         proto              = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
152         data->proto        = modest_protocol_info_get_protocol (proto);
153         g_free (proto);
154
155         data->last_updated = modest_account_mgr_get_int    (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
156         
157         data->password     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
158         data->uri          = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
159         data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
160                                                      MODEST_CONF_VALUE_STRING, TRUE);
161         return data;
162 }
163
164
165 static void
166 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
167                                              ModestServerAccountData* data)
168 {
169         g_return_if_fail (self);
170
171         if (!data)
172                 return; /* not an error */
173
174         g_free (data->account_name);
175         data->account_name = NULL;
176         
177         g_free (data->hostname);
178         data->hostname = NULL;
179         
180         g_free (data->username);
181         data->username = NULL;
182
183         g_free (data->password);
184         data->password = NULL;
185         
186         if (data->options) {
187                 GSList *tmp = data->options;
188                 while (tmp) {
189                         g_free (tmp->data);
190                         tmp = g_slist_next (tmp);
191                 }
192                 g_slist_free (data->options);
193         }
194
195         g_slice_free (ModestServerAccountData, data);
196 }
197
198 /** You must use modest_account_mgr_free_account_data() on the result.
199  */
200 ModestAccountData*
201 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
202 {
203         ModestAccountData *data;
204         gchar *server_account;
205         gchar *default_account;
206         
207         g_return_val_if_fail (self, NULL);
208         g_return_val_if_fail (name, NULL);
209         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);      
210         data = g_slice_new0 (ModestAccountData);
211         
212         data->account_name = g_strdup (name);
213
214         data->display_name = modest_account_mgr_get_string (self, name,
215                                                             MODEST_ACCOUNT_DISPLAY_NAME,
216                                                             FALSE);
217         data->fullname     = modest_account_mgr_get_string (self, name,
218                                                               MODEST_ACCOUNT_FULLNAME,
219                                                                FALSE);
220         data->email        = modest_account_mgr_get_string (self, name,
221                                                             MODEST_ACCOUNT_EMAIL,
222                                                             FALSE);
223         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
224
225         default_account    = modest_account_mgr_get_default_account (self);
226         data->is_default   = (default_account && strcmp (default_account, name) == 0);
227         g_free (default_account);
228
229         /* store */
230         server_account     = modest_account_mgr_get_string (self, name,
231                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
232                                                             FALSE);
233         if (server_account) {
234                 data->store_account =
235                         modest_account_mgr_get_server_account_data (self, server_account);
236                 g_free (server_account);
237         }
238
239         /* transport */
240         server_account = modest_account_mgr_get_string (self, name,
241                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
242                                                         FALSE);
243         if (server_account) {
244                 data->transport_account =
245                         modest_account_mgr_get_server_account_data (self, server_account);
246                 g_free (server_account);
247         }
248
249         return data;
250 }
251
252
253 void
254 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
255 {
256         g_return_if_fail (self);
257
258         if (!data) /* not an error */ 
259                 return;
260
261         g_free (data->account_name);
262         g_free (data->display_name);
263         g_free (data->fullname);
264         g_free (data->email);
265
266         modest_account_mgr_free_server_account_data (self, data->store_account);
267         modest_account_mgr_free_server_account_data (self, data->transport_account);
268         
269         g_slice_free (ModestAccountData, data);
270 }
271
272
273 gchar*
274 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
275 {
276         gchar *account; 
277         ModestConf *conf;
278         GError *err = NULL;
279         
280         g_return_val_if_fail (self, NULL);
281
282         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
283         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
284         
285         if (err) {
286                 g_printerr ("modest: failed to get '%s': %s\n",
287                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
288                 g_error_free (err);
289                 g_free (account);
290                 return  NULL;
291         }
292         
293         /* it's not really an error if there is no default account */
294         if (!account) 
295                 return NULL;
296
297         /* sanity check */
298         if (!modest_account_mgr_account_exists (self, account, FALSE)) {
299                 g_printerr ("modest: default account does not exist\n");
300                 g_free (account);
301                 return NULL;
302         }
303
304         return account;
305 }
306
307
308 gboolean
309 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
310 {
311         ModestConf *conf;
312         
313         g_return_val_if_fail (self,    FALSE);
314         g_return_val_if_fail (account, FALSE);
315         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
316                               FALSE);
317         
318         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
319                 
320         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
321                                        account, NULL);
322
323 }
324
325 gchar*
326 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
327 {
328         gchar *fullname, *email, *from;
329         
330         g_return_val_if_fail (self, NULL);
331         g_return_val_if_fail (name, NULL);
332
333         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
334                                                        FALSE);
335         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
336                                                        FALSE);
337         from = g_strdup_printf ("%s <%s>",
338                                 fullname ? fullname : "",
339                                 email    ? email    : "");
340         g_free (fullname);
341         g_free (email);
342
343         return from;
344 }