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