1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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>
37 modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* name,
40 return modest_account_mgr_set_bool (self, name, MODEST_ACCOUNT_ENABLED, enabled,FALSE);
45 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
47 return modest_account_mgr_get_bool (self, name, MODEST_ACCOUNT_ENABLED, FALSE);
51 compare_option_strings_for_name (const gchar* a, const gchar* b)
53 printf(" debug: compare_option_strings_for_name():a=%s, b=%s\n", a, b);
54 const gchar* sep = strchr(a, '=');
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);
66 name[len] = 0; /* Null-termination. */
68 printf(" debug: name=%s\n", name);
70 gint result = strcmp (name, b);
78 modest_server_account_data_get_option_value (GSList* options_list, const gchar* option_name)
84 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)compare_option_strings_for_name);
86 /* Get the value part of the key=value pair: */
87 const gchar* pair = (const gchar*)option->data;
89 const gchar* sep = strchr(pair, '=');
91 gint len = sep - pair;
93 result = g_strdup(sep+1);
95 /* Avoid returning an empty string instead of NULL. */
96 if(result && strlen(result) == 0) {
108 modest_server_account_data_get_option_bool (GSList* options_list, const gchar* option_name)
113 gboolean result = FALSE;
114 GSList* option = g_slist_find_custom(options_list, option_name, (GCompareFunc)strcmp);
123 modest_account_mgr_get_server_account_option (ModestAccountMgr *self,
124 const gchar* account_name, const gchar* option_name)
126 GSList *option_list = modest_account_mgr_get_list (self, account_name, MODEST_ACCOUNT_OPTIONS,
127 MODEST_CONF_VALUE_STRING, TRUE);
131 gchar *result = modest_server_account_data_get_option_value (option_list, option_name);
133 /* TODO: Should we free the items too, or just the list? */
134 g_slist_free (option_list);
139 static ModestServerAccountData*
140 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
142 ModestServerAccountData *data;
145 g_return_val_if_fail (modest_account_mgr_account_exists (self, name, TRUE), NULL);
146 data = g_slice_new0 (ModestServerAccountData);
148 data->account_name = g_strdup (name);
149 data->hostname = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_HOSTNAME,TRUE);
150 data->username = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_USERNAME,TRUE);
151 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO, TRUE);
152 data->proto = modest_protocol_info_get_protocol (proto);
155 data->last_updated = modest_account_mgr_get_int (self, name, MODEST_ACCOUNT_LAST_UPDATED,TRUE);
157 data->password = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PASSWORD, TRUE);
158 data->uri = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_URI,TRUE);
159 data->options = modest_account_mgr_get_list (self, name, MODEST_ACCOUNT_OPTIONS,
160 MODEST_CONF_VALUE_STRING, TRUE);
166 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
167 ModestServerAccountData* data)
169 g_return_if_fail (self);
172 return; /* not an error */
174 g_free (data->account_name);
175 data->account_name = NULL;
177 g_free (data->hostname);
178 data->hostname = NULL;
180 g_free (data->username);
181 data->username = NULL;
183 g_free (data->password);
184 data->password = NULL;
187 GSList *tmp = data->options;
190 tmp = g_slist_next (tmp);
192 g_slist_free (data->options);
195 g_slice_free (ModestServerAccountData, data);
198 /** You must use modest_account_mgr_free_account_data() on the result.
201 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
203 ModestAccountData *data;
204 gchar *server_account;
205 gchar *default_account;
207 g_return_val_if_fail (self, NULL);
208 g_return_val_if_fail (name, NULL);
209 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,FALSE), NULL);
210 data = g_slice_new0 (ModestAccountData);
212 data->account_name = g_strdup (name);
214 data->display_name = modest_account_mgr_get_string (self, name,
215 MODEST_ACCOUNT_DISPLAY_NAME,
217 data->fullname = modest_account_mgr_get_string (self, name,
218 MODEST_ACCOUNT_FULLNAME,
220 data->email = modest_account_mgr_get_string (self, name,
221 MODEST_ACCOUNT_EMAIL,
223 data->is_enabled = modest_account_mgr_get_enabled (self, name);
225 default_account = modest_account_mgr_get_default_account (self);
226 data->is_default = (default_account && strcmp (default_account, name) == 0);
227 g_free (default_account);
230 server_account = modest_account_mgr_get_string (self, name,
231 MODEST_ACCOUNT_STORE_ACCOUNT,
233 if (server_account) {
234 data->store_account =
235 modest_account_mgr_get_server_account_data (self, server_account);
236 g_free (server_account);
240 server_account = modest_account_mgr_get_string (self, name,
241 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
243 if (server_account) {
244 data->transport_account =
245 modest_account_mgr_get_server_account_data (self, server_account);
246 g_free (server_account);
254 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
256 g_return_if_fail (self);
258 if (!data) /* not an error */
261 g_free (data->account_name);
262 g_free (data->display_name);
263 g_free (data->fullname);
264 g_free (data->email);
266 modest_account_mgr_free_server_account_data (self, data->store_account);
267 modest_account_mgr_free_server_account_data (self, data->transport_account);
269 g_slice_free (ModestAccountData, data);
274 modest_account_mgr_get_default_account (ModestAccountMgr *self)
280 g_return_val_if_fail (self, NULL);
282 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
283 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT, &err);
286 g_printerr ("modest: failed to get '%s': %s\n",
287 MODEST_CONF_DEFAULT_ACCOUNT, err->message);
293 /* it's not really an error if there is no default account */
298 if (!modest_account_mgr_account_exists (self, account, FALSE)) {
299 g_printerr ("modest: default account does not exist\n");
309 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
313 g_return_val_if_fail (self, FALSE);
314 g_return_val_if_fail (account, FALSE);
315 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE),
318 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
320 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
326 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
328 gchar *fullname, *email, *from;
330 g_return_val_if_fail (self, NULL);
331 g_return_val_if_fail (name, NULL);
333 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
335 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
337 from = g_strdup_printf ("%s <%s>",
338 fullname ? fullname : "",