4
# Copyright (C) 2009 Damien Churchill <damoxc@gmail.com>
6
# Deluge is free software.
8
# You may redistribute it and/or modify it under the terms of the
9
# GNU General Public License, as published by the Free Software
10
# Foundation; either version 3 of the License, or (at your option)
13
# deluge is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
# See the GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with deluge. If not, write to:
20
# The Free Software Foundation, Inc.,
21
# 51 Franklin Street, Fifth Floor
22
# Boston, MA 02110-1301, USA.
24
# In addition, as a special exception, the copyright holders give
25
# permission to link the code of portions of this program with the OpenSSL
27
# You must obey the GNU General Public License in all respects for all of
28
# the code used other than OpenSSL. If you modify file(s) with this
29
# exception, you may extend this exception to your version of the file(s),
30
# but you are not obligated to do so. If you do not wish to do so, delete
31
# this exception statement from your version. If you delete this exception
32
# statement from all source files in the program, then also delete it here.
40
from deluge.log import LOG as log
41
from deluge.ui.client import client
42
from deluge.plugins.pluginbase import GtkPluginBase
43
import deluge.component as component
51
"complete": _("Torrent Complete"),
52
"added": _("Torrent Added")
55
EVENTS = ["complete", "added"]
57
class ExecutePreferences(object):
58
def __init__(self, plugin):
62
log.debug("Adding Execute Preferences page")
63
self.glade = gtk.glade.XML(self.get_resource("execute_prefs.glade"))
64
self.glade.signal_autoconnect({
65
"on_add_button_clicked": self.on_add_button_clicked
68
events = self.glade.get_widget("event_combobox")
70
store = gtk.ListStore(str, str)
72
event_label = EVENT_MAP[event]
73
store.append((event_label, event))
74
events.set_model(store)
77
self.plugin.add_preferences_page(_("Execute"),
78
self.glade.get_widget("execute_box"))
79
self.plugin.register_hook("on_show_prefs", self.load_commands)
80
self.plugin.register_hook("on_apply_prefs", self.on_apply_prefs)
84
client.register_event_handler("ExecuteCommandAddedEvent", self.on_command_added_event)
85
client.register_event_handler("ExecuteCommandRemovedEvent", self.on_command_removed_event)
88
self.plugin.remove_preferences_page(_("Execute"))
89
self.plugin.deregister_hook("on_apply_prefs", self.on_apply_prefs)
90
self.plugin.deregister_hook("on_show_prefs", self.load_commands)
92
def get_resource(self, filename):
93
return pkg_resources.resource_filename("execute", os.path.join("data",
96
def add_command(self, command_id, event, command):
97
log.debug("Adding command `%s`", command_id)
98
vbox = self.glade.get_widget("commands_vbox")
99
hbox = gtk.HBox(False, 5)
100
hbox.set_name(command_id + "_" + event)
101
label = gtk.Label(EVENT_MAP[event])
103
entry.set_text(command)
104
button = gtk.Button()
105
button.set_name("remove_%s" % command_id)
106
button.connect("clicked", self.on_remove_button_clicked)
109
img.set_from_stock(gtk.STOCK_REMOVE, gtk.ICON_SIZE_BUTTON)
110
button.set_image(img)
112
hbox.pack_start(label, False, False)
113
hbox.pack_start(entry)
114
hbox.pack_start(button, False, False)
116
vbox.pack_start(hbox)
118
def remove_command(self, command_id):
119
vbox = self.glade.get_widget("commands_vbox")
120
children = vbox.get_children()
121
for child in children:
122
if child.get_name().split("_")[0] == command_id:
126
def clear_commands(self):
127
vbox = self.glade.get_widget("commands_vbox")
128
children = vbox.get_children()
129
for child in children:
132
def load_commands(self):
133
def on_get_commands(commands):
134
self.clear_commands()
135
log.debug("on_get_commands: %s", commands)
136
for command in commands:
137
command_id, event, command = command
138
self.add_command(command_id, event, command)
140
client.execute.get_commands().addCallback(on_get_commands)
142
def on_add_button_clicked(self, *args):
143
command = self.glade.get_widget("command_entry").get_text()
144
events = self.glade.get_widget("event_combobox")
145
event = events.get_model()[events.get_active()][1]
146
client.execute.add_command(event, command)
148
def on_remove_button_clicked(self, widget, *args):
149
command_id = widget.get_name().replace("remove_", "")
150
client.execute.remove_command(command_id)
152
def on_apply_prefs(self):
153
vbox = self.glade.get_widget("commands_vbox")
154
children = vbox.get_children()
155
for child in children:
156
command_id, event = child.get_name().split("_")
157
for widget in child.get_children():
158
if type(widget) == gtk.Entry:
159
command = widget.get_text()
160
client.execute.save_command(command_id, event, command)
162
def on_command_added_event(self, command_id, event, command):
163
log.debug("Adding command %s: %s", event, command)
164
self.add_command(command_id, event, command)
166
def on_command_removed_event(self, command_id):
167
log.debug("Removing command %s", command_id)
168
self.remove_command(command_id)
170
class GtkUI(GtkPluginBase):
173
self.plugin = component.get("PluginManager")
174
self.preferences = ExecutePreferences(self.plugin)
175
self.preferences.load()
178
self.preferences.unload()