* all:
[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_new0 (ModestServerAccountData, 1);
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         data->password     = modest_account_mgr_get_string (self, name,
79                                                             MODEST_ACCOUNT_PASSWORD,
80                                                             TRUE, NULL);
81
82         data->options = modest_account_mgr_get_list (self, name,
83                                                      MODEST_ACCOUNT_OPTIONS,
84                                                      MODEST_CONF_VALUE_STRING,
85                                                      TRUE, NULL);
86         return data;
87 }
88
89
90 static void
91 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
92                                              ModestServerAccountData* data)
93 {
94         g_return_if_fail (self);
95
96         if (!data)
97                 return; /* not an error */
98
99         g_free (data->account_name);
100         data->account_name = NULL;
101         
102         g_free (data->hostname);
103         data->hostname = NULL;
104         
105         g_free (data->username);
106         data->username = NULL;
107
108         g_free (data->password);
109         data->password = NULL;
110         
111         if (data->options) {
112                 GSList *tmp = data->options;
113                 while (tmp) {
114                         g_free (tmp->data);
115                         tmp = g_slist_next (tmp);
116                 }
117                 g_slist_free (data->options);
118         }
119
120         g_free (data);
121 }
122
123 ModestAccountData*
124 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
125 {
126         ModestAccountData *data;
127         gchar *server_account;
128         
129         g_return_val_if_fail (self, NULL);
130         g_return_val_if_fail (name, NULL);
131         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
132                                                                  FALSE, NULL), NULL);   
133         data = g_new0 (ModestAccountData, 1);
134
135         data->account_name = g_strdup (name);
136
137         data->display_name = modest_account_mgr_get_string (self, name,
138                                                             MODEST_ACCOUNT_DISPLAY_NAME,
139                                                             FALSE, NULL);
140         data->fullname      = modest_account_mgr_get_string (self, name,
141                                                               MODEST_ACCOUNT_FULLNAME,
142                                                                FALSE, NULL);
143         data->email        = modest_account_mgr_get_string (self, name,
144                                                             MODEST_ACCOUNT_EMAIL,
145                                                             FALSE, NULL);
146         data->enabled      = modest_account_mgr_get_enabled (self, name);
147
148         /* store */
149         server_account     = modest_account_mgr_get_string (self, name,
150                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
151                                                             FALSE, NULL);
152         if (server_account) {
153                 data->store_account =
154                         modest_account_mgr_get_server_account_data (self,
155                                                                     server_account);
156                 g_free (server_account);
157         }
158
159         /* transport */
160         server_account = modest_account_mgr_get_string (self, name,
161                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
162                                                         FALSE, NULL);
163         if (server_account) {
164                 data->transport_account =
165                         modest_account_mgr_get_server_account_data (self,
166                                                                     server_account);
167                 g_free (server_account);
168         }
169
170         return data;
171 }
172
173
174 void
175 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
176 {
177         g_return_if_fail (self);
178
179         if (!data) /* not an error */ 
180                 return;
181
182         g_free (data->account_name);
183         g_free (data->display_name);
184         g_free (data->fullname);
185         g_free (data->email);
186
187         modest_account_mgr_free_server_account_data (self, data->store_account);
188         modest_account_mgr_free_server_account_data (self, data->transport_account);
189         
190         g_free (data);
191 }
192
193
194 gchar*
195 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
196 {
197         gchar *account; 
198         ModestConf *conf;
199         
200         g_return_val_if_fail (self, NULL);
201
202         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
203         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
204                                           NULL);
205         
206         /* it's not really an error if there is no default account */
207         if (!account) 
208                 return NULL;
209
210         /* sanity check */
211         if (!modest_account_mgr_account_exists (self, account, FALSE, NULL)) {
212                 g_printerr ("modest: default account does not exist\n");
213                 g_free (account);
214                 return NULL;
215         }
216
217         return account;
218 }
219
220
221 gboolean
222 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
223 {
224         ModestConf *conf;
225         
226         g_return_val_if_fail (self,    FALSE);
227         g_return_val_if_fail (account, FALSE);
228         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE, NULL),
229                               FALSE);
230         
231         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
232                 
233         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
234                                        account, NULL);
235
236 }
237
238
239 TnyAccount*
240 modest_account_mgr_get_tny_account (ModestAccountMgr *self, const gchar* account_name,
241                                         TnyAccountType type)
242 {
243         TnyAccount      *account = NULL;
244         TnyList         *accounts;
245         TnyIterator     *iter;
246         gchar           *server_account;
247         const gchar     *conf_key;
248         
249         g_return_val_if_fail (self, NULL);
250         g_return_val_if_fail (account_name, NULL);
251
252         switch (type) {
253         case TNY_ACCOUNT_TYPE_STORE:
254                 conf_key = MODEST_ACCOUNT_STORE_ACCOUNT; break;
255         case TNY_ACCOUNT_TYPE_TRANSPORT:
256                 conf_key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT; break;
257         default:
258                 g_return_val_if_reached (NULL);
259         }
260         
261         server_account = modest_account_mgr_get_string (self, account_name, conf_key, FALSE, NULL);
262         if (!server_account) {
263                 g_printerr ("modest: no %s account specified for %s\n",
264                             type == TNY_ACCOUNT_TYPE_TRANSPORT ? "transport" : "store", account_name);
265                 return NULL;
266         }
267         
268         accounts = tny_simple_list_new ();
269         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()),
270                                         accounts, type);        
271         iter = tny_list_create_iterator (accounts);     
272         while (tny_iterator_is_done (iter)) {
273                 account = TNY_ACCOUNT(tny_iterator_get_current(iter));
274                 if (strcmp (tny_account_get_id (account), server_account) == 0)
275                         break;
276         }
277         
278         g_object_unref (G_OBJECT(iter));
279         g_object_unref (G_OBJECT(accounts));
280         
281         if (!account)
282                 g_printerr ("modest: no tny %s account found for %s\n",
283                             type == TNY_ACCOUNT_TYPE_TRANSPORT ? "transport" : "store", account_name);
284         else {
285                 /* sanity check */
286                 if ((type == TNY_ACCOUNT_TYPE_TRANSPORT && !TNY_IS_TRANSPORT_ACCOUNT(account)) ||
287                     (type == TNY_ACCOUNT_TYPE_STORE && !TNY_IS_STORE_ACCOUNT(account))) {
288                         g_printerr ("modest: tny %s acccount found for %s, but was expecting %s account\n",
289                                     type == TNY_ACCOUNT_TYPE_TRANSPORT ? "transport" : "store", account_name,
290                                     type == TNY_ACCOUNT_TYPE_TRANSPORT ? "store" : "transport");
291                         g_object_unref (G_OBJECT(account));
292                         account = NULL;
293                 }
294         }
295         return account;
296 }
297
298
299 gchar*
300 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
301 {
302         gchar *fullname, *email, *from;
303         
304         g_return_val_if_fail (self, NULL);
305         g_return_val_if_fail (name, NULL);
306
307         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
308                                                        FALSE, NULL);
309         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
310                                                        FALSE, NULL);
311         from = g_strdup_printf ("%s <%s>",
312                                 fullname ? fullname : "",
313                                 email    ? email    : "");
314         g_free (fullname);
315         g_free (email);
316
317         return from;
318 }