~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

Viewing changes to deluge/plugins/execute/execute/gtkui.py

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-11-13 02:39:45 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091113023945-te1bybo2912ejzuc
Tags: 1.2.0~rc3-4
* debian/control: bump build-dep on python-setuptools to (>= 0.6c9).
* debian/patches:
  - 25_r5921_fastresume_files.patch
    new, should fix problems with fresh configs;
  - 30_r5931_ipc_lockfile.patch:
    new, should fix an issue where Deluge will fail to start if there is a
    stale ipc lockfile. (Closes: #555849)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# gtkui.py
 
3
#
 
4
# Copyright (C) 2009 Damien Churchill <damoxc@gmail.com>
 
5
#
 
6
# Deluge is free software.
 
7
#
 
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)
 
11
# any later version.
 
12
#
 
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.
 
17
#
 
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.
 
23
#
 
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
 
26
#    library.
 
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.
 
33
#
 
34
#
 
35
 
 
36
import os
 
37
import gtk
 
38
import pkg_resources
 
39
 
 
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
 
44
import deluge.common
 
45
 
 
46
EXECUTE_ID = 0
 
47
EXECUTE_EVENT = 1
 
48
EXECUTE_COMMAND = 2
 
49
 
 
50
EVENT_MAP = {
 
51
    "complete": _("Torrent Complete"),
 
52
    "added": _("Torrent Added")
 
53
}
 
54
 
 
55
EVENTS = ["complete", "added"]
 
56
 
 
57
class ExecutePreferences(object):
 
58
    def __init__(self, plugin):
 
59
        self.plugin = plugin
 
60
 
 
61
    def load(self):
 
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
 
66
        })
 
67
 
 
68
        events = self.glade.get_widget("event_combobox")
 
69
 
 
70
        store = gtk.ListStore(str, str)
 
71
        for event in EVENTS:
 
72
            event_label = EVENT_MAP[event]
 
73
            store.append((event_label, event))
 
74
        events.set_model(store)
 
75
        events.set_active(0)
 
76
 
 
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)
 
81
 
 
82
        self.load_commands()
 
83
 
 
84
        client.register_event_handler("ExecuteCommandAddedEvent", self.on_command_added_event)
 
85
        client.register_event_handler("ExecuteCommandRemovedEvent", self.on_command_removed_event)
 
86
 
 
87
    def unload(self):
 
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)
 
91
 
 
92
    def get_resource(self, filename):
 
93
        return pkg_resources.resource_filename("execute", os.path.join("data",
 
94
            filename))
 
95
 
 
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])
 
102
        entry = gtk.Entry()
 
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)
 
107
 
 
108
        img = gtk.Image()
 
109
        img.set_from_stock(gtk.STOCK_REMOVE, gtk.ICON_SIZE_BUTTON)
 
110
        button.set_image(img)
 
111
 
 
112
        hbox.pack_start(label, False, False)
 
113
        hbox.pack_start(entry)
 
114
        hbox.pack_start(button, False, False)
 
115
        hbox.show_all()
 
116
        vbox.pack_start(hbox)
 
117
 
 
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:
 
123
                vbox.remove(child)
 
124
                break
 
125
 
 
126
    def clear_commands(self):
 
127
        vbox = self.glade.get_widget("commands_vbox")
 
128
        children = vbox.get_children()
 
129
        for child in children:
 
130
            vbox.remove(child)
 
131
 
 
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)
 
139
 
 
140
        client.execute.get_commands().addCallback(on_get_commands)
 
141
 
 
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)
 
147
 
 
148
    def on_remove_button_clicked(self, widget, *args):
 
149
        command_id = widget.get_name().replace("remove_", "")
 
150
        client.execute.remove_command(command_id)
 
151
 
 
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)
 
161
 
 
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)
 
165
 
 
166
    def on_command_removed_event(self, command_id):
 
167
        log.debug("Removing command %s", command_id)
 
168
        self.remove_command(command_id)
 
169
 
 
170
class GtkUI(GtkPluginBase):
 
171
 
 
172
    def enable(self):
 
173
        self.plugin = component.get("PluginManager")
 
174
        self.preferences = ExecutePreferences(self.plugin)
 
175
        self.preferences.load()
 
176
 
 
177
    def disable(self):
 
178
        self.preferences.unload()