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