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