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