ffe4445c91202b3ea165e4c952590f5be8ee2b98
[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
33
34 gboolean
35 modest_account_mgr_account_set_enabled (ModestAccountMgr *self, const gchar* name,
36                                         gboolean enabled)
37 {
38         return modest_account_mgr_set_bool (self, name,
39                                             MODEST_ACCOUNT_ENABLED, enabled,
40                                             FALSE, NULL);
41 }
42
43
44 gboolean
45 modest_account_mgr_account_get_enabled (ModestAccountMgr *self, const gchar* name)
46 {
47         return modest_account_mgr_get_bool (self, name,
48                                             MODEST_ACCOUNT_ENABLED, FALSE,
49                                             NULL);
50 }
51
52
53 static ModestServerAccountData*
54 modest_account_mgr_get_server_account_data (ModestAccountMgr *self, const gchar* name)
55 {
56         ModestServerAccountData *data;
57         gchar *proto;
58         
59         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
60                                                                  TRUE, NULL), NULL);    
61         data = g_new0 (ModestServerAccountData, 1);
62
63         data->account_name = g_strdup (name);
64         data->hostname     = modest_account_mgr_get_string (self, name,
65                                                             MODEST_ACCOUNT_HOSTNAME,
66                                                             TRUE, NULL);
67         data->username     = modest_account_mgr_get_string (self, name,
68                                                             MODEST_ACCOUNT_USERNAME,
69                                                             TRUE, NULL);
70         
71         proto        = modest_account_mgr_get_string (self, name, MODEST_ACCOUNT_PROTO,
72                                                       TRUE, NULL);
73         data->proto  = modest_protocol_info_get_protocol (proto);
74         g_free (proto);
75         
76         data->password     = modest_account_mgr_get_string (self, name,
77                                                             MODEST_ACCOUNT_PASSWORD,
78                                                             TRUE, NULL);
79         return data;
80 }
81
82
83 static void
84 modest_account_mgr_free_server_account_data (ModestAccountMgr *self,
85                                              ModestServerAccountData* data)
86 {
87         g_return_if_fail (self);
88
89         if (!data)
90                 return; /* not an error */
91
92         g_free (data->account_name);
93         data->account_name = NULL;
94         
95         g_free (data->hostname);
96         data->hostname = NULL;
97         
98         g_free (data->username);
99         data->username = NULL;
100
101         g_free (data->password);
102         data->password = NULL;
103         
104         g_free (data);
105 }
106
107 ModestAccountData*
108 modest_account_mgr_get_account_data     (ModestAccountMgr *self, const gchar* name)
109 {
110         ModestAccountData *data;
111         gchar *server_account;
112         
113         g_return_val_if_fail (self, NULL);
114         g_return_val_if_fail (name, NULL);
115         g_return_val_if_fail (modest_account_mgr_account_exists (self, name,
116                                                                  FALSE, NULL), NULL);   
117         data = g_new0 (ModestAccountData, 1);
118
119         data->account_name = g_strdup (name);
120
121         data->display_name = modest_account_mgr_get_string (self, name,
122                                                             MODEST_ACCOUNT_DISPLAY_NAME,
123                                                             FALSE, NULL);
124         data->fullname      = modest_account_mgr_get_string (self, name,
125                                                               MODEST_ACCOUNT_FULLNAME,
126                                                                FALSE, NULL);
127         data->email        = modest_account_mgr_get_string (self, name,
128                                                             MODEST_ACCOUNT_EMAIL,
129                                                             FALSE, NULL);
130         data->enabled      = modest_account_mgr_account_get_enabled (self, name);
131
132         /* store */
133         server_account     = modest_account_mgr_get_string (self, name,
134                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
135                                                             FALSE, NULL);
136         if (server_account) {
137                 data->store_account =
138                         modest_account_mgr_get_server_account_data (self,
139                                                                     server_account);
140                 g_free (server_account);
141         }
142
143         /* transport */
144         server_account = modest_account_mgr_get_string (self, name,
145                                                         MODEST_ACCOUNT_TRANSPORT_ACCOUNT,
146                                                         FALSE, NULL);
147         if (server_account) {
148                 data->transport_account =
149                         modest_account_mgr_get_server_account_data (self,
150                                                                     server_account);
151                 g_free (server_account);
152         }
153
154         return data;
155 }
156
157
158 void
159 modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data)
160 {
161         g_return_if_fail (self);
162
163         if (!data) /* not an error */ 
164                 return;
165
166         g_free (data->account_name);
167         g_free (data->display_name);
168         g_free (data->fullname);
169         g_free (data->email);
170
171         modest_account_mgr_free_server_account_data (self, data->store_account);
172         modest_account_mgr_free_server_account_data (self, data->transport_account);
173         
174         g_free (data);
175 }
176
177
178 gchar*
179 modest_account_mgr_get_default_account  (ModestAccountMgr *self)
180 {
181         gchar *account; 
182         ModestConf *conf;
183         
184         g_return_val_if_fail (self, NULL);
185
186         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
187         account = modest_conf_get_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
188                                           NULL);
189         
190         /* it's not really an error if there is no default account */
191         if (!account) 
192                 return NULL;
193
194         /* sanity check */
195         if (!modest_account_mgr_account_exists (self, account, FALSE, NULL)) {
196                 g_printerr ("modest: default account does not exist\n");
197                 g_free (account);
198                 return NULL;
199         }
200         return account;
201 }
202
203
204 gboolean
205 modest_account_mgr_set_default_account  (ModestAccountMgr *self, const gchar* account)
206 {
207         ModestConf *conf;
208         
209         g_return_val_if_fail (self,    FALSE);
210         g_return_val_if_fail (account, FALSE);
211         g_return_val_if_fail (modest_account_mgr_account_exists (self, account, FALSE, NULL),
212                               FALSE);
213         
214         conf = MODEST_ACCOUNT_MGR_GET_PRIVATE (self)->modest_conf;
215                 
216         return modest_conf_set_string (conf, MODEST_CONF_DEFAULT_ACCOUNT,
217                                        account, NULL);
218
219 }
220
221
222