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