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