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,
41 MODEST_ACCOUNT_ENABLED, enabled,
47 modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name)
49 return modest_account_mgr_get_bool (self, name,
50 MODEST_ACCOUNT_ENABLED, FALSE,
55 static ModestServerAccountData*
56 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
58 ModestServerAccountData *data;
61 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
63 data = g_slice_new0 (ModestServerAccountData);
65 data->account_name = g_strdup (name);
66 data->hostname = modest_account_mgr_get_string (self, name,
67 MODEST_ACCOUNT_HOSTNAME,
69 data->username = modest_account_mgr_get_string (self, name,
70 MODEST_ACCOUNT_USERNAME,
73 proto = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO,
75 data->proto = modest_protocol_info_get_protocol (proto);
78 data->password = modest_account_mgr_get_string (self, name,
79 MODEST_ACCOUNT_PASSWORD,
82 data->options = modest_account_mgr_get_list (self, name,
83 MODEST_ACCOUNT_OPTIONS,
84 MODEST_CONF_VALUE_STRING,
91 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
92 ModestServerAccountData* data)
94 g_return_if_fail (self);
97 return; /* not an error */
99 g_free (data->account_name);
100 data->account_name = NULL;
102 g_free (data->hostname);
103 data->hostname = NULL;
105 g_free (data->username);
106 data->username = NULL;
108 g_free (data->password);
109 data->password = NULL;
112 GSList *tmp = data->options;
115 tmp = g_slist_next (tmp);
117 g_slist_free (data->options);
120 g_slice_free (ModestServerAccountData, data);
124 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
126 ModestAccountData *data;
127 gchar *server_account;
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,
133 data = g_slice_new0 (ModestAccountData);
135 data->account_name = g_strdup (name);
137 data->display_name = modest_account_mgr_get_string (self, name,
138 MODEST_ACCOUNT_DISPLAY_NAME,
140 data->fullname = modest_account_mgr_get_string (self, name,
141 MODEST_ACCOUNT_FULLNAME,
143 data->email = modest_account_mgr_get_string (self, name,
144 MODEST_ACCOUNT_EMAIL,
146 data->enabled = modest_account_mgr_get_enabled (self, name);
149 server_account = modest_account_mgr_get_string (self, name,
150 MODEST_ACCOUNT_STORE_ACCOUNT,
152 if (server_account) {
153 data->store_account =
154 modest_account_mgr_get_server_account_data (self,
156 g_free (server_account);
160 server_account = modest_account_mgr_get_string (self, name,
161 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
163 if (server_account) {
164 data->transport_account =
165 modest_account_mgr_get_server_account_data (self,
167 g_free (server_account);
175 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
177 g_return_if_fail (self);
179 if (!data) /* not an error */
182 g_free (data->account_name);
183 g_free (data->display_name);
184 g_free (data->fullname);
185 g_free (data->email);
187 modest_account_mgr_free_server_account_data (self, data->store_account);
188 modest_account_mgr_free_server_account_data (self, data->transport_account);
190 g_slice_free (ModestAccountData, data);
195 modest_account_mgr_get_default_account (ModestAccountMgr *self)
200 g_return_val_if_fail (self, NULL);
202 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
203 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
206 /* it's not really an error if there is no default account */
211 if (!modest_account_mgr_account_exists (self, account, FALSE, NULL)) {
212 g_printerr ("modest: default account does not exist\n");
222 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
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),
231 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
233 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
240 modest_account_mgr_get_tny_account (ModestAccountMgr *self, const gchar* account_name,
243 TnyAccount *account = NULL;
246 gchar *server_account;
247 const gchar *conf_key;
249 g_return_val_if_fail (self, NULL);
250 g_return_val_if_fail (account_name, NULL);
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;
258 g_return_val_if_reached (NULL);
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);
268 accounts = tny_simple_list_new ();
269 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()),
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)
278 g_object_unref (G_OBJECT(iter));
279 g_object_unref (G_OBJECT(accounts));
282 g_printerr ("modest: no tny %s account found for %s\n",
283 type == TNY_ACCOUNT_TYPE_TRANSPORT ? "transport" : "store", account_name);
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));
300 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
302 gchar *fullname, *email, *from;
304 g_return_val_if_fail (self, NULL);
305 g_return_val_if_fail (name, NULL);
307 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
309 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
311 from = g_strdup_printf ("%s <%s>",
312 fullname ? fullname : "",