+
+ def get_pixbuf_from_url(self, url, on_success, on_error):
+ # @ todo Test bad image for both paths
+ filepath = self._url_to_cache(url)
+ if os.path.exists(filepath):
+ pix = gtk.gdk.pixbuf_new_from_file(filepath)
+ try:
+ on_success(pix)
+ except Exception:
+ pass
+ doDownload = False
+ else:
+ doDownload = True
+
+ if doDownload:
+ self._get_image(
+ url,
+ lambda filepath: on_success(gtk.gdk.pixbuf_new_from_file(filepath)),
+ on_error,
+ )
+
+ def get_pixbuf_animation_from_store(self, imageName):
+ path = os.path.join(self._storePath, imageName)
+ return gtk.gdk.PixbufAnimation(path)
+
+ def _get_image(self, url, on_success, on_error):
+ self._downloader.add_task(
+ self._browser.download,
+ (url, ),
+ {},
+ lambda image: self._on_get_image(url, image, on_success, on_error),
+ on_error,
+ )
+
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_get_image(self, url, image, on_success, on_error):
+ try:
+ filepath = self._url_to_cache(url)
+ _moduleLogger.info("Saved %s" % filepath)
+ with open(filepath, "wb") as f:
+ f.write(image)
+ on_success(filepath)
+ except Exception, e:
+ on_error(e)
+
+ def _url_to_cache(self, url):
+ filename = url.rsplit("/", 1)[-1]
+ filepath = os.path.join(self._cachePath, filename)
+ return filepath