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