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