Optimized torrentview.cell_data_trackericon

cell_data_trackericon would load the tracker icon with
gtk.gdk.pixbuf_new_from_file_at_size each time it's requested.
These regular requests acumulating to thousands calls to
pixbuf_new_from_file_at_size with a big torrent list.

Now, read the tracker icon from disk once, and cache it.
This commit is contained in:
bendikro 2012-10-11 15:45:04 +02:00 committed by Calum Lind
parent 3f4cfd5c88
commit 36a78d8f21
2 changed files with 28 additions and 8 deletions

View File

@ -116,24 +116,29 @@ def cell_data_statusicon(column, cell, model, row, data):
def cell_data_trackericon(column, cell, model, row, data):
def on_get_icon(icon):
def create_blank_icon():
def create_blank_pixbuf():
i = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 16, 16)
i.fill(0x00000000)
return i
if icon:
try:
icon = gtk.gdk.pixbuf_new_from_file_at_size(icon.get_filename(), 16, 16)
except Exception, e:
icon = create_blank_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:
icon = create_blank_icon()
pixbuf = create_blank_pixbuf()
#Supress Warning: g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed
with warnings.catch_warnings():
warnings.simplefilter("ignore")
if cell.get_property("pixbuf") != icon:
cell.set_property("pixbuf", icon)
if cell.get_property("pixbuf") != pixbuf:
cell.set_property("pixbuf", pixbuf)
host = model[row][data]
if host:

View File

@ -74,6 +74,7 @@ class TrackerIcon(object):
self.filename = os.path.abspath(filename)
self.mimetype = extension_to_mimetype(self.filename.rpartition('.')[2])
self.data = None
self.icon_cache = None
def __eq__(self, other):
"""
@ -122,6 +123,20 @@ class TrackerIcon(object):
"""
return self.filename if full else os.path.basename(self.filename)
def set_cached_icon(self, data):
"""
Set the cached icon data.
"""
self.icon_cache = data
def get_cached_icon(self):
"""
Returns the cached icon data.
"""
return self.icon_cache
class TrackerIcons(Component):
"""
A TrackerIcon factory class