Qt 4.8
Classes | Public Types | Signals | Public Functions | Protected Functions | Private Functions | Properties | List of all members
QNetworkAccessCache Class Reference

#include <qnetworkaccesscache_p.h>

Inheritance diagram for QNetworkAccessCache:
QObject

Classes

class  CacheableObject
 
struct  Node
 

Public Types

typedef QHash< QByteArray, NodeNodeHash
 

Signals

void entryReady (QNetworkAccessCache::CacheableObject *)
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 

Public Functions

void addEntry (const QByteArray &key, CacheableObject *entry)
 
void clear ()
 
bool hasEntry (const QByteArray &key) const
 
 QNetworkAccessCache ()
 
void releaseEntry (const QByteArray &key)
 
void removeEntry (const QByteArray &key)
 
bool requestEntry (const QByteArray &key, QObject *target, const char *member)
 
CacheableObjectrequestEntryNow (const QByteArray &key)
 
 ~QNetworkAccessCache ()
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Protected Functions

void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 

Private Functions

bool emitEntryReady (Node *node, QObject *target, const char *member)
 
void linkEntry (const QByteArray &key)
 Appens the entry given by key to the end of the linked list. More...
 
bool unlinkEntry (const QByteArray &key)
 Removes the entry pointed by key from the linked list. More...
 
void updateTimer ()
 

Properties

NodeHash hash
 
Nodenewest
 
Nodeoldest
 
QBasicTimer timer
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 70 of file qnetworkaccesscache_p.h.

Typedefs

◆ NodeHash

Definition at line 74 of file qnetworkaccesscache_p.h.

Constructors and Destructors

◆ QNetworkAccessCache()

QNetworkAccessCache::QNetworkAccessCache ( )

Definition at line 106 of file qnetworkaccesscache.cpp.

107  : oldest(0), newest(0)
108 {
109 }

◆ ~QNetworkAccessCache()

QNetworkAccessCache::~QNetworkAccessCache ( )

Definition at line 111 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey().

112 {
113  clear();
114 }

Functions

◆ addEntry()

void QNetworkAccessCache::addEntry ( const QByteArray key,
CacheableObject entry 
)

Definition at line 246 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey(), QNetworkAccessFtpBackend::open(), and QHttpThreadDelegate::startRequest().

247 {
248  Q_ASSERT(!key.isEmpty());
249 
250  if (unlinkEntry(key))
251  updateTimer();
252 
253  Node &node = hash[key]; // create the entry in the hash if it didn't exist
254  if (node.useCount)
255  qWarning("QNetworkAccessCache::addEntry: overriding active cache entry '%s'",
256  key.constData());
257  if (node.object)
258  node.object->dispose();
259  node.object = entry;
260  node.object->key = key;
261  node.key = key;
262  node.useCount = 1;
263 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Q_CORE_EXPORT void qWarning(const char *,...)
bool unlinkEntry(const QByteArray &key)
Removes the entry pointed by key from the linked list.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int key
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ clear()

void QNetworkAccessCache::clear ( )

Definition at line 116 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey(), and ~QNetworkAccessCache().

117 {
118  NodeHash hashCopy = hash;
119  hash.clear();
120 
121  // remove all entries
122  NodeHash::Iterator it = hashCopy.begin();
123  NodeHash::Iterator end = hashCopy.end();
124  for ( ; it != end; ++it) {
125  it->object->key.clear();
126  it->object->dispose();
127  }
128 
129  // now delete:
130  hashCopy.clear();
131 
132  timer.stop();
133 
134  oldest = newest = 0;
135 }
void clear()
Removes all items from the hash.
Definition: qhash.h:574
#define it(className, varName)
QHash< QByteArray, Node > NodeHash
void stop()
Stops the timer.
static const KeyPair *const end
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

◆ emitEntryReady()

bool QNetworkAccessCache::emitEntryReady ( Node node,
QObject target,
const char *  member 
)
private

Definition at line 212 of file qnetworkaccesscache.cpp.

Referenced by releaseEntry(), and requestEntry().

213 {
215  target, member, Qt::QueuedConnection))
216  return false;
217 
218  emit entryReady(node->object);
220 
221  return true;
222 }
void entryReady(QNetworkAccessCache::CacheableObject *)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define emit
Definition: qobjectdefs.h:76
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895

◆ entryReady

void QNetworkAccessCache::entryReady ( QNetworkAccessCache::CacheableObject )
signal

◆ hasEntry()

bool QNetworkAccessCache::hasEntry ( const QByteArray key) const

