2007-04-18 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 #if 0 /* Not needed, but works. */
51 static gint
52 compare_option_strings_for_name (const gchar* a, const gchar* b)
53 {
54         /* printf("  debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b); */
55         const gchar* sep = strchr(a, '=');
56         if (!sep)
57                 return -1;
58                 
59         gint len = sep - a;
60         if(len <= 0)
61                 return -1;
62                 
63         /* Get the part of the string before the =.
64          * Note that this allocation is inefficient just so we can do a strcmp. */
65         gchar* name = g_malloc (len+1);
66         memcpy(name, a, len);
67         name[len] = 0; /* Null-termination. */
68         
69         /* printf("    debug: name=%s\n", name); */
70
71         gint result = strcmp (name, b);
72         
73         g_free (name);
74         
75         return result;
76 }
77            
78 gchar*
79 modest_server_account_data_get_option_string (GSList* options_list, const gchar* option_name)
80 {
81         if (!options_list)
82                 return NULL;
83         
84         gchar *result = NULL;
85         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
86         if(option) {
87                 /* Get the value part of the key=value pair: */
88                 const gchar* pair = (const gchar*)option->data;
89                 
90                 const gchar* sep = strchr(pair, '=');
91                 if (sep) {
92                         gint len = sep - pair;
93                         if(len > 0) {
94                                 result = g_strdup(sep+1);
95                                 
96                                 /* Avoid returning an empty string instead of NULL. */
97                                 if(result && strlen(result) == 0) {
98                                         g_free(result);
99                                         result = NULL;
100                                 }
101                         }
102                 }
103         }
104                 
105         return result;
106 }
107
108 gboolean
109 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
110 {
111         if (!options_list)
112                 return FALSE;
113         
114         gboolean result = FALSE;
115         GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
116         if(option) {
117                 return TRUE;
118         }
119                 
120         return result;
121 }
122 #endif
123
124 ModestProtocol
125 modest_server_account_get_secure_auth (ModestAccountMgr *self, 
126         const gchar* account_name)
127 {
128         ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
129         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, 
130                 TRUE /* server account */);
131         if (value) {
132                 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
133                         result = MODEST_PROTOCOL_AUTH_NONE;
134                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
135                         result = MODEST_PROTOCOL_AUTH_PASSWORD;
136                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
137                         result = MODEST_PROTOCOL_AUTH_CRAMMD5;
138                         
139                 g_free (value);
140         }
141         
142         return result;
143 }
144
145 void
146 modest_server_account_set_secure_auth (ModestAccountMgr *self, 
147         const gchar* account_name, ModestProtocol secure_auth)
148 {
149         /* Get the conf string for the enum value: */
150         const gchar* str_value = NULL;
151         if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
152                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
153         else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
154                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
155         else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
156                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
157         
158         /* Set it in the configuration: */
159         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
160 }
161
162 ModestProtocol
163 modest_server_account_get_security (ModestAccountMgr *self, 
164         const gchar* account_name)
165 {
166         ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
167         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY, 
168                 TRUE /* server account */);
169         if (value) {
170                 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
171                         result = MODEST_PROTOCOL_SECURITY_NONE;
172                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
173                         result = MODEST_PROTOCOL_SECURITY_TLS;
174                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
175                         result = MODEST_PROTOCOL_SECURITY_SSL;
176                         
177                 g_free (value);
178         }
179         
180         return result;
181 }
182
183 void
184 modest_server_account_set_security (ModestAccountMgr *self, 
185         const gchar* account_name, ModestProtocol security)
186 {
187         /* Get the conf string for the enum value: */
188         const gchar* str_value = NULL;
189         if (security == MODEST_PROTOCOL_SECURITY_NONE)
190                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
191         else if (security == MODEST_PROTOCOL_SECURITY_TLS)
192                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
193         else if (security == MODEST_PROTOCOL_SECURITY_SSL)
194                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
195         
196         /* Set it in the configuration: */
197         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
198 }
199                      
200 #if 0                     
201 gchar*
202 modest_account_mgr_get_server_account_option (ModestAccountMgr *self, 
203         const gchar* account_name, const gchar* option_name)
204 {
205         GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
206                                                      MODEST_CONF_VALUE_STRING, TRUE);
207         if (!option_list)
208                 return NULL;
209                 
210         gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
211         
212         /* TODO: Should we free the items too, or just the list? */
213         g_slist_free (option_list);
214                 
215         return result;
216 }
217 #endif
218
219 static ModestServerAccountData*
220 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
221 {
222         ModestServerAccountData *data;
223         gchar *proto;
224         
225         g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);      
226         data = g_slice_new0 (ModestServerAccountData);
227         
228         data->account_name = g_strdup (name);
229         data->hostname     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
230         data->username     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);  
231         proto              = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
232         data->proto        = modest_protocol_info_get_protocol (proto);
233         g_free (proto);
234
235         data->last_updated = modest_account_mgr_get_int    (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
236         
237         data->password     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
238         data->uri          = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
239         data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
240                                                      MODEST_CONF_VALUE_STRING, TRUE);
241         return data;
242 }
243
244
245 static void
246 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
247                                              ModestServerAccountData* data)
248 {
249         g_return_if_fail (self);
250
251         if (!data)
252                 return; /* not an error */
253
254         g_free (data->account_name);
255         data->account_name = NULL;
256         
257         g_free (data->hostname);
258         data->hostname = NULL;
259         
260         g_free (data->username);
261         data->username = NULL;
262
263         g_free (data->password);
264         data->password = NULL;
265         
266         if (data->options) {
267                 GSList *tmp = data->options;
268                 while (tmp) {
269                         g_free (tmp->data);
270                         tmp = g_slist_next (tmp);
271                 }
272                 g_slist_free (data->options);
273         }
274
275         g_slice_free (ModestServerAccountData, data);
276 }
277
278 /** You must use modest_account_mgr_free_account_data() on the result.
279  */
280 ModestAccountData*
281 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
282 {
283         ModestAccountData *data;
284         gchar *server_account;
285         gchar *default_account;
286         
287         g_return_val_if_fail (self, NULL);
288         g_return_val_if_fail (name, NULL);
289         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);      
290         data = g_slice_new0 (ModestAccountData);
291         
292         data->account_name = g_strdup (name);
293
294         data->display_name = modest_account_mgr_get_string (self, name,
295                                                             MODEST_ACCOUNT_DISPLAY_NAME,
296                                                             FALSE);
297         data->fullname     = modest_account_mgr_get_string (self, name,
298                                                               MODEST_ACCOUNT_FULLNAME,
299                                                                FALSE);
300         data->email        = modest_account_mgr_get_string (self, name,
301                                                             MODEST_ACCOUNT_EMAIL,
302                                                             FALSE);
303         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
304
305         default_account    = modest_account_mgr_get_default_account (self);
306         data->is_default   = (default_account && strcmp (default_account, name) == 0);
307         g_free (default_account);
308
309         /* store */
310         server_account     = modest_account_mgr_get_string (self, name,
311                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
312                                                             FALSE);
313         if (server_account) {
314                 data->store_account =
315                         modest_account_mgr_get_server_account_data (self, server_account);
316                 g_free (server_account);
317         }
318
319         /* transport */
320         server_account = modest_account_mgr_get_string (self, name,
321                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
322                                                         FALSE);
323         if (server_account) {
324                 data->transport_account =
325                         modest_account_mgr_get_server_account_data (self, server_account);
326                 g_free (server_account);
327         }
328
329         return data;
330 }
331
332
333 void
334 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
335 {
336         g_return_if_fail (self);
337
338         if (!data) /* not an error */ 
339                 return;
340
341         g_free (data->account_name);
342         g_free (data->display_name);
343         g_free (data->fullname);
344         g_free (data->email);
345
346         modest_account_mgr_free_server_account_data (self, data->store_account);
347         modest_account_mgr_free_server_account_data (self, data->transport_account);
348         
349         g_slice_free (ModestAccountData, data);
350 }
351
352
353 gchar*
354 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
355 {
356         gchar *account; 
357         ModestConf *conf;
358         GError *err = NULL;
359         
360         g_return_val_if_fail (self, NULL);
361
362         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
363         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
364         
365         if (err) {
366                 g_printerr ("modest: failed to get '%s': %s\n",
367                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
368                 g_error_free (err);
369                 g_free (account);
370                 return  NULL;
371         }
372         
373         /* it's not really an error if there is no default account */
374         if (!account) 
375                 return NULL;
376
377         /* sanity check */
378         if (!modest_account_mgr_account_exists (self, account, FALSE)) {
379                 g_printerr ("modest: default account does not exist\n");
380                 g_free (account);
381                 return NULL;
382         }
383
384         return account;
385 }
386
387
388 gboolean
389 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
390 {
391         ModestConf *conf;
392         
393         g_return_val_if_fail (self,    FALSE);
394         g_return_val_if_fail (account, FALSE);
395         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
396                               FALSE);
397         
398         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
399                 
400         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
401                                        account, NULL);
402
403 }
404
405 gchar*
406 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
407 {
408         gchar *fullname, *email, *from;
409         
410         g_return_val_if_fail (self, NULL);
411         g_return_val_if_fail (name, NULL);
412
413         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
414                                                        FALSE);
415         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
416                                                        FALSE);
417         from = g_strdup_printf ("%s <%s>",
418                                 fullname ? fullname : "",
419                                 email    ? email    : "");
420         g_free (fullname);
421         g_free (email);
422
423         return from;
424 }