From a78ab9529f2a23b914596a6eb10d6e1edf172352 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Wed, 9 Sep 2009 20:47:12 +0000 Subject: [PATCH] Fix up the Execute plugin's gtk code --- deluge/plugins/execute/execute/core.py | 35 ++- .../execute/execute/data/execute_prefs.glade | 229 +++++++++--------- deluge/plugins/execute/execute/gtkui.py | 93 +++++-- 3 files changed, 217 insertions(+), 140 deletions(-) diff --git a/deluge/plugins/execute/execute/core.py b/deluge/plugins/execute/execute/core.py index 4fd6657b0..ef4c6f6f2 100644 --- a/deluge/plugins/execute/execute/core.py +++ b/deluge/plugins/execute/execute/core.py @@ -43,6 +43,7 @@ from deluge.plugins.pluginbase import CorePluginBase import deluge.component as component from deluge.configmanager import ConfigManager from deluge.core.rpcserver import export +from deluge.event import DelugeEvent DEFAULT_CONFIG = { "commands": [] @@ -52,6 +53,21 @@ EXECUTE_ID = 0 EXECUTE_EVENT = 1 EXECUTE_COMMAND = 2 + +class ExecuteCommandAddedEvent(DelugeEvent): + """ + Emitted when a new command is added. + """ + def __init__(self, command_id, event, command): + self._args = [command_id, event, command] + +class ExecuteCommandRemovedEvent(DelugeEvent): + """ + Emitted when a command is removed. + """ + def __init__(self, command_id): + self._args = [command_id] + class Core(CorePluginBase): def enable(self): self.config = ConfigManager("execute.conf", DEFAULT_CONFIG) @@ -59,7 +75,7 @@ class Core(CorePluginBase): event_manager.register_event_handler("TorrentFinishedEvent", self.on_torrent_finished) log.debug("Example core plugin enabled!") - + def execute_commands(self, torrent_id, event): torrent = component.get("TorrentManager").torrents[torrent_id] info = torrent.get_status(["name", "save_path"]) @@ -81,34 +97,33 @@ class Core(CorePluginBase): event_manager.deregister_event_handler("TorrentFinishedEvent", self.on_torrent_finished) log.debug("Example core plugin disabled!") - + def on_torrent_finished(self, torrent_id): self.execute_commands(torrent_id, "complete") - + ### Exported RPC methods ### @export def add_command(self, event, command): command_id = hashlib.sha1(str(time.time())).hexdigest() self.config["commands"].append((command_id, event, command)) - self.config.save() + component.get("EventManager").emit(ExecuteCommandAddedEvent(command_id, event, command)) @export def get_commands(self): return self.config["commands"] - + @export def remove_command(self, command_id): for command in self.config["commands"]: if command[EXECUTE_ID] == command_id: self.config["commands"].remove(command) + component.get("EventManager").emit(ExecuteCommandRemovedEvent(command_id)) break - self.config.save() - + @export - def save_command(self, command_id, event, command): + def save_command(self, command_id, event, cmd): for command in self.config["commands"]: if command[EXECUTE_ID] == command_id: command[EXECUTE_EVENT] = event - command[EXECUTE_COMMAND] = event + command[EXECUTE_COMMAND] = cmd break - self.config.save() \ No newline at end of file diff --git a/deluge/plugins/execute/execute/data/execute_prefs.glade b/deluge/plugins/execute/execute/data/execute_prefs.glade index d6864a0a0..5f92d9315 100644 --- a/deluge/plugins/execute/execute/data/execute_prefs.glade +++ b/deluge/plugins/execute/execute/data/execute_prefs.glade @@ -6,6 +6,121 @@ True + vertical + + + True + 0 + none + + + True + 5 + 12 + + + True + 3 + 2 + + + True + 0 + Event + + + GTK_FILL + + 5 + + + + + True + 0 + Command + + + 1 + 2 + GTK_FILL + + 5 + + + + + True + True + True + True + + + 1 + 2 + 1 + 2 + + + + + + True + + + + 1 + 2 + + + + + + True + end + + + gtk-add + True + True + True + True + + + + False + False + 0 + + + + + 2 + 2 + 3 + GTK_FILL + + + + + + + + + + True + <b>Add Command</b> + True + + + label_item + + + + + 0 + + True @@ -14,10 +129,13 @@ True + 5 12 True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + vertical @@ -37,117 +155,6 @@ - 0 - - - - - True - 0 - none - - - True - 12 - - - True - 3 - 2 - - - True - 0 - Event - - - GTK_FILL - 5 - - - - - True - 0 - Command - - - 1 - 2 - GTK_FILL - 5 - - - - - True - True - - - 1 - 2 - 1 - 2 - - - - - True - - - - 1 - 2 - - - - - True - - - Add - True - True - True - - - - False - 5 - 0 - - - - - - - - 1 - 2 - 2 - 3 - - - - - - - - - - - - True - <b>Add Command</b> - True - - - label_item - - - - - False 1 diff --git a/deluge/plugins/execute/execute/gtkui.py b/deluge/plugins/execute/execute/gtkui.py index 1ca9fb2b2..7a341ea2b 100644 --- a/deluge/plugins/execute/execute/gtkui.py +++ b/deluge/plugins/execute/execute/gtkui.py @@ -57,27 +57,33 @@ EVENTS = ["complete", "added"] class ExecutePreferences(object): def __init__(self, plugin): self.plugin = plugin - + def load(self): log.debug("Adding Execute Preferences page") self.glade = gtk.glade.XML(self.get_resource("execute_prefs.glade")) self.glade.signal_autoconnect({ "on_add_button_clicked": self.on_add_button_clicked }) - + events = self.glade.get_widget("event_combobox") - + store = gtk.ListStore(str, str) for event in EVENTS: event_label = EVENT_MAP[event] store.append((event_label, event)) events.set_model(store) + events.set_active(0) self.plugin.add_preferences_page(_("Execute"), self.glade.get_widget("execute_box")) self.plugin.register_hook("on_show_prefs", self.load_commands) self.plugin.register_hook("on_apply_prefs", self.on_apply_prefs) + self.load_commands() + + client.register_event_handler("ExecuteCommandAddedEvent", self.on_command_added_event) + client.register_event_handler("ExecuteCommandRemovedEvent", self.on_command_removed_event) + def unload(self): self.plugin.remove_preferences_page(_("Execute")) self.plugin.deregister_hook("on_apply_prefs", self.on_apply_prefs) @@ -86,34 +92,83 @@ class ExecutePreferences(object): def get_resource(self, filename): return pkg_resources.resource_filename("execute", os.path.join("data", filename)) - + + def add_command(self, command_id, event, command): + log.debug("Adding command `%s`", command_id) + vbox = self.glade.get_widget("commands_vbox") + hbox = gtk.HBox(False, 5) + hbox.set_name(command_id + "_" + event) + label = gtk.Label(EVENT_MAP[event]) + entry = gtk.Entry() + entry.set_text(command) + button = gtk.Button() + button.set_name("remove_%s" % command_id) + button.connect("clicked", self.on_remove_button_clicked) + + img = gtk.Image() + img.set_from_stock(gtk.STOCK_REMOVE, gtk.ICON_SIZE_BUTTON) + button.set_image(img) + + hbox.pack_start(label, False, False) + hbox.pack_start(entry) + hbox.pack_start(button, False, False) + hbox.show_all() + vbox.pack_start(hbox) + + def remove_command(self, command_id): + vbox = self.glade.get_widget("commands_vbox") + children = vbox.get_children() + for child in children: + if child.get_name().split("_")[0] == command_id: + vbox.remove(child) + break + + def clear_commands(self): + vbox = self.glade.get_widget("commands_vbox") + children = vbox.get_children() + for child in children: + vbox.remove(child) + def load_commands(self): def on_get_commands(commands): - vbox = self.glade.get_widget("commands_vbox") + self.clear_commands() + log.debug("on_get_commands: %s", commands) for command in commands: command_id, event, command = command - log.debug("Adding command `%s`", command_id) - hbox = gtk.HBox(False, 0) - label = gtk.Label(EVENT_MAP[event]) - entry = gtk.Entry() - entry.set_text(command) - hbox.pack_start(label, padding = 5) - hbox.pack_start(entry) - vbox.pack_start(hbox) - hbox.show_all() + self.add_command(command_id, event, command) + client.execute.get_commands().addCallback(on_get_commands) - + def on_add_button_clicked(self, *args): command = self.glade.get_widget("command_entry").get_text() events = self.glade.get_widget("event_combobox") event = events.get_model()[events.get_active()][1] client.execute.add_command(event, command) + def on_remove_button_clicked(self, widget, *args): + command_id = widget.get_name().replace("remove_", "") + client.execute.remove_command(command_id) + def on_apply_prefs(self): - pass - -class GtkUI(GtkPluginBase): - + vbox = self.glade.get_widget("commands_vbox") + children = vbox.get_children() + for child in children: + command_id, event = child.get_name().split("_") + for widget in child.get_children(): + if type(widget) == gtk.Entry: + command = widget.get_text() + client.execute.save_command(command_id, event, command) + + def on_command_added_event(self, command_id, event, command): + log.debug("Adding command %s: %s", event, command) + self.add_command(command_id, event, command) + + def on_command_removed_event(self, command_id): + log.debug("Removing command %s", command_id) + self.remove_command(command_id) + +class GtkUI(GtkPluginBase): + def enable(self): self.plugin = component.get("PluginManager") self.preferences = ExecutePreferences(self.plugin)