From dca6d839f955d8b98f887ac47cab668dc920b701 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 16 Dec 2009 17:48:12 -0200 Subject: [PATCH] "Cache" images to avoid loading them all the time --- mitterlib/ui/ui_pygtk.py | 55 +++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/mitterlib/ui/ui_pygtk.py b/mitterlib/ui/ui_pygtk.py index a9c9e95..588395a 100644 --- a/mitterlib/ui/ui_pygtk.py +++ b/mitterlib/ui/ui_pygtk.py @@ -98,7 +98,7 @@ class _WorkerThread(threading.Thread, _IdleObject): def run(self): # call the function - _log.debug('Thread %s calling %s', self.name, str(self._function)) + _log.debug('Thread d %s calling %s', self.name, str(self._function)) args = self._args kwargs = self._kwargs @@ -111,7 +111,7 @@ class _WorkerThread(threading.Thread, _IdleObject): self.emit("exception", exc) return - _log.debug('Thread %s completed', self.name) + _log.debug('Thread id %s completed', self.name) self.emit("completed", result) return @@ -138,7 +138,7 @@ class _ThreadManager(object): # this case, the _WorkerThread object. thread_id = widget.name - _log.debug('Thread %s completed, %d threads in the queue', thread_id, + _log.debug('Thread id %s completed, %d threads in the queue', thread_id, len(self._thread_pool)) self._running.remove(thread_id) @@ -213,8 +213,8 @@ class Interface(object): main_window.resize(initial_width, initial_height) main_window.move(initial_x, initial_y) - if self._app_icon: - main_window.set_icon_from_file(self._app_icon) + if self._images['main']: + main_window.set_icon(self._images['main']) main_window.connect('destroy', self._quit_app) main_window.connect('delete-event', self._quit_app) @@ -511,10 +511,8 @@ class Interface(object): 'Philip Reynolds', 'Greg McIntyre', '"Alexander"']) - if self._app_icon: - _log.debug('Icon: %s', self._app_icon) - about_window.set_logo(gtk.gdk.pixbuf_new_from_file( - self._app_icon)) + if self._images['main']: + about_window.set_logo(self._images['main']) about_window.run() about_window.hide() @@ -536,7 +534,7 @@ class Interface(object): # 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 + self._avatars[pic] = self._images['avatar'] cell.set_property('pixbuf', self._avatars[pic]) @@ -803,7 +801,7 @@ class Interface(object): self._grid.get_model().clear() self._new_message_count = 0 if self._statusicon: - self._statusicon.set_from_file(find_image('mitter.png')) + self._statusicon.set_from_pixbuf(self._images['main']) return def _delete_message(self, widget, user_data=None): @@ -972,7 +970,7 @@ class Interface(object): self._new_message_count -= 1 if self._new_message_count == 0 and self._statusicon: - self._statusicon.set_from_file(find_image('mitter.png')) + self._statusicon.set_from_pixbuf(self._images['main']) return True @@ -1013,7 +1011,7 @@ class Interface(object): self._new_message_count += len(results) if self._new_message_count > 0 and self._statusicon: - self._statusicon.set_from_file(find_image('mitter-new.png')) + self._statusicon.set_from_pixbuf(self._images['new-messages']) return @@ -1031,12 +1029,17 @@ class Interface(object): error_win.hide() self._update_statusbar('Auto-update disabled') if self._statusicon: - self._statusicon.set_from_file(find_image('mitter-error.png')) + self._statusicon.set_from_pixbuf(self._images['icon-error']) return ### image download function def _download_pic(self, url): """Download a picture from the web. Can be used in a thread.""" + # recheck if the image wasn't downloaded by other thread + if url in self._avatars: + _log.debug('Image %s available already' % (url)) + return None + request = urllib2.Request(url=url) _log.debug('Starting request of %s' % (url)) response = urllib2.urlopen(request) @@ -1048,6 +1051,9 @@ class Interface(object): ### Results from the picture request def _post_download_pic(self, widget, data): """Called after the data from the picture is available.""" + if not data: + # image appeared in the queue + return (url, data) = data @@ -1143,20 +1149,23 @@ class Interface(object): self._pic_queue = set() # Load images - self._app_icon = find_image('mitter.png') - self._app_icon_alert = find_image('mitter-new.png') - self._reply_pixbuf = gtk.gdk.pixbuf_new_from_file( - find_image('reply.png')) - #self._delete_icon = find_image('icon_trash.gif') - unknown_pixbuf = find_image('unknown.png') if unknown_pixbuf: - self._default_pixmap = gtk.gdk.pixbuf_new_from_file( + default_pixmap = gtk.gdk.pixbuf_new_from_file( unknown_pixbuf) else: - self._default_pixmap = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, + default_pixmap = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=False, bits_per_sample=8, width=48, height=48) + self._images = {} + self._images['main'] = gtk.gdk.pixbuf_new_from_file( + find_image('mitter.png')) + self._images['new-messages'] = gtk.gdk.pixbuf_new_from_file( + find_image('mitter-new.png')) + self._images['icon-error'] = gtk.gdk.pixbuf_new_from_file( + find_image('mitter-error.png')) + self._images['avatar'] = default_pixmap + # This is the ugly bit for speeding up things and making # interthread communication. self._delete_iter = None @@ -1174,7 +1183,7 @@ class Interface(object): if self._options[self.NAMESPACE]['statusicon']: self._statusicon = gtk.StatusIcon() - self._statusicon.set_from_file(find_image('mitter.png')) + self._statusicon.set_from_pixbuf(self._images['main']) else: self._statusicon = None self._threads = _ThreadManager()