Definition at line 265 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey().

266 {
267  return hash.contains(key);
268 }
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872

◆ linkEntry()

void QNetworkAccessCache::linkEntry ( const QByteArray key)
private

Appens the entry given by key to the end of the linked list.

(i.e., makes it the newest entry)

Definition at line 141 of file qnetworkaccesscache.cpp.

Referenced by releaseEntry().

142 {
144  if (it == hash.end())
145  return;
146 
147  Node *const node = &it.value();
148  Q_ASSERT(node != oldest && node != newest);
149  Q_ASSERT(node->older == 0 && node->newer == 0);
150  Q_ASSERT(node->useCount == 0);
151 
152  if (newest) {
153  Q_ASSERT(newest->newer == 0);
154  newest->newer = node;
155  node->older = newest;
156  }
157  if (!oldest) {
158  // there are no entries, so this is the oldest one too
159  oldest = node;
160  }
161 
162  node->timestamp = QDateTime::currentDateTime().addSecs(ExpiryTime);
163  newest = node;
164 }
QDateTime addSecs(int secs) const
Returns a QDateTime object containing a datetime s seconds later than the datetime of this object (or...
Definition: qdatetime.cpp:2869
#define it(className, varName)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

◆ releaseEntry()

void QNetworkAccessCache::releaseEntry ( const QByteArray key)

Definition at line 323 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey(), QNetworkAccessFtpBackend::disconnectFromFtp(), and QHttpThreadDelegate::startRequestSynchronously().

324 {
326  if (it == hash.end()) {
327  qWarning("QNetworkAccessCache::releaseEntry: trying to release key '%s' that is not in cache",
328  key.constData());
329  return;
330  }
331 
332  Node *node = &it.value();
333  Q_ASSERT(node->useCount > 0);
334 
335  // are there other objects waiting?
336  if (!node->receiverQueue.isEmpty()) {
337  // queue another activation
338  Receiver receiver;
339  do {
340  receiver = node->receiverQueue.dequeue();
341  } while (receiver.object.isNull() && !node->receiverQueue.isEmpty());
342 
343  if (!receiver.object.isNull()) {
344  emitEntryReady(node, receiver.object, receiver.member);
345  return;
346  }
347  }
348 
349  if (!--node->useCount) {
350  // no objects waiting; add it back to the expiry list
351  if (node->object->expires)
352  linkEntry(key);
353 
354  if (oldest == node)
355  updateTimer();
356  }
357 }
#define it(className, varName)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Q_CORE_EXPORT void qWarning(const char *,...)
bool emitEntryReady(Node *node, QObject *target, const char *member)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
void linkEntry(const QByteArray &key)
Appens the entry given by key to the end of the linked list.
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

◆ removeEntry()

void QNetworkAccessCache::removeEntry ( const QByteArray key)

Definition at line 359 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey(), and QNetworkAccessFtpBackend::ftpDone().

360 {
362  if (it == hash.end()) {
363  qWarning("QNetworkAccessCache::removeEntry: trying to remove key '%s' that is not in cache",
364  key.constData());
365  return;
366  }
367 
368  Node *node = &it.value();
369  if (unlinkEntry(key))
370  updateTimer();
371  if (node->useCount > 1)
372  qWarning("QNetworkAccessCache::removeEntry: removing active cache entry '%s'",
373  key.constData());
374 
375  node->object->key.clear();
376  hash.remove(node->key);
377 }
int remove(const Key &key)
Removes all the items that have the key from the hash.
Definition: qhash.h:784
#define it(className, varName)
Q_CORE_EXPORT void qWarning(const char *,...)
bool unlinkEntry(const QByteArray &key)
Removes the entry pointed by key from the linked list.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

◆ requestEntry()

bool QNetworkAccessCache::requestEntry ( const QByteArray key,
QObject target,
const char *  member 
)

Definition at line 270 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey(), and QNetworkAccessFtpBackend::open().

271 {
273  if (it == hash.end())
274  return false; // no such entry
275 
276  Node *node = &it.value();
277 
278  if (node->useCount > 0 && !node->object->shareable) {
279  // object is not shareable and is in use
280  // queue for later use
281  Q_ASSERT(node->older == 0 && node->newer == 0);
282  Receiver receiver;
283  receiver.object = target;
284  receiver.member = member;
285  node->receiverQueue.enqueue(receiver);
286 
287  // request queued
288  return true;
289  } else {
290  // node not in use or is shareable
291  if (unlinkEntry(key))
292  updateTimer();
293 
294  ++node->useCount;
295  return emitEntryReady(node, target, member);
296  }
297 }
#define it(className, varName)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool unlinkEntry(const QByteArray &key)
Removes the entry pointed by key from the linked list.
bool emitEntryReady(Node *node, QObject *target, const char *member)
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

◆ requestEntryNow()

QNetworkAccessCache::CacheableObject * QNetworkAccessCache::requestEntryNow ( const QByteArray key)

Definition at line 299 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey(), and QHttpThreadDelegate::startRequest().

300 {
302  if (it == hash.end())
303  return 0;
304  if (it->useCount > 0) {
305  if (it->object->shareable) {
306  ++it->useCount;
307  return it->object;
308  }
309 
310  // object in use and not shareable
311  return 0;
312  }
313 
314  // entry not in use, let the caller have it
315  bool wasOldest = unlinkEntry(key);
316  ++it->useCount;
317 
318  if (wasOldest)
319  updateTimer();
320  return it->object;
321 }
#define it(className, varName)
bool unlinkEntry(const QByteArray &key)
Removes the entry pointed by key from the linked list.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

◆ timerEvent()

void QNetworkAccessCache::timerEvent ( QTimerEvent event)
protectedvirtual

This event handler can be reimplemented in a subclass to receive timer events for the object.

QTimer provides a higher-level interface to the timer functionality, and also more general information about timers. The timer event is passed in the event parameter.

See also
startTimer(), killTimer(), event()

Reimplemented from QObject.

Definition at line 224 of file qnetworkaccesscache.cpp.

Referenced by QNetworkAccessCache::CacheableObject::cacheKey().

225 {
226  // expire old items
228 
229  while (oldest && oldest->timestamp < now) {
230  Node *next = oldest->newer;
231  oldest->object->dispose();
232 
233  hash.remove(oldest->key); // oldest gets deleted
234  oldest = next;
235  }
236 
237  // fixup the list
238  if (oldest)
239  oldest->older = 0;
240  else
241  newest = 0;
242 
243  updateTimer();
244 }
int remove(const Key &key)
Removes all the items that have the key from the hash.
Definition: qhash.h:784
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138

◆ unlinkEntry()

bool QNetworkAccessCache::unlinkEntry ( const QByteArray key)
private

Removes the entry pointed by key from the linked list.

Returns true if the entry removed was the oldest one.

Definition at line 170 of file qnetworkaccesscache.cpp.

Referenced by addEntry(), removeEntry(), requestEntry(), and requestEntryNow().

171 {
173  if (it == hash.end())
174  return false;
175 
176  Node *const node = &it.value();
177 
178  bool wasOldest = false;
179  if (node == oldest) {
180  oldest = node->newer;
181  wasOldest = true;
182  }
183  if (node == newest)
184  newest = node->older;
185  if (node->older)
186  node->older->newer = node->newer;
187  if (node->newer)
188  node->newer->older = node->older;
189 
190  node->newer = node->older = 0;
191  return wasOldest;
192 }
#define it(className, varName)
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

◆ updateTimer()

void QNetworkAccessCache::updateTimer ( )
private

Definition at line 194 of file qnetworkaccesscache.cpp.

Referenced by addEntry(), releaseEntry(), removeEntry(), requestEntry(), requestEntryNow(), and timerEvent().

195 {
196  timer.stop();
197 
198  if (!oldest)
199  return;
200 
202  if (interval <= 0) {
203  interval = 0;
204  } else {
205  // round up the interval
206  interval = (interval + 15) & ~16;
207  }
208 
209  timer.start(interval * 1000, this);
210 }
int secsTo(const QDateTime &) const
Returns the number of seconds from this datetime to the other datetime.
Definition: qdatetime.cpp:2914
void stop()
Stops the timer.
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138
void start(int msec, QObject *obj)
Starts (or restarts) the timer with a msec milliseconds timeout.

Properties

◆ hash

NodeHash QNetworkAccessCache::hash
private

◆ newest

Node* QNetworkAccessCache::newest
private

Definition at line 116 of file qnetworkaccesscache_p.h.

Referenced by clear(), linkEntry(), timerEvent(), and unlinkEntry().

◆ oldest

Node* QNetworkAccessCache::oldest
private

◆ timer

QBasicTimer QNetworkAccessCache::timer
private

Definition at line 118 of file qnetworkaccesscache_p.h.

Referenced by clear(), and updateTimer().


The documentation for this class was generated from the following files: