diff --git a/mitterlib/ui/ui_pygtk.py b/mitterlib/ui/ui_pygtk.py index dd1f941..cd80e48 100644 --- a/mitterlib/ui/ui_pygtk.py +++ b/mitterlib/ui/ui_pygtk.py @@ -26,6 +26,7 @@ import logging import threading import Queue import re +import urllib2 from mitterlib.ui.helpers.image_helpers import find_image @@ -465,13 +466,18 @@ class Interface(object): data = store.get_value(position, 0) pic = data.avatar - if not pic in self._user_pics: - cell.set_property('pixbuf', self._default_pixmap) + if not pic in self._avatars: + self._threads.add_work(self._post_download_pic, + self._exception_download_pic, + self._download_pic, + pic) - # just make sure we download this pic too. - #self.queue_pic(pic) - else: - cell.set_property('pixbuf', self._user_pics[pic]) + # set the user avatar to the default image, so it won't get queued + # again. Once downloaded, the _post_download_pic will update the + # image and force a redraw. + self._avatars[pic] = self._default_pixmap + + cell.set_property('pixbuf', self._user_pics[pic]) return @@ -621,6 +627,40 @@ class Interface(object): _log.debug(str(exception)) return + ### image download function + def _download_pic(self, url): + """Download a picture from the web. Can be used in a thread.""" + request = urllib2.Request(url=url) + _log.debug('Starting request of %s' % (url)) + response = urllib2.urlopen(request) + data = response.read() + _log.debug('Request completed') + + return (url, data) + + ### Results from the picture request + def _post_download_pic(self, widget, data): + """Called after the data from the picture is available.""" + + (url, data) = data + + loader = gtk.gdk.PixbufLoader() + loader.write(data) + loader.close() + + user_pic = loader.get_pixbuf() + user_pic = user_pic.scale_simple(48, 48, gtk.gdk.INTERP_BILINEAR) + self._avatars[url] = user_pic + + self._grid.queue_draw() + return + + def _exception_download_pic(self, widget, exception): + """Called in case we have a problem downloading an user avatar.""" + _log.debug('Exception trying to get an avatar.') + _log.debug(str(exception)) + return + # ------------------------------------------------------------ # Required functions for all interfaces # ------------------------------------------------------------ @@ -633,7 +673,7 @@ class Interface(object): self._connection = connection self._options = options - self._user_pics = {} + self._avatars = {} self._pic_queue = set() # Load images