2007-08-04 18:17:21 +00:00
|
|
|
from itertools import izip
|
2007-08-05 04:07:17 +00:00
|
|
|
from deluge import dgtk
|
|
|
|
from deluge import common
|
2007-08-04 18:17:21 +00:00
|
|
|
import gobject
|
|
|
|
import gtk
|
|
|
|
class PeersTabManager(object):
|
|
|
|
def __init__(self, peer_view, manager):
|
|
|
|
self.peer_view = peer_view
|
|
|
|
self.manager = manager
|
2007-08-05 04:07:17 +00:00
|
|
|
self.peer_unique_id = None
|
2007-08-04 18:17:21 +00:00
|
|
|
# IP int, IP string, Client, Percent Complete, Down Speed, Up Speed
|
|
|
|
# IP int is for faster sorting
|
2007-08-04 20:12:31 +00:00
|
|
|
self.peer_store = gtk.ListStore(gobject.TYPE_UINT, gtk.gdk.Pixbuf,
|
|
|
|
str, str, float, int, int)
|
2007-08-04 18:17:21 +00:00
|
|
|
# Stores IP -> gtk.TreeIter's iter mapping for quick look up
|
|
|
|
# in update_torrent_info_widget
|
2007-08-05 04:07:17 +00:00
|
|
|
self.peer_store_dict = {}
|
2007-08-04 20:12:31 +00:00
|
|
|
self._cached_flags = {}
|
2007-08-06 00:40:18 +00:00
|
|
|
self.show_flags = None
|
|
|
|
self.flag_size = ""
|
2007-08-05 04:07:17 +00:00
|
|
|
|
|
|
|
def clear_peer_store(self):
|
|
|
|
self.peer_store.clear()
|
|
|
|
self.peer_store_dict = {}
|
|
|
|
|
|
|
|
def set_unique_id(self, unique_id):
|
|
|
|
self.peer_unique_id = unique_id
|
|
|
|
|
2007-08-04 18:17:21 +00:00
|
|
|
def build_peers_view(self):
|
|
|
|
def percent(column, cell, model, iter, data):
|
|
|
|
percent = float(model.get_value(iter, data))
|
|
|
|
percent_str = "%.2f%%"%percent
|
|
|
|
cell.set_property("text", percent_str)
|
|
|
|
|
|
|
|
self.peer_view.set_model(self.peer_store)
|
|
|
|
|
2007-08-07 17:04:56 +00:00
|
|
|
# self.ip_column is used in self.ip_column_queue_resize() method
|
|
|
|
self.ip_column = dgtk.add_texticon_column(self.peer_view,
|
|
|
|
_("IP Address"), 1, 2)
|
|
|
|
self.ip_column.set_sort_column_id(0)
|
2007-08-04 20:12:31 +00:00
|
|
|
dgtk.add_text_column(self.peer_view, _("Client"), 3)
|
|
|
|
dgtk.add_func_column(self.peer_view, _("Percent Complete"), percent,
|
|
|
|
4)
|
|
|
|
dgtk.add_func_column(self.peer_view, _("Down Speed"),
|
|
|
|
dgtk.cell_data_speed, 5)
|
|
|
|
dgtk.add_func_column(self.peer_view, _("Up Speed"),
|
|
|
|
dgtk.cell_data_speed, 6)
|
2007-08-05 04:07:17 +00:00
|
|
|
|
2007-08-06 00:40:18 +00:00
|
|
|
def enable_flags(self):
|
|
|
|
self.show_flags = True
|
|
|
|
|
|
|
|
def disable_flags(self):
|
|
|
|
self.show_flags = False
|
|
|
|
|
|
|
|
def clear_flag_cache(self):
|
|
|
|
self._cached_flags = {}
|
|
|
|
|
|
|
|
def set_flag_size(self, size):
|
|
|
|
self.flag_size = size
|
2007-08-07 17:04:56 +00:00
|
|
|
|
|
|
|
def ip_column_queue_resize(self):
|
|
|
|
self.ip_column.queue_resize()
|
2007-08-06 00:40:18 +00:00
|
|
|
|
2007-08-04 20:24:02 +00:00
|
|
|
def get_country_flag_image(self, country):
|
|
|
|
flag_image = None
|
2007-08-06 03:52:16 +00:00
|
|
|
if self.show_flags and country.isalpha():
|
2007-08-04 20:12:31 +00:00
|
|
|
if country in self._cached_flags:
|
2007-08-04 20:24:02 +00:00
|
|
|
flag_image = self._cached_flags[country]
|
2007-08-04 20:12:31 +00:00
|
|
|
else:
|
|
|
|
try:
|
2007-08-07 17:04:56 +00:00
|
|
|
flag_path = "flags%s/%s.png" % (self.flag_size,
|
|
|
|
country.lower())
|
2007-08-04 20:24:02 +00:00
|
|
|
flag_image = gtk.gdk.pixbuf_new_from_file(
|
2007-08-06 00:40:18 +00:00
|
|
|
common.get_pixmap(flag_path))
|
2007-08-04 20:12:31 +00:00
|
|
|
except gobject.GError:
|
|
|
|
pass
|
|
|
|
|
2007-08-04 20:24:02 +00:00
|
|
|
self._cached_flags[country] = flag_image
|
2007-08-04 20:12:31 +00:00
|
|
|
|
2007-08-04 20:24:02 +00:00
|
|
|
return flag_image
|
2007-08-05 04:07:17 +00:00
|
|
|
|
|
|
|
def update_peer_store(self):
|
|
|
|
new_peer_info = self.manager.get_torrent_peer_info(self.peer_unique_id)
|
2007-08-04 18:17:21 +00:00
|
|
|
new_ips = set()
|
|
|
|
|
|
|
|
for peer in new_peer_info:
|
|
|
|
# Update peers already in peers list
|
|
|
|
if peer['ip'] in self.peer_store_dict:
|
|
|
|
iter = self.peer_store_dict[peer['ip']]
|
|
|
|
|
2007-08-04 20:12:31 +00:00
|
|
|
dgtk.update_store(self.peer_store, iter, (1, 4, 5, 6),
|
2007-08-04 20:24:02 +00:00
|
|
|
(self.get_country_flag_image(peer["country"]),
|
2007-08-04 20:12:31 +00:00
|
|
|
round(peer["peer_has"], 2),
|
2007-08-04 18:17:21 +00:00
|
|
|
peer["download_speed"],
|
|
|
|
peer["upload_speed"]))
|
|
|
|
|
|
|
|
if peer['client'] != "":
|
|
|
|
new_ips.add(peer['ip'])
|
|
|
|
|
|
|
|
# Add new peers
|
|
|
|
if peer['ip'] not in self.peer_store_dict:
|
|
|
|
# convert IP to int for sorting purposes
|
|
|
|
ip_int = sum([int(byte) << shift
|
|
|
|
for byte, shift in
|
|
|
|
izip(peer["ip"].split("."),
|
|
|
|
(24, 16, 8, 0))])
|
|
|
|
|
|
|
|
client = peer["client"]
|
|
|
|
try:
|
|
|
|
client = client.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
# Fallback to latin-1 in case peer's client
|
|
|
|
# doesn't use utf-8. utorrent < 1.7 for example
|
|
|
|
client = client.decode('latin-1')
|
2007-08-04 20:12:31 +00:00
|
|
|
|
|
|
|
iter = self.peer_store.append([ip_int,
|
2007-08-04 20:24:02 +00:00
|
|
|
self.get_country_flag_image(peer["country"]),
|
2007-08-04 20:12:31 +00:00
|
|
|
peer["ip"], client, round(peer["peer_has"], 2),
|
|
|
|
peer["download_speed"], peer["upload_speed"]])
|
2007-08-04 18:17:21 +00:00
|
|
|
|
|
|
|
self.peer_store_dict[peer['ip']] = iter
|
|
|
|
|
|
|
|
# Remove peers that no longer exist in new_ips
|
|
|
|
for ip in set(self.peer_store_dict.keys()).difference(new_ips):
|
|
|
|
self.peer_store.remove(self.peer_store_dict[ip])
|
|
|
|
del self.peer_store_dict[ip]
|