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