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