2007-05-28 Murray Cumming <murrayc@murrayc.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-tny-outbox-account.h>
37 #include <modest-account-mgr-helpers.h>
38 #include <modest-init.h>
39 #include <tny-camel-transport-account.h>
40 #include <tny-camel-imap-store-account.h>
41 #include <tny-camel-pop-store-account.h>
42 #include <tny-folder-stats.h>
43
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
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         TnyAccount *local_account  = NULL;
59                 
60         /* The accounts have already been instantiated by 
61          * modest_tny_account_store_get_accounts(), which is the 
62          * TnyAccountStore::get_accounts_func() implementation,
63          * so we just get them here.
64          */
65          
66         /* Per-account outbox folders are each in their own on-disk directory: */
67         if (special_type == TNY_FOLDER_TYPE_OUTBOX) {
68                 const gchar *modest_account_name = 
69                         modest_tny_account_get_parent_modest_account_name_for_server_account (account);
70                 g_assert (modest_account_name);
71
72                 gchar *account_id = g_strdup_printf (
73                         MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s", 
74                         modest_account_name);
75                 
76                 local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
77                                                                         account_id);
78                 g_free (account_id);
79         } else {
80                 /* Other local folders are all in one on-disk directory: */
81                 local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
82                                                                                 MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
83         }
84         
85         if (!local_account) {
86                 g_printerr ("modest: cannot get local account\n");
87                 return NULL;
88         }
89
90         folders = TNY_LIST (tny_simple_list_new ());
91
92         /* There is no need to do this _async, as these are local folders. */
93         /* TODO: However, this seems to fail sometimes when the network is busy, 
94          * returning an empty list. murrayc. */
95         tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
96                                       folders, NULL, NULL);
97         iter = tny_list_create_iterator (folders);
98
99         while (!tny_iterator_is_done (iter)) {
100                 TnyFolder *folder =
101                         TNY_FOLDER (tny_iterator_get_current (iter));
102                 if (modest_tny_folder_get_local_folder_type (folder) == special_type) {
103                         special_folder = folder;
104                         break;
105                 }
106                 
107                 g_object_unref (G_OBJECT(folder));
108                 tny_iterator_next (iter);
109         }
110         
111         g_object_unref (G_OBJECT (folders));
112         g_object_unref (G_OBJECT (iter));
113         g_object_unref (G_OBJECT (local_account));
114
115         return special_folder;
116 }
117
118 /* Camel options: */
119
120 /* These seem to be listed in 
121  * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c 
122  */
123 #define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
124 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
125 #define MODEST_ACCOUNT_OPTION_SSL_ALWAYS "always"
126 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
127
128 /* These seem to be listed in 
129  * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c 
130  */
131 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
132 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
133
134
135 /* Posssible values for tny_account_set_secure_auth_mech().
136  * These might be camel-specific.
137  * Really, tinymail should use an enum.
138  * camel_sasl_authtype() seems to list some possible values.
139  */
140  
141 /* Note that evolution does not offer these for IMAP: */
142 #define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
143 #define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
144
145 /* Caeml's IMAP uses NULL instead for "Password".
146  * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
147 #define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN" 
148 #define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
149
150
151 /**
152  * modest_tny_account_new_from_server_account:
153  * @account_mgr: a valid account mgr instance
154  * @account_name: the server account name for which to create a corresponding tny account
155  * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
156  * 
157  * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
158  * NOTE: this function does not set the camel session or the get/forget password functions
159  * 
160  * Returns: a new TnyAccount or NULL in case of error.
161  */
162 static TnyAccount*
163 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
164                                             ModestServerAccountData *account_data)
165 {       
166         gchar *url = NULL;
167
168         g_return_val_if_fail (account_mgr, NULL);
169         g_return_val_if_fail (account_data, NULL);
170
171         /* sanity checks */
172         if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN) {
173                 g_printerr ("modest: '%s' does not provide a protocol\n",
174                             account_data->account_name);
175                 return NULL;
176         }
177
178         TnyAccount *tny_account = NULL;
179         
180         switch (account_data->proto) {
181         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
182         case MODEST_PROTOCOL_TRANSPORT_SMTP:
183                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
184         case MODEST_PROTOCOL_STORE_POP:
185                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
186         case MODEST_PROTOCOL_STORE_IMAP:
187                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
188         case MODEST_PROTOCOL_STORE_MAILDIR:
189         case MODEST_PROTOCOL_STORE_MBOX:
190                 /* Note that this is not where we create the special local folders account.
191                  * That happens in modest_tny_account_new_for_local_folders() instead.
192                  */
193                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
194         default:
195                 g_return_val_if_reached (NULL);
196         }
197         if (!tny_account) {
198                 g_printerr ("modest: could not create tny account for '%s'\n",
199                             account_data->account_name);
200                 return NULL;
201         }
202         tny_account_set_id (tny_account, account_data->account_name);
203
204         /* Proto */
205         const gchar* proto_name =
206                 modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
207         tny_account_set_proto (tny_account, proto_name);
208
209                
210         /* mbox and maildir accounts use a URI instead of the rest:
211          * Note that this is not where we create the special local folders account.
212          * We do that in modest_tny_account_new_for_local_folders() instead. */
213         if (account_data->uri)  {
214                 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
215                 g_message ("DEBUG: %s: local account-url:\n  %s", __FUNCTION__, account_data->uri);
216         }
217         else {
218                 /* Set camel-specific options: */
219                 
220                 /* Enable secure connection settings: */
221                 /* printf("DEBUG: %s: security=%d\n", __FUNCTION__, account_data->security); */
222                 const gchar* option_security = NULL;
223                 switch (account_data->security) {
224                 case MODEST_PROTOCOL_CONNECTION_NORMAL:
225                         option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_NEVER;
226                         break;
227                 case MODEST_PROTOCOL_CONNECTION_SSL:
228                 case MODEST_PROTOCOL_CONNECTION_TLS:
229                         option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_ALWAYS;;
230                         break;
231                 case MODEST_PROTOCOL_CONNECTION_TLS_OP:
232                         option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
233                         break;
234                 default:
235                         break;
236                 }
237                 
238                 if(option_security)
239                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
240                                                       option_security);
241                 
242                 /* Secure authentication: */
243                 /* printf("DEBUG: %s: secure-auth=%d\n", __FUNCTION__, account_data->secure_auth); */
244                 const gchar* auth_mech_name = NULL;
245                 switch (account_data->secure_auth) {
246                 case MODEST_PROTOCOL_AUTH_NONE:
247                         /* IMAP and POP need at least a password,
248                          * which camel uses if we specify NULL.
249                          * This setting should never happen anyway. */
250                         if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP ||
251                             account_data->proto == MODEST_PROTOCOL_STORE_POP)
252                                 auth_mech_name = NULL;
253                         else if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP)
254                                 auth_mech_name = MODEST_ACCOUNT_AUTH_ANONYMOUS;
255                         else
256                                 auth_mech_name = MODEST_ACCOUNT_AUTH_PLAIN;
257                         break;
258                         
259                 case MODEST_PROTOCOL_AUTH_PASSWORD:
260                         /* Camel use a password for IMAP or POP if we specify NULL,
261                          * For IMAP, at least it will report an error if we use "Password", "Login" or "Plain".
262                          * (POP is know to report an error for Login too. Probably Password and Plain too.) */
263                         if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
264                                 auth_mech_name = NULL;
265                         else if (account_data->proto == MODEST_PROTOCOL_STORE_POP)
266                                 auth_mech_name = NULL;
267                         else
268                                 auth_mech_name = MODEST_ACCOUNT_AUTH_PASSWORD;
269                         break;
270                         
271                 case MODEST_PROTOCOL_AUTH_CRAMMD5:
272                         auth_mech_name = MODEST_ACCOUNT_AUTH_CRAMMD5;
273                         break;
274                         
275                 default:
276                         g_warning ("%s: Unhandled secure authentication setting %d for "
277                                 "account=%s (%s)", __FUNCTION__, account_data->secure_auth,
278                                    account_data->account_name, account_data->hostname);
279                         break;
280                 }
281                 
282                 if(auth_mech_name) 
283                         tny_account_set_secure_auth_mech (tny_account, auth_mech_name);
284                 
285                 if (modest_protocol_info_protocol_is_store(account_data->proto) && 
286                         (account_data->proto == MODEST_PROTOCOL_STORE_IMAP) ) {
287                         /* Other connection options, needed for IMAP. */
288                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
289                                                       MODEST_ACCOUNT_OPTION_USE_LSUB);
290                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
291                                                       MODEST_ACCOUNT_OPTION_CHECK_ALL);
292                 }
293                 
294                 if (account_data->username) 
295                         tny_account_set_user (tny_account, account_data->username);
296                 if (account_data->hostname)
297                         tny_account_set_hostname (tny_account, account_data->hostname);
298                  
299                 /* Set the port: */
300                 if (account_data->port)
301                         tny_account_set_port (tny_account, account_data->port);
302         }
303
304         /* FIXME: for debugging */
305         url = tny_account_get_url_string (TNY_ACCOUNT(tny_account));
306         g_message ("modest: %s:\n  account-url: %s", __FUNCTION__, url);
307         g_free (url);
308         /***********************/
309         
310         /* For transport accounts, now is a good time to create the send queues, 
311          * so that the send queues start trying as soon as possible to send any 
312          * messages that are already in their outboxes: */
313         if ( (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SENDMAIL) ||
314              (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP) ) {
315                 /* modest_runtime_get_send_queue() instantiates and stores the send queue: */
316                 modest_runtime_get_send_queue( TNY_TRANSPORT_ACCOUNT (tny_account));
317         }
318         
319         return tny_account;
320 }
321
322
323 /* we need these dummy functions, or tinymail will complain */
324 static gchar*
325 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
326 {
327         return NULL;
328 }
329 static void
330 forget_pass_dummy (TnyAccount *account)
331 {
332         /* intentionally left blank */
333 }
334
335 TnyAccount*
336 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
337                                      TnyAccountType type,
338                                      TnySessionCamel *session,
339                                      TnyGetPassFunc get_pass_func,
340                                      TnyForgetPassFunc forget_pass_func) 
341 {
342         TnyAccount *tny_account = NULL;
343         ModestAccountData *account_data = NULL;
344         ModestServerAccountData *server_data = NULL;
345
346         g_return_val_if_fail (account_mgr, NULL);
347         g_return_val_if_fail (account_name, NULL);
348
349         account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
350         if (!account_data) {
351                 g_printerr ("modest: cannot get account data for account %s\n", account_name);
352                 return NULL;
353         }
354
355         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
356                 server_data = account_data->store_account;
357         else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
358                 server_data = account_data->transport_account;
359         if (!server_data) {
360                 g_printerr ("modest: no %s account defined for '%s'\n",
361                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
362                             account_data->display_name);
363                 modest_account_mgr_free_account_data (account_mgr, account_data);
364                 return NULL;
365         }
366         
367         tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
368         if (!tny_account) { 
369                 g_printerr ("modest: failed to create tny account for %s (%s)\n",
370                             account_data->account_name, server_data->account_name);
371                 modest_account_mgr_free_account_data (account_mgr, account_data);
372                 return NULL;
373         }
374         
375         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
376         tny_account_set_forget_pass_func (tny_account,
377                                           forget_pass_func ? forget_pass_func : forget_pass_dummy);
378         tny_account_set_pass_func (tny_account,
379                                    get_pass_func ? get_pass_func: get_pass_dummy);
380         
381         /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
382          * account, we set its name to the account of which it is part. */
383         if (account_data->display_name)
384                 tny_account_set_name (tny_account, account_data->display_name); 
385
386         modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account, account_name);
387         
388         modest_account_mgr_free_account_data (account_mgr, account_data);
389
390         return tny_account;
391 }
392
393
394 TnyAccount*
395 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
396 {
397         TnyStoreAccount *tny_account;
398         CamelURL *url;
399         gchar *maildir, *url_string;
400
401         g_return_val_if_fail (account_mgr, NULL);
402         
403         tny_account = tny_camel_store_account_new ();
404         if (!tny_account) {
405                 g_printerr ("modest: cannot create account for local folders");
406                 return NULL;
407         }
408         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
409         
410         /* This path contains directories for each local folder.
411          * We have created them so that TnyCamelStoreAccount can find them 
412          * and report a folder for each directory: */
413         maildir = modest_local_folder_info_get_maildir_path ();
414         url = camel_url_new ("maildir:", NULL);
415         camel_url_set_path (url, maildir);
416         /* Needed by tinymail's DBC assertions */
417         camel_url_set_host (url, "localhost");
418         url_string = camel_url_to_string (url, 0);
419         
420         tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
421         printf("DEBUG: %s:\n  url=%s\n", __FUNCTION__, url_string);
422
423         tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME); 
424         tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID); 
425         tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
426         tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
427         
428         modest_tny_account_set_parent_modest_account_name_for_server_account (
429                 TNY_ACCOUNT (tny_account), MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
430         
431         camel_url_free (url);
432         g_free (maildir);
433         g_free (url_string);
434
435         return TNY_ACCOUNT(tny_account);
436 }
437
438
439 TnyAccount*
440 modest_tny_account_new_for_per_account_local_outbox_folder (ModestAccountMgr *account_mgr, const gchar* account_name, TnySessionCamel *session)
441 {
442         g_return_val_if_fail (account_mgr, NULL);
443         g_return_val_if_fail (account_name, NULL);
444         
445         /* Notice that we create a ModestTnyOutboxAccount here, 
446          * instead of just a TnyCamelStoreAccount,
447          * so that we can later identify this as a special account for internal use only.
448          */
449         TnyStoreAccount *tny_account = TNY_STORE_ACCOUNT (modest_tny_outbox_account_new ());
450         if (!tny_account) {
451                 g_printerr ("modest: cannot create account for per-account local outbox folder.");
452                 return NULL;
453         }
454         
455         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
456         
457         /* Make sure that the paths exists on-disk so that TnyCamelStoreAccount can 
458          * find it to create a TnyFolder for it: */
459         gchar *folder_dir = modest_per_account_local_outbox_folder_info_get_maildir_path_to_outbox_folder (account_name); 
460         modest_init_one_local_folder(folder_dir);
461         g_free (folder_dir);
462         folder_dir = NULL;
463
464         /* This path should contain just one directory - "outbox": */
465         gchar *maildir = 
466                 modest_per_account_local_outbox_folder_info_get_maildir_path (account_name);
467                         
468         CamelURL *url = camel_url_new ("maildir:", NULL);
469         camel_url_set_path (url, maildir);
470         g_free (maildir);
471         
472         /* Needed by tinymail's DBC assertions */
473         camel_url_set_host (url, "localhost");
474         gchar *url_string = camel_url_to_string (url, 0);
475         camel_url_free (url);
476         
477         tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
478         printf("DEBUG: %s:\n  url=%s\n", __FUNCTION__, url_string);
479         g_free (url_string);
480
481         /* This text should never been seen,
482          * because the per-account outbox accounts are not seen directly by the user.
483          * Their folders are merged and shown as one folder. */ 
484         tny_account_set_name (TNY_ACCOUNT(tny_account), "Per-Account Outbox"); 
485         
486         gchar *account_id = g_strdup_printf (
487                 MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s", 
488                 account_name);
489         tny_account_set_id (TNY_ACCOUNT(tny_account), account_id);
490         g_free (account_id);
491         
492         tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
493         tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
494         
495         /* Make this think that it belongs to the modest local-folders parent account: */
496         modest_tny_account_set_parent_modest_account_name_for_server_account (
497                 TNY_ACCOUNT (tny_account), MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
498
499         return TNY_ACCOUNT(tny_account);
500 }
501
502
503
504 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
505
506 typedef struct _RecurseFoldersHelper {
507         TnyStatsFunc function;
508         guint sum;
509         guint folders;
510 } RecurseFoldersHelper;
511
512 static void
513 recurse_folders (TnyFolderStore *store, 
514                  TnyFolderStoreQuery *query, 
515                  RecurseFoldersHelper *helper)
516 {
517         TnyIterator *iter;
518         TnyList *folders = tny_simple_list_new ();
519
520         tny_folder_store_get_folders (store, folders, query, NULL);
521         iter = tny_list_create_iterator (folders);
522
523         helper->folders += tny_list_get_length (folders);
524
525         while (!tny_iterator_is_done (iter)) {
526                 TnyFolderStats *stats;
527                 TnyFolder *folder;
528
529                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
530                 stats = tny_folder_get_stats (folder);
531
532                 /* initially, we sometimes get -1 from tinymail; ignore that */
533                 if (helper->function && helper->function (stats) > 0)
534                         helper->sum += helper->function (stats);
535
536                 if (TNY_IS_FOLDER_STORE (folder)) {
537                         recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
538                 }
539             
540                 g_object_unref (folder);
541                 g_object_unref (stats);
542                 tny_iterator_next (iter);
543         }
544          g_object_unref (G_OBJECT (iter));
545          g_object_unref (G_OBJECT (folders));
546 }
547
548 gint 
549 modest_tny_folder_store_get_folder_count (TnyFolderStore *self)
550 {
551         RecurseFoldersHelper *helper;
552         gint retval;
553
554         g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
555
556         /* Create helper */
557         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
558         helper->function = NULL;
559         helper->sum = 0;
560         helper->folders = 0;
561
562         recurse_folders (self, NULL, helper);
563
564         retval = helper->folders;
565
566         g_free (helper);
567
568         return retval;
569 }
570
571 gint
572 modest_tny_folder_store_get_message_count (TnyFolderStore *self)
573 {
574         RecurseFoldersHelper *helper;
575         gint retval;
576
577         g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
578         
579         /* Create helper */
580         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
581         helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
582         helper->sum = 0;
583
584         recurse_folders (self, NULL, helper);
585
586         retval = helper->sum;
587
588         g_free (helper);
589
590         return retval;
591 }
592
593 gint 
594 modest_tny_folder_store_get_local_size (TnyFolderStore *self)
595 {
596         RecurseFoldersHelper *helper;
597         gint retval;
598
599         g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
600
601         /* Create helper */
602         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
603         helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
604         helper->sum = 0;
605
606         recurse_folders (self, NULL, helper);
607
608         retval = helper->sum;
609
610         g_free (helper);
611
612         return retval;
613 }
614
615 const gchar* modest_tny_account_get_parent_modest_account_name_for_server_account (TnyAccount *self)
616 {
617         return (const gchar *)g_object_get_data (G_OBJECT (self), "modest_account");
618 }
619
620 void modest_tny_account_set_parent_modest_account_name_for_server_account (TnyAccount *self, const gchar* parent_modest_acount_name)
621 {
622         g_object_set_data_full (G_OBJECT(self), "modest_account",
623                                 (gpointer*) g_strdup (parent_modest_acount_name), g_free);
624 }
625
626