* First implementation of Root folders view
[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 #include <tny-folder-stats.h>
41
42 /* for now, ignore the account ===> the special folders are the same,
43  * local folders for all accounts
44  * this might change, ie, IMAP might have server-side sent-items
45  */
46 TnyFolder *
47 modest_tny_account_get_special_folder (TnyAccount *account,
48                                        TnyFolderType special_type)
49 {
50         TnyList *folders;
51         TnyIterator *iter;
52         TnyFolder *special_folder = NULL;
53         TnyAccount *local_account;
54         
55         g_return_val_if_fail (account, NULL);
56         g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
57                               NULL);
58         
59         local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
60                                                                         MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
61         if (!local_account) {
62                 g_printerr ("modest: cannot get local account\n");
63                 return NULL;
64         }
65
66         folders = TNY_LIST (tny_simple_list_new ());
67
68         /* no need to do this _async, as these are local folders */
69         tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
70                                       folders, NULL, NULL);
71         iter = tny_list_create_iterator (folders);
72
73         while (!tny_iterator_is_done (iter)) {
74                 TnyFolder *folder =
75                         TNY_FOLDER (tny_iterator_get_current (iter));
76                 if (modest_tny_folder_get_local_folder_type (folder) == special_type) {
77                         special_folder = folder;
78                         break;
79                 }
80                 g_object_unref (G_OBJECT(folder));
81                 tny_iterator_next (iter);
82         }
83         g_object_unref (G_OBJECT (folders));
84         g_object_unref (G_OBJECT (iter));
85         g_object_unref (G_OBJECT (local_account));
86
87         return special_folder;
88 }
89
90
91 /**
92  * modest_tny_account_new_from_server_account:
93  * @account_mgr: a valid account mgr instance
94  * @account_name: the server account name for which to create a corresponding tny account
95  * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
96  * 
97  * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
98  * NOTE: this function does not set the camel session or the get/forget password functions
99  * 
100  * Returns: a new TnyAccount or NULL in case of error.
101  */
102 static TnyAccount*
103 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
104                                             ModestServerAccountData *account_data)
105 {
106         TnyAccount *tny_account;
107                 
108         g_return_val_if_fail (account_mgr, NULL);
109         g_return_val_if_fail (account_data, NULL);
110
111         /* sanity checks */
112         if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) {
113                 g_printerr ("modest: '%s' does not provide a protocol\n",
114                             account_data->account_name);
115                 return NULL;
116         }
117
118         switch (account_data->proto) {
119         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
120         case MODEST_PROTOCOL_TRANSPORT_SMTP:
121                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
122         case MODEST_PROTOCOL_STORE_POP:
123                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
124         case MODEST_PROTOCOL_STORE_IMAP:
125                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
126         case MODEST_PROTOCOL_STORE_MAILDIR:
127         case MODEST_PROTOCOL_STORE_MBOX:
128                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
129         default:
130                 g_return_val_if_reached (NULL);
131         }
132         if (!tny_account) {
133                 g_printerr ("modest: could not create tny account for '%s'\n",
134                             account_data->account_name);
135                 return NULL;
136         }
137         tny_account_set_id (tny_account, account_data->account_name);
138
139         /* Proto */
140         tny_account_set_proto (tny_account,
141                                modest_protocol_info_get_protocol_name(account_data->proto));
142         if (account_data->uri) 
143                 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
144         else {
145                 if (account_data->options) {
146                         GSList *options = account_data->options;
147                         while (options) {
148                                 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
149                                                               options->data);
150                                 options = g_slist_next (options);
151                         }
152                 }
153                 if (account_data->username) 
154                         tny_account_set_user (tny_account, account_data->username);
155                 if (account_data->hostname)
156                         tny_account_set_hostname (tny_account, account_data->hostname);
157         }
158         return tny_account;
159 }
160
161
162 /* we need these dummy functions, or tinymail will complain */
163 static gchar*
164 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
165 {
166         return NULL;
167 }
168 static void
169 forget_pass_dummy (TnyAccount *account)
170 {
171         /* intentionally left blank */
172 }
173
174 TnyAccount*
175 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
176                                      TnyAccountType type,
177                                      TnySessionCamel *session,
178                                      TnyGetPassFunc get_pass_func,
179                                      TnyForgetPassFunc forget_pass_func) 
180 {
181         TnyAccount *tny_account = NULL;
182         ModestAccountData *account_data = NULL;
183         ModestServerAccountData *server_data = NULL;
184
185         g_return_val_if_fail (account_mgr, NULL);
186         g_return_val_if_fail (account_name, NULL);
187
188         account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
189         if (!account_data) {
190                 g_printerr ("modest: cannot get account data for account %s\n", account_name);
191                 return NULL;
192         }
193
194         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
195                 server_data = account_data->store_account;
196         else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
197                 server_data = account_data->transport_account;
198         if (!server_data) {
199                 g_printerr ("modest: no %s account defined for '%s'\n",
200                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
201                             account_data->display_name);
202                 modest_account_mgr_free_account_data (account_mgr, account_data);
203                 return NULL;
204         }
205         
206         tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
207         if (!tny_account) { 
208                 g_printerr ("modest: failed to create tny account for %s (%s)\n",
209                             account_data->account_name, server_data->account_name);
210                 modest_account_mgr_free_account_data (account_mgr, account_data);
211                 return NULL;
212         }
213         
214         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
215         tny_account_set_forget_pass_func (tny_account,
216                                           forget_pass_func ? forget_pass_func : forget_pass_dummy);
217         tny_account_set_pass_func (tny_account,
218                                    get_pass_func ? get_pass_func: get_pass_dummy);
219
220         /* this name is what shows up in the folder view -- so for some POP/IMAP/... server
221          * account, we set its name to the acount of which it is part */
222         if (account_data->display_name)
223                 tny_account_set_name (tny_account, account_data->display_name); 
224
225         g_object_set_data_full (G_OBJECT(tny_account), "modest_account",
226                                 (gpointer*) g_strdup (account_name), g_free);
227         
228         modest_account_mgr_free_account_data (account_mgr, account_data);
229         return tny_account;
230 }
231
232
233 TnyAccount*
234 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
235 {
236         TnyStoreAccount *tny_account;
237         CamelURL *url;
238         gchar *maildir, *url_string;
239
240         g_return_val_if_fail (account_mgr, NULL);
241         
242         tny_account = tny_camel_store_account_new ();
243         if (!tny_account) {
244                 g_printerr ("modest: cannot create account for local folders");
245                 return NULL;
246         }
247         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
248         
249         maildir = modest_local_folder_info_get_maildir_path ();
250         url = camel_url_new ("maildir:", NULL);
251         camel_url_set_path (url, maildir);
252         /* Needed by tinymail's DBC assertions */
253         camel_url_set_host (url, "localhost");
254         url_string = camel_url_to_string (url, 0);
255         
256         tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
257
258         tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME); 
259         tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID); 
260         tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
261         tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
262         
263         g_object_set_data (G_OBJECT(tny_account), "modest_account",
264                            (gpointer*)MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
265         
266         camel_url_free (url);
267         g_free (maildir);
268         g_free (url_string);
269
270         return TNY_ACCOUNT(tny_account);
271 }
272
273 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
274
275 typedef struct _RecurseFoldersHelper {
276         TnyStatsFunc function;
277         guint sum;
278 } RecurseFoldersHelper;
279
280 static void
281 recurse_folders (TnyFolderStore *store, 
282                  TnyFolderStoreQuery *query, 
283                  RecurseFoldersHelper *helper)
284 {
285         TnyIterator *iter;
286         TnyList *folders = tny_simple_list_new ();
287
288         tny_folder_store_get_folders (store, folders, query, NULL);
289         iter = tny_list_create_iterator (folders);
290
291         while (!tny_iterator_is_done (iter))
292         {
293                 TnyFolderStats *stats;
294                 TnyFolder *folder;
295
296                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
297                 stats = tny_folder_get_stats (folder);
298
299                 helper->sum += helper->function (stats);
300
301                 recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
302             
303                 g_object_unref (folder);
304                 g_object_unref (stats);
305                 tny_iterator_next (iter);
306         }
307          g_object_unref (G_OBJECT (iter));
308          g_object_unref (G_OBJECT (folders));
309 }
310
311 gint 
312 modest_tny_account_get_folder_count (TnyAccount *self)
313 {
314         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
315
316         return 8;
317 }
318
319 gint
320 modest_tny_account_get_message_count (TnyAccount *self)
321 {
322         RecurseFoldersHelper *helper;
323         gint retval;
324
325         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
326
327         /* Create helper */
328         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
329         helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
330         helper->sum = 0;
331
332         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
333
334         retval = helper->sum;
335
336         g_free (helper);
337
338         return retval;
339 }
340
341 gint 
342 modest_tny_account_get_local_size (TnyAccount *self)
343 {
344         RecurseFoldersHelper *helper;
345         gint retval;
346
347         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
348
349         /* Create helper */
350         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
351         helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
352         helper->sum = 0;
353
354         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
355
356         retval = helper->sum;
357
358         g_free (helper);
359
360         return retval;
361 }