From c649da9949e4ad7484f80b755f687d822068569f Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Sun, 20 Jun 2010 22:41:55 +0100 Subject: [PATCH] Partially populate the Plugin tab in the prefs UI so plugins can be enabled and disabled --- terminatorlib/plugin.py | 31 ++++++++++++++++-- terminatorlib/preferences.glade | 53 +++++++++++++++++++++++++++++-- terminatorlib/prefseditor.py | 56 ++++++++++++++++++++++++++++++++- 3 files changed, 135 insertions(+), 5 deletions(-) diff --git a/terminatorlib/plugin.py b/terminatorlib/plugin.py index 99c3aa2b..859c609c 100755 --- a/terminatorlib/plugin.py +++ b/terminatorlib/plugin.py @@ -39,6 +39,7 @@ class Plugin(object): class PluginRegistry(borg.Borg): """Definition of a class to store plugin instances""" + available_plugins = None instances = None path = None done = None @@ -61,6 +62,8 @@ class PluginRegistry(borg.Borg): self.path) if not self.done: self.done = False + if not self.available_plugins: + self.available_plugins = {} def load_plugins(self, testing=False): """Load all plugins present in the plugins/ directory in our module""" @@ -85,12 +88,15 @@ class PluginRegistry(borg.Borg): try: module = __import__(plugin[:-3], None, None, ['']) for item in getattr(module, 'AVAILABLE'): + if item not in self.available_plugins.keys(): + func = getattr(module, item) + self.available_plugins[item] = func + if not testing and item not in config['enabled_plugins']: dbg('plugin %s not enabled, skipping' % item) continue if item not in self.instances: - func = getattr(module, item) - self.instances[item] = func() + self.instances[item] = func() except Exception, ex: err('PluginRegistry::load_plugins: Importing plugin %s \ failed: %s' % (plugin, ex)) @@ -111,6 +117,27 @@ for %s' % (len(self.instances), capability)) """Return all plugins""" return(self.instances) + def get_available_plugins(self): + """Return a list of all available plugins whether they are enabled or + disabled""" + return(self.available_plugins.keys()) + + def is_enabled(self, plugin): + """Return a boolean value indicating whether a plugin is enabled or + not""" + return(self.instances.has_key(plugin)) + + def enable(self, plugin): + """Enable a plugin""" + dbg("Enabling %s" % plugin) + if plugin not in self.instances: + self.instances[plugin] = self.available_plugins[plugin]() + + def disable(self, plugin): + """Disable a plugin""" + dbg("Disabling %s" % plugin) + del(self.instances[plugin]) + # This is where we should define a base class for each type of plugin we # support diff --git a/terminatorlib/preferences.glade b/terminatorlib/preferences.glade index 90316dbd..74eae78b 100644 --- a/terminatorlib/preferences.glade +++ b/terminatorlib/preferences.glade @@ -255,6 +255,14 @@ + + + + + + + + 5 normal @@ -2854,9 +2862,50 @@ - + True - Not yet implemented + + + True + True + PluginListStore + adjustment2 + adjustment3 + 0 + + + Plugin + True + + + + + + 1 + + + + + + 0 + + + + + + + 0 + + + + + True + This plugin has no configuration options + + + 1 + + 4 diff --git a/terminatorlib/prefseditor.py b/terminatorlib/prefseditor.py index d6a02b39..887c4ccc 100755 --- a/terminatorlib/prefseditor.py +++ b/terminatorlib/prefseditor.py @@ -15,6 +15,7 @@ import config from keybindings import Keybindings, KeymapError from translation import _ from terminator import Terminator +from plugin import PluginRegistry def color2hex(widget): """Pull the colour values out of a Gtk ColorPicker widget and return them @@ -26,6 +27,7 @@ def color2hex(widget): class PrefsEditor: """Class implementing the various parts of the preferences editor""" config = None + registry = None keybindings = None window = None builder = None @@ -260,7 +262,19 @@ class PrefsEditor: keyval, mask]) ## Plugins tab - # FIXME: Implement this + # Populate the plugin list + widget = guiget('pluginlist') + liststore = widget.get_model() + self.registry = PluginRegistry() + plugins = self.registry.get_available_plugins() + self.pluginiters = {} + for plugin in plugins: + self.pluginiters[plugin] = liststore.append([plugin, + self.registry.is_enabled(plugin)]) + selection = widget.get_selection() + selection.connect('changed', self.on_plugin_selection_changed) + if len(self.pluginiters) > 0: + selection.select_iter(liststore.get_iter_first()) def set_profile_values(self, profile): """Update the profile values for a given profile""" @@ -985,6 +999,46 @@ class PrefsEditor: else: widget.set_sensitive(True) + def on_plugin_selection_changed(self, selection): + """A different plugin was selected""" + (listmodel, rowiter) = selection.get_selected() + if not rowiter: + # Something is wrong, just jump to the first item in the list + treeview = selection.get_tree_view() + liststore = treeview.get_model() + selection.select_iter(liststore.get_iter_first()) + return + plugin = listmodel.get_value(rowiter, 0) + self.set_plugin(plugin) + self.previous_plugin_selection = plugin + + widget = self.builder.get_object('plugintogglebutton') + + def on_plugin_toggled(self, cell, path): + """A plugin has been enabled or disabled""" + treeview = self.builder.get_object('pluginlist') + model = treeview.get_model() + plugin = model[path][0] + + state = self.registry.is_enabled(plugin) + if state: + self.registry.disable(plugin) + else: + self.registry.enable(plugin) + state = self.registry.is_enabled(plugin) + # Update the treeview + model[path][1] = state + + enabled_plugins = self.registry.get_all_plugins().keys() + self.config['enabled_plugins'] = enabled_plugins + self.config.save() + + def set_plugin(self, plugin): + """Show the preferences for the selected plugin, if any""" + pluginpanelabel = self.builder.get_object('pluginpanelabel') + pluginconfig = self.config.plugin_get_config(plugin) + # FIXME: Implement this, we need to auto-construct a UI for the plugin + def on_profile_name_edited(self, cell, path, newtext): """Update a profile name""" oldname = cell.get_property('text')