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