3 Module for handling images
5 __copyright__
= "Copyright (c) 2002-2005 Free Software Foundation, Inc."
7 Straw is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
12 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA. """
20 import NetworkConstants
28 from straw
import helpers
30 STATUS_IMAGE_WAITING
= None
31 STATUS_IMAGE_BROKEN
= None
33 class CacheEntry(object):
34 def __init__(self
, image
, count
, pollstopper
= None, restore
= False):
37 self
._pollstopper
= pollstopper
43 doc
="The image object that is associated with this class"
46 def fset(self
, image
):
48 return property(**locals())
55 def fset(self
, count
):
58 return property(**locals())
62 doc
="the stopper object for this cache entry"
64 return self
._pollstopper
65 def fset(self
, pollstopper
):
66 self
._pollstopper
= pollstopper
68 pollstopper
.connect('stopped', self
._polling
_stopped
)
69 return property(**locals())
71 def incr_count(self
): self
._count
+= 1
72 def decr_count(self
): self
._count
-= 1
74 def _polling_stopped(self
, poller
):
75 self
._pollstopper
= None
77 def _save_count(self
):
79 #ItemStore.get_instance().set_image_count(self._image.url, self._count)
82 class Cache(gobject
.GObject
):
83 """Image cache with explicit reference counting."""
86 'image-updated' : (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
,
87 (gobject
.TYPE_STRING
, gobject
.TYPE_PYOBJECT
))
91 gobject
.GObject
.__init
__(self
)
92 # self.__cache contains items of the form [Image, refcount]
95 def __getitem__(self
, key
):
96 print "got getitem for %s" % key
97 print "cache is %s" % self
.__cache
98 return self
.__cache
[key
].image
100 def add_refer(self
, key
, restore
= False, item
= None):
101 if key
not in self
.__cache
:
104 image
= Image(key
, Image
.WAITING
)
105 ic
= ImageConsumer(image
)
108 if item
and item
.feed
:
109 headers
['Referer'] = item
.feed
.location
111 stopper
= URLFetch
.get_instance().request(
113 priority
=NetworkConstants
.PRIORITY_IMAGE
,
115 #except URLFetch.RequestSchemeException, e:
118 error
.logtb("ImageCache.add_refer: ", str(e
))
120 self
.__cache
[key
] = CacheEntry(
121 image
, 1, PollManager
.PollStopper(stopper
, image
), restore
=restore
)
123 image
= Image(key
, Image
.DATA_IN_DB
)
124 self
.__cache
[key
] = CacheEntry(image
, 1, pollstopper
=None, restore
=restore
)
125 elif key
in self
.__cache
and not restore
:
126 self
.__cache
[key
].incr_count()
128 def remove_refer(self
, key
):
129 if self
.__cache
.has_key(key
):
130 entry
= self
.__cache
[key
]
133 del self
.__cache
[key
]
134 ItemStore
.get_instance().update_image(key
, None)
135 self
.emit('image-updated', key
, None)
137 def image_updated(self
, url
, data
):
138 self
.__cache
[url
].pollstopper
= None
139 self
.set_image(url
,data
)
140 ItemStore
.get_instance().update_image(url
,data
)
141 self
.emit('image-updated', url
, data
)
143 def set_image(self
, url
, image
):
144 if self
.__cache
.has_key(url
):
145 self
.__cache
[url
].incr_count()
147 self
.__cache
[url
] = CacheEntry(image
, 1)
149 def set_image_with_count(self
, url
, image
, count
, restore
=False):
150 self
.__cache
[url
] = CacheEntry(image
, count
, restore
=restore
)
152 def stop_transfer(self
, url
):
153 image
= self
.__cache
.get(url
, None)
154 if image
and image
.pollstopper
:
155 self
.__cache
[url
].pollstopper
.stop()
156 self
.__cache
[url
].pollstopper
= None
163 def __init__(self
, url
, status
= DATA_IN_DB
):
168 if self
.status
== self
.WAITING
:
169 return STATUS_IMAGE_WAITING
170 elif self
.status
== self
.DATA_IN_DB
:
171 data
= self
.read_data()
173 self
.status
= self
.FAILED
174 return STATUS_IMAGE_BROKEN
177 self
.status
= self
.FAILED
178 return STATUS_IMAGE_BROKEN
180 def set_data(self
, data
):
181 cache
.image_updated(self
.url
, data
)
182 self
.status
= self
.DATA_IN_DB
184 def set_failed(self
):
185 self
.status
= self
.FAILED
188 istore
= ItemStore
.get_instance()
189 return istore
.read_image(self
.url
)
191 def _return_id(self
):
192 return "%d %s" % (id(self
), self
.url
)
197 def __init__(self
, imobj
):
200 def http_results(self
, status
, data
):
202 self
._imobj
.set_data(data
)
204 self
._imobj
.set_failed()
206 def http_failed(self
, exception
):
207 self
._imobj
.set_failed()
209 def operation_stopped(self
):
210 # XXX operation stopped is not really failed
211 self
._imobj
.set_failed()
214 global STATUS_IMAGE_WAITING
215 global STATUS_IMAGE_BROKEN
216 STATUS_IMAGE_WAITING
= open(
217 straw
.STRAW_DATA_DIR
+ "/image-loading.svg").read()
218 STATUS_IMAGE_BROKEN
= open(
219 straw
.STRAW_DATA_DIR
+ "/image-missing.svg").read()