deluge/delugegtk.py

350 lines
13 KiB
Python
Raw Normal View History

2006-12-08 19:06:49 +00:00
#!/usr/bin/env python
2006-11-28 22:28:37 +00:00
#
# delugegtk.py
#
# Copyright (C) Zach Tibbitts 2006 <zach@collegegeek.org>
2007-01-08 19:38:19 +00:00
#
# This program is free software; you can 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, or (at your option)
# any later version.
#
2007-01-08 19:38:19 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2007-01-08 19:38:19 +00:00
# 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
2007-01-08 19:38:19 +00:00
# along with this program. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
2006-11-28 22:28:37 +00:00
2007-01-12 20:07:55 +00:00
import sys, os, os.path, gettext
2007-01-11 21:53:25 +00:00
import deluge, dcommon, dgtk
2006-11-28 22:28:37 +00:00
import pygtk
pygtk.require('2.0')
2007-01-11 21:53:25 +00:00
import gtk, gtk.glade, gobject
2007-01-11 00:58:23 +00:00
import xdg, xdg.BaseDirectory
2007-01-12 20:07:55 +00:00
import dbus, dbus.service
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
import dbus.glib
2006-11-28 22:28:37 +00:00
2007-01-12 20:07:55 +00:00
class DelugeGTK(dbus.service.Object):
def __init__(self, bus_name=dbus.service.BusName('org.deluge_torrent.Deluge',
bus=dbus.SessionBus()), object_path='/org/deluge_torrent/DelugeObject'):
dbus.service.Object.__init__(self, bus_name, object_path)
self.is_running = False
self.torrent_file_queue = []
2007-01-11 00:58:23 +00:00
#Start the Deluge Manager:
2007-01-11 21:53:25 +00:00
self.manager = deluge.Manager("DE", "0500", "Deluge 0.5.0",
2007-01-11 00:58:23 +00:00
os.path.expanduser("~") + "/Temp")
#xdg.BaseDirectory.save_config_path("deluge-svn"))
#Set up the interface:
2006-11-28 22:28:37 +00:00
self.gladefile = dcommon.get_glade_file("delugegtk.glade")
self.wtree = gtk.glade.XML(self.gladefile)
self.window = self.wtree.get_widget("main_window")
2007-01-12 20:07:55 +00:00
self.window.hide()
2006-12-22 23:24:42 +00:00
self.toolbar = self.wtree.get_widget("tb_middle")
2006-11-28 22:28:37 +00:00
if(self.window):
2007-01-11 21:53:25 +00:00
self.window.connect("destroy", self.quit)
2006-11-30 11:01:29 +00:00
self.window.set_title(dcommon.PROGRAM_NAME + " " + dcommon.PROGRAM_VERSION)
2006-11-28 22:28:37 +00:00
self.window.set_icon_from_file(dcommon.get_pixmap("deluge32.png"))
2006-12-08 19:02:22 +00:00
## Create the system tray icon
self.tray = dgtk.TrayIcon(self)
## Create the about dialog
self.abt = dgtk.AboutDialog()
## Create the preferences dialog
2006-12-08 20:26:35 +00:00
self.prf = dgtk.PreferencesDialog()
2006-12-08 19:02:22 +00:00
2007-01-12 20:07:55 +00:00
self.wtree.signal_autoconnect({
2006-11-28 22:28:37 +00:00
## File Menu
2007-01-12 20:07:55 +00:00
"new_torrent": self.new_torrent_clicked,
"add_torrent": self.add_torrent_clicked,
"remove_torrent" : self.remove_torrent_clicked,
2006-12-22 23:24:42 +00:00
"menu_quit": self.quit,
## Edit Menu
2006-12-08 20:26:35 +00:00
"pref_clicked": self.prf.show_pref,
"plugins_clicked": self.prf.show_plugins,
2007-01-11 21:53:25 +00:00
## View Menu
2007-01-12 20:40:56 +00:00
"infopane_toggle": self.infopane_toggle,
2007-01-11 21:53:25 +00:00
"size_toggle": self.size_toggle,
"status_toggle": self.status_toggle,
"seeders_toggle": self.seeders_toggle,
"peers_toggle": self.peers_toggle,
"dl_toggle": self.dl_toggle,
"ul_toggle": self.ul_toggle,
"eta_toggle": self.eta_toggle,
"share_toggle": self.share_toggle,
2006-11-28 22:28:37 +00:00
## Help Menu
2006-12-08 19:02:22 +00:00
"show_about_dialog": self.abt.show,
2007-01-12 20:07:55 +00:00
## Toolbar
"update_tracker": self.update_tracker,
2007-01-11 21:53:25 +00:00
## Other events
"torrentrow_click": self.torrentview_clicked,
2007-01-12 20:07:55 +00:00
})
2006-11-28 22:28:37 +00:00
## Create the torrent listview
2006-12-22 23:24:42 +00:00
self.view = self.wtree.get_widget("torrent_view")
# UID, Q#, Name, Size, Progress, Message, Seeders, Peers, DL, UL, ETA, Share
self.store = gtk.ListStore(int, int, str, str, int, str, str, str, str, str, str, str)
self.view.set_model(self.store)
2006-12-23 03:30:25 +00:00
self.view.set_rules_hint(True)
2006-12-08 19:02:22 +00:00
2006-12-24 04:08:34 +00:00
## Initializes the columns for the torrent_view
2007-01-12 20:07:55 +00:00
# Just found out there are built-in pygtk methods with similar functionality
# to these, perhaps I should look into using those.
2006-12-22 23:24:42 +00:00
self.queue_column = dgtk.add_text_column(self.view, "#", 1)
self.name_column = dgtk.add_text_column(self.view, "Name", 2)
self.size_column = dgtk.add_text_column(self.view, "Size", 3)
self.status_column = dgtk.add_progress_column(self.view, "Status", 4, 5)
self.seed_column = dgtk.add_text_column(self.view, "Seeders", 6)
self.peer_column = dgtk.add_text_column(self.view, "Peers", 7)
self.dl_column = dgtk.add_text_column(self.view, "Download", 8)
self.ul_column = dgtk.add_text_column(self.view, "Upload", 9)
2006-12-23 04:35:06 +00:00
self.eta_column = dgtk.add_text_column(self.view, "ETA", 10)
2006-12-22 23:24:42 +00:00
self.share_column = dgtk.add_text_column(self.view, "Share Ratio", 11)
2006-11-28 22:28:37 +00:00
2006-12-24 04:08:34 +00:00
self.status_column.set_expand(True)
self.file_view = self.wtree.get_widget("file_view")
self.file_store = gtk.ListStore(str, bool)
self.file_view.set_model(self.file_store)
self.filename_column = dgtk.add_text_column(self.file_view, "Filename", 0)
self.filetoggle_column = dgtk.add_toggle_column(self.file_view, "DL?", 0)
self.filename_column.set_expand(True)
2007-01-07 22:50:26 +00:00
## Should probably use rules-hint for other treevies as well
2006-12-24 04:08:34 +00:00
self.peer_view = self.wtree.get_widget("peer_view")
self.peer_store = gtk.ListStore(str, str, str, str, str)
self.peer_view.set_model(self.peer_store)
self.peer_ip_column = dgtk.add_text_column(self.peer_view, "IP Address", 0)
self.peer_client_column = dgtk.add_text_column(self.peer_view, "Client", 1)
## Note: change this column to use a progress column before 0.5 is released
self.peer_complete_column = dgtk.add_text_column(self.peer_view, "Percent Complete", 2)
self.peer_download_column = dgtk.add_text_column(self.peer_view, "Download Rate", 3)
self.peer_upload_column = dgtk.add_text_column(self.peer_view, "Upload Rate", 4)
2007-01-11 23:15:22 +00:00
#Torrent Summary tab
2007-01-12 20:07:55 +00:00
# Look into glade's widget prefix function
2007-01-11 23:15:22 +00:00
self.text_summary_title = self.wtree.get_widget("summary_title")
self.text_summary_total_size = self.wtree.get_widget("summary_total_size")
self.text_summary_pieces = self.wtree.get_widget("summary_pieces")
self.text_summary_total_downloaded = self.wtree.get_widget("summary_total_downloaded")
self.text_summary_total_uploaded = self.wtree.get_widget("summary_total_uploaded")
self.text_summary_download_rate = self.wtree.get_widget("summary_download_rate")
self.text_summary_upload_rate = self.wtree.get_widget("summary_upload_rate")
self.text_summary_percentage_done = self.wtree.get_widget("summary_percentage_done")
self.text_summary_share_ratio = self.wtree.get_widget("summary_share_ratio")
self.text_summary_downloaded_this_session = self.wtree.get_widget("summary_downloaded_this_session")
self.text_summary_uplodaded_this_session = self.wtree.get_widget("summary_uploaded_this_session")
self.text_summary_tracker = self.wtree.get_widget("summary_tracker")
self.text_summary_tracker_response = self.wtree.get_widget("summary_tracker_response")
self.text_summary_tacker_status = self.wtree.get_widget("summary_tracker_status")
self.text_summary_next_announce = self.wtree.get_widget("summary_next_announce")
self.text_summary_compact_allocation = self.wtree.get_widget("summary_compact_allocation")
self.text_summary_eta = self.wtree.get_widget("summary_eta")
2007-01-11 00:58:23 +00:00
## Interface created
2007-01-12 20:07:55 +00:00
## external_add_torrent should only be called from outside the class
@dbus.service.method('org.deluge_torrent.DelugeInterface')
def external_add_torrent(self, torrent_file):
print "Ding!"
print "Got torrent externally:", os.path.basename(torrent_file)
print "\tNow, what to do with it?"
if self.is_running:
print "\t\tthe client seems to already be running, i'll try and add the torrent"
uid = self.manager.add_torrent(torrent_file, ".", True)
2007-01-11 21:53:25 +00:00
self.store.append(self.get_list_from_unique_id(uid))
2007-01-12 20:07:55 +00:00
else:
print "\t\tthe client hasn't started yet, I'll queue the torrent"
self.torrent_file_queue.append(torrent_file)
2006-12-24 04:08:34 +00:00
2007-01-11 00:58:23 +00:00
## Start the timer that updates the interface
2007-01-12 20:07:55 +00:00
def start(self, hidden=False):
if not hidden:
self.window.show()
# go through torrent files to add
#dummy preferences values:
use_default_download_location = True
default_download_location = "."
for torrent_file in self.torrent_file_queue:
print "adding torrent", torrent_file
try:
self.manager.add_torrent(torrent_file, ".", True)
except deluge.DelugeError:
print "duplicate torrent found, ignoring", torrent_file
## add torrents in manager to interface
for uid in self.manager.get_unique_IDs():
self.store.append(self.get_list_from_unique_id(uid))
2007-01-11 00:58:23 +00:00
gobject.timeout_add(1000, self.update)
2007-01-12 20:07:55 +00:00
try:
self.is_running = True
gtk.main()
except KeyboardInterrupt:
self.manager.quit()
2007-01-11 00:58:23 +00:00
## Call via a timer to update the interface
def update(self):
2007-01-11 21:53:25 +00:00
# Make sure that the interface still exists
try:
2007-01-11 05:01:11 +00:00
tab = self.wtree.get_widget("torrent_info").get_current_page()
2007-01-11 21:53:25 +00:00
except AttributeError:
return False
2007-01-11 23:15:22 +00:00
# If no torrent is selected, select the first torrent:
(temp, selection) = self.view.get_selection().get_selected()
if selection is None:
self.view.get_selection().select_path("0")
2007-01-12 20:40:56 +00:00
#Torrent List
itr = self.store.get_iter_first()
if itr is None:
return True
while itr is not None:
uid = self.store.get_value(itr, 0)
try:
state = self.manager.get_torrent_state(uid)
tlist = self.get_list_from_unique_id(uid)
for i in range(12):
self.store.set_value(itr, i, tlist[i])
itr = self.store.iter_next(itr)
except deluge.InvalidUniqueIDError:
self.store.remove(itr)
if not self.store.iter_is_valid(itr):
itr = None
if tab == 0: #Details Pane
2007-01-11 23:15:22 +00:00
state = self.manager.get_torrent_state(self.get_selected_torrent())
self.text_summary_title.set_text(str(state["name"]))
self.text_summary_total_size.set_text(str(state["total_size"]))
self.text_summary_pieces.set_text(str(state["pieces"]))
self.text_summary_total_downloaded.set_text(str(state["total_download"]))
self.text_summary_total_uploaded.set_text(str(state["total_upload"]))
self.text_summary_download_rate.set_text(str(state["download_rate"]))
self.text_summary_upload_rate.set_text(str(state["upload_rate"]))
self.text_summary_percentage_done.set_text(str(state["progress"]))
self.text_summary_share_ratio.set_text(str(self.calc_share_ratio(state)))
#self.text_summary_downloaded_this_session.set_text(str(state[""]))
#self.text_summary_uplodaded_this_session.set_text(str(state[""]))
self.text_summary_tracker.set_text(str(state["tracker"]))
#self.text_summary_tracker_response.set_text(str(state[""]))
self.text_summary_tacker_status.set_text(str(state["tracker_ok"]))
self.text_summary_next_announce.set_text(str(state["next_announce"]))
#self.text_summary_compact_allocation.set_text(str(state[""]))
#self.text_summary_eta.set_text(str(state[""]))
2007-01-12 20:40:56 +00:00
elif tab == 1: #Peers List
2007-01-11 23:15:22 +00:00
uid = self.get_selected_torrent()
self.peer_store.clear()
peer_data = self.manager.get_torrent_peer_info(uid)
for peer in peer_data:
# ip client percent dl ul
self.peer_store.append([peer["ip"], peer["client"], peer["peer_has"],
peer["download_speed"], peer["upload_speed"]])
2007-01-12 20:40:56 +00:00
elif tab == 2: #File List
2007-01-11 21:53:25 +00:00
pass
else:
pass
2007-01-11 00:58:23 +00:00
return True
2007-01-11 23:15:22 +00:00
def calc_share_ratio(self, torrent_state):
if torrent_state["total_upload"] == 0:
return 0
elif torrent_state["total_download"] == 0:
return "infinite"
else:
ratio = float(torrent_state["total_upload"]) / float(torrent_state["total_download"])
return ratio
2007-01-11 05:01:11 +00:00
def get_selected_torrent(self):
return self.store.get_value(self.view.get_selection().get_selected()[1], 0)
2007-01-11 00:58:23 +00:00
# UID, Q#, Name, Size, Progress, Message, Seeders, Peers, DL, UL, ETA, Share
2007-01-11 21:53:25 +00:00
def get_list_from_unique_id(self, unique_id):
2007-01-11 00:58:23 +00:00
state = self.manager.get_torrent_state(unique_id)
2007-01-11 05:01:11 +00:00
queue = int(state['queue_pos']) + 1
name = state['name']
size = state['total_size']
progress = int(state['progress'] * 100)
message = deluge.STATE_MESSAGES[state['state']]
seeds = str(state['num_seeds']) + " (" + str(state['total_seeds']) + ")"
peers = str(state['num_peers']) + " (" + str(state['total_peers']) + ")"
dlrate = state['download_rate']
ulrate = state['upload_rate']
eta = "NULL"
share = "NULL"
return [unique_id, queue, name, size, progress, message,
seeds, peers, dlrate, ulrate, eta, share]
2006-12-22 23:24:42 +00:00
2007-01-12 20:07:55 +00:00
def new_torrent_clicked(self, obj=None):
2006-11-28 22:28:37 +00:00
pass
2007-01-12 20:07:55 +00:00
def add_torrent_clicked(self, obj=None):
2007-01-11 00:58:23 +00:00
torrent = dgtk.show_file_open_dialog()
if torrent is not None:
uid = self.manager.add_torrent(torrent, ".", True)
2007-01-11 21:53:25 +00:00
self.store.append(self.get_list_from_unique_id(uid))
2007-01-11 05:01:11 +00:00
2007-01-12 20:07:55 +00:00
def remove_torrent_clicked(self, obj=None):
2007-01-11 05:01:11 +00:00
self.manager.remove_torrent(self.get_selected_torrent(), False)
2006-12-22 23:24:42 +00:00
2007-01-12 20:07:55 +00:00
def update_tracker(self, obj=None):
self.manager.update_tracker(get_selected_torrent())
2007-01-11 21:53:25 +00:00
def torrentview_clicked(self, widget, event):
pass
2007-01-12 20:40:56 +00:00
def infopane_toggle(self, widget):
if widget.get_active():
self.wtree.get_widget("torrent_info").show()
else:
self.wtree.get_widget("torrent_info").hide()
2007-01-11 21:53:25 +00:00
def size_toggle(self, obj):
self.size_column.set_visible(obj.get_active())
def status_toggle(self, obj):
self.status_column.set_visible(obj.get_active())
def seeders_toggle(self, obj):
self.seed_column.set_visible(obj.get_active())
def peers_toggle(self, obj):
self.peer_column.set_visible(obj.get_active())
def dl_toggle(self, obj):
self.dl_column.set_visible(obj.get_active())
def ul_toggle(self, obj):
self.ul_column.set_visible(obj.get_active())
def eta_toggle(self, obj):
self.eta_column.set_visible(obj.get_active())
def share_toggle(self, obj):
self.share_column.set_visible(obj.get_active())
2006-12-22 23:24:42 +00:00
def quit(self, obj=None):
2007-01-11 00:58:23 +00:00
self.manager.quit()
2007-01-11 21:53:25 +00:00
gtk.main_quit()
2006-12-22 23:24:42 +00:00
2007-01-11 00:58:23 +00:00
2006-12-04 23:59:45 +00:00
2006-12-08 20:26:35 +00:00
## For testing purposes, create a copy of the interface
2006-11-28 22:28:37 +00:00
if __name__ == "__main__":
2007-01-11 00:58:23 +00:00
interface = DelugeGTK()
2007-01-12 20:07:55 +00:00
interface.start()