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