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):
20 "next": "button_next.png",
21 "prev": "button_prev.png",
23 "pause": "button_pause.png",
24 "play": "button_play.png",
25 "stop": "button_stop.png",
26 "add": "button_add.png",
27 "remove": "button_remove.png",
28 "favorite": "button_favorite.png",
29 "next_pressed": "button_next_pressed.png",
30 "prev_pressed": "button_prev_pressed.png",
31 "home_pressed": "icon.png",
32 "pause_pressed": "button_pause_pressed.png",
33 "play_pressed": "button_play_pressed.png",
34 "stop_pressed": "button_stop_pressed.png",
35 "add_pressed": "button_add_pressed.png",
36 "remove_pressed": "button_remove_pressed.png",
37 "favorite_pressed": "button_favorite_pressed.png",
39 "radio_header": "radio_header.png",
40 "conference_background": "background_conference_p.png",
41 "conference_background_landscape": "background_conference_l.png",
42 "magazine_background": "background_magazines_p.png",
43 "magazine_background_landscape": "background_magazines_l.png",
44 "scripture_background": "background_scriptures_p.png",
45 "scripture_background_landscape": "background_scriptures_l.png",
47 "conferences": "label_conference.png",
48 "magazines": "label_magazines.png",
49 "radio": "label_radio.png",
50 "scriptures": "label_scriptures.png",
52 "loading": "loading.gif",
54 "nomagazineimage": "nomagazineimage.png",
57 def __init__(self, storePath, cachePath):
58 self._storePath = storePath
59 self._cachePath = cachePath
61 self._browser = browser_emu.MozillaEmulator()
62 self._downloader = go_utils.AsyncPool()
65 self._downloader.start()
68 self._downloader.stop()
70 def get_surface_from_store(self, imageName):
71 path = os.path.join(self._storePath, imageName)
72 image = cairo.ImageSurface.create_from_png(path)
75 def get_image_from_store(self, imageName):
76 path = os.path.join(self._storePath, imageName)
78 image.set_from_file(path)
81 def set_image_from_store(self, image, imageName):
82 path = os.path.join(self._storePath, imageName)
83 image.set_from_file(path)
86 def get_pixbuf_from_store(self, imageName):
87 path = os.path.join(self._storePath, imageName)
88 return gtk.gdk.pixbuf_new_from_file(path)
90 def get_pixbuf_from_url(self, url, on_success, on_error):
91 # @ todo Test bad image for both paths
92 filepath = self._url_to_cache(url)
93 if os.path.exists(filepath):
94 pix = gtk.gdk.pixbuf_new_from_file(filepath)
106 lambda filepath: on_success(gtk.gdk.pixbuf_new_from_file(filepath)),
110 def get_pixbuf_animation_from_store(self, imageName):
111 path = os.path.join(self._storePath, imageName)
112 return gtk.gdk.PixbufAnimation(path)
114 def _get_image(self, url, on_success, on_error):
115 self._downloader.add_task(
116 self._browser.download,
119 lambda image: self._on_get_image(url, image, on_success, on_error),
123 @misc_utils.log_exception(_moduleLogger)
124 def _on_get_image(self, url, image, on_success, on_error):
126 filepath = self._url_to_cache(url)
127 _moduleLogger.info("Saved %s" % filepath)
128 with open(filepath, "wb") as f:
134 def _url_to_cache(self, url):
135 filename = url.rsplit("/", 1)[-1]
136 filepath = os.path.join(self._cachePath, filename)