+ /* Outbox is a special case, using a derived TnyAccount: */
+ if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
+ //g_warning ("BUG: should not be reached");
+ /* should be handled with the MERGE_FOLDER above*/
+ g_object_unref (G_OBJECT(account));
+ return TRUE;
+ }
+
+ const gchar* account_id = tny_account_get_id (account);
+ if (!account_id) {
+ g_warning ("BUG: account without account id");
+ g_object_unref (G_OBJECT(account));
+ return FALSE;
+ }
+
+ g_object_unref (G_OBJECT(account));
+ return (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
+}
+
+gboolean
+modest_tny_folder_is_memory_card_folder (TnyFolder *folder)
+{
+ g_return_val_if_fail (folder, FALSE);
+
+ /* The merge folder is a special case,
+ * used to merge the per-account local outbox folders.
+ * and can have no get_account() implementation.
+ */
+ if (TNY_IS_MERGE_FOLDER (folder))
+ return FALSE;
+
+ TnyAccount* account = tny_folder_get_account ((TnyFolder*)folder);
+ if (!account)
+ return FALSE;
+
+ const gchar* account_id = tny_account_get_id (account);
+ if (!account_id) {
+ g_object_unref (account);
+ return FALSE;
+ }
+
+ g_object_unref (account);
+
+ return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
+}
+
+gboolean
+modest_tny_folder_is_remote_folder (TnyFolder *folder)
+{
+ gboolean is_local = TRUE;
+
+ g_return_val_if_fail (TNY_IS_FOLDER(folder), FALSE);
+
+ is_local = ((modest_tny_folder_is_local_folder(folder)) ||
+ (modest_tny_folder_is_memory_card_folder(folder)));
+
+
+ return !is_local;
+}
+
+
+TnyFolderType
+modest_tny_folder_get_local_or_mmc_folder_type (TnyFolder *folder)
+{
+ g_return_val_if_fail (folder, TNY_FOLDER_TYPE_INVALID);
+ g_return_val_if_fail (modest_tny_folder_is_local_folder(folder)||
+ modest_tny_folder_is_memory_card_folder(folder),
+ TNY_FOLDER_TYPE_INVALID);
+
+ /* The merge folder is a special case,
+ * used to merge the per-account local outbox folders.
+ * and can have no get_account() implementation.
+ * We should do something more sophisticated if we
+ * ever use TnyMergeFolder for anything else.
+ */
+ if (TNY_IS_MERGE_FOLDER (folder))
+ return TNY_FOLDER_TYPE_OUTBOX;
+
+ /* Outbox is a special case, using a derived TnyAccount: */
+ TnyAccount* parent_account = tny_folder_get_account (folder);
+ if (parent_account && MODEST_IS_TNY_OUTBOX_ACCOUNT (parent_account)) {
+ g_object_unref (parent_account);
+ return TNY_FOLDER_TYPE_OUTBOX;
+ }
+
+ if (parent_account) {
+ g_object_unref (parent_account);
+ parent_account = NULL;
+ }
+
+ /* we need to use the camel functions, because we want the
+ * _full name_, that is, the full path name of the folder,
+ * to distinguish between 'Outbox' and 'myfunkyfolder/Outbox'
+ */
+
+ const gchar *full_name = tny_camel_folder_get_full_name (TNY_CAMEL_FOLDER (folder));
+ /* printf ("DEBUG: %s: full_name=%s\n", __FUNCTION__, full_name); */
+
+ if (!full_name)
+ return TNY_FOLDER_TYPE_UNKNOWN;
+ else
+ return modest_local_folder_info_get_type (full_name);
+}
+
+gboolean
+modest_tny_folder_is_outbox_for_account (TnyFolder *folder, TnyAccount *account)
+{
+ g_return_val_if_fail(folder, FALSE);
+ g_return_val_if_fail(account, FALSE);
+
+ if (modest_tny_folder_get_local_or_mmc_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX)
+ return FALSE;
+
+ return TRUE;
+}
+
+gchar*
+modest_tny_folder_get_header_unique_id (TnyHeader *header)
+{
+ TnyFolder *folder;
+ gchar *url, *retval;
+ gchar *uid;
+
+ g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
+
+ folder = tny_header_get_folder (header);
+ if (!folder)
+ return NULL;
+
+ url = tny_folder_get_url_string (folder);
+ uid = tny_header_dup_uid (header);
+
+ retval = g_strjoin ("/", url, uid, NULL);
+ g_free (uid);
+
+ g_free (url);
+ g_object_unref (folder);
+
+ return retval;
+}
+
+TnyAccount *
+modest_tny_folder_get_account (TnyFolder *folder)
+{
+ TnyAccount *account = NULL;
+
+ g_return_val_if_fail (TNY_IS_FOLDER(folder), NULL);
+
+ if (TNY_IS_MERGE_FOLDER (folder)) {
+ /* TnyMergeFolder does not support get_account(),
+ * because it could be merging folders from multiple accounts.
+ * So we assume that this is the local folders account: */
+
+ account = modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
+ } else {
+ account = tny_folder_get_account (folder);
+ }
+
+ return account;
+}
+
+/*
+ * It's probably better to use a query to get the folders that match
+ * new_name but currently tinymail only provides a match by name using
+ * regular expressions and we want an exact matching. We're not using
+ * a regular expression for the exact name because we'd need first to
+ * escape @new_name and it's not easy sometimes.
+ *
+ * The code that uses the query is available in revision 3152.
+ */
+gboolean
+modest_tny_folder_has_subfolder_with_name (TnyFolderStore *parent,
+ const gchar *new_name,
+ gboolean non_strict)
+{
+ TnyList *subfolders = NULL;
+ TnyIterator *iter = NULL;
+ TnyFolder *folder = NULL;
+ GError *err = NULL;
+ gboolean has_name = FALSE;
+
+ g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), FALSE);
+ g_return_val_if_fail (new_name, FALSE);
+
+ /* Get direct subfolders */
+ subfolders = tny_simple_list_new ();
+ tny_folder_store_get_folders (parent, subfolders, NULL, FALSE, &err);
+
+ /* Check names */
+ iter = tny_list_create_iterator (subfolders);
+ while (!tny_iterator_is_done (iter) && !has_name) {
+
+ const gchar *name;
+
+ folder = (TnyFolder*)tny_iterator_get_current (iter);
+ if (!folder || ! TNY_IS_FOLDER(folder)) {
+ g_warning ("%s: invalid folder", __FUNCTION__);
+ tny_iterator_next (iter);
+ continue;
+ }
+
+ name = tny_folder_get_name (folder);
+ if (!name) {
+ g_warning ("%s: folder name == NULL", __FUNCTION__);
+ g_object_unref (folder);
+ tny_iterator_next (iter);
+ continue;
+ }
+
+ /* is it simply the same folder name? */
+ if (strcmp (name, new_name) == 0)
+ has_name = TRUE;
+ /* or is it the same when ignoring case (non-strict mode)? */
+ else if (non_strict && modest_text_utils_utf8_strcmp (name, new_name, TRUE) == 0)
+ has_name = TRUE;
+ /* or is the name equal to the display name of some folder, in the current locale? */
+ else if (non_strict) {
+ TnyFolderType type = modest_tny_folder_guess_folder_type (folder);
+ if (type != TNY_FOLDER_TYPE_INVALID && type != TNY_FOLDER_TYPE_NORMAL)
+ has_name = !(modest_text_utils_utf8_strcmp (modest_local_folder_info_get_type_display_name (type),
+ new_name,
+ TRUE));
+ } else {
+ has_name = FALSE;
+ }
+
+ g_object_unref (folder);
+ tny_iterator_next(iter);
+ }
+
+ /* free */
+ g_object_unref (iter);
+ g_object_unref (subfolders);
+
+ return has_name;
+}
+
+gboolean
+modest_tny_folder_is_ancestor (TnyFolder *folder,
+ TnyFolderStore *ancestor)
+{
+ TnyFolderStore *tmp = NULL;
+ gboolean found = FALSE;
+
+ tmp = TNY_FOLDER_STORE (folder);
+ while (!found && tmp && !TNY_IS_ACCOUNT (tmp)) {
+ TnyFolderStore *folder_store;
+
+ folder_store = tny_folder_get_folder_store (TNY_FOLDER (tmp));
+ if (ancestor == folder_store)
+ found = TRUE;
+ else
+ tmp = folder_store;
+ g_object_unref (folder_store);
+ }
+ return found;
+}
+
+gchar *
+modest_tny_folder_get_display_name (TnyFolder *folder)
+{
+ TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
+ gchar *fname;
+
+ g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
+
+ fname = g_strdup (tny_folder_get_name (folder));
+ type = tny_folder_get_folder_type (folder);
+
+ if (modest_tny_folder_is_local_folder (TNY_FOLDER (folder)) ||
+ modest_tny_folder_is_memory_card_folder (TNY_FOLDER (folder))) {
+ type = modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (folder));
+ if (type != TNY_FOLDER_TYPE_UNKNOWN) {
+ g_free (fname);
+ fname = g_strdup (modest_local_folder_info_get_type_display_name (type));
+ }
+ } else {
+ /* Sometimes an special folder is reported by the server as
+ NORMAL, like some versions of Dovecot */
+ if (type == TNY_FOLDER_TYPE_NORMAL ||
+ type == TNY_FOLDER_TYPE_UNKNOWN) {
+ type = modest_tny_folder_guess_folder_type (TNY_FOLDER (folder));
+ }
+ }
+
+ if (type == TNY_FOLDER_TYPE_INBOX) {
+ g_free (fname);
+ fname = g_strdup (_("mcen_me_folder_inbox"));
+ }
+
+ return fname;