[geary/wip/create-folders-713492: 4/4] Add basic ensure_special_folder implementation



commit 764acfb3aafe463926c653f40a9ac6982cb669c3
Author: Charles Lindsay <chaz yorba org>
Date:   Fri Jan 31 16:00:55 2014 -0800

    Add basic ensure_special_folder implementation

 src/engine/abstract/geary-abstract-account.vala    |    3 +
 src/engine/api/geary-account.vala                  |    9 ++
 .../imap-engine/imap-engine-generic-account.vala   |   93 ++++++++++++++++++++
 3 files changed, 105 insertions(+), 0 deletions(-)
---
diff --git a/src/engine/abstract/geary-abstract-account.vala b/src/engine/abstract/geary-abstract-account.vala
index 1104300..9697ee7 100644
--- a/src/engine/abstract/geary-abstract-account.vala
+++ b/src/engine/abstract/geary-abstract-account.vala
@@ -104,6 +104,9 @@ public abstract class Geary.AbstractAccount : BaseObject, Geary.Account {
             .first_matching(f => f.special_folder_type == special);
     }
     
+    public abstract async Geary.Folder ensure_special_folder_async(Geary.SpecialFolderType special,
+        Cancellable? cancellable) throws Error;
+    
     public abstract async void send_email_async(Geary.ComposedEmail composed, Cancellable? cancellable = 
null)
         throws Error;
     
diff --git a/src/engine/api/geary-account.vala b/src/engine/api/geary-account.vala
index 89902c0..2db9cdd 100644
--- a/src/engine/api/geary-account.vala
+++ b/src/engine/api/geary-account.vala
@@ -280,6 +280,15 @@ public interface Geary.Account : BaseObject {
     public abstract Geary.Folder? get_special_folder(Geary.SpecialFolderType special) throws Error;
     
     /**
+     * Returns the folder with the given special folder type.  The folder will be created if it
+     * doesn't already exist.  An error will be thrown if the folder doesn't exist and can't be
+     * created.  The only valid special folder types that can be ensured are: DRAFTS, SENT,
+     * SPAM, and TRASH.
+     */
+    public abstract async Geary.Folder ensure_special_folder_async(Geary.SpecialFolderType special,
+        Cancellable? cancellable = null) throws Error;
+    
+    /**
      * Submits a ComposedEmail for delivery.  Messages may be scheduled for later delivery or immediately
      * sent.  Subscribe to the "email-sent" signal to be notified of delivery.  Note that that signal
      * does not return the ComposedEmail object but an RFC822-formatted object.  Allowing for the
diff --git a/src/engine/imap-engine/imap-engine-generic-account.vala 
b/src/engine/imap-engine/imap-engine-generic-account.vala
index 29d227a..1a34e8a 100644
--- a/src/engine/imap-engine/imap-engine-generic-account.vala
+++ b/src/engine/imap-engine/imap-engine-generic-account.vala
@@ -429,6 +429,99 @@ private abstract class Geary.ImapEngine.GenericAccount : Geary.AbstractAccount {
         return build_folder((ImapDB.Folder) yield local.fetch_folder_async(path, cancellable));
     }
     
+    private Gee.HashMap<Geary.SpecialFolderType, Gee.ArrayList<string>> get_mailbox_search_names() {
+        Gee.HashMap<Geary.SpecialFolderType, string> mailbox_search_names
+            = new Gee.HashMap<Geary.SpecialFolderType, string>();
+        mailbox_search_names.set(Geary.SpecialFolderType.DRAFTS,
+            // List of folder names to match for Drafts, separated by |.  Please add localized common
+            // names for the Drafts folder, leaving in the English names as well.  The first in the list
+            // will be the default, so please add the most common localized name to the front.
+            _("Drafts | Draft"));
+        mailbox_search_names.set(Geary.SpecialFolderType.SENT,
+            // List of folder names to match for Sent Mail, separated by |.  Please add localized common
+            // names for the Sent Mail folder, leaving in the English names as well.  The first in the list
+            // will be the default, so please add the most common localized name to the front.
+            _("Sent | Sent Mail | Sent Email | Sent E-Mail"));
+        mailbox_search_names.set(Geary.SpecialFolderType.SPAM,
+            // List of folder names to match for Spam, separated by |.  Please add localized common
+            // names for the Spam folder, leaving in the English names as well.  The first in the list
+            // will be the default, so please add the most common localized name to the front.
+            _("Junk | Spam | Junk Mail | Junk Email | Junk E-Mail | Bulk Mail | Bulk Email | Bulk E-Mail"));
+        mailbox_search_names.set(Geary.SpecialFolderType.TRASH,
+            // List of folder names to match for Trash, separated by |.  Please add localized common
+            // names for the Trash folder, leaving in the English names as well.  The first in the list
+            // will be the default, so please add the most common localized name to the front.
+            _("Trash | Rubbish | Rubbish Bin"));
+        
+        Gee.HashMap<Geary.SpecialFolderType, Gee.ArrayList<string>> compiled
+            = new Gee.HashMap<Geary.SpecialFolderType, Gee.ArrayList<string>>();
+        
+        foreach (Gee.Map.Entry<Geary.SpecialFolderType, string> e in mailbox_search_names) {
+            compiled.set(e.key, Geary.iterate_array<string>(e.value.split("|"))
+                .map<string>(n => n.strip()).to_array_list());
+        }
+        
+        return compiled;
+    }
+    
+    private Geary.FolderPath get_create_special_folder_path(Geary.SpecialFolderType special) {
+        Gee.HashMap<Geary.SpecialFolderType, Gee.ArrayList<string>> mailbox_search_names
+            = get_mailbox_search_names();
+        
+        string? name;
+        switch (special) {
+            case Geary.SpecialFolderType.DRAFTS:
+                name = information.drafts_folder_name;
+            break;
+            
+            case Geary.SpecialFolderType.SENT:
+                name = information.sent_mail_folder_name;
+            break;
+            
+            case Geary.SpecialFolderType.SPAM:
+                name = information.spam_folder_name;
+            break;
+            
+            case Geary.SpecialFolderType.TRASH:
+                name = information.trash_folder_name;
+            break;
+            
+            default:
+                assert_not_reached();
+        }
+        
+        if (Geary.String.is_empty_or_whitespace(name))
+            name = Geary.traverse<string>(mailbox_search_names.get(special)).first();
+        
+        return new Imap.FolderRoot(name, null);
+    }
+    
+    public virtual async Geary.Folder ensure_special_folder_async(Geary.SpecialFolderType special,
+        Cancellable? cancellable) throws Error {
+        switch (special) {
+            case Geary.SpecialFolderType.DRAFTS:
+            case Geary.SpecialFolderType.SENT:
+            case Geary.SpecialFolderType.SPAM:
+            case Geary.SpecialFolderType.TRASH:
+            break;
+            
+            default:
+                throw new EngineError.BAD_PARAMETERS(
+                    "Invalid special folder type %s passed to ensure_special_folder_async",
+                    special.to_string());
+        }
+        
+        check_open();
+        
+        Geary.Folder? folder = get_special_folder(special);
+        if (folder != null)
+            return folder;
+        
+        Geary.FolderPath path = get_create_special_folder_path(name);
+        yield remote.create_folder_async(path, cancellable);
+        return yield fetch_folder_async(path, cancellable);
+    }
+    
     private async void update_folders_async(Gee.Map<FolderPath, Geary.Folder> existing_folders,
         Gee.Map<FolderPath, Imap.Folder> remote_folders, Cancellable? cancellable) {
         // update all remote folders properties in the local store and active in the system


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]