From 980d1aa09aea62d03ab3441ab2f9a2986fcd920d Mon Sep 17 00:00:00 2001 From: Sadrul Habib Chowdhury Date: Tue, 23 Oct 2007 08:25:24 -0400 Subject: [PATCH] Some code refactoring, and some minor UI changes. Created a TorList custom tree, which will deal with TorRow's. Bound the buttons to some keystrokes on the window. XXX: Added a signal receiver, which doesn't quite work. --- example/delugent/delugent.py | 227 ++++++++++++++++++++++++++++--------------- example/delugent/signals.py | 84 ++++++++++++++++ 2 files changed, 230 insertions(+), 81 deletions(-) create mode 100644 example/delugent/signals.py diff --git a/example/delugent/delugent.py b/example/delugent/delugent.py index f1377d8..63e6c4f 100755 --- a/example/delugent/delugent.py +++ b/example/delugent/delugent.py @@ -25,8 +25,11 @@ USA """ import deluge.ui.functions as functions +from deluge.log import LOG as log + import gnt import gobject +import signals status_keys = ["state", "name", @@ -109,84 +112,146 @@ class TorRow(gobject.GObject): gobject.type_register(TorRow) -win = gnt.Box(homo = False, vert = True) -win.set_toplevel(True) -win.set_title("Delugent") -win.set_pad(0) -win.set_alignment(gnt.ALIGN_MID) - -list = gnt.Tree() -list.set_property('columns', TorRow.COLS) -list.set_show_title(True) - -titles = {TorRow.COL_NAME : "Name", - TorRow.COL_STATE : "State", - TorRow.COL_SIZE : "Size", - TorRow.COL_PROG : "Progr", - TorRow.COL_SD : "Sd", - TorRow.COL_PR : "Pr", - TorRow.COL_DS : "DL Sp", - TorRow.COL_US : "Up Sp", - TorRow.COL_ETA : "ETA" -} -widths = {TorRow.COL_NAME : 30, - TorRow.COL_STATE : 7, - TorRow.COL_SIZE : 5, - TorRow.COL_PROG : 5, - TorRow.COL_SD : 3, - TorRow.COL_PR : 3, - TorRow.COL_DS : 6, - TorRow.COL_US : 6, - TorRow.COL_ETA : 6 -} - -# Set column titles -for col in titles: - list.set_column_title(col, titles[col]) -# Set column widths and alignments -for col in widths: - list.set_col_width(col, widths[col]) -for col in [TorRow.COL_ETA, TorRow.COL_SD, TorRow.COL_PR, TorRow.COL_DS, TorRow.COL_US, TorRow.COL_SIZE, TorRow.COL_PROG]: - list.set_column_resizable(col, False) -for col in [TorRow.COL_SIZE, TorRow.COL_DS, TorRow.COL_SD, TorRow.COL_PR, TorRow.COL_US, TorRow.COL_PROG]: - list.set_column_is_right_aligned(col, True) - -win.add_widget(list) -width, height = gnt.screen_size() -list.set_size(width, height) - -hbox = gnt.Box(homo = False, vert = False) - -add = gnt.Button("Add") -hbox.add_widget(add) - -rm = gnt.Button("Remove") -hbox.add_widget(rm) - -hbox.add_widget(gnt.Label("|")) - -pause = gnt.Button("Pause") -hbox.add_widget(pause) - -hbox.add_widget(gnt.Label("|")) - -mup = gnt.Button("Up") -hbox.add_widget(mup) - -mdown = gnt.Button("Down") -hbox.add_widget(mdown) - -win.add_widget(hbox) - -win.show() - -# Add the torrents in the list -session_state = functions.get_session_state() -for torrent_id in session_state: - row = TorRow(torrent_id) - list.add_row_after(row, row.info(), None) - -gnt.gnt_main() - -gnt.gnt_quit() - +class TorList(gnt.Tree): + def __init__(self): + gnt.Tree.__init__(self) + self.set_property('columns', TorRow.COLS) + self.set_show_title(True) + + titles = {TorRow.COL_NAME : "Name", + TorRow.COL_STATE : "State", + TorRow.COL_SIZE : "Size", + TorRow.COL_PROG : "Progr", + TorRow.COL_SD : "Sd", + TorRow.COL_PR : "Pr", + TorRow.COL_DS : "DL Sp", + TorRow.COL_US : "Up Sp", + TorRow.COL_ETA : "ETA" + } + widths = {TorRow.COL_NAME : 30, + TorRow.COL_STATE : 7, + TorRow.COL_SIZE : 5, + TorRow.COL_PROG : 5, + TorRow.COL_SD : 3, + TorRow.COL_PR : 3, + TorRow.COL_DS : 6, + TorRow.COL_US : 6, + TorRow.COL_ETA : 6 + } + + # Set column titles + for col in titles: + self.set_column_title(col, titles[col]) + # Set column widths and alignments + for col in widths: + self.set_col_width(col, widths[col]) + for col in [TorRow.COL_ETA, TorRow.COL_SD, TorRow.COL_PR, TorRow.COL_DS, TorRow.COL_US, TorRow.COL_SIZE, TorRow.COL_PROG]: + self.set_column_resizable(col, False) + for col in [TorRow.COL_SIZE, TorRow.COL_DS, TorRow.COL_SD, TorRow.COL_PR, TorRow.COL_US, TorRow.COL_PROG]: + self.set_column_is_right_aligned(col, True) + + def get_row(self, id): + tors = self.get_rows() + for tor in tors: + if tor.id == id: + return tor + return None + + def remove_torrent(self, id): + tor = self.get_row(id) + if tor: self.remove(tor) + + def add_torrent(self, id): + row = TorRow(id) + self.add_row_after(row, row.info(), None) + + def update_torrent(self, id, tor = None): + if not tor: + tor = self.get_row(id) + info = tor.info() + for i in range(0, TorRow.COLS): + self.change_text(tor, i, info[i]) + + def update_all(self): + for tor in self.get_rows(): + self.update_torrent(tor.id, tor) + +gobject.type_register(TorList) + +def main(): + win = gnt.Box(homo = False, vert = True) + win.set_toplevel(True) + win.set_title("Delugent") + win.set_pad(0) + win.set_alignment(gnt.ALIGN_MID) + + list = TorList() + + win.add_widget(list) + width, height = gnt.screen_size() + list.set_size(width, height) + + hbox = gnt.Box(homo = False, vert = False) + + add = gnt.Button("Add") + def add_clicked(b): + def file_selected(widget, path, file): + files = widget.get_selected_multi_files() + log.debug("adding files", files) + functions.add_torrent_file(files) + widget.destroy() + fl = gnt.FileSel() + fl.set_title("Select a Torrent") + fl.set_multi_select(True) + fl.connect("file_selected", file_selected) + fl.show() + + add.connect('activate', add_clicked) + hbox.add_widget(add) + gnt.gnt_util_set_trigger_widget(win, gnt.KEY_INS, add) + + rm = gnt.Button("Remove") + def rm_clicked(b): + tor = list.get_selection_data() + functions.remove_torrent([tor.id]) + rm.connect('activate', rm_clicked) + hbox.add_widget(rm) + gnt.gnt_util_set_trigger_widget(win, gnt.KEY_DEL, rm) + + hbox.add_widget(gnt.Label("|")) + + pause = gnt.Button("Pause") + def pause_clicked(b): + tor = list.get_selection_data() + functions.pause_torrent([tor.id]) + pause.connect('activate', pause_clicked) + hbox.add_widget(pause) + gnt.gnt_util_set_trigger_widget(win, 'p', pause) + + hbox.add_widget(gnt.Label("|")) + + mup = gnt.Button("Up") + hbox.add_widget(mup) + gnt.gnt_util_set_trigger_widget(win, gnt.KEY_CTRL_UP, mup) + + mdown = gnt.Button("Down") + hbox.add_widget(mdown) + gnt.gnt_util_set_trigger_widget(win, gnt.KEY_CTRL_DOWN, mdown) + + win.add_widget(hbox) + + win.show() + + # Add the torrents in the list + session_state = functions.get_session_state() + for torrent_id in session_state: + list.add_torrent(torrent_id) + + signalhandler = signals.Signals(list) + gnt.gnt_main() + + gnt.gnt_quit() + del signalhandler + +if __name__ == "__main__": + main() diff --git a/example/delugent/signals.py b/example/delugent/signals.py new file mode 100644 index 0000000..ab43e4b --- /dev/null +++ b/example/delugent/signals.py @@ -0,0 +1,84 @@ +# +# signals.py +# +# Copyright (C) 2007 Andrew Resch ('andar') +# 2007 Sadrul Habib Chowdhury +# +# Deluge and Delugent are 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 2 of the License, or (at your option) +# any later version. +# +# deluge and delugent 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. + +from deluge.ui.signalreceiver import SignalReceiver +from deluge.log import LOG as log + +class Signals: + def __init__(self, list): + self.torrentlist = list + self.receiver = SignalReceiver(6200, "http://localhost:6666") + self.receiver.start() + self.receiver.connect_to_signal("torrent_added", + self.torrent_added_signal) + self.receiver.connect_to_signal("torrent_removed", + self.torrent_removed_signal) + self.receiver.connect_to_signal("torrent_paused", self.torrent_paused) + self.receiver.connect_to_signal("torrent_resumed", self.torrent_resumed) + self.receiver.connect_to_signal("torrent_all_paused", + self.torrent_all_paused) + self.receiver.connect_to_signal("torrent_all_resumed", + self.torrent_all_resumed) + + def __del__(self): + self.receiver.stop() + del self.receiver + + def torrent_added_signal(self, torrent_id): + log.debug("torrent_added signal received..") + log.debug("torrent id: %s", torrent_id) + # Add the torrent to the treeview + self.torrentlist.add_torrent(torrent_id) + + def torrent_removed_signal(self, torrent_id): + log.debug("torrent_remove signal received..") + log.debug("torrent id: %s", torrent_id) + # Remove the torrent from the treeview + self.torrentlist.remove_torrent(torrent_id) + + def torrent_paused(self, torrent_id): + log.debug("torrent_paused signal received..") + self.torrentlist.update_torrent(torrent_id) + + def torrent_resumed(self, torrent_id): + log.debug("torrent_resumed signal received..") + self.torrentlist.update_torrent(torrent_id) + + def torrent_all_paused(self): + log.debug("torrent_all_paused signal received..") + self.torrentlist.update_all() + + def torrent_all_resumed(self): + log.debug("torrent_all_resumed signal received..") + self.torrentlist.update_all() -- 2.11.4.GIT