[geary/wip/create-folders-713492] WIP



commit c1ee5782113f5532067bcf206d86e40ed22a2cb2
Author: Charles Lindsay <chaz yorba org>
Date:   Mon Feb 3 18:24:18 2014 -0800

    WIP

 src/engine/abstract/geary-abstract-account.vala    |    2 +-
 src/engine/api/geary-account.vala                  |    4 +-
 .../imap-engine/imap-engine-generic-account.vala   |  106 +++++++++++++++++---
 3 files changed, 93 insertions(+), 19 deletions(-)
---
diff --git a/src/engine/abstract/geary-abstract-account.vala b/src/engine/abstract/geary-abstract-account.vala
index 9697ee7..18705c9 100644
--- a/src/engine/abstract/geary-abstract-account.vala
+++ b/src/engine/abstract/geary-abstract-account.vala
@@ -104,7 +104,7 @@ 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,
+    public abstract async Geary.Folder get_required_special_folder_async(Geary.SpecialFolderType special,
         Cancellable? cancellable) throws Error;
     
     public abstract async void send_email_async(Geary.ComposedEmail composed, Cancellable? cancellable = 
null)
diff --git a/src/engine/api/geary-account.vala b/src/engine/api/geary-account.vala
index 2db9cdd..ef9a9f5 100644
--- a/src/engine/api/geary-account.vala
+++ b/src/engine/api/geary-account.vala
@@ -282,10 +282,10 @@ public interface Geary.Account : BaseObject {
     /**
      * 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,
+     * created.  The only valid special folder types that can be required are: DRAFTS, SENT,
      * SPAM, and TRASH.
      */
-    public abstract async Geary.Folder ensure_special_folder_async(Geary.SpecialFolderType special,
+    public abstract async Geary.Folder get_required_special_folder_async(Geary.SpecialFolderType special,
         Cancellable? cancellable = null) throws Error;
     
     /**
diff --git a/src/engine/imap-engine/imap-engine-generic-account.vala 
b/src/engine/imap-engine/imap-engine-generic-account.vala
index 92dcc5f..a1e60c8 100644
--- a/src/engine/imap-engine/imap-engine-generic-account.vala
+++ b/src/engine/imap-engine/imap-engine-generic-account.vala
@@ -464,9 +464,11 @@ private abstract class Geary.ImapEngine.GenericAccount : Geary.AbstractAccount {
         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();
+    private async Geary.Folder ensure_special_folder_async(Geary.SpecialFolderType special,
+        Cancellable? cancellable) throws Error {
+        Geary.Folder? folder = get_special_folder(special);
+        if (folder != null)
+            return folder;
         
         string? name;
         switch (special) {
@@ -490,13 +492,74 @@ private abstract class Geary.ImapEngine.GenericAccount : Geary.AbstractAccount {
                 assert_not_reached();
         }
         
-        if (Geary.String.is_empty_or_whitespace(name))
-            name = Geary.traverse<string>(mailbox_search_names.get(special)).first();
+        Geary.FolderPath? path = null;
+        if (!Geary.String.is_empty_or_whitespace(name)) {
+            // We picked which folder to create previously, so always continue
+            // to use that name.
+            
+            // TODO: look at key file's save string array function
+            path = FolderPath.deserialize(name);
+        } else {
+            // We are picking for the first time the folder to use.
+            
+            Gee.ArrayList<string> search_names = get_mailbox_search_names().get(special);
+            foreach (string search_name in search_names) {
+                Geary.FolderPath search_path = FolderPath.deserialize(search_name);
+                // TODO: normalize strings before searching...
+                if (search_path in folder_map.keys) {
+                    path = search_path;
+                    break;
+                }
+            }
+            if (path == null) {
+                foreach (string search_name in search_names) {
+                    Geary.FolderPath search_path = null;// TODO: Construct INBOX.path...
+                    if (search_path in folder_map.keys) {
+                        path = search_path;
+                        break;
+                    }
+                }
+            }
+            
+            if (path == null)
+                path = FolderPath.deserialize(search_names[0]);
+            
+            switch (special) {
+                case Geary.SpecialFolderType.DRAFTS:
+                    information.drafts_folder_name = path.serialize();
+                break;
+                
+                case Geary.SpecialFolderType.SENT:
+                    information.sent_mail_folder_name = path.serialize();
+                break;
+                
+                case Geary.SpecialFolderType.SPAM:
+                    information.spam_folder_name = path.serialize();
+                break;
+                
+                case Geary.SpecialFolderType.TRASH:
+                    information.trash_folder_name = path.serialize();
+                break;
+                
+                default:
+                    assert_not_reached();
+            }
+            // TODO: save information?
+        }
+        
+        if (path in folder_map.keys) {
+            folder = folder_map.get(path);
+        } else {
+            // TODO: ignore error due to already existing.
+            yield remote.create_folder_async(path, cancellable);
+            folder = yield fetch_folder_async(path, cancellable);
+        }
         
-        return new Imap.FolderRoot(name, null);
+        folder.set_special_folder_type(special);
+        return folder;
     }
     
-    public override async Geary.Folder ensure_special_folder_async(Geary.SpecialFolderType special,
+    public override async Geary.Folder get_required_special_folder_async(Geary.SpecialFolderType special,
         Cancellable? cancellable) throws Error {
         switch (special) {
             case Geary.SpecialFolderType.DRAFTS:
@@ -507,19 +570,24 @@ private abstract class Geary.ImapEngine.GenericAccount : Geary.AbstractAccount {
             
             default:
                 throw new EngineError.BAD_PARAMETERS(
-                    "Invalid special folder type %s passed to ensure_special_folder_async",
+                    "Invalid special folder type %s passed to get_required_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(special);
-        yield remote.create_folder_async(path, cancellable);
-        return yield fetch_folder_async(path, cancellable);
+        return yield ensure_special_folder_async(special, cancellable);
+    }
+    
+    private async void ensure_special_folders_async(Cancellable? cancellable) throws Error {
+        Geary.SpecialFolderType[] required = {
+            Geary.SpecialFolderType.DRAFTS,
+            Geary.SpecialFolderType.SENT,
+            Geary.SpecialFolderType.SPAM,
+            Geary.SpecialFolderType.TRASH,
+        };
+        foreach (Geary.SpecialFolderType special in required)
+            yield ensure_special_folder_async(special, cancellable);
     }
     
     private async void update_folders_async(Gee.Map<FolderPath, Geary.Folder> existing_folders,
@@ -593,7 +661,7 @@ private abstract class Geary.ImapEngine.GenericAccount : Geary.AbstractAccount {
                 debug("Unable to fetch local folder after cloning: %s", convert_err.message);
             }
         }
-        Gee.Collection<Geary.Folder> engine_added = new Gee.ArrayList<Geary.Folder>();
+        Gee.Collection<GenericFolder> engine_added = new Gee.ArrayList<Geary.Folder>();
         engine_added.add_all(build_folders(folders_to_build));
         
         // TODO: Remove local folders no longer available remotely.
@@ -616,6 +684,12 @@ private abstract class Geary.ImapEngine.GenericAccount : Geary.AbstractAccount {
             if (altered.size > 0)
                 notify_folders_contents_altered(altered);
         }
+        
+        try {
+            yield ensure_special_folders_async(cancellable);
+        } catch (Error e) {
+            warning("Unable to ensure special folders: %s", e.message);
+        }
     }
     
     public override async void send_email_async(Geary.ComposedEmail composed,


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