21 #include "tdehtml_pagecache.h"
23 #include <kstaticdeleter.h>
24 #include <tdetempfile.h>
25 #include <kstandarddirs.h>
27 #include <tqintdict.h>
30 #include <sys/types.h>
35 #ifndef TDEHTML_PAGE_CACHE_SIZE
36 #define TDEHTML_PAGE_CACHE_SIZE 12
39 template class TQPtrList<TDEHTMLPageCacheDelivery>;
40 class TDEHTMLPageCacheEntry
44 TDEHTMLPageCacheEntry(
long id);
46 ~TDEHTMLPageCacheEntry();
48 void addData(
const TQByteArray &data);
53 {
return m_complete; }
55 TDEHTMLPageCacheDelivery *fetchData(TQObject *recvObj,
const char *recvSlot);
59 TQValueList<TQByteArray> m_data;
63 class TDEHTMLPageCachePrivate
67 TQIntDict<TDEHTMLPageCacheEntry> dict;
68 TQPtrList<TDEHTMLPageCacheDelivery> delivery;
69 TQPtrList<TDEHTMLPageCacheEntry> expireQueue;
73 TDEHTMLPageCacheEntry::TDEHTMLPageCacheEntry(
long id) : m_id(id), m_complete(false)
80 TDEHTMLPageCacheEntry::~TDEHTMLPageCacheEntry()
87 TDEHTMLPageCacheEntry::addData(
const TQByteArray &data)
89 if (m_file->status() == 0)
90 m_file->dataStream()->writeRawBytes(data.data(), data.size());
94 TDEHTMLPageCacheEntry::endData()
97 if ( m_file->status() == 0) {
98 m_file->dataStream()->device()->flush();
99 m_file->dataStream()->device()->at(0);
104 TDEHTMLPageCacheDelivery *
105 TDEHTMLPageCacheEntry::fetchData(TQObject *recvObj,
const char *recvSlot)
108 int fd = dup(m_file->handle());
109 lseek(fd, 0, SEEK_SET);
110 TDEHTMLPageCacheDelivery *delivery =
new TDEHTMLPageCacheDelivery(fd);
111 recvObj->connect(delivery, TQ_SIGNAL(emitData(
const TQByteArray&)), recvSlot);
112 delivery->recvObj = recvObj;
128 TDEHTMLPageCache::TDEHTMLPageCache()
130 d =
new TDEHTMLPageCachePrivate;
132 d->deliveryActive =
false;
135 TDEHTMLPageCache::~TDEHTMLPageCache()
137 d->delivery.setAutoDelete(
true);
138 d->dict.setAutoDelete(
true);
145 TDEHTMLPageCacheEntry *entry =
new TDEHTMLPageCacheEntry(d->newId);
146 d->dict.insert(d->newId, entry);
147 d->expireQueue.append(entry);
148 if (d->expireQueue.count() > TDEHTML_PAGE_CACHE_SIZE)
150 TDEHTMLPageCacheEntry *entry = d->expireQueue.take(0);
151 d->dict.remove(entry->m_id);
160 TDEHTMLPageCacheEntry *entry = d->dict.find(
id);
162 entry->addData(data);
168 TDEHTMLPageCacheEntry *entry = d->dict.find(
id);
176 TDEHTMLPageCacheEntry *entry = d->dict.take(
id);
179 d->expireQueue.removeRef(entry);
187 return (d->dict.find(
id) != 0);
193 TDEHTMLPageCacheEntry *entry = d->dict.find(
id);
195 return entry->isComplete();
202 TDEHTMLPageCacheEntry *entry = d->dict.find(
id);
203 if (!entry || !entry->isComplete())
return;
206 d->expireQueue.removeRef(entry);
207 d->expireQueue.append(entry);
209 d->delivery.append( entry->fetchData(recvObj, recvSlot) );
210 if (!d->deliveryActive)
212 d->deliveryActive =
true;
213 TQTimer::singleShot(20,
this, TQ_SLOT(sendData()));
220 TDEHTMLPageCacheDelivery *next;
221 for(TDEHTMLPageCacheDelivery* delivery = d->delivery.first();
225 next = d->delivery.next();
226 if (delivery->recvObj == recvObj)
228 d->delivery.removeRef(delivery);
235 TDEHTMLPageCache::sendData()
237 if (d->delivery.isEmpty())
239 d->deliveryActive =
false;
242 TDEHTMLPageCacheDelivery *delivery = d->delivery.take(0);
246 TQByteArray byteArray;
248 int n = read(delivery->fd, buf, 8192);
250 if ((n < 0) && (errno == EINTR))
253 d->delivery.append( delivery );
258 delivery->emitData(byteArray);
263 byteArray.setRawData(buf, n);
264 delivery->emitData(byteArray);
265 byteArray.resetRawData(buf, n);
266 d->delivery.append( delivery );
268 TQTimer::singleShot(0,
this, TQ_SLOT(sendData()));
274 TDEHTMLPageCacheEntry *entry = d->dict.find(
id);
277 int fd = entry->m_file->handle();
278 if ( fd < 0 )
return;
280 off_t pos = lseek(fd, 0, SEEK_CUR);
281 lseek(fd, 0, SEEK_SET);
287 int n = read(fd, buf, 8192);
288 if ((n < 0) && (errno == EINTR))
300 str->writeRawBytes(buf, n);
304 if (pos != (off_t)-1)
305 lseek(fd, pos, SEEK_SET);
308 TDEHTMLPageCacheDelivery::~TDEHTMLPageCacheDelivery()
313 #include "tdehtml_pagecache.moc"
Singleton Object that handles a binary cache on top of the http cache management of tdeio.
long createCacheEntry()
Create a new cache entry.
void addData(long id, const TQByteArray &data)
Add data to the cache entry with id id.
void endData(long id)
Signal end of data for the cache entry with id id.
void saveData(long id, TQDataStream *str)
Save the data of cache entry id to the datastream str.
static TDEHTMLPageCache * self()
static "constructor".
void fetchData(long id, TQObject *recvObj, const char *recvSlot)
Fetch data for cache entry id and send it to slot recvSlot in the object recvObj.
void cancelEntry(long id)
Cancel the entry.
void cancelFetch(TQObject *recvObj)
Cancel sending data to recvObj.
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())