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);
79 data->last_updated = modest_account_mgr_get_int (self, name,
80 MODEST_ACCOUNT_LAST_UPDATED,
83 data->password = modest_account_mgr_get_string (self, name,
84 MODEST_ACCOUNT_PASSWORD,
87 data->uri = modest_account_mgr_get_string (self, name,
90 data->options = modest_account_mgr_get_list (self, name,
91 MODEST_ACCOUNT_OPTIONS,
92 MODEST_CONF_VALUE_STRING,
99 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
100 ModestServerAccountData* data)
102 g_return_if_fail (self);
105 return; /* not an error */
107 g_free (data->account_name);
108 data->account_name = NULL;
110 g_free (data->hostname);
111 data->hostname = NULL;
113 g_free (data->username);
114 data->username = NULL;
116 g_free (data->password);
117 data->password = NULL;
120 GSList *tmp = data->options;
123 tmp = g_slist_next (tmp);
125 g_slist_free (data->options);
128 g_slice_free (ModestServerAccountData, data);
132 modest_account_mgr_get_account_data (ModestAccountMgr *self, const gchar* name)
134 ModestAccountData *data;
135 gchar *server_account;
136 gchar *default_account;
138 g_return_val_if_fail (self, NULL);
139 g_return_val_if_fail (name, NULL);
140 g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
142 data = g_slice_new0 (ModestAccountData);
144 data->account_name = g_strdup (name);
146 data->display_name = modest_account_mgr_get_string (self, name,
147 MODEST_ACCOUNT_DISPLAY_NAME,
149 data->fullname = modest_account_mgr_get_string (self, name,
150 MODEST_ACCOUNT_FULLNAME,
152 data->email = modest_account_mgr_get_string (self, name,
153 MODEST_ACCOUNT_EMAIL,
155 data->is_enabled = modest_account_mgr_get_enabled (self, name);
157 default_account = modest_account_mgr_get_default_account (self);
158 data->is_default = (default_account && strcmp (default_account, name) == 0);
159 g_free (default_account);
162 server_account = modest_account_mgr_get_string (self, name,
163 MODEST_ACCOUNT_STORE_ACCOUNT,
165 if (server_account) {
166 data->store_account =
167 modest_account_mgr_get_server_account_data (self,
169 g_free (server_account);
173 server_account = modest_account_mgr_get_string (self, name,
174 MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
176 if (server_account) {
177 data->transport_account =
178 modest_account_mgr_get_server_account_data (self,
180 g_free (server_account);
188 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
190 g_return_if_fail (self);
192 if (!data) /* not an error */
195 g_free (data->account_name);
196 g_free (data->display_name);
197 g_free (data->fullname);
198 g_free (data->email);
200 modest_account_mgr_free_server_account_data (self, data->store_account);
201 modest_account_mgr_free_server_account_data (self, data->transport_account);
203 g_slice_free (ModestAccountData, data);
208 modest_account_mgr_get_default_account (ModestAccountMgr *self)
213 g_return_val_if_fail (self, NULL);
215 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
216 account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
219 /* it's not really an error if there is no default account */
224 if (!modest_account_mgr_account_exists (self, account, FALSE, NULL)) {
225 g_printerr ("modest: default account does not exist\n");
235 modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* account)
239 g_return_val_if_fail (self, FALSE);
240 g_return_val_if_fail (account, FALSE);
241 g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE, NULL),
244 conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
246 return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
252 modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name)
254 gchar *fullname, *email, *from;
256 g_return_val_if_fail (self, NULL);
257 g_return_val_if_fail (name, NULL);
259 fullname = modest_account_mgr_get_string (self, name,MODEST_ACCOUNT_FULLNAME,
261 email = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_EMAIL,
263 from = g_strdup_printf ("%s <%s>",
264 fullname ? fullname : "",