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 import deluge.component as component
from deluge.configmanager import ConfigManager from deluge.configmanager import ConfigManager
from deluge.core.rpcserver import export from deluge.core.rpcserver import export
from deluge.event import DelugeEvent
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
"commands": [] "commands": []
@ -52,6 +53,21 @@ EXECUTE_ID = 0
EXECUTE_EVENT = 1 EXECUTE_EVENT = 1
EXECUTE_COMMAND = 2 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): class Core(CorePluginBase):
def enable(self): def enable(self):
self.config = ConfigManager("execute.conf", DEFAULT_CONFIG) self.config = ConfigManager("execute.conf", DEFAULT_CONFIG)
@ -90,7 +106,7 @@ class Core(CorePluginBase):
def add_command(self, event, command): def add_command(self, event, command):
command_id = hashlib.sha1(str(time.time())).hexdigest() command_id = hashlib.sha1(str(time.time())).hexdigest()
self.config["commands"].append((command_id, event, command)) self.config["commands"].append((command_id, event, command))
self.config.save() component.get("EventManager").emit(ExecuteCommandAddedEvent(command_id, event, command))
@export @export
def get_commands(self): def get_commands(self):
@ -101,14 +117,13 @@ class Core(CorePluginBase):
for command in self.config["commands"]: for command in self.config["commands"]:
if command[EXECUTE_ID] == command_id: if command[EXECUTE_ID] == command_id:
self.config["commands"].remove(command) self.config["commands"].remove(command)
component.get("EventManager").emit(ExecuteCommandRemovedEvent(command_id))
break break
self.config.save()
@export @export
def save_command(self, command_id, event, command): def save_command(self, command_id, event, cmd):
for command in self.config["commands"]: for command in self.config["commands"]:
if command[EXECUTE_ID] == command_id: if command[EXECUTE_ID] == command_id:
command[EXECUTE_EVENT] = event command[EXECUTE_EVENT] = event
command[EXECUTE_COMMAND] = event command[EXECUTE_COMMAND] = cmd
break break
self.config.save()

View File

@ -6,6 +6,121 @@
<child> <child>
<widget class="GtkVBox" id="execute_box"> <widget class="GtkVBox" id="execute_box">
<property name="visible">True</property> <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> <child>
<widget class="GtkFrame" id="commands_frame"> <widget class="GtkFrame" id="commands_frame">
<property name="visible">True</property> <property name="visible">True</property>
@ -14,10 +129,13 @@
<child> <child>
<widget class="GtkAlignment" id="commands_alignment"> <widget class="GtkAlignment" id="commands_alignment">
<property name="visible">True</property> <property name="visible">True</property>
<property name="top_padding">5</property>
<property name="left_padding">12</property> <property name="left_padding">12</property>
<child> <child>
<widget class="GtkVBox" id="commands_vbox"> <widget class="GtkVBox" id="commands_vbox">
<property name="visible">True</property> <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> <child>
<placeholder/> <placeholder/>
</child> </child>
@ -37,117 +155,6 @@
</child> </child>
</widget> </widget>
<packing> <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> <property name="position">1</property>
</packing> </packing>
</child> </child>

View File

@ -72,12 +72,18 @@ class ExecutePreferences(object):
event_label = EVENT_MAP[event] event_label = EVENT_MAP[event]
store.append((event_label, event)) store.append((event_label, event))
events.set_model(store) events.set_model(store)
events.set_active(0)
self.plugin.add_preferences_page(_("Execute"), self.plugin.add_preferences_page(_("Execute"),
self.glade.get_widget("execute_box")) self.glade.get_widget("execute_box"))
self.plugin.register_hook("on_show_prefs", self.load_commands) self.plugin.register_hook("on_show_prefs", self.load_commands)
self.plugin.register_hook("on_apply_prefs", self.on_apply_prefs) 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): def unload(self):
self.plugin.remove_preferences_page(_("Execute")) self.plugin.remove_preferences_page(_("Execute"))
self.plugin.deregister_hook("on_apply_prefs", self.on_apply_prefs) self.plugin.deregister_hook("on_apply_prefs", self.on_apply_prefs)
@ -87,20 +93,50 @@ class ExecutePreferences(object):
return pkg_resources.resource_filename("execute", os.path.join("data", return pkg_resources.resource_filename("execute", os.path.join("data",
filename)) filename))
def load_commands(self): def add_command(self, command_id, event, command):
def on_get_commands(commands):
vbox = self.glade.get_widget("commands_vbox")
for command in commands:
command_id, event, command = command
log.debug("Adding command `%s`", command_id) log.debug("Adding command `%s`", command_id)
hbox = gtk.HBox(False, 0) vbox = self.glade.get_widget("commands_vbox")
hbox = gtk.HBox(False, 5)
hbox.set_name(command_id + "_" + event)
label = gtk.Label(EVENT_MAP[event]) label = gtk.Label(EVENT_MAP[event])
entry = gtk.Entry() entry = gtk.Entry()
entry.set_text(command) entry.set_text(command)
hbox.pack_start(label, padding = 5) 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(entry)
vbox.pack_start(hbox) hbox.pack_start(button, False, False)
hbox.show_all() 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):
self.clear_commands()
log.debug("on_get_commands: %s", commands)
for command in commands:
command_id, event, command = command
self.add_command(command_id, event, command)
client.execute.get_commands().addCallback(on_get_commands) client.execute.get_commands().addCallback(on_get_commands)
def on_add_button_clicked(self, *args): def on_add_button_clicked(self, *args):
@ -109,8 +145,27 @@ class ExecutePreferences(object):
event = events.get_model()[events.get_active()][1] event = events.get_model()[events.get_active()][1]
client.execute.add_command(event, command) 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): def on_apply_prefs(self):
pass 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): class GtkUI(GtkPluginBase):