From 10fc58779baa0e5baa94f3a697a9ffa355763f61 Mon Sep 17 00:00:00 2001
From: bendikro <bendikro@gmail.com>
Date: Mon, 18 Feb 2013 01:18:54 +0100
Subject: [PATCH] Fixed bug in tracker icons for torrentview.

---
 deluge/ui/gtkui/filtertreeview.py |    2 +-
 deluge/ui/gtkui/torrentview.py    |   65 ++++++++++++++++++++++---------------
 deluge/ui/tracker_icons.py        |   18 ++++++++++
 3 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/deluge/ui/gtkui/filtertreeview.py b/deluge/ui/gtkui/filtertreeview.py
index 51cde26..8c29c39 100644
--- a/deluge/ui/gtkui/filtertreeview.py
+++ b/deluge/ui/gtkui/filtertreeview.py
@@ -247,7 +247,7 @@ def on_get_icon(icon):
             self.filters[(cat, value)] = row
 
             if cat == "tracker_host" and value not in ("All", "Error") and value:
-                d = self.tracker_icons.get(value)
+                d = self.tracker_icons.fetch(value)
                 d.addCallback(on_get_icon)
 
         self.treestore.set_value(row, FILTER_COLUMN, True)
diff --git a/deluge/ui/gtkui/torrentview.py b/deluge/ui/gtkui/torrentview.py
index cdfbcc8..a15ea7e 100644
--- a/deluge/ui/gtkui/torrentview.py
+++ b/deluge/ui/gtkui/torrentview.py
@@ -109,38 +109,49 @@ def cell_data_statusicon(column, cell, model, row, data):
     except KeyError:
         pass
 
-def cell_data_trackericon(column, cell, model, row, data):
-    def on_get_icon(icon):
-        def create_blank_pixbuf():
-            i = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 16, 16)
-            i.fill(0x00000000)
-            return i
-
-        if icon:
-            pixbuf = icon.get_cached_icon()
-            if not pixbuf:
-                try:
-                    pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(icon.get_filename(), 16, 16)
-                except gobject.GError, e:
-                    # Failed to load the pixbuf (Bad image file), so set a blank pixbuf
-                    pixbuf = create_blank_pixbuf()
-                finally:
-                    icon.set_cached_icon(pixbuf)
-        else:
-            pixbuf = create_blank_pixbuf()
+def create_blank_pixbuf():
+    i = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 16, 16)
+    i.fill(0x00000000)
+    return i
+
+def set_icon(icon, cell):
+    if icon:
+        pixbuf = icon.get_cached_icon()
+        if pixbuf is None:
+            try:
+                pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(icon.get_filename(), 16, 16)
+            except gobject.GError, e:
+                # Failed to load the pixbuf (Bad image file), so set a blank pixbuf
+                pixbuf = create_blank_pixbuf()
+            finally:
+                icon.set_cached_icon(pixbuf)
+    else:
+        pixbuf = create_blank_pixbuf()
+
+    #Suppress Warning: g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed
+    with warnings.catch_warnings():
+        warnings.simplefilter("ignore")
+        cell.set_property("pixbuf", pixbuf)
 
-        #Suppress Warning: g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed
-        with warnings.catch_warnings():
-            warnings.simplefilter("ignore")
-            if cell.get_property("pixbuf") != pixbuf:
-                cell.set_property("pixbuf", pixbuf)
+last_host = None
 
+def cell_data_trackericon(column, cell, model, row, data):
+    global last_host
     host = model[row][data]
     if host:
-        d = component.get("TrackerIcons").get(host)
-        d.addCallback(on_get_icon)
+        if last_host == host:
+            return
+        else:
+            last_host = host
+
+        if not component.get("TrackerIcons").has(host):
+            # Set blank icon while waiting for the icon to be loaded
+            set_icon(None, cell)
+            component.get("TrackerIcons").fetch(host)
+        else:
+            set_icon(component.get("TrackerIcons").get(host), cell)
     else:
-        on_get_icon(None)
+        set_icon(None, cell)
 
 def cell_data_progress(column, cell, model, row, data):
     """Display progress bar with text"""
diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py
index ffc099c..8fa58fc 100644
--- a/deluge/ui/tracker_icons.py
+++ b/deluge/ui/tracker_icons.py
@@ -178,12 +178,30 @@ def __init__(self, icon_dir=None, no_icon=None):
         self.pending = {}
         self.redirects = {}
 
+    def has(self, host):
+        return host in self.icons
+
     def get(self, host):
         """
         Returns a TrackerIcon for the given tracker's host
 
         :param host: the host to obtain the TrackerIcon for
         :type host: string
+        :returns: the TrackerIcon for the host
+        :rtype: TrackerIcon
+        """
+        host = host.lower()
+        if host in self.icons:
+            return self.icons[host]
+        else:
+            return None
+
+    def fetch(self, host):
+        """
+        Returns a TrackerIcon for the given tracker's host
+
+        :param host: the host to obtain the TrackerIcon for
+        :type host: string
         :returns: a Deferred which fires with the TrackerIcon for the given host
         :rtype: Deferred
         """
-- 
1.7.10

