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