[geary/mjog/email-templates: 67/72] Plugins.SpecialFolders: Actually implement the edit draft action



commit 8ca3de00a35b69cbb3b89989be650a20cb3368e0
Author: Michael Gratton <mike vee net>
Date:   Mon Apr 20 11:22:26 2020 +1000

    Plugins.SpecialFolders: Actually implement the edit draft action
    
    Fixes #803

 .../plugin/special-folders/special-folders.vala    | 59 +++++++++++++++++-----
 1 file changed, 46 insertions(+), 13 deletions(-)
---
diff --git a/src/client/plugin/special-folders/special-folders.vala 
b/src/client/plugin/special-folders/special-folders.vala
index 3bd5cf4f..aa9e120d 100644
--- a/src/client/plugin/special-folders/special-folders.vala
+++ b/src/client/plugin/special-folders/special-folders.vala
@@ -21,8 +21,8 @@ public class Plugin.SpecialFolders :
     PluginBase, FolderExtension, EmailExtension {
 
 
-    // InfoBar button action name
-    private const string ACTION_NAME = "empty-folder";
+    private const string ACTION_EDIT = "edit-draft";
+    private const string ACTION_EMPTY = "empty-folder";
 
     // InfoBar priority
     private const int PRIORITY = 0;
@@ -37,43 +37,53 @@ public class Plugin.SpecialFolders :
     }
 
 
-    private FolderStore? folder_store = null;
-    private GLib.SimpleAction? empty_action = null;
-
     private EmailStore? email_store = null;
+    private FolderStore? folder_store = null;
 
     private Gee.Map<Folder,InfoBar> info_bars =
         new Gee.HashMap<Folder,InfoBar>();
 
+    private GLib.SimpleAction? edit_action = null;
+    private GLib.SimpleAction? empty_action = null;
+
     private GLib.Cancellable cancellable = new GLib.Cancellable();
 
 
     public override async void activate() throws GLib.Error {
+        this.email_store = yield this.email.get_email_store();
+        this.email_store.email_displayed.connect(on_email_displayed);
+
         this.folder_store = yield this.folders.get_folder_store();
         this.folder_store.folder_selected.connect(on_folder_selected);
         this.folder_store.folders_type_changed.connect(on_folders_type_changed);
 
+        this.edit_action = new GLib.SimpleAction(
+            ACTION_EDIT, this.email_store.email_identifier_variant_type
+        );
+        this.edit_action.activate.connect(on_edit_activated);
+        this.plugin_application.register_action(this.edit_action);
+
         this.empty_action = new GLib.SimpleAction(
-            ACTION_NAME, this.folder_store.folder_variant_type
+            ACTION_EMPTY, this.folder_store.folder_variant_type
         );
         this.empty_action.activate.connect(on_empty_activated);
         this.plugin_application.register_action(this.empty_action);
-
-        this.email_store = yield this.email.get_email_store();
-        this.email_store.email_displayed.connect(on_email_displayed);
     }
 
     public override async void deactivate(bool is_shutdown) throws GLib.Error {
+        this.plugin_application.deregister_action(this.edit_action);
+        this.edit_action = null;
+
         this.plugin_application.deregister_action(this.empty_action);
-        this.empty_action.activate.disconnect(on_empty_activated);
         this.empty_action = null;
 
+        this.email_store.email_displayed.disconnect(on_email_displayed);
+        this.email_store = null;
+
         this.folder_store.folder_selected.disconnect(on_folder_selected);
         this.folder_store.folders_type_changed.disconnect(on_folders_type_changed);
         this.folder_store = null;
 
-        this.email_store.email_displayed.disconnect(on_email_displayed);
-
         this.cancellable.cancel();
     }
 
@@ -130,6 +140,16 @@ public class Plugin.SpecialFolders :
         }
     }
 
+    private async void edit_draft(EmailIdentifier id) {
+        try {
+            var composer = this.plugin_application.new_composer(id.account);
+            yield composer.edit_email(id);
+            composer.show();
+        } catch (GLib.Error err) {
+            warning("Unable to construct composer: %s", err.message);
+        }
+    }
+
     private InfoBar get_folder_info_bar(Folder target) {
         var bar = this.info_bars.get(target);
         if (bar == null) {
@@ -158,7 +178,7 @@ public class Plugin.SpecialFolders :
             // Translators: Info bar button label for editing a draft
             // email
             _("Edit"),
-            this.empty_action,
+            this.edit_action,
             target.identifier.to_variant()
         );
         return bar;
@@ -190,6 +210,19 @@ public class Plugin.SpecialFolders :
         }
     }
 
+    private void on_edit_activated(GLib.Action action, GLib.Variant? target) {
+        if (this.email_store != null && target != null) {
+            EmailIdentifier? id = this.email_store.get_email_identifier_from_variant(
+                target
+            );
+            if (id != null) {
+                this.edit_draft.begin(id);
+            } else {
+                warning("Bad draft id");
+            }
+        }
+    }
+
     private void on_empty_activated(GLib.Action action, GLib.Variant? target) {
         if (this.folder_store != null && target != null) {
             Folder? folder = this.folder_store.get_folder_from_variant(target);


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