Switching to the approved backgrounds which encouraged me to also implement landscape...
[watersofshiloah] / src / imagestore.py
1 from __future__ import with_statement
2
3 import os
4 import logging
5
6 import cairo
7 import gtk
8
9 import browser_emu
10 from util import go_utils
11 import util.misc as misc_utils
12
13
14 _moduleLogger = logging.getLogger(__name__)
15
16
17 class ImageStore(object):
18
19         STORE_LOOKUP = {
20                 "next": "next.png",
21                 "prev": "prev.png",
22                 "home": "home.png",
23                 "pause": "pause.png",
24                 "play": "play.png",
25                 "stop": "stop.png",
26                 "pause_pressed": "pausepressed.png",
27                 "play_pressed": "playpressed.png",
28                 "stop_pressed": "stoppressed.png",
29
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",
39
40                 "loading": "loading.gif",
41
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",
49
50                 "conferences": "conference.png",
51                 "magazines": "magazines.png",
52                 "mormonmessages": "mormonmessages.png",
53                 "radio": "radio.png",
54                 "scriptures": "scriptures.png",
55
56                 "more": "more.png",
57                 "icon": "icon.png",
58                 "nomagazineimage": "nomagazineimage.png",
59         }
60
61         def __init__(self, storePath, cachePath):
62                 self._storePath = storePath
63                 self._cachePath = cachePath
64
65                 self._browser = browser_emu.MozillaEmulator()
66                 self._downloader = go_utils.AsyncPool()
67
68         def start(self):
69                 self._downloader.start()
70
71         def stop(self):
72                 self._downloader.stop()
73
74         def get_surface_from_store(self, imageName):
75                 path = os.path.join(self._storePath, imageName)
76                 image = cairo.ImageSurface.create_from_png(path)
77                 return image
78
79         def get_image_from_store(self, imageName):
80                 path = os.path.join(self._storePath, imageName)
81                 image = gtk.Image()
82                 image.set_from_file(path)
83                 return image
84
85         def set_image_from_store(self, image, imageName):
86                 path = os.path.join(self._storePath, imageName)
87                 image.set_from_file(path)
88                 return image
89
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)
93
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)
99                         try:
100                                 on_success(pix)
101                         except Exception:
102                                 pass
103                         doDownload = False
104                 else:
105                         doDownload = True
106
107                 if doDownload:
108                         self._get_image(
109                                 url,
110                                 lambda filepath: on_success(gtk.gdk.pixbuf_new_from_file(filepath)),
111                                 on_error,
112                         )
113
114         def get_pixbuf_animation_from_store(self, imageName):
115                 path = os.path.join(self._storePath, imageName)
116                 return gtk.gdk.PixbufAnimation(path)
117
118         def _get_image(self, url, on_success, on_error):
119                 self._downloader.add_task(
120                         self._browser.download,
121                         (url, ),
122                         {},
123                         lambda image: self._on_get_image(url, image, on_success, on_error),
124                         on_error,
125                 )
126
127         @misc_utils.log_exception(_moduleLogger)
128         def _on_get_image(self, url, image, on_success, on_error):
129                 try:
130                         filepath = self._url_to_cache(url)
131                         _moduleLogger.info("Saved %s" % filepath)
132                         with open(filepath, "wb") as f:
133                                 f.write(image)
134                         on_success(filepath)
135                 except Exception, e:
136                         on_error(e)
137
138         def _url_to_cache(self, url):
139                 filename = url.rsplit("/", 1)[-1]
140                 filepath = os.path.join(self._cachePath, filename)
141                 return filepath