+void modest_tny_account_get_mmc_account_name (TnyStoreAccount* self, ModestTnyAccountGetMmcAccountNameCallback callback, gpointer user_data)
+{
+#ifdef MODEST_PLATFORM_MAEMO
+ /* Just use the hard-coded path for the single memory card,
+ * rather than try to figure out the path to the specific card by
+ * looking at the maildir URI:
+ */
+ const gchar *uri_real = MODEST_MCC1_VOLUMEPATH_URI;
+
+ /*
+ gchar* uri = tny_account_get_url_string (TNY_ACCOUNT (self));
+ if (!uri)
+ return;
+
+ TODO: This gets the name of the folder, but we want the name of the volume.
+ gchar *uri_real = NULL;
+ const gchar* prefix = "maildir://localhost/";
+ if ((strstr (uri, prefix) == uri) && (strlen(uri) > strlen(prefix)) )
+ uri_real = g_strconcat ("file:///", uri + strlen (prefix), NULL);
+ */
+
+ if (uri_real) {
+ //This is freed in the callback:
+ GetMmcAccountNameData * callback_data = g_slice_new0(GetMmcAccountNameData);
+ callback_data->account = self;
+ g_object_ref (callback_data->account); /* Unrefed when we destroy the struct. */
+ callback_data->callback = callback;
+ callback_data->user_data = user_data;
+
+ /* TODO: gnome_vfs_volume_get_display_name() does not return
+ * the same string. But why not? Why does hildon needs its own
+ * function for this?
+ */
+ /* printf ("DEBUG: %s Calling hildon_file_system_info_async_new() with URI=%s\n", __FUNCTION__, uri_real); */
+ hildon_file_system_info_async_new(uri_real,
+ on_modest_file_system_info, callback_data /* user_data */);
+
+ /* g_free (uri_real); */
+ }
+
+ /* g_free (uri); */
+#endif
+}
+
+
+
+TnyAccount*
+modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session,
+ const gchar* location_filepath)
+{
+
+
+ /* Make sure that the directories exist: */
+ modest_init_local_folders (location_filepath);
+
+ TnyStoreAccount *tny_account;
+ CamelURL *url;
+ gchar *maildir, *url_string;
+
+ g_return_val_if_fail (account_mgr, NULL);
+ g_return_val_if_fail (session, NULL);
+
+
+ if (!location_filepath) {
+ /* A NULL filepath means that this is the special local-folders maildir
+ * account: */
+ tny_account = TNY_STORE_ACCOUNT (modest_tny_local_folders_account_new ());
+ }
+ else {
+ /* Else, for instance, a per-account outbox maildir account: */
+ tny_account = TNY_STORE_ACCOUNT (tny_camel_store_account_new ());
+ }
+
+ if (!tny_account) {
+ g_printerr ("modest: %s: cannot create account for local folders. filepath=%s",
+ __FUNCTION__, location_filepath);
+ return NULL;
+ }
+ tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
+
+ /* This path contains directories for each local folder.
+ * We have created them so that TnyCamelStoreAccount can find them
+ * and report a folder for each directory: */
+ maildir = modest_local_folder_info_get_maildir_path (location_filepath);
+ url = camel_url_new ("maildir:", NULL);
+ camel_url_set_path (url, maildir);
+ /* Needed by tinymail's DBC assertions */
+ camel_url_set_host (url, "localhost");
+ url_string = camel_url_to_string (url, 0);
+
+ tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
+/* printf("DEBUG: %s:\n url=%s\n", __FUNCTION__, url_string); */
+
+ /* TODO: Use a more generic way of identifying memory card paths,
+ * and of marking accounts as memory card accounts, maybe
+ * via a derived TnyCamelStoreAccount ? */
+ const gboolean is_mmc =
+ location_filepath &&
+ (strcmp (location_filepath, MODEST_MCC1_VOLUMEPATH) == 0);
+
+ /* The name of memory card locations will be updated asynchronously.
+ * This is just a default: */
+ const gchar *name = is_mmc ? _("Memory Card") :
+ MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME;
+ tny_account_set_name (TNY_ACCOUNT(tny_account), name);
+
+ /* Get the correct display name for memory cards, asynchronously: */
+ if (location_filepath) {
+ GError *error = NULL;
+ gchar *uri = g_filename_to_uri(location_filepath, NULL, &error);
+ if (error) {
+ g_warning ("%s: g_filename_to_uri(%s) failed: %s", __FUNCTION__,
+ location_filepath, error->message);
+ g_error_free (error);
+ error = NULL;
+ } else if (uri) {
+ /* Get the account name asynchronously:
+ * This might not happen soon enough, so some UI code might
+ * need to call this again, specifying a callback.
+ */
+ modest_tny_account_get_mmc_account_name (tny_account, NULL, NULL);
+
+ g_free (uri);
+ uri = NULL;
+ }
+ }
+
+
+ const gchar* id = is_mmc ? MODEST_MMC_ACCOUNT_ID :
+ MODEST_LOCAL_FOLDERS_ACCOUNT_ID;
+ tny_account_set_id (TNY_ACCOUNT(tny_account), id);
+
+ tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
+ tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
+
+ tny_account_set_connection_policy (TNY_ACCOUNT (tny_account), modest_default_connection_policy_new ());
+
+ modest_tny_account_set_parent_modest_account_name_for_server_account (
+ TNY_ACCOUNT (tny_account), id);
+
+ camel_url_free (url);
+ g_free (maildir);
+ g_free (url_string);
+
+ return TNY_ACCOUNT(tny_account);
+}
+
+
+TnyAccount*
+modest_tny_account_new_for_per_account_local_outbox_folder (ModestAccountMgr *account_mgr,
+ const gchar* account_name,
+ TnySessionCamel *session)
+{
+ g_return_val_if_fail (account_mgr, NULL);
+ g_return_val_if_fail (account_name, NULL);
+ g_return_val_if_fail (session, NULL);
+
+ /* Notice that we create a ModestTnyOutboxAccount here,
+ * instead of just a TnyCamelStoreAccount,
+ * so that we can later identify this as a special account for internal use only.
+ */
+ TnyStoreAccount *tny_account = TNY_STORE_ACCOUNT (modest_tny_outbox_account_new ());
+ if (!tny_account) {
+ g_printerr ("modest: cannot create account for per-account local outbox folder.");
+ return NULL;
+ }
+
+ tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
+
+ /* Make sure that the paths exists on-disk so that TnyCamelStoreAccount can
+ * find it to create a TnyFolder for it: */
+ gchar *folder_dir = modest_per_account_local_outbox_folder_info_get_maildir_path_to_outbox_folder (account_name);
+ modest_init_one_local_folder(folder_dir);
+ g_free (folder_dir);
+ folder_dir = NULL;
+
+ /* This path should contain just one directory - "outbox": */
+ gchar *maildir =
+ modest_per_account_local_outbox_folder_info_get_maildir_path (account_name);
+
+ CamelURL *url = camel_url_new ("maildir:", NULL);
+ camel_url_set_path (url, maildir);
+ g_free (maildir);
+
+ /* Needed by tinymail's DBC assertions */
+ camel_url_set_host (url, "localhost");
+ gchar *url_string = camel_url_to_string (url, 0);
+ camel_url_free (url);
+
+ tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
+/* printf("DEBUG: %s:\n url=%s\n", __FUNCTION__, url_string); */
+ g_free (url_string);
+
+ /* This text should never been seen,
+ * because the per-account outbox accounts are not seen directly by the user.
+ * Their folders are merged and shown as one folder. */
+ tny_account_set_name (TNY_ACCOUNT(tny_account), "Per-Account Outbox");
+
+ gchar *account_id = g_strdup_printf (
+ MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s",
+ account_name);
+ tny_account_set_id (TNY_ACCOUNT(tny_account), account_id);
+ g_free (account_id);
+
+ tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
+ tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
+
+ tny_account_set_connection_policy (TNY_ACCOUNT (tny_account), modest_default_connection_policy_new ());
+
+ /* Make this think that it belongs to the modest local-folders parent account: */
+ modest_tny_account_set_parent_modest_account_name_for_server_account (
+ TNY_ACCOUNT (tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
+
+ return TNY_ACCOUNT(tny_account);
+}
+
+
+
+typedef struct _RecurseFoldersAsyncHelper {
+ ModestFolderStats stats;
+ guint pending_calls;
+ GetFolderStatsCallback callback;
+ GetFolderStatsCallback status_callback;
+ gpointer user_data;
+} RecurseFoldersAsyncHelper;
+
+static void
+recurse_folders_async_cb (TnyFolderStore *folder_store,
+ gboolean canceled,
+ TnyList *list,
+ GError *err,
+ gpointer user_data)
+{
+ RecurseFoldersAsyncHelper *helper;
+ TnyIterator *iter;
+
+ helper = (RecurseFoldersAsyncHelper *) user_data;
+
+ /* A goto just to avoid an indentation level */
+ if (err || canceled)
+ goto next_folder;
+
+ /* Retrieve children */
+ iter = tny_list_create_iterator (list);
+ while (!tny_iterator_is_done (iter)) {
+ TnyList *folders = NULL;
+ TnyFolderStore *folder = NULL;
+
+ folders = tny_simple_list_new ();
+ folder = (TnyFolderStore*) tny_iterator_get_current (iter);
+
+ /* Add pending call */
+ helper->pending_calls++;
+ helper->stats.folders++;
+ if (TNY_IS_FOLDER (folder)) {
+ helper->stats.msg_count += tny_folder_get_all_count (TNY_FOLDER (folder));
+ helper->stats.local_size += tny_folder_get_local_size (TNY_FOLDER (folder));
+ }
+
+ /* notify */
+ if (helper->status_callback)
+ helper->status_callback (helper->stats, helper->user_data);
+
+ /* Avoid the outbox */
+ if (!TNY_IS_MERGE_FOLDER (folder) &&
+ (TNY_IS_FOLDER (folder) &&
+ tny_folder_get_folder_type (TNY_FOLDER (folder)) != TNY_FOLDER_TYPE_OUTBOX))
+ tny_folder_store_get_folders_async (folder, folders, NULL,
+ recurse_folders_async_cb,
+ NULL, helper);
+ g_object_unref (folders);
+ g_object_unref (G_OBJECT (folder));
+
+ tny_iterator_next (iter);
+ }
+ g_object_unref (G_OBJECT (iter));
+
+next_folder:
+ /* Remove my own pending call */
+ helper->pending_calls--;
+
+ /* This means that we have all the folders */
+ if (helper->pending_calls == 0) {
+ /* notify */
+ if (helper->callback)
+ helper->callback (helper->stats, helper->user_data);
+
+ /* Free resources */
+ g_slice_free (RecurseFoldersAsyncHelper, helper);
+ }
+}
+
+void
+modest_tny_folder_store_get_folder_stats (TnyFolderStore *self,
+ GetFolderStatsCallback callback,
+ GetFolderStatsCallback status_callback,
+ gpointer user_data)
+{
+ RecurseFoldersAsyncHelper *helper;
+ TnyList *folders;
+
+ g_return_if_fail (TNY_IS_FOLDER_STORE (self));
+
+ /* Create helper */
+ helper = g_slice_new0 (RecurseFoldersAsyncHelper);
+ helper->pending_calls = 1;
+ helper->callback = callback;
+ helper->status_callback = status_callback;
+ helper->user_data = user_data;
+
+ if (TNY_IS_FOLDER (self)) {
+ helper->stats.msg_count = tny_folder_get_all_count (TNY_FOLDER (self));
+ helper->stats.local_size = tny_folder_get_local_size (TNY_FOLDER (self));
+ }
+
+ folders = tny_simple_list_new ();
+ tny_folder_store_get_folders_async (TNY_FOLDER_STORE (self),
+ folders, NULL,
+ recurse_folders_async_cb,
+ NULL, helper);
+ g_object_unref (folders);
+}
+
+const gchar*
+modest_tny_account_get_parent_modest_account_name_for_server_account (TnyAccount *self)
+{
+ return (const gchar *)g_object_get_data (G_OBJECT (self), "modest_account");
+}
+
+void
+modest_tny_account_set_parent_modest_account_name_for_server_account (TnyAccount *self,
+ const gchar* parent_modest_account_name)
+{
+ g_object_set_data_full (G_OBJECT(self), "modest_account",
+ (gpointer) g_strdup (parent_modest_account_name), g_free);
+}
+
+gboolean
+modest_tny_account_is_virtual_local_folders (TnyAccount *self)
+{
+ /* We should make this more sophisticated if we ever use ModestTnyLocalFoldersAccount
+ * for anything else. */
+ return MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self);
+}
+
+
+gboolean
+modest_tny_account_is_memory_card_account (TnyAccount *self)
+{
+ const gchar* account_id = NULL;
+
+ g_return_val_if_fail (TNY_ACCOUNT (self), FALSE);
+
+ if (!self)
+ return FALSE;
+
+ account_id = tny_account_get_id (self);
+
+ if (!account_id)
+ return FALSE;
+ else
+ return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
+}
+
+gboolean
+modest_tny_folder_store_is_remote (TnyFolderStore *folder_store)
+{
+ TnyAccount *account = NULL;
+ gboolean result = TRUE;
+
+ g_return_val_if_fail(TNY_IS_FOLDER_STORE(folder_store), FALSE);
+
+ if (TNY_IS_FOLDER (folder_store)) {
+ /* Get the folder's parent account: */
+ account = tny_folder_get_account(TNY_FOLDER(folder_store));
+ } else if (TNY_IS_ACCOUNT (folder_store)) {
+ account = TNY_ACCOUNT(folder_store);
+ g_object_ref(account);
+ }
+
+ if (account != NULL) {
+ if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
+ if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
+ !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
+ /* This must be a maildir account, which does
+ * not require a connection: */
+ result = FALSE;
+ }
+ }
+ g_object_unref (account);
+ } else {
+ result = FALSE;
+ }
+
+ return result;