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