source: trunk/src/network/access/qabstractnetworkcache.cpp@ 157

Last change on this file since 157 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qabstractnetworkcache.h"
43#include "qabstractnetworkcache_p.h"
44
45#include <qdatetime.h>
46#include <qurl.h>
47
48#include <qdebug.h>
49
50QT_BEGIN_NAMESPACE
51
52class QNetworkCacheMetaDataPrivate : public QSharedData
53{
54
55public:
56 QNetworkCacheMetaDataPrivate()
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
71 QUrl url;
72 QDateTime lastModified;
73 QDateTime expirationDate;
74 QNetworkCacheMetaData::RawHeaderList headers;
75 QNetworkCacheMetaData::AttributesMap attributes;
76 bool saveToDisk;
77
78 static void save(QDataStream &out, const QNetworkCacheMetaData &metaData);
79 static void load(QDataStream &in, QNetworkCacheMetaData &metaData);
80};
81Q_GLOBAL_STATIC(QNetworkCacheMetaDataPrivate, metadata_shared_invalid)
82
83/*!
84 \class QNetworkCacheMetaData
85 \since 4.5
86 \inmodule QtNetwork
87
88 \brief The QNetworkCacheMetaData class provides cache information.
89
90 QNetworkCacheMetaData provides information about a cache file including
91 the url, when it was last modified, when the cache file was created, headers
92 for file and if the file should be saved onto a disk.
93
94 \sa QAbstractNetworkCache
95*/
96
97/*!
98 \typedef QNetworkCacheMetaData::RawHeader
99
100 Synonym for QPair<QByteArray, QByteArray>
101*/
102
103/*!
104 \typedef QNetworkCacheMetaData::RawHeaderList
105
106 Synonym for QList<RawHeader>
107*/
108
109/*!
110 \typedef QNetworkCacheMetaData::AttributesMap
111
112 Synonym for QHash<QNetworkRequest::Attribute, QVariant>
113*/
114
115/*!
116 Constructs an invalid network cache meta data.
117
118 \sa isValid()
119 */
120QNetworkCacheMetaData::QNetworkCacheMetaData()
121 : d(new QNetworkCacheMetaDataPrivate)
122{
123}
124
125/*!
126 Destroys the network cache meta data.
127 */
128QNetworkCacheMetaData::~QNetworkCacheMetaData()
129{
130 // QSharedDataPointer takes care of freeing d
131}
132
133/*!
134 Constructs a copy of the \a other QNetworkCacheMetaData.
135 */
136QNetworkCacheMetaData::QNetworkCacheMetaData(const QNetworkCacheMetaData &other)
137 : d(other.d)
138{
139}
140
141/*!
142 Makes a copy of the \a other QNetworkCacheMetaData and returns a reference to the copy.
143 */
144QNetworkCacheMetaData &QNetworkCacheMetaData::operator=(const QNetworkCacheMetaData &other)
145{
146 d = other.d;
147 return *this;
148}
149
150/*!
151 Returns true if this meta data is equal to the \a other meta data; otherwise returns false.
152
153 \sa operator!=()
154 */
155bool QNetworkCacheMetaData::operator==(const QNetworkCacheMetaData &other) const
156{
157 if (d == other.d)
158 return true;
159 if (d && other.d)
160 return *d == *other.d;
161 return false;
162}
163
164/*!
165 \fn bool QNetworkCacheMetaData::operator!=(const QNetworkCacheMetaData &other) const
166
167 Returns true if this meta data is not equal to the \a other meta data; otherwise returns false.
168
169 \sa operator==()
170 */
171
172/*!
173 Returns true if this network cache meta data has attributes that have been set otherwise false.
174 */
175bool QNetworkCacheMetaData::isValid() const
176{
177 return !(*d == *metadata_shared_invalid());
178}
179
180/*!
181 Returns is this cache should be allowed to be stored on disk.
182
183 Some cache implementations can keep these cache items in memory for performance reasons,
184 but for security reasons they should not be written to disk.
185
186 Specifically with http, documents marked with Pragma: no-cache, or have a Cache-control set to
187 no-store or no-cache or any https document that doesn't have "Cache-control: public" set will
188 set the saveToDisk to false.
189
190 \sa setSaveToDisk()
191 */
192bool QNetworkCacheMetaData::saveToDisk() const
193{
194 return d->saveToDisk;
195}
196
197/*!
198 Sets whether this network cache meta data and associated content should be
199 allowed to be stored on disk to \a allow.
200
201 \sa saveToDisk()
202 */
203void QNetworkCacheMetaData::setSaveToDisk(bool allow)
204{
205 d->saveToDisk = allow;
206}
207
208/*!
209 Returns the URL this network cache meta data is referring to.
210
211 \sa setUrl()
212 */
213QUrl QNetworkCacheMetaData::url() const
214{
215 return d->url;
216}
217
218/*!
219 Sets the URL this network cache meta data to to be \a url.
220
221 The password and fragment are removed from the url.
222
223 \sa url()
224 */
225void QNetworkCacheMetaData::setUrl(const QUrl &url)
226{
227 d->url = url;
228 d->url.setPassword(QString());
229 d->url.setFragment(QString());
230}
231
232/*!
233 Returns a list of all raw headers that are set in this meta data.
234 The list is in the same order that the headers were set.
235
236 \sa setRawHeaders()
237 */
238QNetworkCacheMetaData::RawHeaderList QNetworkCacheMetaData::rawHeaders() const
239{
240 return d->headers;
241}
242
243/*!
244 Sets the raw headers to \a list.
245
246 \sa rawHeaders()
247 */
248void QNetworkCacheMetaData::setRawHeaders(const RawHeaderList &list)
249{
250 d->headers = list;
251}
252
253/*!
254 Returns the date and time when the meta data was last modified.
255 */
256QDateTime QNetworkCacheMetaData::lastModified() const
257{
258 return d->lastModified;
259}
260
261/*!
262 Sets the date and time when the meta data was last modified to \a dateTime.
263 */
264void QNetworkCacheMetaData::setLastModified(const QDateTime &dateTime)
265{
266 d->lastModified = dateTime;
267}
268
269/*!
270 Returns the date and time when the meta data expires.
271 */
272QDateTime QNetworkCacheMetaData::expirationDate() const
273{
274 return d->expirationDate;
275}
276
277/*!
278 Sets the date and time when the meta data expires to \a dateTime.
279 */
280void QNetworkCacheMetaData::setExpirationDate(const QDateTime &dateTime)
281{
282 d->expirationDate = dateTime;
283}
284
285/*!
286 Returns all the attributes stored with this cache item.
287
288 \sa setAttributes(), QNetworkRequest::Attribute
289*/
290QNetworkCacheMetaData::AttributesMap QNetworkCacheMetaData::attributes() const
291{
292 return d->attributes;
293}
294
295/*!
296 Sets all attributes of this cache item to be the map \a attributes.
297
298 \sa attributes(), QNetworkRequest::setAttribute()
299*/
300void QNetworkCacheMetaData::setAttributes(const AttributesMap &attributes)
301{
302 d->attributes = attributes;
303}
304
305/*!
306 \relates QNetworkCacheMetaData
307 \since 4.5
308
309 Writes \a metaData to the \a out stream.
310
311 \sa {Format of the QDataStream operators}
312*/
313QDataStream &operator<<(QDataStream &out, const QNetworkCacheMetaData &metaData)
314{
315 QNetworkCacheMetaDataPrivate::save(out, metaData);
316 return out;
317}
318
319static inline QDataStream &operator<<(QDataStream &out, const QNetworkCacheMetaData::AttributesMap &hash)
320{
321 out << quint32(hash.size());
322 QNetworkCacheMetaData::AttributesMap::ConstIterator it = hash.end();
323 QNetworkCacheMetaData::AttributesMap::ConstIterator begin = hash.begin();
324 while (it != begin) {
325 --it;
326 out << int(it.key()) << it.value();
327 }
328 return out;
329}
330
331void QNetworkCacheMetaDataPrivate::save(QDataStream &out, const QNetworkCacheMetaData &metaData)
332{
333 // note: if you change the contents of the meta data here
334 // remember to bump the cache version in qnetworkdiskcache.cpp CurrentCacheVersion
335 out << metaData.url();
336 out << metaData.expirationDate();
337 out << metaData.lastModified();
338 out << metaData.saveToDisk();
339 out << metaData.attributes();
340 out << metaData.rawHeaders();
341}
342
343/*!
344 \relates QNetworkCacheMetaData
345 \since 4.5
346
347 Reads a QNetworkCacheMetaData from the stream \a in into \a metaData.
348
349 \sa {Format of the QDataStream operators}
350*/
351QDataStream &operator>>(QDataStream &in, QNetworkCacheMetaData &metaData)
352{
353 QNetworkCacheMetaDataPrivate::load(in, metaData);
354 return in;
355}
356
357static inline QDataStream &operator>>(QDataStream &in, QNetworkCacheMetaData::AttributesMap &hash)
358{
359 hash.clear();
360 QDataStream::Status oldStatus = in.status();
361 in.resetStatus();
362 hash.clear();
363
364 quint32 n;
365 in >> n;
366
367 for (quint32 i = 0; i < n; ++i) {
368 if (in.status() != QDataStream::Ok)
369 break;
370
371 int k;
372 QVariant t;
373 in >> k >> t;
374 hash.insertMulti(QNetworkRequest::Attribute(k), t);
375 }
376
377 if (in.status() != QDataStream::Ok)
378 hash.clear();
379 if (oldStatus != QDataStream::Ok)
380 in.setStatus(oldStatus);
381 return in;
382}
383
384void QNetworkCacheMetaDataPrivate::load(QDataStream &in, QNetworkCacheMetaData &metaData)
385{
386 in >> metaData.d->url;
387 in >> metaData.d->expirationDate;
388 in >> metaData.d->lastModified;
389 in >> metaData.d->saveToDisk;
390 in >> metaData.d->attributes;
391 in >> metaData.d->headers;
392}
393
394/*!
395 \class QAbstractNetworkCache
396 \since 4.5
397 \inmodule QtNetwork
398
399 \brief The QAbstractNetworkCache class provides the interface for cache implementations.
400
401 QAbstractNetworkCache is the base class for every standard cache that is used be
402 QNetworkAccessManager. QAbstractNetworkCache is an abstract class and cannot be
403 instantiated.
404
405 \sa QNetworkDiskCache
406*/
407
408/*!
409 Constructs an abstract network cache with the given \a parent.
410*/
411QAbstractNetworkCache::QAbstractNetworkCache(QObject *parent)
412 : QObject(*new QAbstractNetworkCachePrivate, parent)
413{
414}
415
416/*!
417 \internal
418*/
419QAbstractNetworkCache::QAbstractNetworkCache(QAbstractNetworkCachePrivate &dd, QObject *parent)
420 : QObject(dd, parent)
421{
422}
423
424/*!
425 Destroys the cache.
426
427 Any operations that have not been inserted are discarded.
428
429 \sa insert()
430 */
431QAbstractNetworkCache::~QAbstractNetworkCache()
432{
433}
434
435/*!
436 \fn QNetworkCacheMetaData QAbstractNetworkCache::metaData(const QUrl &url) = 0
437 Returns the meta data for the url \a url.
438
439 If the url is valid and the cache contains the data for url,
440 a valid QNetworkCacheMetaData is returned.
441
442 In the base class this is a pure virtual function.
443
444 \sa updateMetaData(), data()
445*/
446
447/*!
448 \fn void QAbstractNetworkCache::updateMetaData(const QNetworkCacheMetaData &metaData) = 0
449 Updates the cache meta date for the metaData's url to \a metaData
450
451 If the cache does not contains a cache item for the url then no action is taken.
452
453 In the base class this is a pure virtual function.
454
455 \sa metaData(), prepare()
456*/
457
458/*!
459 \fn QIODevice *QAbstractNetworkCache::data(const QUrl &url) = 0
460 Returns the data associated with \a url.
461
462 It is up to the application that requests the data to delete
463 the QIODevice when done with it.
464
465 If there is no cache for \a url, the url is invalid, or if there
466 is an internal cache error 0 is returned.
467
468 In the base class this is a pure virtual function.
469
470 \sa metaData(), prepare()
471*/
472
473/*!
474 \fn bool QAbstractNetworkCache::remove(const QUrl &url) = 0
475 Removes the cache entry for \a url, returning true if success otherwise false.
476
477 In the base class this is a pure virtual function.
478
479 \sa clear(), prepare()
480*/
481
482/*!
483 \fn QIODevice *QAbstractNetworkCache::prepare(const QNetworkCacheMetaData &metaData) = 0
484 Returns the device that should be populated with the data for
485 the cache item \a metaData. When all of the data has been written
486 insert() should be called. If metaData is invalid or the url in
487 the metadata is invalid 0 is returned.
488
489 The cache owns the device and will take care of deleting it when
490 it is inserted or removed.
491
492 To cancel a prepared inserted call remove() on the metadata's url.
493
494 In the base class this is a pure virtual function.
495
496 \sa remove(), updateMetaData(), insert()
497*/
498
499/*!
500 \fn void QAbstractNetworkCache::insert(QIODevice *device) = 0
501 Inserts the data in \a device and the prepared meta data into the cache.
502 After this function is called the data and meta data should be retrievable
503 using data() and metaData().
504
505 To cancel a prepared inserted call remove() on the metadata's url.
506
507 In the base class this is a pure virtual function.
508
509 \sa prepare(), remove()
510*/
511
512/*!
513 \fn qint64 QAbstractNetworkCache::cacheSize() const = 0
514 Returns the current size taken up by the cache. Depending upon
515 the cache implementation this might be disk or memory size.
516
517 In the base class this is a pure virtual function.
518
519 \sa clear()
520*/
521
522/*!
523 \fn void QAbstractNetworkCache::clear() = 0
524 Removes all items from the cache. Unless there was failures
525 clearing the cache cacheSize() should return 0 after a call to clear.
526
527 In the base class this is a pure virtual function.
528
529 \sa cacheSize(), remove()
530*/
531
532QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.