Created
September 18, 2010 04:44
-
-
Save magcius/585366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# main.py | |
# | |
# Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com> | |
# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com> | |
# | |
# Deluge is free software. | |
# | |
# You may redistribute it and/or modify it under the terms of the | |
# GNU General Public License, as published by the Free Software | |
# Foundation; either version 3 of the License, or (at your option) | |
# any later version. | |
# | |
# deluge is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
# See the GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with deluge. If not, write to: | |
# The Free Software Foundation, Inc., | |
# 51 Franklin Street, Fifth Floor | |
# Boston, MA 02110-1301, USA. | |
# | |
# In addition, as a special exception, the copyright holders give | |
# permission to link the code of portions of this program with the OpenSSL | |
# library. | |
# You must obey the GNU General Public License in all respects for all of | |
# the code used other than OpenSSL. If you modify file(s) with this | |
# exception, you may extend this exception to your version of the file(s), | |
# but you are not obligated to do so. If you do not wish to do so, delete | |
# this exception statement from your version. If you delete this exception | |
# statement from all source files in the program, then also delete it here. | |
# | |
# | |
import urwid | |
from twisted.internet import defer, reactor | |
from deluge import component | |
from deluge.ui.console import colors | |
from deluge.ui.urwid import UI_PATH | |
from deluge.ui.client import client | |
from deluge.ui.ui import _UI | |
class Urwid(_UI): | |
help = """Starts the Deluge urwid interface""" | |
def __init__(self): | |
super(Urwid, self).__init__("urwid") | |
def start(self): | |
super(Urwid, self).start() | |
UrwidUI(self.args) | |
def start(): | |
Urwid().start() | |
class FilteredListWalker(urwid.SimpleListWalker): | |
def _get_widget(self, object, pos): | |
return object, pos | |
def get_focus(self): | |
object, pos = super(FilteredListWalker, self).get_focus() | |
return self._get_widget(object, pos) | |
def get_next(self, pos): | |
object, pos = super(FilteredListWalker, self).get_next(pos) | |
return self._get_widget(object, pos) | |
def get_prev(self, pos): | |
object, pos = super(FilteredListWalker, self).get_prev(pos) | |
return self._get_widget(object, pos) | |
class TorrentListWalker(FilteredListWalker): | |
def _get_widget(self, object, pos): | |
if object: | |
return urwid.Text(object["name"]), pos | |
return object, pos | |
class UrwidUI(component.Component): | |
def __init__(self, args=None): | |
component.Component.__init__(self, "UrwidUI") | |
def on_connect(result): | |
component.start() | |
client.connect().addCallback(on_connect) | |
self.torrents = TorrentListWalker([]) | |
listbox = urwid.ListBox(self.torrents) | |
root = urwid.Frame(listbox) | |
self.loop = urwid.MainLoop(root, event_loop=urwid.TwistedEventLoop(), unhandled_input=self.exit_on_q) | |
self.loop.run() | |
def exit_on_q(self, input): | |
if input in ('q', 'Q'): | |
raise urwid.ExitMainLoop | |
def start(self): | |
# This gets fired once we have received the torrents list from the core | |
self.started_deferred = defer.Deferred() | |
def on_session_state(result): | |
def on_torrents_status(torrents): | |
for torrent_id, status in torrents.items(): | |
status["torrent_id"] = torrent_id | |
self.torrents.append(status) | |
self.started_deferred.callback(True) | |
client.core.get_torrents_status({"id": result}, ["name"]).addCallback(on_torrents_status) | |
client.core.get_session_state().addCallback(on_session_state) | |
# Register some event handlers to keep the torrent list up-to-date | |
client.register_event_handler("TorrentAddedEvent", self.on_torrent_added_event) | |
client.register_event_handler("TorrentRemovedEvent", self.on_torrent_removed_event) | |
def on_torrent_added_event(self, torrent_id): | |
def on_torrent_status(status): | |
self.torrents.append((torrent_id, status["name"])) | |
client.core.get_torrent_status(torrent_id, ["name"]).addCallback(on_torrent_status) | |
self.loop.draw_screen() | |
def on_torrent_removed_event(self, torrent_id): | |
for index, (tid, name) in enumerate(self.torrents): | |
if torrent_id == tid: | |
del self.torrents[index] | |
if __name__ == "__main__": | |
start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment