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