Fix up the Execute plugin's gtk code

This commit is contained in:
Andrew Resch 2009-09-09 20:47:12 +00:00
parent 3d4e05b5e0
commit a78ab9529f
3 changed files with 217 additions and 140 deletions

View File

@ -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()

View File

@ -6,6 +6,121 @@
<child>
<widget class="GtkVBox" id="execute_box">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<widget class="GtkFrame" id="add_frame">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<widget class="GtkAlignment" id="add_alignment">
<property name="visible">True</property>
<property name="top_padding">5</property>
<property name="left_padding">12</property>
<child>
<widget class="GtkTable" id="add_table">
<property name="visible">True</property>
<property name="n_rows">3</property>
<property name="n_columns">2</property>
<child>
<widget class="GtkLabel" id="event_label">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Event</property>
</widget>
<packing>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
<property name="x_padding">5</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="command_label">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Command</property>
</widget>
<packing>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
<property name="x_padding">5</property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="command_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
<property name="has_default">True</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkComboBox" id="event_combobox">
<property name="visible">True</property>
<property name="items" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="y_options"></property>
</packing>
</child>
<child>
<widget class="GtkHButtonBox" id="hbuttonbox1">
<property name="visible">True</property>
<property name="layout_style">end</property>
<child>
<widget class="GtkButton" id="button_add">
<property name="label" translatable="yes">gtk-add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="on_add_button_clicked"/>
</widget>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</widget>
<packing>
<property name="right_attach">2</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options"></property>
</packing>
</child>
</widget>
</child>
</widget>
</child>
<child>
<widget class="GtkLabel" id="add_label">
<property name="visible">True</property>
<property name="label" translatable="yes">&lt;b&gt;Add Command&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
<property name="type">label_item</property>
</packing>
</child>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkFrame" id="commands_frame">
<property name="visible">True</property>
@ -14,10 +129,13 @@
<child>
<widget class="GtkAlignment" id="commands_alignment">
<property name="visible">True</property>
<property name="top_padding">5</property>
<property name="left_padding">12</property>
<child>
<widget class="GtkVBox" id="commands_vbox">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
@ -37,117 +155,6 @@
</child>
</widget>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<widget class="GtkFrame" id="add_frame">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<widget class="GtkAlignment" id="add_alignment">
<property name="visible">True</property>
<property name="left_padding">12</property>
<child>
<widget class="GtkTable" id="add_table">
<property name="visible">True</property>
<property name="n_rows">3</property>
<property name="n_columns">2</property>
<child>
<widget class="GtkLabel" id="event_label">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Event</property>
</widget>
<packing>
<property name="x_options">GTK_FILL</property>
<property name="x_padding">5</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="command_label">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Command</property>
</widget>
<packing>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">GTK_FILL</property>
<property name="x_padding">5</property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="command_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkComboBox" id="event_combobox">
<property name="visible">True</property>
<property name="items" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<child>
<widget class="GtkButton" id="add_button">
<property name="label" translatable="yes">Add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_add_button_clicked"/>
</widget>
<packing>
<property name="expand">False</property>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</widget>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</widget>
</child>
</widget>
</child>
<child>
<widget class="GtkLabel" id="add_label">
<property name="visible">True</property>
<property name="label" translatable="yes">&lt;b&gt;Add Command&lt;/b&gt;</property>
<property name="use_markup">True</property>
</widget>
<packing>
<property name="type">label_item</property>
</packing>
</child>
</widget>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>

View File

@ -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)