47 #include "QtCore/qscopedpointer.h" 57 #define CACHE_POSTFIX QLatin1String(".d") 58 #define PREPARED_SLASH QLatin1String("prepared/") 59 #define CACHE_VERSION 7 60 #define DATA_DIR QLatin1String("data") 62 #define MAX_COMPRESSION_SIZE (1024 * 1024 * 3) 64 #ifndef QT_NO_NETWORKDISKCACHE 122 QHashIterator<QIODevice*, QCacheItem*>
it(
d->inserting);
123 while (it.hasNext()) {
135 return d->cacheDirectory;
150 #if defined(QNETWORKDISKCACHE_DEBUG) 151 qDebug() <<
"QNetworkDiskCache::setCacheDirectory()" << cacheDir;
156 d->cacheDirectory = cacheDir;
157 QDir dir(
d->cacheDirectory);
171 #if defined(QNETWORKDISKCACHE_DEBUG) 172 qDebug() <<
"QNetworkDiskCache::cacheSize()";
175 if (
d->cacheDirectory.isEmpty())
177 if (
d->currentCacheSize < 0) {
179 that->d_func()->currentCacheSize = that->
expire();
181 return d->currentCacheSize;
189 #if defined(QNETWORKDISKCACHE_DEBUG) 190 qDebug() <<
"QNetworkDiskCache::prepare()" << metaData.
url();
196 if (
d->cacheDirectory.isEmpty()) {
197 qWarning() <<
"QNetworkDiskCache::prepare() The cache directory is not set";
202 if (header.
first.toLower() ==
"content-length") {
215 device = &(cacheItem->
data);
217 QString templateName =
d->tmpCacheFileName();
224 qWarning() <<
"QNetworkDiskCache::prepare() unable to open temporary file";
229 device = cacheItem->
file;
231 d->inserting[device] = cacheItem.
take();
240 #if defined(QNETWORKDISKCACHE_DEBUG) 241 qDebug() <<
"QNetworkDiskCache::insert()" << device;
245 if (it ==
d->inserting.end()) {
246 qWarning() <<
"QNetworkDiskCache::insert() called on a device we don't know about" << device;
252 d->inserting.erase(it);
266 helper.
mkpath(dataDirectory);
267 for (
uint i = 0; i < 16 ; i++) {
269 QString subdir = dataDirectory + str;
270 helper.
mkdir(subdir);
285 qWarning() <<
"QNetworkDiskCache: couldn't remove the cache file " <<
fileName;
290 if (currentCacheSize > 0)
291 currentCacheSize += 1024 + cacheItem->
size();
292 currentCacheSize = q->expire();
293 if (!cacheItem->
file) {
294 QString templateName = tmpCacheFileName();
308 currentCacheSize += cacheItem->
file->
size();
312 if (cacheItem->
metaData.
url() == lastItem.metaData.url())
321 #if defined(QNETWORKDISKCACHE_DEBUG) 322 qDebug() <<
"QNetworkDiskCache::remove()" << url;
327 QHashIterator<QIODevice*, QCacheItem*>
it(
d->inserting);
328 while (it.hasNext()) {
333 d->inserting.remove(it.key());
338 if (
d->lastItem.metaData.url() == url)
340 return d->removeFile(
d->cacheFileName(url));
348 #if defined(QNETWORKDISKCACHE_DEBUG) 349 qDebug() <<
"QNetworkDiskCache::removFile()" << file;
359 currentCacheSize -= size;
370 #if defined(QNETWORKDISKCACHE_DEBUG) 371 qDebug() <<
"QNetworkDiskCache::metaData()" << url;
374 if (
d->lastItem.metaData.url() == url)
375 return d->lastItem.metaData;
386 #if defined(QNETWORKDISKCACHE_DEBUG) 390 QFile file(fileName);
393 if (!
d->lastItem.read(&file,
false)) {
398 return d->lastItem.metaData;
406 #if defined(QNETWORKDISKCACHE_DEBUG) 407 qDebug() <<
"QNetworkDiskCache::data()" << url;
413 if (
d->lastItem.metaData.url() == url &&
d->lastItem.data.isOpen()) {
415 buffer->
setData(
d->lastItem.data.data());
421 if (!
d->lastItem.read(file.
data(),
true)) {
426 if (
d->lastItem.data.isOpen()) {
429 buffer->
setData(
d->lastItem.data.data());
435 #if !defined(Q_OS_WINCE) && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_SYMBIAN) 436 p = file->
map(file->
pos(), size);
439 buffer->
setData((
const char *)p, size);
447 return buffer.
take();
455 #if defined(QNETWORKDISKCACHE_DEBUG) 456 qDebug() <<
"QNetworkDiskCache::updateMetaData()" << metaData.
url();
461 #if defined(QNETWORKDISKCACHE_DEBUG) 462 qDebug() <<
"QNetworkDiskCache::updateMetaData(), no device!";
469 #if defined(QNETWORKDISKCACHE_DEBUG) 470 qDebug() <<
"QNetworkDiskCache::updateMetaData(), no new device!" << url;
475 while (!oldDevice->
atEnd()) {
477 newDevice->
write(data, s);
491 return d->maximumCacheSize;
504 bool expireCache = (size <
d->maximumCacheSize);
505 d->maximumCacheSize = size;
507 d->currentCacheSize =
expire();
532 return d->currentCacheSize;
535 qWarning() <<
"QNetworkDiskCache::expire() The cache directory is not set";
553 totalSize += info.
size();
557 int removedFiles = 0;
560 while (i != cacheItems.
constEnd()) {
561 if (totalSize < goal)
571 #if defined(QNETWORKDISKCACHE_DEBUG) 572 if (removedFiles > 0) {
573 qDebug() <<
"QNetworkDiskCache::expire()" 574 <<
"Removed:" << removedFiles
575 <<
"Kept:" << cacheItems.
count() - removedFiles;
586 #if defined(QNETWORKDISKCACHE_DEBUG) 587 qDebug() <<
"QNetworkDiskCache::clear()";
590 qint64 size =
d->maximumCacheSize;
591 d->maximumCacheSize = 0;
592 d->currentCacheSize =
expire();
593 d->maximumCacheSize = size;
631 QString fullpath = dataDirectory + uniqueFileName(url);
643 if (header.
first.toLower() ==
"content-length") {
651 if (header.
first.toLower() ==
"content-type") {
660 if (sizeOk && typeOk)
679 bool compressed = canCompress();
715 if (readData && compressed) {
726 return metaData.isValid();
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...
The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.
The QDir class provides access to directory structures and their contents.
QNetworkDiskCache(QObject *parent=0)
Creates a new disk cache.
static uint hash(const uchar *p, int n)
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.
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
~QNetworkDiskCache()
Destroys the cache object.
void storeItem(QCacheItem *item)
bool rename(const QString &newName)
Renames the file currently specified by fileName() to newName.
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
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.
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.
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 open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
QDateTime created() const
Returns the date and time when the file was created.
bool open(OpenMode openMode)
Reimplemented Function
QMap< Key, T >::iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
#define at(className, varName)
The QByteArray class provides an array of bytes.
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.
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.
bool mkdir(const QString &dirName) const
Creates a sub-directory called dirName.
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's value.
The QBuffer class provides a QIODevice interface for a QByteArray.
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.
The QDirIterator class provides an iterator for directory entrylists.
The QString class provides a Unicode character string.
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.
The QObject class is the base class of all Qt objects.
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
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
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.
void setMaximumCacheSize(qint64 size)
Sets the maximum size of the disk cache to be size in bytes.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Q_CORE_EXPORT void qDebug(const char *,...)
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.
bool isOpen() const
Returns true if the device is open; otherwise returns false.
static bool isEmpty(const char *str)
void writeCompressedData(QFile *device) const
QNetworkCacheMetaData metaData
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
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.
QNetworkCacheMetaData metaData(const QUrl &url)
Reimplemented Function
#define MAX_COMPRESSION_SIZE
Q_CORE_EXPORT void qWarning(const char *,...)
QString tmpCacheFileName() const
qint64 pos() const
Reimplemented Function
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.
void setFragment(const QString &fragment)
Sets the fragment of the URL to fragment.
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.
qint64 size() const
Returns the size of the file.
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it's valid; otherwise an empty QByteArray is returne...
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.
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
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.
The QFile class provides an interface for reading from and writing to files.
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.
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's value.
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
bool remove()
Removes the file specified by fileName().
QString cacheDirectory() const
Returns the location where cached files will be stored.
The QDataStream class provides serialization of binary data to a QIODevice.
void writeHeader(QFile *device) const
FileError error() const
Returns the file error status.
uchar * map(qint64 offset, qint64 size, MemoryMapFlags flags=NoOptions)
Maps size bytes of the file into memory starting at offset.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
The QFileInfo class provides system-independent file information.
The QIODevice class is the base interface class of all I/O devices in Qt.
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.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
static QString fileName(const QString &fileUrl)
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)...
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
void setPassword(const QString &password)
Sets the URL's password to password.
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