Qt 4.8
qnetworkdiskcache.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 //#define QNETWORKDISKCACHE_DEBUG
43 
44 
45 #include "qnetworkdiskcache.h"
46 #include "qnetworkdiskcache_p.h"
47 #include "QtCore/qscopedpointer.h"
48 
49 #include <qfile.h>
50 #include <qdir.h>
51 #include <qdatetime.h>
52 #include <qdiriterator.h>
53 #include <qurl.h>
54 #include <qcryptographichash.h>
55 #include <qdebug.h>
56 
57 #define CACHE_POSTFIX QLatin1String(".d")
58 #define PREPARED_SLASH QLatin1String("prepared/")
59 #define CACHE_VERSION 7
60 #define DATA_DIR QLatin1String("data")
61 
62 #define MAX_COMPRESSION_SIZE (1024 * 1024 * 3)
63 
64 #ifndef QT_NO_NETWORKDISKCACHE
65 
67 
113 {
114 }
115 
120 {
122  QHashIterator<QIODevice*, QCacheItem*> it(d->inserting);
123  while (it.hasNext()) {
124  it.next();
125  delete it.value();
126  }
127 }
128 
133 {
134  Q_D(const QNetworkDiskCache);
135  return d->cacheDirectory;
136 }
137 
149 {
150 #if defined(QNETWORKDISKCACHE_DEBUG)
151  qDebug() << "QNetworkDiskCache::setCacheDirectory()" << cacheDir;
152 #endif
154  if (cacheDir.isEmpty())
155  return;
156  d->cacheDirectory = cacheDir;
157  QDir dir(d->cacheDirectory);
158  d->cacheDirectory = dir.absolutePath();
159  if (!d->cacheDirectory.endsWith(QLatin1Char('/')))
160  d->cacheDirectory += QLatin1Char('/');
161 
162  d->dataDirectory = d->cacheDirectory + DATA_DIR + QString::number(CACHE_VERSION) + QLatin1Char('/');
163  d->prepareLayout();
164 }
165 
170 {
171 #if defined(QNETWORKDISKCACHE_DEBUG)
172  qDebug() << "QNetworkDiskCache::cacheSize()";
173 #endif
174  Q_D(const QNetworkDiskCache);
175  if (d->cacheDirectory.isEmpty())
176  return 0;
177  if (d->currentCacheSize < 0) {
178  QNetworkDiskCache *that = const_cast<QNetworkDiskCache*>(this);
179  that->d_func()->currentCacheSize = that->expire();
180  }
181  return d->currentCacheSize;
182 }
183 
188 {
189 #if defined(QNETWORKDISKCACHE_DEBUG)
190  qDebug() << "QNetworkDiskCache::prepare()" << metaData.url();
191 #endif
193  if (!metaData.isValid() || !metaData.url().isValid() || !metaData.saveToDisk())
194  return 0;
195 
196  if (d->cacheDirectory.isEmpty()) {
197  qWarning() << "QNetworkDiskCache::prepare() The cache directory is not set";
198  return 0;
199  }
200 
201  foreach (QNetworkCacheMetaData::RawHeader header, metaData.rawHeaders()) {
202  if (header.first.toLower() == "content-length") {
203  qint64 size = header.second.toInt();
204  if (size > (maximumCacheSize() * 3)/4)
205  return 0;
206  break;
207  }
208  }
209  QScopedPointer<QCacheItem> cacheItem(new QCacheItem);
210  cacheItem->metaData = metaData;
211 
212  QIODevice *device = 0;
213  if (cacheItem->canCompress()) {
214  cacheItem->data.open(QBuffer::ReadWrite);
215  device = &(cacheItem->data);
216  } else {
217  QString templateName = d->tmpCacheFileName();
218  QT_TRY {
219  cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data);
220  } QT_CATCH(...) {
221  cacheItem->file = 0;
222  }
223  if (!cacheItem->file || !cacheItem->file->open()) {
224  qWarning() << "QNetworkDiskCache::prepare() unable to open temporary file";
225  cacheItem.reset();
226  return 0;
227  }
228  cacheItem->writeHeader(cacheItem->file);
229  device = cacheItem->file;
230  }
231  d->inserting[device] = cacheItem.take();
232  return device;
233 }
234 
239 {
240 #if defined(QNETWORKDISKCACHE_DEBUG)
241  qDebug() << "QNetworkDiskCache::insert()" << device;
242 #endif
244  QHash<QIODevice*, QCacheItem*>::iterator it = d->inserting.find(device);
245  if (it == d->inserting.end()) {
246  qWarning() << "QNetworkDiskCache::insert() called on a device we don't know about" << device;
247  return;
248  }
249 
250  d->storeItem(it.value());
251  delete it.value();
252  d->inserting.erase(it);
253 }
254 
255 
261 {
262  QDir helper;
264 
265  //Create directory and subdirectories 0-F
266  helper.mkpath(dataDirectory);
267  for (uint i = 0; i < 16 ; i++) {
268  QString str = QString::number(i, 16);
269  QString subdir = dataDirectory + str;
270  helper.mkdir(subdir);
271  }
272 }
273 
274 
276 {
278  Q_ASSERT(cacheItem->metaData.saveToDisk());
279 
280  QString fileName = cacheFileName(cacheItem->metaData.url());
281  Q_ASSERT(!fileName.isEmpty());
282 
283  if (QFile::exists(fileName)) {
284  if (!QFile::remove(fileName)) {
285  qWarning() << "QNetworkDiskCache: couldn't remove the cache file " << fileName;
286  return;
287  }
288  }
289 
290  if (currentCacheSize > 0)
291  currentCacheSize += 1024 + cacheItem->size();
292  currentCacheSize = q->expire();
293  if (!cacheItem->file) {
294  QString templateName = tmpCacheFileName();
295  cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data);
296  if (cacheItem->file->open()) {
297  cacheItem->writeHeader(cacheItem->file);
298  cacheItem->writeCompressedData(cacheItem->file);
299  }
300  }
301 
302  if (cacheItem->file
303  && cacheItem->file->isOpen()
304  && cacheItem->file->error() == QFile::NoError) {
305  cacheItem->file->setAutoRemove(false);
306  // ### use atomic rename rather then remove & rename
307  if (cacheItem->file->rename(fileName))
308  currentCacheSize += cacheItem->file->size();
309  else
310  cacheItem->file->setAutoRemove(true);
311  }
312  if (cacheItem->metaData.url() == lastItem.metaData.url())
313  lastItem.reset();
314 }
315 
320 {
321 #if defined(QNETWORKDISKCACHE_DEBUG)
322  qDebug() << "QNetworkDiskCache::remove()" << url;
323 #endif
325 
326  // remove is also used to cancel insertions, not a common operation
327  QHashIterator<QIODevice*, QCacheItem*> it(d->inserting);
328  while (it.hasNext()) {
329  it.next();
330  QCacheItem *item = it.value();
331  if (item && item->metaData.url() == url) {
332  delete item;
333  d->inserting.remove(it.key());
334  return true;
335  }
336  }
337 
338  if (d->lastItem.metaData.url() == url)
339  d->lastItem.reset();
340  return d->removeFile(d->cacheFileName(url));
341 }
342 
347 {
348 #if defined(QNETWORKDISKCACHE_DEBUG)
349  qDebug() << "QNetworkDiskCache::removFile()" << file;
350 #endif
351  if (file.isEmpty())
352  return false;
353  QFileInfo info(file);
354  QString fileName = info.fileName();
355  if (!fileName.endsWith(CACHE_POSTFIX))
356  return false;
357  qint64 size = info.size();
358  if (QFile::remove(file)) {
359  currentCacheSize -= size;
360  return true;
361  }
362  return false;
363 }
364 
369 {
370 #if defined(QNETWORKDISKCACHE_DEBUG)
371  qDebug() << "QNetworkDiskCache::metaData()" << url;
372 #endif
374  if (d->lastItem.metaData.url() == url)
375  return d->lastItem.metaData;
376  return fileMetaData(d->cacheFileName(url));
377 }
378 
385 {
386 #if defined(QNETWORKDISKCACHE_DEBUG)
387  qDebug() << "QNetworkDiskCache::fileMetaData()" << fileName;
388 #endif
389  Q_D(const QNetworkDiskCache);
390  QFile file(fileName);
391  if (!file.open(QFile::ReadOnly))
392  return QNetworkCacheMetaData();
393  if (!d->lastItem.read(&file, false)) {
394  file.close();
396  that->removeFile(fileName);
397  }
398  return d->lastItem.metaData;
399 }
400 
405 {
406 #if defined(QNETWORKDISKCACHE_DEBUG)
407  qDebug() << "QNetworkDiskCache::data()" << url;
408 #endif
411  if (!url.isValid())
412  return 0;
413  if (d->lastItem.metaData.url() == url && d->lastItem.data.isOpen()) {
414  buffer.reset(new QBuffer);
415  buffer->setData(d->lastItem.data.data());
416  } else {
417  QScopedPointer<QFile> file(new QFile(d->cacheFileName(url)));
419  return 0;
420 
421  if (!d->lastItem.read(file.data(), true)) {
422  file->close();
423  remove(url);
424  return 0;
425  }
426  if (d->lastItem.data.isOpen()) {
427  // compressed
428  buffer.reset(new QBuffer);
429  buffer->setData(d->lastItem.data.data());
430  } else {
431  buffer.reset(new QBuffer);
432  // ### verify that QFile uses the fd size and not the file name
433  qint64 size = file->size() - file->pos();
434  const uchar *p = 0;
435 #if !defined(Q_OS_WINCE) && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_SYMBIAN)
436  p = file->map(file->pos(), size);
437 #endif
438  if (p) {
439  buffer->setData((const char *)p, size);
440  file.take()->setParent(buffer.data());
441  } else {
442  buffer->setData(file->readAll());
443  }
444  }
445  }
446  buffer->open(QBuffer::ReadOnly);
447  return buffer.take();
448 }
449 
454 {
455 #if defined(QNETWORKDISKCACHE_DEBUG)
456  qDebug() << "QNetworkDiskCache::updateMetaData()" << metaData.url();
457 #endif
458  QUrl url = metaData.url();
459  QIODevice *oldDevice = data(url);
460  if (!oldDevice) {
461 #if defined(QNETWORKDISKCACHE_DEBUG)
462  qDebug() << "QNetworkDiskCache::updateMetaData(), no device!";
463 #endif
464  return;
465  }
466 
467  QIODevice *newDevice = prepare(metaData);
468  if (!newDevice) {
469 #if defined(QNETWORKDISKCACHE_DEBUG)
470  qDebug() << "QNetworkDiskCache::updateMetaData(), no new device!" << url;
471 #endif
472  return;
473  }
474  char data[1024];
475  while (!oldDevice->atEnd()) {
476  qint64 s = oldDevice->read(data, 1024);
477  newDevice->write(data, s);
478  }
479  delete oldDevice;
480  insert(newDevice);
481 }
482 
489 {
490  Q_D(const QNetworkDiskCache);
491  return d->maximumCacheSize;
492 }
493 
502 {
504  bool expireCache = (size < d->maximumCacheSize);
505  d->maximumCacheSize = size;
506  if (expireCache)
507  d->currentCacheSize = expire();
508 }
509 
529 {
531  if (d->currentCacheSize >= 0 && d->currentCacheSize < maximumCacheSize())
532  return d->currentCacheSize;
533 
534  if (cacheDirectory().isEmpty()) {
535  qWarning() << "QNetworkDiskCache::expire() The cache directory is not set";
536  return 0;
537  }
538 
539  // close file handle to prevent "in use" error when QFile::remove() is called
540  d->lastItem.reset();
541 
544 
546  qint64 totalSize = 0;
547  while (it.hasNext()) {
548  QString path = it.next();
549  QFileInfo info = it.fileInfo();
550  QString fileName = info.fileName();
551  if (fileName.endsWith(CACHE_POSTFIX)) {
552  cacheItems.insert(info.created(), path);
553  totalSize += info.size();
554  }
555  }
556 
557  int removedFiles = 0;
558  qint64 goal = (maximumCacheSize() * 9) / 10;
560  while (i != cacheItems.constEnd()) {
561  if (totalSize < goal)
562  break;
563  QString name = i.value();
564  QFile file(name);
565  qint64 size = file.size();
566  file.remove();
567  totalSize -= size;
568  ++removedFiles;
569  ++i;
570  }
571 #if defined(QNETWORKDISKCACHE_DEBUG)
572  if (removedFiles > 0) {
573  qDebug() << "QNetworkDiskCache::expire()"
574  << "Removed:" << removedFiles
575  << "Kept:" << cacheItems.count() - removedFiles;
576  }
577 #endif
578  return totalSize;
579 }
580 
585 {
586 #if defined(QNETWORKDISKCACHE_DEBUG)
587  qDebug() << "QNetworkDiskCache::clear()";
588 #endif
590  qint64 size = d->maximumCacheSize;
591  d->maximumCacheSize = 0;
592  d->currentCacheSize = expire();
593  d->maximumCacheSize = size;
594 }
595 
600 {
601  QUrl cleanUrl = url;
602  cleanUrl.setPassword(QString());
603  cleanUrl.setFragment(QString());
604 
606  hash.addData(cleanUrl.toEncoded());
607  // convert sha1 to base36 form and return first 8 bytes for use as string
608  QByteArray id = QByteArray::number(*(qlonglong*)hash.result().data(), 36).left(8);
609  // generates <one-char subdir>/<8-char filname.d>
610  uint code = (uint)id.at(id.length()-1) % 16;
611  QString pathFragment = QString::number(code, 16) + QLatin1Char('/')
613 
614  return pathFragment;
615 }
616 
618 {
619  //The subdirectory is presumed to be already read for use.
621 }
622 
627 {
628  if (!url.isValid())
629  return QString();
630 
631  QString fullpath = dataDirectory + uniqueFileName(url);
632  return fullpath;
633 }
634 
639 {
640  bool sizeOk = false;
641  bool typeOk = false;
643  if (header.first.toLower() == "content-length") {
644  qint64 size = header.second.toLongLong();
645  if (size > MAX_COMPRESSION_SIZE)
646  return false;
647  else
648  sizeOk = true;
649  }
650 
651  if (header.first.toLower() == "content-type") {
652  QByteArray type = header.second;
653  if (type.startsWith("text/")
654  || (type.startsWith("application/")
655  && (type.endsWith("javascript") || type.endsWith("ecmascript"))))
656  typeOk = true;
657  else
658  return false;
659  }
660  if (sizeOk && typeOk)
661  return true;
662  }
663  return false;
664 }
665 
666 enum
667 {
668  CacheMagic = 0xe8,
670 };
671 
672 void QCacheItem::writeHeader(QFile *device) const
673 {
674  QDataStream out(device);
675 
676  out << qint32(CacheMagic);
677  out << qint32(CurrentCacheVersion);
678  out << metaData;
679  bool compressed = canCompress();
680  out << compressed;
681 }
682 
684 {
685  QDataStream out(device);
686 
687  out << qCompress(data.data());
688 }
689 
694 bool QCacheItem::read(QFile *device, bool readData)
695 {
696  reset();
697 
698  QDataStream in(device);
699 
700  qint32 marker;
701  qint32 v;
702  in >> marker;
703  in >> v;
704  if (marker != CacheMagic)
705  return true;
706 
707  // If the cache magic is correct, but the version is not we should remove it
708  if (v != CurrentCacheVersion)
709  return false;
710 
711  bool compressed;
712  QByteArray dataBA;
713  in >> metaData;
714  in >> compressed;
715  if (readData && compressed) {
716  in >> dataBA;
717  data.setData(qUncompress(dataBA));
719  }
720 
721  // quick and dirty check if metadata's URL field and the file's name are in synch
722  QString expectedFilename = QNetworkDiskCachePrivate::uniqueFileName(metaData.url());
723  if (!device->fileName().endsWith(expectedFilename))
724  return false;
725 
726  return metaData.isValid();
727 }
728 
730 
731 #endif // QT_NO_NETWORKDISKCACHE
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.
Definition: qcontainerfwd.h:59
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
double d
Definition: qnumeric_p.h:62
QNetworkDiskCache(QObject *parent=0)
Creates a new disk cache.
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
QNetworkCacheMetaData fileMetaData(const QString &fileName) const
Returns the QNetworkCacheMetaData for the cache file fileName.
static QString uniqueFileName(const QUrl &url)
Given a URL, generates a unique enough filename (and subdirectory)
bool mkpath(const QString &dirPath) const
Creates the directory path dirPath.
Definition: qdir.cpp:1477
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
Definition: qfile.cpp:470
~QNetworkDiskCache()
Destroys the cache object.
void storeItem(QCacheItem *item)
bool rename(const QString &newName)
Renames the file currently specified by fileName() to newName.
Definition: qfile.cpp:766
qint64 size() const
int type
Definition: qmetatype.cpp:239
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
void setAutoRemove(bool b)
Sets the QTemporaryFile into auto-remove mode if b is true.
static mach_timebase_info_data_t info
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qint32
Definition: qglobal.h:937
bool hasNext() const
Returns true if there is at least one more entry in the directory; otherwise, false is returned...
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes)
T * data() const
Returns the value of the pointer referenced by this object.
void updateMetaData(const QNetworkCacheMetaData &metaData)
Reimplemented Function
#define it(className, varName)
bool isValid() const
Returns true if this network cache meta data has attributes that have been set otherwise false...
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
Definition: qbuffer.cpp:315
QDateTime created() const
Returns the date and time when the file was created.
Definition: qfileinfo.cpp:1278
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
QMap< Key, T >::iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:982
#define at(className, varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Q_CORE_EXPORT QTextStream & reset(QTextStream &s)
The QTemporaryFile class is an I/O device that operates on temporary files.
T * take()
Returns the value of the pointer referenced by this object.
T1 first
Definition: qpair.h:65
The QNetworkDiskCache class provides a very basic disk cache.
void insert(QIODevice *device)
Reimplemented Function
QString fileName() const
Returns the name of the file, excluding the path.
Definition: qfileinfo.cpp:726
T2 second
Definition: qpair.h:66
bool mkdir(const QString &dirName) const
Creates a sub-directory called dirName.
Definition: qdir.cpp:1424
bool open()
A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the d...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qhash.h:348
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
bool removeFile(const QString &file)
Put all of the misc file removing into one function to be extra safe.
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QDirIterator class provides an iterator for directory entrylists.
Definition: qdiriterator.h:54
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define DATA_DIR
QString cacheFileName(const QUrl &url) const
Generates fully qualified path of cached resource from a URL.
QFileInfo fileInfo() const
Returns a QFileInfo for the current directory entry.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
bool startsWith(const QByteArray &a) const
Returns true if this byte array starts with byte array ba; otherwise returns false.
QIODevice * data(const QUrl &url)
Reimplemented Function
#define Q_D(Class)
Definition: qglobal.h:2482
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
Definition: qiodevice.cpp:711
QUrl url() const
Returns the URL this network cache meta data is referring to.
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
void setMaximumCacheSize(qint64 size)
Sets the maximum size of the disk cache to be size in bytes.
#define Q_Q(Class)
Definition: qglobal.h:2483
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
RawHeaderList rawHeaders() const
Returns a list of all raw headers that are set in this meta data.
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
Q_CORE_EXPORT void qDebug(const char *,...)
unsigned char uchar
Definition: qglobal.h:994
bool read(QFile *device, bool readData)
Returns false if the file is a cache file, but is an older version and should be removed otherwise tr...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
static bool isEmpty(const char *str)
void writeCompressedData(QFile *device) const
QNetworkCacheMetaData metaData
#define CACHE_VERSION
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString absolutePath() const
Returns the absolute path (a path that starts with "/" or with a drive specification), which may contain symbolic links, but never contains redundant ".", ".." or multiple separators.
Definition: qdir.cpp:619
QNetworkCacheMetaData metaData(const QUrl &url)
Reimplemented Function
const char * name
#define MAX_COMPRESSION_SIZE
#define PREPARED_SLASH
QTemporaryFile * file
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
qint64 pos() const
Reimplemented Function
Definition: qfile.cpp:1720
__int64 qint64
Definition: qglobal.h:942
qint64 cacheSize() const
Reimplemented Function
void prepareLayout()
Create subdirectories and other housekeeping on the filesystem.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
#define QT_CATCH(A)
Definition: qglobal.h:1537
void setFragment(const QString &fragment)
Sets the fragment of the URL to fragment.
Definition: qurl.cpp:5669
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.
int count(const Key &key, const T &value) const
Returns the number of items with key key and value value.
Definition: qmap.h:1071
qint64 size() const
Returns the size of the file.
Definition: qfile.cpp:1707
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
Q_CORE_EXPORT QByteArray qCompress(const uchar *data, int nbytes, int compressionLevel=-1)
bool remove(const QUrl &url)
Reimplemented Function
static const char *const filters[3]
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
bool canCompress() const
We compress small text and JavaScript files.
void clear()
Reimplemented Function
QString next()
Advances the iterator to the next entry, and returns the file path of this new entry.
qint64 size() const
Returns the file size in bytes.
Definition: qfileinfo.cpp:1248
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
void addData(const char *data, int length)
Adds the first length chars of data to the cryptographic hash.
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
QByteArray result() const
Returns the final hash value.
virtual qint64 expire()
Cleans the cache so that its size is under the maximum cache size.
const T & value() const
Returns the current item&#39;s value.
Definition: qmap.h:325
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
bool remove()
Removes the file specified by fileName().
Definition: qfile.cpp:715
QString cacheDirectory() const
Returns the location where cached files will be stored.
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
void writeHeader(QFile *device) const
FileError error() const
Returns the file error status.
Definition: qfile.cpp:1984
uchar * map(qint64 offset, qint64 size, MemoryMapFlags flags=NoOptions)
Maps size bytes of the file into memory starting at offset.
Definition: qfile.cpp:1460
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
void setCacheDirectory(const QString &cacheDir)
Sets the directory where cached files will be stored to cacheDir.
virtual void close()
Calls QFile::flush() and closes the file.
Definition: qfile.cpp:1680
qint64 qlonglong
Definition: qglobal.h:951
bool saveToDisk() const
Returns is this cache should be allowed to be stored on disk.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
static QString fileName(const QString &fileUrl)
#define CACHE_POSTFIX
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
#define QT_TRY
Definition: qglobal.h:1536
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void setPassword(const QString &password)
Sets the URL&#39;s password to password.
Definition: qurl.cpp:4736
The QNetworkCacheMetaData class provides cache information.
qint64 maximumCacheSize() const
Returns the current maximum size in bytes for the disk cache.
The QCryptographicHash class provides a way to generate cryptographic hashes.
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...
The QAbstractNetworkCache class provides the interface for cache implementations. ...
QIODevice * prepare(const QNetworkCacheMetaData &metaData)
Reimplemented Function