+typedef struct
+{
+ TnyStoreAccount *account;
+
+ ModestTnyAccountGetMmcAccountNameCallback callback;
+ gpointer user_data;
+} GetMmcAccountNameData;
+
+
+
+
+/* Gets the memory card name: */
+static void
+on_modest_file_system_info(HildonFileSystemInfoHandle *handle,
+ HildonFileSystemInfo *info,
+ const GError *error, gpointer data)
+{
+ GetMmcAccountNameData *callback_data = (GetMmcAccountNameData*)data;
+
+ if (error) {
+ g_warning ("%s: error=%s", __FUNCTION__, error->message);
+ }
+
+ TnyAccount *account = TNY_ACCOUNT (callback_data->account);
+
+ const gchar *previous_display_name = NULL;
+
+ const gchar *display_name = NULL;
+ if (!error && info) {
+ display_name = hildon_file_system_info_get_display_name(info);
+ previous_display_name = tny_account_get_name (account);
+ }
+
+ /* printf ("DEBUG: %s: display name=%s\n", __FUNCTION__, display_name); */
+ tny_account_set_name (account, display_name);
+
+ /* Inform the application that the name is now ready: */
+ if (callback_data->callback)
+ (*(callback_data->callback)) (callback_data->account,
+ callback_data->user_data);
+
+ g_object_unref (callback_data->account);
+ g_slice_free (GetMmcAccountNameData, callback_data);
+}
+
+void modest_tny_account_get_mmc_account_name (TnyStoreAccount* self, ModestTnyAccountGetMmcAccountNameCallback callback, gpointer user_data)
+{
+ /* 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); */
+}
+
+
+
+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);
+
+ 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);
+
+ /* 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 gint (*TnyStatsFunc) (TnyFolderStats *stats);
+#define TASK_GET_ALL_COUNT 0
+#define TASK_GET_LOCAL_SIZE 1
+#define TASK_GET_FOLDER_COUNT 2
+
+typedef struct _RecurseFoldersHelper {
+ gint task;
+ guint sum;
+ guint folders;
+} RecurseFoldersHelper;
+
+static void
+recurse_folders (TnyFolderStore *store,
+ TnyFolderStoreQuery *query,
+ RecurseFoldersHelper *helper)
+{
+ TnyIterator *iter;
+ TnyList *folders = tny_simple_list_new ();
+
+ tny_folder_store_get_folders (store, folders, query, NULL);
+ iter = tny_list_create_iterator (folders);
+
+ helper->folders += tny_list_get_length (folders);
+
+ while (!tny_iterator_is_done (iter)) {
+ TnyFolder *folder;
+
+ folder = TNY_FOLDER (tny_iterator_get_current (iter));
+ if (folder) {
+ if (helper->task == TASK_GET_ALL_COUNT)
+ helper->sum += tny_folder_get_all_count (folder);
+
+ if (helper->task == TASK_GET_LOCAL_SIZE)
+ helper->sum += tny_folder_get_local_size (folder);
+
+ if (TNY_IS_FOLDER_STORE (folder))
+ recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
+
+ g_object_unref (folder);
+ }
+
+ tny_iterator_next (iter);
+ }
+ g_object_unref (G_OBJECT (iter));
+ g_object_unref (G_OBJECT (folders));
+}
+
+gint
+modest_tny_folder_store_get_folder_count (TnyFolderStore *self)
+{
+ RecurseFoldersHelper *helper;
+ gint retval;
+
+ g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
+
+ /* Create helper */
+ helper = g_malloc0 (sizeof (RecurseFoldersHelper));
+ helper->task = TASK_GET_FOLDER_COUNT;
+ helper->sum = 0;
+ helper->folders = 0;
+
+ recurse_folders (self, NULL, helper);
+
+ retval = helper->folders;
+
+ g_free (helper);
+
+ return retval;
+}
+
+gint
+modest_tny_folder_store_get_message_count (TnyFolderStore *self)
+{
+ RecurseFoldersHelper *helper;
+ gint retval;
+
+ g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
+
+ /* Create helper */
+ helper = g_malloc0 (sizeof (RecurseFoldersHelper));
+ helper->task = TASK_GET_ALL_COUNT;
+ helper->sum = 0;
+
+ recurse_folders (self, NULL, helper);
+
+ retval = helper->sum;
+
+ g_free (helper);
+
+ return retval;
+}
+
+gint
+modest_tny_folder_store_get_local_size (TnyFolderStore *self)
+{
+ RecurseFoldersHelper *helper;
+ gint retval;
+
+ g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
+
+ /* Create helper */
+ helper = g_malloc0 (sizeof (RecurseFoldersHelper));
+ helper->task = TASK_GET_LOCAL_SIZE;
+ helper->sum = 0;
+
+ recurse_folders (self, NULL, helper);
+
+ retval = helper->sum;
+
+ g_free (helper);
+
+ return retval;
+}
+
+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_acount_name)
+{
+ g_object_set_data_full (G_OBJECT(self), "modest_account",
+ (gpointer) g_strdup (parent_modest_acount_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);
+}