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