* updates: cleanup, caching accounts, refactoring and work on
[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,
41                                             MODEST_ACCOUNT_ENABLED, enabled,
42                                             FALSE, NULL);
43 }
44
45
46 gboolean
47 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
48 {
49         return modest_account_mgr_get_bool (self, name,
50                                             MODEST_ACCOUNT_ENABLED, FALSE,
51                                             NULL);
52 }
53
54
55 static ModestServerAccountData*
56 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
57 {
58         ModestServerAccountData *data;
59         gchar *proto;
60         
61         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
62                                                                  TRUE, NULL), NULL);    
63         data = g_slice_new0 (ModestServerAccountData);
64         
65         data->account_name = g_strdup (name);
66         data->hostname     = modest_account_mgr_get_string (self, name,
67                                                             MODEST_ACCOUNT_HOSTNAME,
68                                                             TRUE, NULL);
69         data->username     = modest_account_mgr_get_string (self, name,
70                                                             MODEST_ACCOUNT_USERNAME,
71                                                             TRUE, NULL);
72         
73         proto        = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO,
74                                                       TRUE, NULL);
75         data->proto  = modest_protocol_info_get_protocol (proto);
76         g_free (proto);
77
78
79         data->last_updated = modest_account_mgr_get_int    (self, name,
80                                                             MODEST_ACCOUNT_LAST_UPDATED,
81                                                             TRUE, NULL);
82         
83         data->password     = modest_account_mgr_get_string (self, name,
84                                                             MODEST_ACCOUNT_PASSWORD,
85                                                             TRUE, NULL);
86         
87         data->uri          = modest_account_mgr_get_string (self, name,
88                                                             MODEST_ACCOUNT_URI,
89                                                             TRUE, NULL);
90         data->options = modest_account_mgr_get_list (self, name,
91                                                      MODEST_ACCOUNT_OPTIONS,
92                                                      MODEST_CONF_VALUE_STRING,
93                                                      TRUE, NULL);
94         return data;
95 }
96
97
98 static void
99 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
100                                              ModestServerAccountData* data)
101 {
102         g_return_if_fail (self);
103
104         if (!data)
105                 return; /* not an error */
106
107         g_free (data->account_name);
108         data->account_name = NULL;
109         
110         g_free (data->hostname);
111         data->hostname = NULL;
112         
113         g_free (data->username);
114         data->username = NULL;
115
116         g_free (data->password);
117         data->password = NULL;
118         
119         if (data->options) {
120                 GSList *tmp = data->options;
121                 while (tmp) {
122                         g_free (tmp->data);
123                         tmp = g_slist_next (tmp);
124                 }
125                 g_slist_free (data->options);
126         }
127
128         g_slice_free (ModestServerAccountData, data);
129 }
130
131 ModestAccountData*
132 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
133 {
134         ModestAccountData *data;
135         gchar *server_account;
136         gchar *default_account;
137         
138         g_return_val_if_fail (self, NULL);
139         g_return_val_if_fail (name, NULL);
140         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
141                                                                  FALSE, NULL), NULL);   
142         data = g_slice_new0 (ModestAccountData);
143
144         data->account_name = g_strdup (name);
145
146         data->display_name = modest_account_mgr_get_string (self, name,
147                                                             MODEST_ACCOUNT_DISPLAY_NAME,
148                                                             FALSE, NULL);
149         data->fullname     = modest_account_mgr_get_string (self, name,
150                                                               MODEST_ACCOUNT_FULLNAME,
151                                                                FALSE, NULL);
152         data->email        = modest_account_mgr_get_string (self, name,
153                                                             MODEST_ACCOUNT_EMAIL,
154                                                             FALSE, NULL);
155         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
156
157         default_account    = modest_account_mgr_get_default_account (self);
158         data->is_default   = (default_account && strcmp (default_account, name) == 0);
159         g_free (default_account);
160
161         /* store */
162         server_account     = modest_account_mgr_get_string (self, name,
163                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
164                                                             FALSE, NULL);
165         if (server_account) {
166                 data->store_account =
167                         modest_account_mgr_get_server_account_data (self,
168                                                                     server_account);
169                 g_free (server_account);
170         }
171
172         /* transport */
173         server_account = modest_account_mgr_get_string (self, name,
174                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
175                                                         FALSE, NULL);
176         if (server_account) {
177                 data->transport_account =
178                         modest_account_mgr_get_server_account_data (self,
179                                                                     server_account);
180                 g_free (server_account);
181         }
182
183         return data;
184 }
185
186
187 void
188 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
189 {
190         g_return_if_fail (self);
191
192         if (!data) /* not an error */ 
193                 return;
194
195         g_free (data->account_name);
196         g_free (data->display_name);
197         g_free (data->fullname);
198         g_free (data->email);
199
200         modest_account_mgr_free_server_account_data (self, data->store_account);
201         modest_account_mgr_free_server_account_data (self, data->transport_account);
202         
203         g_slice_free (ModestAccountData, data);
204 }
205
206
207 gchar*
208 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
209 {
210         gchar *account; 
211         ModestConf *conf;
212         
213         g_return_val_if_fail (self, NULL);
214
215         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
216         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
217                                           NULL);
218         
219         /* it's not really an error if there is no default account */
220         if (!account) 
221                 return NULL;
222
223         /* sanity check */
224         if (!modest_account_mgr_account_exists (self, account, FALSE, NULL)) {
225                 g_printerr ("modest: default account does not exist\n");
226                 g_free (account);
227                 return NULL;
228         }
229
230         return account;
231 }
232
233
234 gboolean
235 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
236 {
237         ModestConf *conf;
238         
239         g_return_val_if_fail (self,    FALSE);
240         g_return_val_if_fail (account, FALSE);
241         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE, NULL),
242                               FALSE);
243         
244         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
245                 
246         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
247                                        account, NULL);
248
249 }
250
251 gchar*
252 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
253 {
254         gchar *fullname, *email, *from;
255         
256         g_return_val_if_fail (self, NULL);
257         g_return_val_if_fail (name, NULL);
258
259         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
260                                                        FALSE, NULL);
261         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
262                                                        FALSE, NULL);
263         from = g_strdup_printf ("%s <%s>",
264                                 fullname ? fullname : "",
265                                 email    ? email    : "");
266         g_free (fullname);
267         g_free (email);
268
269         return from;
270 }