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