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": "background_conference_p.png",
44 "conference_background_landscape": "background_conference_l.png",
45 "magazine_background": "background_magazines_p.png",
46 "magazine_background_landscape": "background_magazines_l.png",
47 "scripture_background": "background_scriptures_p.png",
48 "scripture_background_landscape": "background_scriptures_l.png",
50 "conferences": "conference.png",
51 "magazines": "magazines.png",
52 "mormonmessages": "mormonmessages.png",
54 "scriptures": "scriptures.png",
58 "nomagazineimage": "nomagazineimage.png",
61 def __init__(self, storePath, cachePath):
62 self._storePath = storePath
63 self._cachePath = cachePath
65 self._browser = browser_emu.MozillaEmulator()
66 self._downloader = go_utils.AsyncPool()
69 self._downloader.start()
72 self._downloader.stop()
74 def get_surface_from_store(self, imageName):
75 path = os.path.join(self._storePath, imageName)
76 image = cairo.ImageSurface.create_from_png(path)
79 def get_image_from_store(self, imageName):
80 path = os.path.join(self._storePath, imageName)
82 image.set_from_file(path)
85 def set_image_from_store(self, image, imageName):
86 path = os.path.join(self._storePath, imageName)
87 image.set_from_file(path)
90 def get_pixbuf_from_store(self, imageName):
91 path = os.path.join(self._storePath, imageName)
92 return gtk.gdk.pixbuf_new_from_file(path)
94 def get_pixbuf_from_url(self, url, on_success, on_error):
95 # @ todo Test bad image for both paths
96 filepath = self._url_to_cache(url)
97 if os.path.exists(filepath):
98 pix = gtk.gdk.pixbuf_new_from_file(filepath)
110 lambda filepath: on_success(gtk.gdk.pixbuf_new_from_file(filepath)),
114 def get_pixbuf_animation_from_store(self, imageName):
115 path = os.path.join(self._storePath, imageName)
116 return gtk.gdk.PixbufAnimation(path)
118 def _get_image(self, url, on_success, on_error):
119 self._downloader.add_task(
120 self._browser.download,
123 lambda image: self._on_get_image(url, image, on_success, on_error),
127 @misc_utils.log_exception(_moduleLogger)
128 def _on_get_image(self, url, image, on_success, on_error):
130 filepath = self._url_to_cache(url)
131 _moduleLogger.info("Saved %s" % filepath)
132 with open(filepath, "wb") as f:
138 def _url_to_cache(self, url):
139 filename = url.rsplit("/", 1)[-1]
140 filepath = os.path.join(self._cachePath, filename)