+/* TODO: Notify the treemodel somehow that the display name
+ * is now available. We should probably request this from the cell_data_func
+ * so we can provide a treerowreference. */
+static void
+on_modest_file_system_info(HildonFileSystemInfoHandle *handle,
+ HildonFileSystemInfo *info,
+ const GError *error, gpointer data)
+{
+ TnyAccount *account = TNY_ACCOUNT (data);
+
+ if (error) {
+/* printf (" DEBUG: %s: error=%s\n", __FUNCTION__, error->message); */
+ }
+
+ const gchar *display_name = NULL;
+ if (!error && info) {
+ display_name = hildon_file_system_info_get_display_name(info);
+ }
+
+ if (display_name) {
+ /* printf ("DEBUG: %s: display name=%s\n", __FUNCTION__, display_name); */
+ tny_account_set_name (account, display_name);
+ }
+}
+
+
+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);
+
+ 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) {
+ /* 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?
+ */
+ hildon_file_system_info_async_new(uri,
+ on_modest_file_system_info, tny_account /* user_data */);
+
+ 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);
+
+ /* 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);
+
+typedef struct _RecurseFoldersHelper {
+ TnyStatsFunc function;
+ 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)) {
+ TnyFolderStats *stats;
+ TnyFolder *folder;
+
+ folder = TNY_FOLDER (tny_iterator_get_current (iter));
+ stats = tny_folder_get_stats (folder);
+
+ /* initially, we sometimes get -1 from tinymail; ignore that */
+ if (helper->function && helper->function (stats) > 0)
+ helper->sum += helper->function (stats);
+
+ if (TNY_IS_FOLDER_STORE (folder)) {
+ recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
+ }
+
+ g_object_unref (folder);
+ g_object_unref (stats);
+ 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->function = NULL;
+ 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->function = (TnyStatsFunc) tny_folder_stats_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->function = (TnyStatsFunc) tny_folder_stats_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);
+}
+
+