Starting on removing LDS Church affiliations
[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": "button_next.png",
21                 "prev": "button_prev.png",
22                 "home": "home.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": "home_pressed.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",
38
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",
46
47                 "conferences": "label_conference.png",
48                 "magazines": "label_magazines.png",
49                 "radio": "label_radio.png",
50                 "scriptures": "label_scriptures.png",
51
52                 "loading": "loading.gif",
53                 "icon": "icon.png",
54                 "nomagazineimage": "nomagazineimage.png",
55         }
56
57         def __init__(self, storePath, cachePath):
58                 self._storePath = storePath
59                 self._cachePath = cachePath
60
61                 self._browser = browser_emu.MozillaEmulator()
62                 self._downloader = go_utils.AsyncPool()
63
64         def start(self):
65                 self._downloader.start()
66
67         def stop(self):
68                 self._downloader.stop()
69
70         def get_surface_from_store(self, imageName):
71                 path = os.path.join(self._storePath, imageName)
72                 image = cairo.ImageSurface.create_from_png(path)
73                 return image
74
75         def get_image_from_store(self, imageName):
76                 path = os.path.join(self._storePath, imageName)
77                 image = gtk.Image()
78                 image.set_from_file(path)
79                 return image
80
81         def set_image_from_store(self, image, imageName):
82                 path = os.path.join(self._storePath, imageName)
83                 image.set_from_file(path)
84                 return image
85
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)
89
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)
95                         try:
96                                 on_success(pix)
97                         except Exception:
98                                 pass
99                         doDownload = False
100                 else:
101                         doDownload = True
102
103                 if doDownload:
104                         self._get_image(
105                                 url,
106                                 lambda filepath: on_success(gtk.gdk.pixbuf_new_from_file(filepath)),
107                                 on_error,
108                         )
109
110         def get_pixbuf_animation_from_store(self, imageName):
111                 path = os.path.join(self._storePath, imageName)
112                 return gtk.gdk.PixbufAnimation(path)
113
114         def _get_image(self, url, on_success, on_error):
115                 self._downloader.add_task(
116                         self._browser.download,
117                         (url, ),
118                         {},
119                         lambda image: self._on_get_image(url, image, on_success, on_error),
120                         on_error,
121                 )
122
123         @misc_utils.log_exception(_moduleLogger)
124         def _on_get_image(self, url, image, on_success, on_error):
125                 try:
126                         filepath = self._url_to_cache(url)
127                         _moduleLogger.info("Saved %s" % filepath)
128                         with open(filepath, "wb") as f:
129                                 f.write(image)
130                         on_success(filepath)
131                 except Exception, e:
132                         on_error(e)
133
134         def _url_to_cache(self, url):
135                 filename = url.rsplit("/", 1)[-1]
136                 filepath = os.path.join(self._cachePath, filename)
137                 return filepath