Qt 4.8
qabstractnetworkcache.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractnetworkcache.h"
44 
45 #include <qdatetime.h>
46 #include <qurl.h>
47 
48 #include <qdebug.h>
49 
51 
53 {
54 
55 public:
57  : QSharedData()
58  , saveToDisk(true)
59  {}
60 
61  bool operator==(const QNetworkCacheMetaDataPrivate &other) const
62  {
63  return
64  url == other.url
65  && lastModified == other.lastModified
66  && expirationDate == other.expirationDate
67  && headers == other.headers
68  && saveToDisk == other.saveToDisk;
69  }
70 
76  bool saveToDisk;
77 
78  static void save(QDataStream &out, const QNetworkCacheMetaData &metaData);
79  static void load(QDataStream &in, QNetworkCacheMetaData &metaData);
80 };
81 Q_GLOBAL_STATIC(QNetworkCacheMetaDataPrivate, metadata_shared_invalid)
82 
83 
125 {
126 }
127 
132 {
133  // QSharedDataPointer takes care of freeing d
134 }
135 
140  : d(other.d)
141 {
142 }
143 
148 {
149  d = other.d;
150  return *this;
151 }
152 
159 {
160  if (d == other.d)
161  return true;
162  if (d && other.d)
163  return *d == *other.d;
164  return false;
165 }
166 
182 {
183  return !(*d == *metadata_shared_invalid());
184 }
185 
199 {
200  return d->saveToDisk;
201 }
202 
210 {
211  d->saveToDisk = allow;
212 }
213 
220 {
221  return d->url;
222 }
223 
232 {
233  d->url = url;
234  d->url.setPassword(QString());
235  d->url.setFragment(QString());
236 }
237 
245 {
246  return d->headers;
247 }
248 
255 {
256  d->headers = list;
257 }
258 
263 {
264  return d->lastModified;
265 }
266 
271 {
272  d->lastModified = dateTime;
273 }
274 
279 {
280  return d->expirationDate;
281 }
282 
287 {
288  d->expirationDate = dateTime;
289 }
290 
302 {
303  return d->attributes;
304 }
305 
317 {
319 }
320 
333 {
334  QNetworkCacheMetaDataPrivate::save(out, metaData);
335  return out;
336 }
337 
339 {
340  out << quint32(hash.size());
343  while (it != begin) {
344  --it;
345  out << int(it.key()) << it.value();
346  }
347  return out;
348 }
349 
351 {
352  // note: if you change the contents of the meta data here
353  // remember to bump the cache version in qnetworkdiskcache.cpp CurrentCacheVersion
354  out << metaData.url();
355  out << metaData.expirationDate();
356  out << metaData.lastModified();
357  out << metaData.saveToDisk();
358  out << metaData.attributes();
359  out << metaData.rawHeaders();
360 }
361 
374 {
376  return in;
377 }
378 
380 {
381  hash.clear();
382  QDataStream::Status oldStatus = in.status();
383  in.resetStatus();
384  hash.clear();
385 
386  quint32 n;
387  in >> n;
388 
389  for (quint32 i = 0; i < n; ++i) {
390  if (in.status() != QDataStream::Ok)
391  break;
392 
393  int k;
394  QVariant t;
395  in >> k >> t;
397  }
398 
399  if (in.status() != QDataStream::Ok)
400  hash.clear();
401  if (oldStatus != QDataStream::Ok)
402  in.setStatus(oldStatus);
403  return in;
404 }
405 
407 {
408  in >> metaData.d->url;
409  in >> metaData.d->expirationDate;
410  in >> metaData.d->lastModified;
411  in >> metaData.d->saveToDisk;
412  in >> metaData.d->attributes;
413  in >> metaData.d->headers;
414 }
415 
437  : QObject(*new QAbstractNetworkCachePrivate, parent)
438 {
439 }
440 
445  : QObject(dd, parent)
446 {
447 }
448 
457 {
458 }
459 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
~QNetworkCacheMetaData()
Destroys the network cache meta data.
QNetworkCacheMetaData()
Constructs an invalid network cache meta data.
double d
Definition: qnumeric_p.h:62
Status status() const
Returns the status of the data stream.
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
void setSaveToDisk(bool allow)
Sets whether this network cache meta data and associated content should be allowed to be stored on di...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void clear()
Removes all items from the hash.
Definition: qhash.h:574
Status
This enum describes the current status of the data stream.
Definition: qdatastream.h:101
#define it(className, varName)
bool isValid() const
Returns true if this network cache meta data has attributes that have been set otherwise false...
const_iterator ConstIterator
Qt-style synonym for QHash::const_iterator.
Definition: qhash.h:474
void setExpirationDate(const QDateTime &dateTime)
Sets the date and time when the meta data expires to dateTime.
static void load(QDataStream &in, QNetworkCacheMetaData &metaData)
void setAttributes(const AttributesMap &attributes)
Sets all attributes of this cache item to be the map attributes.
void setStatus(Status status)
Sets the status of the data stream to the status given.
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void setLastModified(const QDateTime &dateTime)
Sets the date and time when the meta data was last modified to dateTime.
QDateTime expirationDate() const
Returns the date and time when the meta data expires.
QUrl url() const
Returns the URL this network cache meta data is referring to.
RawHeaderList rawHeaders() const
Returns a list of all raw headers that are set in this meta data.
void setUrl(const QUrl &url)
Sets the URL this network cache meta data to to be url.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QDataStream & operator<<(QDataStream &out, const QNetworkCacheMetaData &metaData)
Writes metaData to the out stream.
QDateTime lastModified() const
Returns the date and time when the meta data was last modified.
bool operator==(const QNetworkCacheMetaDataPrivate &other) const
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
AttributesMap attributes() const
Returns all the attributes stored with this cache item.
static void save(QDataStream &out, const QNetworkCacheMetaData &metaData)
QSharedDataPointer< QNetworkCacheMetaDataPrivate > d
QDataStream & operator>>(QDataStream &in, QNetworkCacheMetaData &metaData)
Reads a QNetworkCacheMetaData from the stream in into metaData.
QNetworkCacheMetaData::AttributesMap attributes
void setFragment(const QString &fragment)
Sets the fragment of the URL to fragment.
Definition: qurl.cpp:5669
Attribute
Attribute codes for the QNetworkRequest and QNetworkReply.
virtual ~QAbstractNetworkCache()
Destroys the cache.
void setRawHeaders(const RawHeaderList &headers)
Sets the raw headers to list.
QNetworkCacheMetaData & operator=(const QNetworkCacheMetaData &other)
Makes a copy of the other QNetworkCacheMetaData and returns a reference to the copy.
int size() const
Returns the number of items in the hash.
Definition: qhash.h:295
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
The QSharedData class is a base class for shared data objects.
Definition: qshareddata.h:56
void resetStatus()
Resets the status of the data stream.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
unsigned int quint32
Definition: qglobal.h:938
bool operator==(const QNetworkCacheMetaData &other) const
Returns true if this meta data is equal to the other meta data; otherwise returns false...
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:464
QAbstractNetworkCache(QObject *parent=0)
Constructs an abstract network cache with the given parent.
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
iterator insertMulti(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:772
QNetworkCacheMetaData::RawHeaderList headers
bool saveToDisk() const
Returns is this cache should be allowed to be stored on disk.
void setPassword(const QString &password)
Sets the URL&#39;s password to password.
Definition: qurl.cpp:4736
The QNetworkCacheMetaData class provides cache information.