1
# class template for various html widgets
14
import SimpleImageCache
16
IMG_REGEX = re.compile("<img.*?src=[\",\'](.*?)[\",\'].*?>", re.IGNORECASE|re.DOTALL)
18
class PTVGtkHtml(PTVhtml.PTVhtml):
19
def __init__(self, view, home, share_path):
20
PTVhtml.PTVhtml.__init__(self, view, home, share_path)
22
self._document_lock = threading.Lock()
23
self._image_cache = SimpleImageCache.SimpleImageCache()
25
self._last_link_time = 0
29
f = open(os.path.join(share_path, "gtkhtml.css"))
30
for l in f.readlines(): self._css += l
32
self._image_pool = ThreadPool.ThreadPool(5, "PlanetView")
37
self._image_pool.joinAll(False, False)
43
def post_show_init(self, widget):
45
import SimpleImageCache
48
htmlview = gtkhtml2.View()
49
self._document = gtkhtml2.Document()
50
self._document.connect("link-clicked", self._link_clicked)
51
htmlview.connect("on_url", self._on_url)
52
self._document.connect("request-url", self._request_url)
53
htmlview.get_vadjustment().set_value(0)
54
htmlview.get_hadjustment().set_value(0)
56
self._document.clear()
57
htmlview.set_document(self._document)
58
self._htmlview = htmlview
60
widget.set_property("shadow-type",gtk.SHADOW_IN)
61
widget.set_hadjustment(self._htmlview.get_hadjustment())
62
widget.set_vadjustment(self._htmlview.get_vadjustment())
63
widget.add(self._htmlview)
64
self._scrolled_window = widget
66
def build_header(self, html=""):
67
header = ["""<html><head>
68
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
69
<style type="text/css">
72
<title>title</title>""" % self._css]
74
header.append("""</head>""")
75
return "\n".join(header)
77
def render(self, html, stream_url="file:///", display_id=None):
78
self._document_lock.acquire()
79
imgs = IMG_REGEX.findall(html)
82
if not self._image_cache.is_cached(url):
86
self._document.clear()
87
self._document.open_stream("text/html")
88
d = { "background_color": self._view.get_bg_color(),
89
"loading": _("Loading images...")}
90
self._document.write_stream("""<html><style type="text/css">
91
body { background-color: %(background_color)s; }</style><body><i>%(loading)s</i></body></html>""" % d)
92
self._document.close_stream()
93
self._document_lock.release()
96
self._dl_total = uncached
99
if not self._image_cache.is_cached(url):
100
self._image_pool.queueTask(self._do_download_image, (url, display_id), self._image_dl_cb)
101
self._image_pool.queueTask(self._download_done, (display_id, html))
103
self._scrolled_window.get_hadjustment().set_value(0)
104
self._scrolled_window.get_vadjustment().set_value(0)
105
self._document.clear()
106
self._document.open_stream("text/html")
107
self._document.write_stream(html)
108
self._document.close_stream()
109
self._document_lock.release()
111
def dl_interrupt(self):
112
self._image_pool.joinAll(False, False)
116
def _do_download_image(self, args):
117
url, display_id = args
118
self._image_cache.get_image(url)
119
#print "do download", display_id
122
def _image_dl_cb(self, display_id):
123
#print "dl_cb", display_id, self._view.get_display_id()
124
if display_id == self._view.get_display_id():
127
def _download_done(self, args):
128
display_id, html = args
131
last_count = self._dl_count
132
#print "dl_done", display_id, self._view.get_display_id()
133
while display_id == self._view.get_display_id() and count < (10 * 2):
134
if last_count != self._dl_count:
135
#if downloads are still coming in, reset counter
136
last_count = self._dl_count
138
if self._dl_count >= self._dl_total:
139
gobject.idle_add(self._images_loaded, display_id, html)
143
gobject.idle_add(self._images_loaded, display_id, html)
146
def _images_loaded(self, display_id, html):
147
#if we're changing, nevermind.
148
#also make sure entry is the same and that we shouldn't be blanks
149
#print "loaded", display_id, self._view.get_display_id()
150
if display_id == self._view.get_display_id():
151
va = self._scrolled_window.get_vadjustment()
152
ha = self._scrolled_window.get_hadjustment()
153
self._document_lock.acquire()
154
self._document.clear()
155
self._document.open_stream("text/html")
156
self._document.write_stream(html)
157
self._document.close_stream()
158
self._document_lock.release()
161
def _request_url(self, document, url, stream):
163
image = self._image_cache.get_image(url)
166
except Exception, ex:
169
def _link_clicked(self, document, link):
170
if not utils.RUNNING_HILDON:
172
self.emit('open-uri', link)
174
def _on_url(self, view, url):
175
if utils.RUNNING_HILDON:
177
#prevent double-clicks
178
print now - self._last_link_time
179
if now - self._last_link_time < 1.0:
180
logging.debug("detected double-click, ignoring")
182
self._last_link_time = now
186
self.emit('open-uri', link)
190
self.emit('link-message', url)