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