Updated translatable strings
[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 ModestAccountData*
111 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
112 {
113         ModestAccountData *data;
114         gchar *server_account;
115         gchar *default_account;
116         
117         g_return_val_if_fail (self, NULL);
118         g_return_val_if_fail (name, NULL);
119         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);      
120         data = g_slice_new0 (ModestAccountData);
121         
122         data->account_name = g_strdup (name);
123
124         data->display_name = modest_account_mgr_get_string (self, name,
125                                                             MODEST_ACCOUNT_DISPLAY_NAME,
126                                                             FALSE);
127         data->fullname     = modest_account_mgr_get_string (self, name,
128                                                               MODEST_ACCOUNT_FULLNAME,
129                                                                FALSE);
130         data->email        = modest_account_mgr_get_string (self, name,
131                                                             MODEST_ACCOUNT_EMAIL,
132                                                             FALSE);
133         data->is_enabled   = modest_account_mgr_get_enabled (self, name);
134
135         default_account    = modest_account_mgr_get_default_account (self);
136         data->is_default   = (default_account && strcmp (default_account, name) == 0);
137         g_free (default_account);
138
139         /* store */
140         server_account     = modest_account_mgr_get_string (self, name,
141                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
142                                                             FALSE);
143         if (server_account) {
144                 data->store_account =
145                         modest_account_mgr_get_server_account_data (self, server_account);
146                 g_free (server_account);
147         }
148
149         /* transport */
150         server_account = modest_account_mgr_get_string (self, name,
151                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
152                                                         FALSE);
153         if (server_account) {
154                 data->transport_account =
155                         modest_account_mgr_get_server_account_data (self, server_account);
156                 g_free (server_account);
157         }
158
159         return data;
160 }
161
162
163 void
164 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
165 {
166         g_return_if_fail (self);
167
168         if (!data) /* not an error */ 
169                 return;
170
171         g_free (data->account_name);
172         g_free (data->display_name);
173         g_free (data->fullname);
174         g_free (data->email);
175
176         modest_account_mgr_free_server_account_data (self, data->store_account);
177         modest_account_mgr_free_server_account_data (self, data->transport_account);
178         
179         g_slice_free (ModestAccountData, data);
180 }
181
182
183 gchar*
184 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
185 {
186         gchar *account; 
187         ModestConf *conf;
188         GError *err = NULL;
189         
190         g_return_val_if_fail (self, NULL);
191
192         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
193         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
194         
195         if (err) {
196                 g_printerr ("modest: failed to get '%s': %s\n",
197                             MODEST_CONF_DEFAULT_ACCOUNT, err->message);
198                 g_error_free (err);
199                 g_free (account);
200                 return  NULL;
201         }
202         
203         /* it's not really an error if there is no default account */
204         if (!account) 
205                 return NULL;
206
207         /* sanity check */
208         if (!modest_account_mgr_account_exists (self, account, FALSE)) {
209                 g_printerr ("modest: default account does not exist\n");
210                 g_free (account);
211                 return NULL;
212         }
213
214         return account;
215 }
216
217
218 gboolean
219 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
220 {
221         ModestConf *conf;
222         
223         g_return_val_if_fail (self,    FALSE);
224         g_return_val_if_fail (account, FALSE);
225         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
226                               FALSE);
227         
228         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
229                 
230         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
231                                        account, NULL);
232
233 }
234
235 gchar*
236 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
237 {
238         gchar *fullname, *email, *from;
239         
240         g_return_val_if_fail (self, NULL);
241         g_return_val_if_fail (name, NULL);
242
243         fullname      = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
244                                                        FALSE);
245         email         = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
246                                                        FALSE);
247         from = g_strdup_printf ("%s <%s>",
248                                 fullname ? fullname : "",
249                                 email    ? email    : "");
250         g_free (fullname);
251         g_free (email);
252
253         return from;
254 }