[geary/mjog/email-templates] Plugin.EmailTemplate: Create templates folder if it does not exist



commit 96f107f15cb3b3c675392dcc626410099f9be847
Author: Michael Gratton <mike vee net>
Date:   Wed Apr 22 19:10:05 2020 +1000

    Plugin.EmailTemplate: Create templates folder if it does not exist

 .../plugin/email-templates/email-templates.vala    | 38 ++++++++++++++++++++--
 1 file changed, 35 insertions(+), 3 deletions(-)
---
diff --git a/src/client/plugin/email-templates/email-templates.vala 
b/src/client/plugin/email-templates/email-templates.vala
index 5882e515..b6a266e6 100644
--- a/src/client/plugin/email-templates/email-templates.vala
+++ b/src/client/plugin/email-templates/email-templates.vala
@@ -66,10 +66,12 @@ public class Plugin.EmailTemplates :
 
 
     public override async void activate() throws GLib.Error {
-        Geary.iterate_array(UNLOC_NAMES.split("|")).map<string>(
+        // Add localised first, so if we need to create a folder it
+        // will be created localised.
+        Geary.iterate_array(LOC_NAMES.split("|")).map<string>(
             (name) => name.strip()
         ).add_all_to(this.folder_names);
-        Geary.iterate_array(LOC_NAMES.split("|")).map<string>(
+        Geary.iterate_array(UNLOC_NAMES.split("|")).map<string>(
             (name) => name.strip()
         ).add_all_to(this.folder_names);
 
@@ -203,6 +205,20 @@ public class Plugin.EmailTemplates :
         }
     }
 
+    private async void create_folder(Account account) {
+        try {
+            yield this.folder_store.create_personal_folder(
+                account,
+                this.folder_names[0],
+                this.cancellable
+            );
+            // Don't need to explicitly register the folder here, it
+            // will get picked up via the available signal
+        } catch (GLib.Error err) {
+            warning("Failed to create templates folder: %s", err.message);
+        }
+    }
+
     private void update_folder(Folder target) {
         var info_bar = this.info_bars.get(target);
         if (info_bar != null) {
@@ -269,11 +285,27 @@ public class Plugin.EmailTemplates :
     }
 
     private void on_folders_available(Gee.Collection<Folder> available) {
+        Folder? inbox = null;
+        var found_templates = false;
         foreach (var folder in available) {
-            if (folder.display_name in this.folder_names) {
+            if (folder.used_as == INBOX) {
+                inbox = folder;
+            } else if (folder.display_name in this.folder_names) {
                 register_folder(folder);
+                found_templates = true;
             }
         }
+
+        // XXX There is no way at the moment to determine when all
+        // local folders have been loaded, but since they are all done
+        // in once batch, it's a safe bet that if we've seen the
+        // Inbox, then the local folder set should contain a templates
+        // folder, if one is available. If there isn't, we need to
+        // create it.
+        if (!found_templates && inbox != null) {
+            debug("Creating templates folder");
+            this.create_folder.begin(inbox.account);
+        }
     }
 
     private void on_folders_unavailable(Gee.Collection<Folder> unavailable) {


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