2007-08-29 10:16:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# tab_peers.py
|
|
|
|
#
|
|
|
|
# Copyright (C) Marcos Pinto 2007 <markybob@gmail.com>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# This program 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 this program. 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.
|
|
|
|
|
2007-08-04 18:17:21 +00:00
|
|
|
from itertools import izip
|
2007-08-17 03:44:32 +00:00
|
|
|
import deluge
|
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-17 03:44:32 +00:00
|
|
|
self.config_file = deluge.common.CONFIG_DIR + "/peers.conf"
|
|
|
|
self.config = deluge.pref.Preferences(self.config_file, False,
|
|
|
|
defaults={'ip_p_width': 120,
|
|
|
|
'client_p_width' : 120,
|
|
|
|
'percent_p_width': 90,
|
|
|
|
'down_p_width': 150,
|
|
|
|
'up_p_width': 115})
|
|
|
|
try:
|
|
|
|
self.config.load()
|
|
|
|
except IOError:
|
|
|
|
# File does not exist
|
|
|
|
pass
|
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,
|
2007-08-17 03:44:32 +00:00
|
|
|
_("IP Address"), 1, 2, width=140)
|
2007-08-07 17:04:56 +00:00
|
|
|
self.ip_column.set_sort_column_id(0)
|
2007-08-17 03:44:32 +00:00
|
|
|
client = dgtk.add_text_column(self.peer_view, _("Client"), 3, width=120)
|
2007-08-15 05:11:18 +00:00
|
|
|
percent = dgtk.add_func_column(self.peer_view, _("Percent Complete"),
|
2007-08-17 03:44:32 +00:00
|
|
|
percent, 4, width=150)
|
2007-08-15 05:11:18 +00:00
|
|
|
down = dgtk.add_func_column(self.peer_view, _("Down Speed"),
|
2007-08-17 03:44:32 +00:00
|
|
|
dgtk.cell_data_speed, 5, width=115)
|
2007-08-15 05:11:18 +00:00
|
|
|
up = dgtk.add_func_column(self.peer_view, _("Up Speed"),
|
2007-08-17 03:44:32 +00:00
|
|
|
dgtk.cell_data_speed, 6, width=115)
|
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]
|