2007-04-15 Sergio Villar Senin <svillar@igalia.com>
[modest] / src / modest-tny-account.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-tny-platform-factory.h>
31 #include <modest-tny-account.h>
32 #include <modest-tny-account-store.h>
33 #include <modest-runtime.h>
34 #include <tny-simple-list.h>
35 #include <modest-tny-folder.h>
36 #include <modest-account-mgr-helpers.h>
37 #include <tny-camel-transport-account.h>
38 #include <tny-camel-imap-store-account.h>
39 #include <tny-camel-pop-store-account.h>
40
41 /* for now, ignore the account ===> the special folders are the same,
42  * local folders for all accounts
43  * this might change, ie, IMAP might have server-side sent-items
44  */
45 TnyFolder *
46 modest_tny_account_get_special_folder (TnyAccount *account,
47                                        TnyFolderType special_type)
48 {
49         TnyList *folders;
50         TnyIterator *iter;
51         TnyFolder *special_folder = NULL;
52         TnyAccount *local_account;
53         
54         g_return_val_if_fail (account, NULL);
55         g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
56                               NULL);
57         
58         local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
59                                                                         MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
60         if (!local_account) {
61                 g_printerr ("modest: cannot get local account\n");
62                 return NULL;
63         }
64
65         folders = TNY_LIST (tny_simple_list_new ());
66
67         /* no need to do this _async, as these are local folders */
68         tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
69                                       folders, NULL, NULL);
70         iter = tny_list_create_iterator (folders);
71
72         while (!tny_iterator_is_done (iter)) {
73                 TnyFolder *folder =
74                         TNY_FOLDER (tny_iterator_get_current (iter));
75                 if (modest_tny_folder_get_local_folder_type (folder) == special_type) {
76                         special_folder = folder;
77                         break;
78                 }
79                 g_object_unref (G_OBJECT(folder));
80                 tny_iterator_next (iter);
81         }
82         g_object_unref (G_OBJECT (folders));
83         g_object_unref (G_OBJECT (iter));
84         g_object_unref (G_OBJECT (local_account));
85
86         return special_folder;
87 }
88
89
90 /**
91  * modest_tny_account_new_from_server_account:
92  * @account_mgr: a valid account mgr instance
93  * @account_name: the server account name for which to create a corresponding tny account
94  * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
95  * 
96  * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
97  * NOTE: this function does not set the camel session or the get/forget password functions
98  * 
99  * Returns: a new TnyAccount or NULL in case of error.
100  */
101 static TnyAccount*
102 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
103                                             ModestServerAccountData *account_data)
104 {
105         TnyAccount *tny_account;
106                 
107         g_return_val_if_fail (account_mgr, NULL);
108         g_return_val_if_fail (account_data, NULL);
109
110         /* sanity checks */
111         if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) {
112                 g_printerr ("modest: '%s' does not provide a protocol\n",
113                             account_data->account_name);
114                 return NULL;
115         }
116
117         switch (account_data->proto) {
118         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
119         case MODEST_PROTOCOL_TRANSPORT_SMTP:
120                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
121         case MODEST_PROTOCOL_STORE_POP:
122                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
123         case MODEST_PROTOCOL_STORE_IMAP:
124                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
125         case MODEST_PROTOCOL_STORE_MAILDIR:
126         case MODEST_PROTOCOL_STORE_MBOX:
127                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
128         default:
129                 g_return_val_if_reached (NULL);
130         }
131         if (!tny_account) {
132                 g_printerr ("modest: could not create tny account for '%s'\n",
133                             account_data->account_name);
134                 return NULL;
135         }
136         tny_account_set_id (tny_account, account_data->account_name);
137
138         /* Proto */
139         tny_account_set_proto (tny_account,
140                                modest_protocol_info_get_protocol_name(account_data->proto));
141         if (account_data->uri) 
142                 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
143         else {
144                 if (account_data->options) {
145                         GSList *options = account_data->options;
146                         while (options) {
147                                 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
148                                                               options->data);
149                                 options = g_slist_next (options);
150                         }
151                 }
152                 if (account_data->username) 
153                         tny_account_set_user (tny_account, account_data->username);
154                 if (account_data->hostname)
155                         tny_account_set_hostname (tny_account, account_data->hostname);
156         }
157         return tny_account;
158 }
159
160
161 /* we need these dummy functions, or tinymail will complain */
162 static gchar*
163 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
164 {
165         return NULL;
166 }
167 static void
168 forget_pass_dummy (TnyAccount *account)
169 {
170         /* intentionally left blank */
171 }
172
173 TnyAccount*
174 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
175                                      TnyAccountType type,
176                                      TnySessionCamel *session,
177                                      TnyGetPassFunc get_pass_func,
178                                      TnyForgetPassFunc forget_pass_func) 
179 {
180         TnyAccount *tny_account = NULL;
181         ModestAccountData *account_data = NULL;
182         ModestServerAccountData *server_data = NULL;
183
184         g_return_val_if_fail (account_mgr, NULL);
185         g_return_val_if_fail (account_name, NULL);
186
187         account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
188         if (!account_data) {
189                 g_printerr ("modest: cannot get account data for account %s\n", account_name);
190                 return NULL;
191         }
192
193         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
194                 server_data = account_data->store_account;
195         else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
196                 server_data = account_data->transport_account;
197         if (!server_data) {
198                 g_printerr ("modest: no %s account defined for '%s'\n",
199                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
200                             account_data->display_name);
201                 modest_account_mgr_free_account_data (account_mgr, account_data);
202                 return NULL;
203         }
204         
205         tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
206         if (!tny_account) { 
207                 g_printerr ("modest: failed to create tny account for %s (%s)\n",
208                             account_data->account_name, server_data->account_name);
209                 modest_account_mgr_free_account_data (account_mgr, account_data);
210                 return NULL;
211         }
212         
213         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
214         tny_account_set_forget_pass_func (tny_account,
215                                           forget_pass_func ? forget_pass_func : forget_pass_dummy);
216         tny_account_set_pass_func (tny_account,
217                                    get_pass_func ? get_pass_func: get_pass_dummy);
218
219         /* this name is what shows up in the folder view -- so for some POP/IMAP/... server
220          * account, we set its name to the acount of which it is part */
221         if (account_data->display_name)
222                 tny_account_set_name (tny_account, account_data->display_name); 
223
224         g_object_set_data_full (G_OBJECT(tny_account), "modest_account",
225                                 (gpointer*) g_strdup (account_name), g_free);
226         
227         modest_account_mgr_free_account_data (account_mgr, account_data);
228         return tny_account;
229 }
230
231
232 TnyAccount*
233 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
234 {
235         TnyStoreAccount *tny_account;
236         CamelURL *url;
237         gchar *maildir, *url_string;
238
239         g_return_val_if_fail (account_mgr, NULL);
240         
241         tny_account = tny_camel_store_account_new ();
242         if (!tny_account) {
243                 g_printerr ("modest: cannot create account for local folders");
244                 return NULL;
245         }
246         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
247         
248         maildir = modest_local_folder_info_get_maildir_path ();
249         url = camel_url_new ("maildir:", NULL);
250         camel_url_set_path (url, maildir);
251         /* Needed by tinymail's DBC assertions */
252         camel_url_set_host (url, "localhost");
253         url_string = camel_url_to_string (url, 0);
254         
255         tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
256
257         tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME); 
258         tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID); 
259         tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
260         tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
261         
262         g_object_set_data (G_OBJECT(tny_account), "modest_account",
263                            (gpointer*)MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
264         
265         camel_url_free (url);
266         g_free (maildir);
267         g_free (url_string);
268
269         return TNY_ACCOUNT(tny_account);
270 }
271