1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <modest-platform.h>
31 #include <modest-tny-platform-factory.h>
32 #include <modest-tny-account.h>
33 #include <modest-tny-account-store.h>
34 #include <modest-default-connection-policy.h>
35 #include <modest-tny-local-folders-account.h>
36 #include <modest-runtime.h>
37 #include <tny-simple-list.h>
38 #include <modest-tny-folder.h>
39 #include <modest-tny-outbox-account.h>
40 #include <modest-transport-account-decorator.h>
41 #include <modest-account-mgr-helpers.h>
42 #include <modest-init.h>
43 #include <tny-camel-transport-account.h>
44 #include <tny-camel-imap-store-account.h>
45 #include <tny-camel-pop-store-account.h>
46 #include <tny-folder-stats.h>
47 #include <tny-merge-folder.h>
48 #include <modest-debug.h>
50 #ifdef MODEST_PLATFORM_MAEMO
51 #ifdef MODEST_HAVE_HILDON0_WIDGETS
52 #include <hildon-widgets/hildon-file-system-info.h>
54 #include <hildon/hildon-file-system-info.h>
58 /* we need these dummy functions, or tinymail will complain */
59 static gchar * get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel);
60 static void forget_pass_dummy (TnyAccount *account);
63 modest_tny_account_get_special_folder (TnyAccount *account,
64 TnyFolderType special_type)
66 TnyList *folders = NULL;
67 TnyIterator *iter = NULL;
68 TnyFolder *special_folder = NULL;
69 TnyAccount *local_account = NULL;
72 g_return_val_if_fail (account, NULL);
73 g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
76 /* The accounts have already been instantiated by
77 * modest_tny_account_store_get_accounts(), which is the
78 * TnyAccountStore::get_accounts_func() implementation,
79 * so we just get them here.
82 /* Per-account outbox folders are each in their own on-disk directory: */
83 if ((special_type == TNY_FOLDER_TYPE_OUTBOX) &&
84 (!modest_tny_account_is_virtual_local_folders (account))) {
87 const gchar *modest_account_name;
90 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
91 if (!modest_account_name) {
92 g_warning ("%s: could not get modest account name", __FUNCTION__);
96 account_id = g_strdup_printf (
97 MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s",
100 local_account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
101 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
105 /* Other local folders are all in one on-disk directory: */
106 local_account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
107 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
108 MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
109 if (!local_account) {
110 g_printerr ("modest: cannot get local account\n");
114 folders = TNY_LIST (tny_simple_list_new ());
116 /* There is no need to do this _async, as these are local folders. */
117 /* TODO: However, this seems to fail sometimes when the network is busy,
118 * returning an empty list. murrayc. */
119 tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account), folders, NULL, &error);
121 g_warning ("%s: tny_folder_store_get_folders() failed:%s\n", __FUNCTION__, error->message);
122 g_error_free (error);
126 if (tny_list_get_length (folders) == 0) {
127 gchar* url_string = tny_account_get_url_string (local_account);
128 g_printerr ("modest: %s: tny_folder_store_get_folders(%s) returned an empty list\n",
129 __FUNCTION__, url_string);
134 iter = tny_list_create_iterator (folders);
136 while (!tny_iterator_is_done (iter)) {
137 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
139 if (modest_tny_folder_get_local_or_mmc_folder_type (folder) == special_type) {
140 special_folder = folder;
141 break; /* Leaving a ref for the special_folder return value. */
143 g_object_unref (folder);
145 tny_iterator_next (iter);
150 g_object_unref (folders);
152 g_object_unref (iter);
154 g_object_unref (local_account);
156 return special_folder;
160 * create_tny_account:
161 * @session: A valid TnySessionCamel instance.
162 * @account_data: the server account for which to create a corresponding tny account
164 * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
165 * NOTE: this function does not set the camel session or the get/forget password functions
167 * Returns: a new TnyAccount or NULL in case of error.
170 create_tny_account (TnySessionCamel *session,
171 ModestServerAccountSettings *server_settings)
173 TnyAccount *tny_account = NULL;
174 ModestTransportStoreProtocol protocol;
175 const gchar* proto_name;
177 g_return_val_if_fail (session, NULL);
178 g_return_val_if_fail (server_settings, NULL);
179 protocol = modest_server_account_settings_get_protocol (server_settings);
180 g_return_val_if_fail (protocol != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
183 case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
184 case MODEST_PROTOCOL_TRANSPORT_SMTP:
185 tny_account = TNY_ACCOUNT (modest_transport_account_decorator_new ()); break;
186 case MODEST_PROTOCOL_STORE_POP:
187 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
188 case MODEST_PROTOCOL_STORE_IMAP:
189 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
190 case MODEST_PROTOCOL_STORE_MAILDIR:
191 case MODEST_PROTOCOL_STORE_MBOX:
192 /* Note that this is not where we create the special local folders account.
193 * That happens in modest_tny_account_new_for_local_folders() instead.
195 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
197 g_return_val_if_reached (NULL);
201 g_printerr ("modest: %s: could not create tny account for '%s'\n",
202 __FUNCTION__, modest_server_account_settings_get_account_name (server_settings));
205 tny_account_set_id (tny_account, modest_server_account_settings_get_account_name (server_settings));
207 /* This must be set quite early, or other set() functions will fail. */
208 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (tny_account), session);
211 proto_name = modest_protocol_info_get_transport_store_protocol_name(protocol);
212 tny_account_set_proto (tny_account, proto_name);
221 /* These seem to be listed in
222 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c
224 #define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
225 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
226 /* This is a tinymail camel-lite specific option,
227 * roughly equivalent to "always" in regular camel,
228 * which is appropriate for a generic "SSL" connection option: */
229 #define MODEST_ACCOUNT_OPTION_SSL_WRAPPED "wrapped"
230 /* Not used in our UI so far: */
231 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
232 /* This is a tinymailcamel-lite specific option that is not in regular camel. */
233 #define MODEST_ACCOUNT_OPTION_SSL_TLS "tls"
235 /* These seem to be listed in
236 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c
238 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
239 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
241 /* Posssible values for tny_account_set_secure_auth_mech().
242 * These might be camel-specific.
243 * Really, tinymail should use an enum.
244 * camel_sasl_authtype() seems to list some possible values.
247 /* Note that evolution does not offer these for IMAP: */
248 #define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
249 #define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
251 /* Caeml's IMAP uses NULL instead for "Password".
252 * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
253 #define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN"
254 #define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
258 * update_tny_account:
259 * @account_mgr: a valid account mgr instance
260 * @account_data: the server account for which to create a corresponding tny account
262 * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
263 * NOTE: this function does not set the camel session or the get/forget password functions
265 * Returns: a new TnyAccount or NULL in case of error.
268 update_tny_account (TnyAccount *tny_account,
269 ModestServerAccountSettings *server_settings)
271 const gchar *account_name;
273 g_return_val_if_fail (server_settings, FALSE);
274 account_name = modest_server_account_settings_get_account_name (server_settings);
275 g_return_val_if_fail (account_name, FALSE);
276 g_return_val_if_fail (tny_account, FALSE);
278 /* Do not change the id if it's not needed */
279 if (tny_account_get_id (tny_account) &&
280 strcmp (tny_account_get_id (tny_account), account_name))
281 tny_account_set_id (tny_account, account_name);
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 uri = modest_server_account_settings_get_uri (server_settings);
288 tny_account_set_url_string (TNY_ACCOUNT(tny_account), uri);
290 /* Set camel-specific options: */
291 /* Enable secure connection settings: */
292 TnyPair *option_security = NULL;
293 const gchar* auth_mech_name = NULL;
294 ModestTransportStoreProtocol protocol;
295 ModestConnectionProtocol security;
296 ModestAuthProtocol auth_protocol;
297 const gchar *username;
298 const gchar *hostname;
301 /* First of all delete old options */
302 tny_camel_account_clear_options (TNY_CAMEL_ACCOUNT (tny_account));
304 protocol = modest_server_account_settings_get_protocol (server_settings);
305 security = modest_server_account_settings_get_security (server_settings);
306 auth_protocol = modest_server_account_settings_get_auth_protocol (server_settings);
309 case MODEST_PROTOCOL_CONNECTION_NORMAL:
310 option_security = tny_pair_new (MODEST_ACCOUNT_OPTION_SSL,MODEST_ACCOUNT_OPTION_SSL_NEVER);
312 case MODEST_PROTOCOL_CONNECTION_SSL:
313 /* Apparently, use of "IMAPS" (specified in our UI spec), implies
314 * use of the "wrapped" option: */
315 option_security = tny_pair_new (MODEST_ACCOUNT_OPTION_SSL,MODEST_ACCOUNT_OPTION_SSL_WRAPPED);
317 case MODEST_PROTOCOL_CONNECTION_TLS:
318 option_security = tny_pair_new (MODEST_ACCOUNT_OPTION_SSL,MODEST_ACCOUNT_OPTION_SSL_TLS);
320 case MODEST_PROTOCOL_CONNECTION_TLS_OP:
321 /* This is not actually in our UI: */
322 option_security = tny_pair_new (MODEST_ACCOUNT_OPTION_SSL,MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE);
329 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account), option_security);
331 /* Secure authentication: */
332 switch (auth_protocol) {
333 case MODEST_PROTOCOL_AUTH_NONE:
334 /* IMAP and POP need at least a password,
335 * which camel uses if we specify NULL.
336 * This setting should never happen anyway. */
337 if (protocol == MODEST_PROTOCOL_STORE_IMAP ||
338 protocol == MODEST_PROTOCOL_STORE_POP)
339 auth_mech_name = NULL;
341 auth_mech_name = MODEST_ACCOUNT_AUTH_PLAIN;
344 case MODEST_PROTOCOL_AUTH_PASSWORD:
345 /* Camel use a password for IMAP or POP if we specify NULL,
346 * For IMAP, at least it will report an error if we use "Password", "Login" or "Plain".
347 * (POP is know to report an error for Login too. Probably Password and Plain too.) */
348 if (protocol == MODEST_PROTOCOL_STORE_IMAP)
349 auth_mech_name = NULL;
350 else if (protocol == MODEST_PROTOCOL_STORE_POP)
351 auth_mech_name = NULL;
353 auth_mech_name = MODEST_ACCOUNT_AUTH_PASSWORD;
356 case MODEST_PROTOCOL_AUTH_CRAMMD5:
357 auth_mech_name = MODEST_ACCOUNT_AUTH_CRAMMD5;
361 g_warning ("%s: Unhandled secure authentication setting %d for "
362 "account_name=%s (%s)", __FUNCTION__, auth_protocol,
363 account_name, modest_server_account_settings_get_hostname (server_settings));
368 tny_account_set_secure_auth_mech (tny_account, auth_mech_name);
370 if (modest_protocol_info_protocol_is_store(protocol) &&
371 (protocol == MODEST_PROTOCOL_STORE_IMAP) ) {
372 /* Other connection options, needed for IMAP. */
373 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
374 tny_pair_new (MODEST_ACCOUNT_OPTION_USE_LSUB, ""));
375 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
376 tny_pair_new (MODEST_ACCOUNT_OPTION_CHECK_ALL, ""));
379 username = modest_server_account_settings_get_username (server_settings);
380 if (username && strlen (username) > 0)
381 tny_account_set_user (tny_account, username);
382 hostname = modest_server_account_settings_get_hostname (server_settings);
384 tny_account_set_hostname (tny_account, hostname);
387 port = modest_server_account_settings_get_port (server_settings);
389 tny_account_set_port (tny_account, port);
393 gchar *url = tny_account_get_url_string (TNY_ACCOUNT(tny_account));
394 g_debug ("%s:\n account-url: %s\n", __FUNCTION__, url);
402 modest_tny_account_new_from_server_account_name (ModestAccountMgr *account_mgr,
403 TnySessionCamel *session,
404 const gchar *server_account_name,
405 TnyGetPassFunc get_pass_func,
406 TnyForgetPassFunc forget_pass_func)
408 ModestServerAccountSettings *server_settings;
409 TnyAccount *tny_account;
411 g_return_val_if_fail (session, NULL);
412 g_return_val_if_fail (server_account_name, NULL);
415 server_settings = modest_account_mgr_load_server_settings (account_mgr, server_account_name);
416 if (!server_settings)
419 tny_account = TNY_ACCOUNT (tny_camel_transport_account_new ());
422 const gchar* proto_name = NULL;
423 tny_account_set_id (tny_account, server_account_name);
424 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (tny_account), session);
425 proto_name = modest_protocol_info_get_transport_store_protocol_name
426 (modest_server_account_settings_get_protocol (server_settings));
427 tny_account_set_proto (tny_account, proto_name);
428 modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account, server_account_name);
432 g_warning ("%s: failed to create tny_account", __FUNCTION__);
433 else if (!update_tny_account (tny_account, server_settings))
434 g_warning ("%s: failed to initialize tny_account", __FUNCTION__);
436 tny_account_set_forget_pass_func (tny_account,
437 forget_pass_func ? forget_pass_func : forget_pass_dummy);
438 tny_account_set_pass_func (tny_account,
439 get_pass_func ? get_pass_func: get_pass_dummy);
442 tny_account_set_connection_policy (tny_account, modest_default_connection_policy_new ());
444 g_object_unref (server_settings);
452 /* we need these dummy functions, or tinymail will complain */
454 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
459 forget_pass_dummy (TnyAccount *account)
461 /* intentionally left blank */
466 set_online_callback (TnyCamelAccount *account, gboolean canceled, GError *err, gpointer user_data)
468 TnyAccountStore *account_store;
470 account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
472 if (err && !canceled) {
473 /* It seems err is forgotten here ... if the disk is full ! */
475 tny_account_store_alert (
477 TNY_ACCOUNT (account), TNY_ALERT_TYPE_ERROR, FALSE,
480 g_warning ("err: %s", err->message);
485 modest_tny_account_update_from_account (TnyAccount *tny_account,
486 TnyGetPassFunc get_pass_func,
487 TnyForgetPassFunc forget_pass_func)
489 ModestAccountSettings *settings = NULL;
490 ModestServerAccountSettings *server_settings = NULL;
491 ModestAccountMgr *account_mgr;
492 const gchar *account_name;
494 const gchar *display_name;
495 TnyConnectionStatus conn_status;
497 g_return_val_if_fail (tny_account, FALSE);
499 account_mgr = modest_runtime_get_account_mgr ();
500 account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (tny_account);
501 type = tny_account_get_account_type (tny_account);
502 settings = modest_account_mgr_load_account_settings (account_mgr, account_name);
504 g_printerr ("modest: %s: cannot get account data for account %s\n",
505 __FUNCTION__, account_name);
509 display_name = modest_account_settings_get_display_name (settings);
511 if (type == TNY_ACCOUNT_TYPE_STORE)
512 server_settings = modest_account_settings_get_store_settings (settings);
513 else if (type == TNY_ACCOUNT_TYPE_TRANSPORT)
514 server_settings = modest_account_settings_get_transport_settings (settings);
516 if (modest_server_account_settings_get_account_name (server_settings) == NULL) {
517 g_printerr ("modest: no %s account defined for '%s'\n",
518 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
520 g_object_unref (server_settings);
521 g_object_unref (settings);
525 update_tny_account (tny_account, server_settings);
527 /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
528 * account, we set its name to the account of which it is part. */
531 tny_account_set_name (tny_account, display_name);
533 g_object_unref (server_settings);
534 g_object_unref (settings);
536 tny_account_set_connection_policy (tny_account, modest_default_connection_policy_new ());
538 /* The callback will have an error for you if the reconnect
539 * failed. Please handle it (this is TODO). */
541 conn_status = tny_account_get_connection_status (tny_account);
542 if (conn_status != TNY_CONNECTION_STATUS_DISCONNECTED) {
543 TnyAccountStore *account_store = NULL;
545 account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(tny_account),
549 modest_tny_account_store_forget_already_asked (MODEST_TNY_ACCOUNT_STORE (account_store),
553 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(tny_account), TRUE,
554 set_online_callback, "online");
563 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr,
564 const gchar *account_name,
566 TnySessionCamel *session,
567 TnyGetPassFunc get_pass_func,
568 TnyForgetPassFunc forget_pass_func)
570 TnyAccount *tny_account = NULL;
571 ModestAccountSettings *settings = NULL;
572 ModestServerAccountSettings *server_settings = NULL;
573 const gchar *display_name;
575 g_return_val_if_fail (account_mgr, NULL);
576 g_return_val_if_fail (account_name, NULL);
577 g_return_val_if_fail (session, NULL);
578 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
581 settings = modest_account_mgr_load_account_settings (account_mgr, account_name);
583 g_printerr ("modest: %s: cannot get account data for account %s\n",
584 __FUNCTION__, account_name);
587 display_name = modest_account_settings_get_display_name (settings);
589 if (type == TNY_ACCOUNT_TYPE_STORE)
590 server_settings = modest_account_settings_get_store_settings (settings);
591 else if (type == TNY_ACCOUNT_TYPE_TRANSPORT)
592 server_settings = modest_account_settings_get_transport_settings (settings);
594 if (modest_server_account_settings_get_account_name (server_settings) == NULL) {
595 g_printerr ("modest: no %s account defined for '%s'\n",
596 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
598 g_object_unref (server_settings);
599 g_object_unref (settings);
603 tny_account = create_tny_account (session, server_settings);
605 g_printerr ("modest: failed to create tny account for %s (%s)\n",
607 modest_server_account_settings_get_account_name (server_settings));
608 g_object_unref (server_settings);
609 g_object_unref (settings);
612 update_tny_account (tny_account, server_settings);
614 /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
615 * account, we set its name to the account of which it is part. */
618 tny_account_set_name (tny_account, display_name);
620 tny_account_set_forget_pass_func (tny_account,
621 forget_pass_func ? forget_pass_func : forget_pass_dummy);
622 tny_account_set_pass_func (tny_account,
623 get_pass_func ? get_pass_func: get_pass_dummy);
625 tny_account_set_connection_policy (tny_account, modest_default_connection_policy_new ());
627 modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account,
629 g_object_unref (server_settings);
630 g_object_unref (settings);
637 TnyStoreAccount *account;
639 ModestTnyAccountGetMmcAccountNameCallback callback;
641 } GetMmcAccountNameData;
645 #ifdef MODEST_PLATFORM_MAEMO
646 /* Gets the memory card name: */
648 on_modest_file_system_info (HildonFileSystemInfoHandle *handle,
649 HildonFileSystemInfo *info,
650 const GError *error, gpointer data)
652 GetMmcAccountNameData *callback_data = (GetMmcAccountNameData*)data;
655 g_warning ("%s: error=%s", __FUNCTION__, error->message);
658 TnyAccount *account = TNY_ACCOUNT (callback_data->account);
660 const gchar *previous_display_name = NULL;
662 const gchar *display_name = NULL;
663 if (!error && info) {
664 display_name = hildon_file_system_info_get_display_name(info);
665 previous_display_name = tny_account_get_name (account);
668 /* printf ("DEBUG: %s: display name=%s\n", __FUNCTION__, display_name); */
669 if (display_name && previous_display_name &&
670 (strcmp (display_name, previous_display_name) != 0)) {
671 tny_account_set_name (account, display_name);
674 /* Inform the application that the name is now ready: */
675 if (callback_data->callback)
676 (*(callback_data->callback)) (callback_data->account,
677 callback_data->user_data);
679 g_object_unref (callback_data->account);
680 g_slice_free (GetMmcAccountNameData, callback_data);
684 void modest_tny_account_get_mmc_account_name (TnyStoreAccount* self, ModestTnyAccountGetMmcAccountNameCallback callback, gpointer user_data)
686 #ifdef MODEST_PLATFORM_MAEMO
687 /* Just use the hard-coded path for the single memory card,
688 * rather than try to figure out the path to the specific card by
689 * looking at the maildir URI:
691 const gchar *uri_real = MODEST_MCC1_VOLUMEPATH_URI;
694 gchar* uri = tny_account_get_url_string (TNY_ACCOUNT (self));
698 TODO: This gets the name of the folder, but we want the name of the volume.
699 gchar *uri_real = NULL;
700 const gchar* prefix = "maildir://localhost/";
701 if ((strstr (uri, prefix) == uri) && (strlen(uri) > strlen(prefix)) )
702 uri_real = g_strconcat ("file:///", uri + strlen (prefix), NULL);
706 //This is freed in the callback:
707 GetMmcAccountNameData * callback_data = g_slice_new0(GetMmcAccountNameData);
708 callback_data->account = self;
709 g_object_ref (callback_data->account); /* Unrefed when we destroy the struct. */
710 callback_data->callback = callback;
711 callback_data->user_data = user_data;
713 /* TODO: gnome_vfs_volume_get_display_name() does not return
714 * the same string. But why not? Why does hildon needs its own
717 /* printf ("DEBUG: %s Calling hildon_file_system_info_async_new() with URI=%s\n", __FUNCTION__, uri_real); */
718 hildon_file_system_info_async_new(uri_real,
719 on_modest_file_system_info, callback_data /* user_data */);
721 /* g_free (uri_real); */
731 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session,
732 const gchar* location_filepath)
736 /* Make sure that the directories exist: */
737 modest_init_local_folders (location_filepath);
739 TnyStoreAccount *tny_account;
741 gchar *maildir, *url_string;
743 g_return_val_if_fail (account_mgr, NULL);
744 g_return_val_if_fail (session, NULL);
747 if (!location_filepath) {
748 /* A NULL filepath means that this is the special local-folders maildir
750 tny_account = TNY_STORE_ACCOUNT (modest_tny_local_folders_account_new ());
753 /* Else, for instance, a per-account outbox maildir account: */
754 tny_account = TNY_STORE_ACCOUNT (tny_camel_store_account_new ());
758 g_printerr ("modest: %s: cannot create account for local folders. filepath=%s",
759 __FUNCTION__, location_filepath);
762 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
764 /* This path contains directories for each local folder.
765 * We have created them so that TnyCamelStoreAccount can find them
766 * and report a folder for each directory: */
767 maildir = modest_local_folder_info_get_maildir_path (location_filepath);
768 url = camel_url_new ("maildir:", NULL);
769 camel_url_set_path (url, maildir);
770 /* Needed by tinymail's DBC assertions */
771 camel_url_set_host (url, "localhost");
772 url_string = camel_url_to_string (url, 0);
774 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
775 /* printf("DEBUG: %s:\n url=%s\n", __FUNCTION__, url_string); */
777 /* TODO: Use a more generic way of identifying memory card paths,
778 * and of marking accounts as memory card accounts, maybe
779 * via a derived TnyCamelStoreAccount ? */
780 const gboolean is_mmc =
782 (strcmp (location_filepath, MODEST_MCC1_VOLUMEPATH) == 0);
784 /* The name of memory card locations will be updated asynchronously.
785 * This is just a default: */
786 const gchar *name = is_mmc ? _("Memory Card") :
787 MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME;
788 tny_account_set_name (TNY_ACCOUNT(tny_account), name);
790 /* Get the correct display name for memory cards, asynchronously: */
791 if (location_filepath) {
792 GError *error = NULL;
793 gchar *uri = g_filename_to_uri(location_filepath, NULL, &error);
795 g_warning ("%s: g_filename_to_uri(%s) failed: %s", __FUNCTION__,
796 location_filepath, error->message);
797 g_error_free (error);
800 /* Get the account name asynchronously:
801 * This might not happen soon enough, so some UI code might
802 * need to call this again, specifying a callback.
804 modest_tny_account_get_mmc_account_name (tny_account, NULL, NULL);
812 const gchar* id = is_mmc ? MODEST_MMC_ACCOUNT_ID :
813 MODEST_LOCAL_FOLDERS_ACCOUNT_ID;
814 tny_account_set_id (TNY_ACCOUNT(tny_account), id);
816 tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
817 tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
819 tny_account_set_connection_policy (TNY_ACCOUNT (tny_account), modest_default_connection_policy_new ());
821 modest_tny_account_set_parent_modest_account_name_for_server_account (
822 TNY_ACCOUNT (tny_account), id);
824 camel_url_free (url);
828 return TNY_ACCOUNT(tny_account);
833 modest_tny_account_new_for_per_account_local_outbox_folder (ModestAccountMgr *account_mgr,
834 const gchar* account_name,
835 TnySessionCamel *session)
837 g_return_val_if_fail (account_mgr, NULL);
838 g_return_val_if_fail (account_name, NULL);
839 g_return_val_if_fail (session, NULL);
841 /* Notice that we create a ModestTnyOutboxAccount here,
842 * instead of just a TnyCamelStoreAccount,
843 * so that we can later identify this as a special account for internal use only.
845 TnyStoreAccount *tny_account = TNY_STORE_ACCOUNT (modest_tny_outbox_account_new ());
847 g_printerr ("modest: cannot create account for per-account local outbox folder.");
851 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
853 /* Make sure that the paths exists on-disk so that TnyCamelStoreAccount can
854 * find it to create a TnyFolder for it: */
855 gchar *folder_dir = modest_per_account_local_outbox_folder_info_get_maildir_path_to_outbox_folder (account_name);
856 modest_init_one_local_folder(folder_dir);
860 /* This path should contain just one directory - "outbox": */
862 modest_per_account_local_outbox_folder_info_get_maildir_path (account_name);
864 CamelURL *url = camel_url_new ("maildir:", NULL);
865 camel_url_set_path (url, maildir);
868 /* Needed by tinymail's DBC assertions */
869 camel_url_set_host (url, "localhost");
870 gchar *url_string = camel_url_to_string (url, 0);
871 camel_url_free (url);
873 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
874 /* printf("DEBUG: %s:\n url=%s\n", __FUNCTION__, url_string); */
877 /* This text should never been seen,
878 * because the per-account outbox accounts are not seen directly by the user.
879 * Their folders are merged and shown as one folder. */
880 tny_account_set_name (TNY_ACCOUNT(tny_account), "Per-Account Outbox");
882 gchar *account_id = g_strdup_printf (
883 MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s",
885 tny_account_set_id (TNY_ACCOUNT(tny_account), account_id);
888 tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
889 tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
891 tny_account_set_connection_policy (TNY_ACCOUNT (tny_account), modest_default_connection_policy_new ());
893 /* Make this think that it belongs to the modest local-folders parent account: */
894 modest_tny_account_set_parent_modest_account_name_for_server_account (
895 TNY_ACCOUNT (tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
897 return TNY_ACCOUNT(tny_account);
902 typedef struct _RecurseFoldersAsyncHelper {
903 ModestFolderStats stats;
905 GetFolderStatsCallback callback;
906 GetFolderStatsCallback status_callback;
908 } RecurseFoldersAsyncHelper;
911 recurse_folders_async_cb (TnyFolderStore *folder_store,
917 RecurseFoldersAsyncHelper *helper;
920 helper = (RecurseFoldersAsyncHelper *) user_data;
922 /* A goto just to avoid an indentation level */
926 /* Retrieve children */
927 iter = tny_list_create_iterator (list);
928 while (!tny_iterator_is_done (iter)) {
929 TnyList *folders = NULL;
930 TnyFolderStore *folder = NULL;
932 folders = tny_simple_list_new ();
933 folder = (TnyFolderStore*) tny_iterator_get_current (iter);
935 /* Add pending call */
936 helper->pending_calls++;
937 helper->stats.folders++;
938 if (TNY_IS_FOLDER (folder)) {
939 helper->stats.msg_count += tny_folder_get_all_count (TNY_FOLDER (folder));
940 helper->stats.local_size += tny_folder_get_local_size (TNY_FOLDER (folder));
944 if (helper->status_callback)
945 helper->status_callback (helper->stats, helper->user_data);
947 /* Avoid the outbox */
948 if (!TNY_IS_MERGE_FOLDER (folder) &&
949 (TNY_IS_FOLDER (folder) &&
950 tny_folder_get_folder_type (TNY_FOLDER (folder)) != TNY_FOLDER_TYPE_OUTBOX))
951 tny_folder_store_get_folders_async (folder, folders, NULL,
952 recurse_folders_async_cb,
954 g_object_unref (folders);
955 g_object_unref (G_OBJECT (folder));
957 tny_iterator_next (iter);
959 g_object_unref (G_OBJECT (iter));
962 /* Remove my own pending call */
963 helper->pending_calls--;
965 /* This means that we have all the folders */
966 if (helper->pending_calls == 0) {
968 if (helper->callback)
969 helper->callback (helper->stats, helper->user_data);
972 g_slice_free (RecurseFoldersAsyncHelper, helper);
977 modest_tny_folder_store_get_folder_stats (TnyFolderStore *self,
978 GetFolderStatsCallback callback,
979 GetFolderStatsCallback status_callback,
982 RecurseFoldersAsyncHelper *helper;
985 g_return_if_fail (TNY_IS_FOLDER_STORE (self));
988 helper = g_slice_new0 (RecurseFoldersAsyncHelper);
989 helper->pending_calls = 1;
990 helper->callback = callback;
991 helper->status_callback = status_callback;
992 helper->user_data = user_data;
994 if (TNY_IS_FOLDER (self)) {
995 helper->stats.msg_count = tny_folder_get_all_count (TNY_FOLDER (self));
996 helper->stats.local_size = tny_folder_get_local_size (TNY_FOLDER (self));
999 folders = tny_simple_list_new ();
1000 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (self),
1002 recurse_folders_async_cb,
1004 g_object_unref (folders);
1008 modest_tny_account_get_parent_modest_account_name_for_server_account (TnyAccount *self)
1010 return (const gchar *)g_object_get_data (G_OBJECT (self), "modest_account");
1014 modest_tny_account_set_parent_modest_account_name_for_server_account (TnyAccount *self,
1015 const gchar* parent_modest_account_name)
1017 g_object_set_data_full (G_OBJECT(self), "modest_account",
1018 (gpointer) g_strdup (parent_modest_account_name), g_free);
1022 modest_tny_account_is_virtual_local_folders (TnyAccount *self)
1024 /* We should make this more sophisticated if we ever use ModestTnyLocalFoldersAccount
1025 * for anything else. */
1026 return MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self);
1031 modest_tny_account_is_memory_card_account (TnyAccount *self)
1033 const gchar* account_id = NULL;
1035 g_return_val_if_fail (TNY_ACCOUNT (self), FALSE);
1040 account_id = tny_account_get_id (self);
1045 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
1049 modest_tny_folder_store_is_remote (TnyFolderStore *folder_store)
1051 TnyAccount *account = NULL;
1052 gboolean result = TRUE;
1054 g_return_val_if_fail(TNY_IS_FOLDER_STORE(folder_store), FALSE);
1056 if (TNY_IS_FOLDER (folder_store)) {
1057 /* Get the folder's parent account: */
1058 account = tny_folder_get_account(TNY_FOLDER(folder_store));
1059 } else if (TNY_IS_ACCOUNT (folder_store)) {
1060 account = TNY_ACCOUNT(folder_store);
1061 g_object_ref(account);
1064 if (account != NULL) {
1065 if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
1066 if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
1067 !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
1068 /* This must be a maildir account, which does
1069 * not require a connection: */
1073 g_object_unref (account);