author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 16 Apr 2010 15:50:13 +0300 | |
changeset 18 | 2f34d5167611 |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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 |
||
50 |
QT_BEGIN_NAMESPACE |
|
51 |
||
52 |
class QNetworkCacheMetaDataPrivate : public QSharedData |
|
53 |
{ |
|
54 |
||
55 |
public: |
|
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 |
}; |
|
81 |
Q_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 |
*/ |
|
120 |
QNetworkCacheMetaData::QNetworkCacheMetaData() |
|
121 |
: d(new QNetworkCacheMetaDataPrivate) |
|
122 |
{ |
|
123 |
} |
|
124 |
||
125 |
/*! |
|
126 |
Destroys the network cache meta data. |
|
127 |
*/ |
|
128 |
QNetworkCacheMetaData::~QNetworkCacheMetaData() |
|
129 |
{ |
|
130 |
// QSharedDataPointer takes care of freeing d |
|
131 |
} |
|
132 |
||
133 |
/*! |
|
134 |
Constructs a copy of the \a other QNetworkCacheMetaData. |
|
135 |
*/ |
|
136 |
QNetworkCacheMetaData::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 |
*/ |
|
144 |
QNetworkCacheMetaData &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 |
*/ |
|
155 |
bool 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 |
*/ |
|
175 |
bool 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 |
*/ |
|
192 |
bool 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 |
*/ |
|
203 |
void 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 |
*/ |
|
213 |
QUrl 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 |
*/ |
|
225 |
void 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 |
*/ |
|
238 |
QNetworkCacheMetaData::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 |
*/ |
|
248 |
void 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 |
*/ |
|
256 |
QDateTime 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 |
*/ |
|
264 |
void 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 |
*/ |
|
272 |
QDateTime 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 |
*/ |
|
280 |
void QNetworkCacheMetaData::setExpirationDate(const QDateTime &dateTime) |
|
281 |
{ |
|
282 |
d->expirationDate = dateTime; |
|
283 |
} |
|
284 |
||
285 |
/*! |
|
286 |
\since 4.6 |
|
287 |
||
288 |
Returns all the attributes stored with this cache item. |
|
289 |
||
290 |
\sa setAttributes(), QNetworkRequest::Attribute |
|
291 |
*/ |
|
292 |
QNetworkCacheMetaData::AttributesMap QNetworkCacheMetaData::attributes() const |
|
293 |
{ |
|
294 |
return d->attributes; |
|
295 |
} |
|
296 |
||
297 |
/*! |
|
298 |
\since 4.6 |
|
299 |
||
300 |
Sets all attributes of this cache item to be the map \a attributes. |
|
301 |
||
302 |
\sa attributes(), QNetworkRequest::setAttribute() |
|
303 |
*/ |
|
304 |
void QNetworkCacheMetaData::setAttributes(const AttributesMap &attributes) |
|
305 |
{ |
|
306 |
d->attributes = attributes; |
|
307 |
} |
|
308 |
||
309 |
/*! |
|
310 |
\relates QNetworkCacheMetaData |
|
311 |
\since 4.5 |
|
312 |
||
313 |
Writes \a metaData to the \a out stream. |
|
314 |
||
315 |
\sa {Format of the QDataStream operators} |
|
316 |
*/ |
|
317 |
QDataStream &operator<<(QDataStream &out, const QNetworkCacheMetaData &metaData) |
|
318 |
{ |
|
319 |
QNetworkCacheMetaDataPrivate::save(out, metaData); |
|
320 |
return out; |
|
321 |
} |
|
322 |
||
323 |
static inline QDataStream &operator<<(QDataStream &out, const QNetworkCacheMetaData::AttributesMap &hash) |
|
324 |
{ |
|
325 |
out << quint32(hash.size()); |
|
326 |
QNetworkCacheMetaData::AttributesMap::ConstIterator it = hash.end(); |
|
327 |
QNetworkCacheMetaData::AttributesMap::ConstIterator begin = hash.begin(); |
|
328 |
while (it != begin) { |
|
329 |
--it; |
|
330 |
out << int(it.key()) << it.value(); |
|
331 |
} |
|
332 |
return out; |
|
333 |
} |
|
334 |
||
335 |
void QNetworkCacheMetaDataPrivate::save(QDataStream &out, const QNetworkCacheMetaData &metaData) |
|
336 |
{ |
|
337 |
// note: if you change the contents of the meta data here |
|
338 |
// remember to bump the cache version in qnetworkdiskcache.cpp CurrentCacheVersion |
|
339 |
out << metaData.url(); |
|
340 |
out << metaData.expirationDate(); |
|
341 |
out << metaData.lastModified(); |
|
342 |
out << metaData.saveToDisk(); |
|
343 |
out << metaData.attributes(); |
|
344 |
out << metaData.rawHeaders(); |
|
345 |
} |
|
346 |
||
347 |
/*! |
|
348 |
\relates QNetworkCacheMetaData |
|
349 |
\since 4.5 |
|
350 |
||
351 |
Reads a QNetworkCacheMetaData from the stream \a in into \a metaData. |
|
352 |
||
353 |
\sa {Format of the QDataStream operators} |
|
354 |
*/ |
|
355 |
QDataStream &operator>>(QDataStream &in, QNetworkCacheMetaData &metaData) |
|
356 |
{ |
|
357 |
QNetworkCacheMetaDataPrivate::load(in, metaData); |
|
358 |
return in; |
|
359 |
} |
|
360 |
||
361 |
static inline QDataStream &operator>>(QDataStream &in, QNetworkCacheMetaData::AttributesMap &hash) |
|
362 |
{ |
|
363 |
hash.clear(); |
|
364 |
QDataStream::Status oldStatus = in.status(); |
|
365 |
in.resetStatus(); |
|
366 |
hash.clear(); |
|
367 |
||
368 |
quint32 n; |
|
369 |
in >> n; |
|
370 |
||
371 |
for (quint32 i = 0; i < n; ++i) { |
|
372 |
if (in.status() != QDataStream::Ok) |
|
373 |
break; |
|
374 |
||
375 |
int k; |
|
376 |
QVariant t; |
|
377 |
in >> k >> t; |
|
378 |
hash.insertMulti(QNetworkRequest::Attribute(k), t); |
|
379 |
} |
|
380 |
||
381 |
if (in.status() != QDataStream::Ok) |
|
382 |
hash.clear(); |
|
383 |
if (oldStatus != QDataStream::Ok) |
|
384 |
in.setStatus(oldStatus); |
|
385 |
return in; |
|
386 |
} |
|
387 |
||
388 |
void QNetworkCacheMetaDataPrivate::load(QDataStream &in, QNetworkCacheMetaData &metaData) |
|
389 |
{ |
|
390 |
in >> metaData.d->url; |
|
391 |
in >> metaData.d->expirationDate; |
|
392 |
in >> metaData.d->lastModified; |
|
393 |
in >> metaData.d->saveToDisk; |
|
394 |
in >> metaData.d->attributes; |
|
395 |
in >> metaData.d->headers; |
|
396 |
} |
|
397 |
||
398 |
/*! |
|
399 |
\class QAbstractNetworkCache |
|
400 |
\since 4.5 |
|
401 |
\inmodule QtNetwork |
|
402 |
||
403 |
\brief The QAbstractNetworkCache class provides the interface for cache implementations. |
|
404 |
||
405 |
QAbstractNetworkCache is the base class for every standard cache that is used be |
|
406 |
QNetworkAccessManager. QAbstractNetworkCache is an abstract class and cannot be |
|
407 |
instantiated. |
|
408 |
||
409 |
\sa QNetworkDiskCache |
|
410 |
*/ |
|
411 |
||
412 |
/*! |
|
413 |
Constructs an abstract network cache with the given \a parent. |
|
414 |
*/ |
|
415 |
QAbstractNetworkCache::QAbstractNetworkCache(QObject *parent) |
|
416 |
: QObject(*new QAbstractNetworkCachePrivate, parent) |
|
417 |
{ |
|
418 |
} |
|
419 |
||
420 |
/*! |
|
421 |
\internal |
|
422 |
*/ |
|
423 |
QAbstractNetworkCache::QAbstractNetworkCache(QAbstractNetworkCachePrivate &dd, QObject *parent) |
|
424 |
: QObject(dd, parent) |
|
425 |
{ |
|
426 |
} |
|
427 |
||
428 |
/*! |
|
429 |
Destroys the cache. |
|
430 |
||
431 |
Any operations that have not been inserted are discarded. |
|
432 |
||
433 |
\sa insert() |
|
434 |
*/ |
|
435 |
QAbstractNetworkCache::~QAbstractNetworkCache() |
|
436 |
{ |
|
437 |
} |
|
438 |
||
439 |
/*! |
|
440 |
\fn QNetworkCacheMetaData QAbstractNetworkCache::metaData(const QUrl &url) = 0 |
|
441 |
Returns the meta data for the url \a url. |
|
442 |
||
443 |
If the url is valid and the cache contains the data for url, |
|
444 |
a valid QNetworkCacheMetaData is returned. |
|
445 |
||
446 |
In the base class this is a pure virtual function. |
|
447 |
||
448 |
\sa updateMetaData(), data() |
|
449 |
*/ |
|
450 |
||
451 |
/*! |
|
452 |
\fn void QAbstractNetworkCache::updateMetaData(const QNetworkCacheMetaData &metaData) = 0 |
|
453 |
Updates the cache meta date for the metaData's url to \a metaData |
|
454 |
||
455 |
If the cache does not contains a cache item for the url then no action is taken. |
|
456 |
||
457 |
In the base class this is a pure virtual function. |
|
458 |
||
459 |
\sa metaData(), prepare() |
|
460 |
*/ |
|
461 |
||
462 |
/*! |
|
463 |
\fn QIODevice *QAbstractNetworkCache::data(const QUrl &url) = 0 |
|
464 |
Returns the data associated with \a url. |
|
465 |
||
466 |
It is up to the application that requests the data to delete |
|
467 |
the QIODevice when done with it. |
|
468 |
||
469 |
If there is no cache for \a url, the url is invalid, or if there |
|
470 |
is an internal cache error 0 is returned. |
|
471 |
||
472 |
In the base class this is a pure virtual function. |
|
473 |
||
474 |
\sa metaData(), prepare() |
|
475 |
*/ |
|
476 |
||
477 |
/*! |
|
478 |
\fn bool QAbstractNetworkCache::remove(const QUrl &url) = 0 |
|
479 |
Removes the cache entry for \a url, returning true if success otherwise false. |
|
480 |
||
481 |
In the base class this is a pure virtual function. |
|
482 |
||
483 |
\sa clear(), prepare() |
|
484 |
*/ |
|
485 |
||
486 |
/*! |
|
487 |
\fn QIODevice *QAbstractNetworkCache::prepare(const QNetworkCacheMetaData &metaData) = 0 |
|
488 |
Returns the device that should be populated with the data for |
|
489 |
the cache item \a metaData. When all of the data has been written |
|
490 |
insert() should be called. If metaData is invalid or the url in |
|
491 |
the metadata is invalid 0 is returned. |
|
492 |
||
493 |
The cache owns the device and will take care of deleting it when |
|
494 |
it is inserted or removed. |
|
495 |
||
496 |
To cancel a prepared inserted call remove() on the metadata's url. |
|
497 |
||
498 |
In the base class this is a pure virtual function. |
|
499 |
||
500 |
\sa remove(), updateMetaData(), insert() |
|
501 |
*/ |
|
502 |
||
503 |
/*! |
|
504 |
\fn void QAbstractNetworkCache::insert(QIODevice *device) = 0 |
|
505 |
Inserts the data in \a device and the prepared meta data into the cache. |
|
506 |
After this function is called the data and meta data should be retrievable |
|
507 |
using data() and metaData(). |
|
508 |
||
509 |
To cancel a prepared inserted call remove() on the metadata's url. |
|
510 |
||
511 |
In the base class this is a pure virtual function. |
|
512 |
||
513 |
\sa prepare(), remove() |
|
514 |
*/ |
|
515 |
||
516 |
/*! |
|
517 |
\fn qint64 QAbstractNetworkCache::cacheSize() const = 0 |
|
518 |
Returns the current size taken up by the cache. Depending upon |
|
519 |
the cache implementation this might be disk or memory size. |
|
520 |
||
521 |
In the base class this is a pure virtual function. |
|
522 |
||
523 |
\sa clear() |
|
524 |
*/ |
|
525 |
||
526 |
/*! |
|
527 |
\fn void QAbstractNetworkCache::clear() = 0 |
|
528 |
Removes all items from the cache. Unless there was failures |
|
529 |
clearing the cache cacheSize() should return 0 after a call to clear. |
|
530 |
||
531 |
In the base class this is a pure virtual function. |
|
532 |
||
533 |
\sa cacheSize(), remove() |
|
534 |
*/ |
|
535 |
||
536 |
QT_END_NAMESPACE |