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