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 static ModestProtocol
125 get_secure_auth_for_conf_string(const gchar* value)
126 {
127         ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
128         if (value) {
129                 if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE) == 0)
130                         result = MODEST_PROTOCOL_AUTH_NONE;
131                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD) == 0)
132                         result = MODEST_PROTOCOL_AUTH_PASSWORD;
133                 else if (strcmp(value, MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5) == 0)
134                         result = MODEST_PROTOCOL_AUTH_CRAMMD5;
135         }
136         
137         return result;
138 }
139
140 ModestProtocol
141 modest_server_account_get_secure_auth (ModestAccountMgr *self, 
142         const gchar* account_name)
143 {
144         ModestProtocol result = MODEST_PROTOCOL_AUTH_NONE;
145         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, 
146                 TRUE /* server account */);
147         if (value) {
148                 result = get_secure_auth_for_conf_string (value);
149                         
150                 g_free (value);
151         }
152         
153         return result;
154 }
155
156
157 void
158 modest_server_account_set_secure_auth (ModestAccountMgr *self, 
159         const gchar* account_name, ModestProtocol secure_auth)
160 {
161         /* Get the conf string for the enum value: */
162         const gchar* str_value = NULL;
163         if (secure_auth == MODEST_PROTOCOL_AUTH_NONE)
164                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_NONE;
165         else if (secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD)
166                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_PASSWORD;
167         else if (secure_auth == MODEST_PROTOCOL_AUTH_CRAMMD5)
168                 str_value = MODEST_ACCOUNT_AUTH_MECH_VALUE_CRAMMD5;
169         
170         /* Set it in the configuration: */
171         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_AUTH_MECH, str_value, TRUE);
172 }
173
174 static ModestProtocol
175 get_security_for_conf_string(const gchar* value)
176 {
177         ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
178         if (value) {
179                 if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NONE) == 0)
180                         result = MODEST_PROTOCOL_SECURITY_NONE;
181                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_NORMAL) == 0)
182                         result = MODEST_PROTOCOL_SECURITY_TLS;
183                 else if (strcmp(value, MODEST_ACCOUNT_SECURITY_VALUE_SSL) == 0)
184                         result = MODEST_PROTOCOL_SECURITY_SSL;
185         }
186         
187         return result;
188 }
189
190 ModestProtocol
191 modest_server_account_get_security (ModestAccountMgr *self, 
192         const gchar* account_name)
193 {
194         ModestProtocol result = MODEST_PROTOCOL_SECURITY_NONE;
195         gchar* value = modest_account_mgr_get_string (self, account_name, MODEST_ACCOUNT_SECURITY, 
196                 TRUE /* server account */);
197         if (value) {
198                 result = get_security_for_conf_string (value);
199                         
200                 g_free (value);
201         }
202         
203         return result;
204 }
205
206 void
207 modest_server_account_set_security (ModestAccountMgr *self, 
208         const gchar* account_name, ModestProtocol security)
209 {
210         /* Get the conf string for the enum value: */
211         const gchar* str_value = NULL;
212         if (security == MODEST_PROTOCOL_SECURITY_NONE)
213                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NONE;
214         else if (security == MODEST_PROTOCOL_SECURITY_TLS)
215                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_NORMAL;
216         else if (security == MODEST_PROTOCOL_SECURITY_SSL)
217                 str_value = MODEST_ACCOUNT_SECURITY_VALUE_SSL;
218         
219         /* Set it in the configuration: */
220         modest_account_mgr_set_string (self, account_name, MODEST_ACCOUNT_SECURITY, str_value, TRUE);
221 }
222                      
223 #if 0                     
224 gchar*
225 modest_account_mgr_get_server_account_option (ModestAccountMgr *self, 
226         const gchar* account_name, const gchar* option_name)
227 {
228         GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
229                                                      MODEST_CONF_VALUE_STRING, TRUE);
230         if (!option_list)
231                 return NULL;
232                 
233         gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
234         
235         /* TODO: Should we free the items too, or just the list? */
236         g_slist_free (option_list);
237                 
238         return result;
239 }
240 #endif
241
242 static ModestServerAccountData*
243 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
244 {
245         ModestServerAccountData *data;
246         gchar *proto;
247         
248         g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);      
249         data = g_slice_new0 (ModestServerAccountData);
250         
251         data->account_name = g_strdup (name);
252         data->hostname     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
253         data->username     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);  
254         proto              = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
255         data->proto        = modest_protocol_info_get_protocol (proto);
256         g_free (proto);
257
258         data->port         = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_PORT, TRUE);
259         
260         gchar *secure_auth_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_AUTH_MECH, TRUE);
261         data->secure_auth  = get_secure_auth_for_conf_string(secure_auth_str);
262         g_free (secure_auth_str);
263                 
264         gchar *security_str = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_SECURITY, TRUE);
265         data->security     = get_security_for_conf_string(security_str);
266         g_free (security_str);
267         
268         data->last_updated = modest_account_mgr_get_int    (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
269         
270         data->password     = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
271         data->uri          = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
272         data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
273                                                      MODEST_CONF_VALUE_STRING, TRUE);
274                                                    
275         
276         return data;
277 }
278
279
280 static void
281 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
282                                              ModestServerAccountData* data)
283 {
284         g_return_if_fail (self);
285
286         if (!data)
287                 return; /* not an error */
288
289         g_free (data->account_name);
290         data->account_name = NULL;
291         
292         g_free (data->hostname);
293         data->hostname = NULL;
294         
295         g_free (data->username);
296         data->username = NULL;
297
298         g_free (data->password);
299         data->password = NULL;
300         
301         if (data->options) {
302                 GSList *tmp = data->options;
303                 while (tmp) {
304                         g_free (tmp->data);
305                         tmp = g_slist_next (tmp);
306                 }
307                 g_slist_free (data->options);
308         }
309
310         g_slice_free (ModestServerAccountData, data);
311 }
312
313 /** You must use modest_account_mgr_free_account_data() on the result.
314  */
315 ModestAccountData*
316 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
317 {
318         ModestAccountData *data;
319         gchar *server_account;
320         gchar *default_account;
321         
322         g_return_val_if_fail (self, NULL);
323         g_return_val_if_fail (name, NULL);
324         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);      
325         data = g_slice_new0 (ModestAccountData);
326         
327         data->account_name = g_strdup (name);
328
329         data->display_name = modest_account_mgr_get_string (self, name,
330                                                             MODEST_ACCOUNT_DISPLAY_NAME,
331                                                             FALSE);
332         data->fullname     = modest_account_mgr_get_string (self, name,
333                                                               MODEST_ACCOUNT_FULLNAME,
334                                                                FALSE);
335         data->email        = modest_account_mgr_get_string (self, name,
336                                                             MODEST_ACCOUNT_EMAIL,
337                                                             FALSE);
338         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
339
340         default_account    = modest_account_mgr_get_default_account (self);
341         data->is_default   = (default_account && strcmp (default_account, name) == 0);
342         g_free (default_account);
343
344         /* store */
345         server_account     = modest_account_mgr_get_string (self, name,
346                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
347                                                             FALSE);
348         if (server_account) {
349                 data->store_account =
350                         modest_account_mgr_get_server_account_data (self, server_account);
351                 g_free (server_account);
352         }
353
354         /* transport */
355         server_account = modest_account_mgr_get_string (self, name,
356                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
357                                                         FALSE);
358         if (server_account) {
359                 data->transport_account =
360                         modest_account_mgr_get_server_account_data (self, server_account);
361                 g_free (server_account);
362         }
363
364         return data;
365 }
366
367
368 void
369 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
370 {
371         g_return_if_fail (self);
372
373         if (!data) /* not an error */ 
374                 return;
375
376         g_free (data->account_name);
377         g_free (data->display_name);
378         g_free (data->fullname);
379         g_free (data->email);
380
381         modest_account_mgr_free_server_account_data (self, data->store_account);
382         modest_account_mgr_free_server_account_data (self, data->transport_account);
383         
384         g_slice_free (ModestAccountData, data);
385 }
386
387
388 gchar*
389 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
390 {
391         gchar *account; 
392         ModestConf *conf;
393         GError *err = NULL;
394         
395         g_return_val_if_fail (self, NULL);
396
397         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
398         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
399         
400         if (err) {
401                 g_printerr ("modest: failed to get '%s': %s\n",
402                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
403                 g_error_free (err);
404                 g_free (account);
405                 return  NULL;
406         }
407         
408         /* it's not really an error if there is no default account */
409         if (!account) 
410                 return NULL;
411
412         /* sanity check */
413         if (!modest_account_mgr_account_exists (self, account, FALSE)) {
414                 g_printerr ("modest: default account does not exist\n");
415                 g_free (account);
416                 return NULL;
417         }
418
419         return account;
420 }
421
422
423 gboolean
424 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
425 {
426         ModestConf *conf;
427         
428         g_return_val_if_fail (self,    FALSE);
429         g_return_val_if_fail (account, FALSE);
430         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
431                               FALSE);
432         
433         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
434                 
435         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
436                                        account, NULL);
437
438 }
439
440 gchar*
441 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
442 {
443         gchar *fullname, *email, *from;
444         
445         g_return_val_if_fail (self, NULL);
446         g_return_val_if_fail (name, NULL);
447
448         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
449                                                        FALSE);
450         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
451                                                        FALSE);
452         from = g_strdup_printf ("%s <%s>",
453                                 fullname ? fullname : "",
454                                 email    ? email    : "");
455         g_free (fullname);
456         g_free (email);
457
458         return from;
459 }