[geary/mjog/mail-merge-plugin: 3/12] Components.PreferencesWindow: Update plugin rows when deps change



commit 8a14a1006cd3c5beb8cef5c334357099ef281a89
Author: Michael Gratton <mike vee net>
Date:   Wed Apr 29 22:50:55 2020 +1000

    Components.PreferencesWindow: Update plugin rows when deps change
    
    Hook up to PluginManager's signals and ensure plugin rows are updated
    when a plugin's dependencies are also loaded or unloaded.

 .../components/components-preferences-window.vala  | 122 ++++++++++++---------
 1 file changed, 73 insertions(+), 49 deletions(-)
---
diff --git a/src/client/components/components-preferences-window.vala 
b/src/client/components/components-preferences-window.vala
index 17119f52..59874526 100644
--- a/src/client/components/components-preferences-window.vala
+++ b/src/client/components/components-preferences-window.vala
@@ -16,6 +16,78 @@ public class Components.PreferencesWindow : Hdy.PreferencesWindow {
         { ACTION_CLOSE, on_close },
     };
 
+    private class PluginRow : Hdy.ActionRow {
+
+        private Peas.PluginInfo plugin;
+        private Application.PluginManager plugins;
+        private Gtk.Switch sw = new Gtk.Switch();
+
+
+        public PluginRow(Peas.PluginInfo plugin,
+                         Application.PluginManager plugins) {
+            this.plugin = plugin;
+            this.plugins = plugins;
+
+            this.sw.active = plugin.is_loaded();
+            this.sw.notify["active"].connect_after(() => update_plugin());
+            this.sw.valign = CENTER;
+
+            var row = new Hdy.ActionRow();
+            this.title = plugin.get_name();
+            this.subtitle = plugin.get_description();
+            this.activatable_widget = this.sw;
+            this.add_action(this.sw);
+
+            plugins.plugin_activated.connect((info) => {
+                    if (this.plugin == info) {
+                        this.sw.active = true;
+                    }
+                });
+            plugins.plugin_deactivated.connect((info) => {
+                    if (this.plugin == info) {
+                        this.sw.active = false;
+                    }
+                });
+            plugins.plugin_error.connect((info) => {
+                    if (this.plugin == info) {
+                        this.sw.active = false;
+                        this.sw.sensitive = false;
+                    }
+                });
+        }
+
+        private void update_plugin() {
+            if (this.sw.active && !this.plugin.is_loaded()) {
+                bool loaded = false;
+                try {
+                    loaded = this.plugins.load_optional(this.plugin);
+                } catch (GLib.Error err) {
+                    warning(
+                        "Plugin %s not able to be loaded: %s",
+                        plugin.get_name(), err.message
+                    );
+                }
+                if (!loaded) {
+                    this.sw.active = false;
+                }
+            } else if (!sw.active && this.plugin.is_loaded()) {
+                bool unloaded = false;
+                try {
+                    unloaded = this.plugins.unload_optional(this.plugin);
+                } catch (GLib.Error err) {
+                    warning(
+                        "Plugin %s not able to be loaded: %s",
+                        plugin.get_name(), err.message
+                    );
+                }
+                if (!unloaded) {
+                    this.sw.active = true;
+                }
+            }
+        }
+
+    }
+
 
     public static void add_accelerators(Application.Client app) {
         app.add_window_accelerators(ACTION_CLOSE, { "Escape" } );
@@ -171,7 +243,7 @@ public class Components.PreferencesWindow : Hdy.PreferencesWindow {
         if (application != null) {
             foreach (Peas.PluginInfo plugin in
                      this.plugins.get_optional_plugins()) {
-                group.add(new_plugin_row(plugin));
+                group.add(new PluginRow(plugin, this.plugins));
             }
         }
 
@@ -186,54 +258,6 @@ public class Components.PreferencesWindow : Hdy.PreferencesWindow {
         add(page);
     }
 
-    private Hdy.ActionRow new_plugin_row(Peas.PluginInfo plugin) {
-        var @switch = new Gtk.Switch();
-        @switch.active = plugin.is_loaded();
-        @switch.notify["active"].connect_after(
-            () => enable_plugin(plugin, switch)
-        );
-        @switch.valign = CENTER;
-
-        var row = new Hdy.ActionRow();
-        row.title = plugin.get_name();
-        row.subtitle = plugin.get_description();
-        row.activatable_widget = @switch;
-        row.add_action(@switch);
-
-        return row;
-    }
-
-    private void enable_plugin(Peas.PluginInfo plugin, Gtk.Switch @switch) {
-        if (@switch.active && !plugin.is_loaded()) {
-            bool loaded = false;
-            try {
-                loaded = this.plugins.load_optional(plugin);
-            } catch (GLib.Error err) {
-                warning(
-                    "Plugin %s not able to be loaded: %s",
-                    plugin.get_name(), err.message
-                );
-            }
-            if (!loaded) {
-                @switch.active = false;
-            }
-        } else if (!@switch.active && plugin.is_loaded()) {
-            bool unloaded = false;
-            try {
-                unloaded = this.plugins.unload_optional(plugin);
-            } catch (GLib.Error err) {
-                warning(
-                    "Plugin %s not able to be loaded: %s",
-                    plugin.get_name(), err.message
-                );
-            }
-            if (!unloaded) {
-                @switch.active = true;
-            }
-        }
-    }
-
-
     private void on_close() {
         close();
     }


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