1 from __future__ import with_statement
10 from util import go_utils
11 import util.misc as misc_utils
14 _moduleLogger = logging.getLogger(__name__)
17 class ImageStore(object):
26 "pause_pressed": "pausepressed.png",
27 "play_pressed": "playpressed.png",
28 "stop_pressed": "stoppressed.png",
30 "small_next": "small_next.png",
31 "small_prev": "small_prev.png",
32 "small_home": "small_home.png",
33 "small_pause": "small_pause.png",
34 "small_play": "small_play.png",
35 "small_stop": "small_stop.png",
36 "small_pause_pressed": "small_pausepressed.png",
37 "small_play_pressed": "small_playpressed.png",
38 "small_stop_pressed": "small_stoppressed.png",
40 "loading": "loading.gif",
42 "radio_header": "radio_header.png",
43 "conference_background": "conference_bg.png",
44 "magazine_background": "magazine_bg.png",
45 "scripture_background": "scripture_bg.png",
47 "conferences": "conference.png",
48 "magazines": "magazines.png",
49 "mormonmessages": "mormonmessages.png",
51 "scriptures": "scriptures.png",
55 "nomagazineimage": "nomagazineimage.png",
58 def __init__(self, storePath, cachePath):
59 self._storePath = storePath
60 self._cachePath = cachePath
62 self._browser = browser_emu.MozillaEmulator()
63 self._downloader = go_utils.AsyncPool()
66 self._downloader.start()
69 self._downloader.stop()
71 def get_surface_from_store(self, imageName):
72 path = os.path.join(self._storePath, imageName)
73 image = cairo.ImageSurface.create_from_png(path)
76 def get_image_from_store(self, imageName):
77 path = os.path.join(self._storePath, imageName)
79 image.set_from_file(path)
82 def set_image_from_store(self, image, imageName):
83 path = os.path.join(self._storePath, imageName)
84 image.set_from_file(path)
87 def get_pixbuf_from_store(self, imageName):
88 path = os.path.join(self._storePath, imageName)
89 return gtk.gdk.pixbuf_new_from_file(path)
91 def get_pixbuf_from_url(self, url, on_success, on_error):
92 # @ todo Test bad image for both paths
93 filepath = self._url_to_cache(url)
94 if os.path.exists(filepath):
95 pix = gtk.gdk.pixbuf_new_from_file(filepath)
107 lambda filepath: on_success(gtk.gdk.pixbuf_new_from_file(filepath)),
111 def get_pixbuf_animation_from_store(self, imageName):
112 path = os.path.join(self._storePath, imageName)
113 return gtk.gdk.PixbufAnimation(path)
115 def _get_image(self, url, on_success, on_error):
116 self._downloader.add_task(
117 self._browser.download,
120 lambda image: self._on_get_image(url, image, on_success, on_error),
124 @misc_utils.log_exception(_moduleLogger)
125 def _on_get_image(self, url, image, on_success, on_error):
127 filepath = self._url_to_cache(url)
128 _moduleLogger.info("Saved %s" % filepath)
129 with open(filepath, "wb") as f:
135 def _url_to_cache(self, url):
136 filename = url.rsplit("/", 1)[-1]
137 filepath = os.path.join(self._cachePath, filename)