9#include "QtCore/qscopedpointer.h"
22#define CACHE_POSTFIX ".d"_L1
23#define CACHE_VERSION 8
24#define DATA_DIR "data"_L1
26#define MAX_COMPRESSION_SIZE (1024 * 1024 * 3)
91 return d->cacheDirectory;
106#if defined(QNETWORKDISKCACHE_DEBUG)
107 qDebug() <<
"QNetworkDiskCache::setCacheDirectory()" << cacheDir;
112 d->cacheDirectory = cacheDir;
114 d->cacheDirectory =
dir.absolutePath();
115 if (!
d->cacheDirectory.endsWith(u
'/'))
116 d->cacheDirectory += u
'/';
127#if defined(QNETWORKDISKCACHE_DEBUG)
128 qDebug(
"QNetworkDiskCache::cacheSize()");
131 if (
d->cacheDirectory.isEmpty())
133 if (
d->currentCacheSize < 0) {
135 that->d_func()->currentCacheSize = that->
expire();
137 return d->currentCacheSize;
145#if defined(QNETWORKDISKCACHE_DEBUG)
152 if (
d->cacheDirectory.isEmpty()) {
153 qWarning(
"QNetworkDiskCache::prepare() The cache directory is not set");
158 for (
const auto &
header : headers) {
166 std::unique_ptr<QCacheItem> cacheItem = std::make_unique<QCacheItem>();
170 if (cacheItem->canCompress()) {
172 device = &(cacheItem->data);
178 cacheItem->file =
nullptr;
181 qWarning(
"QNetworkDiskCache::prepare() unable to open temporary file");
185 cacheItem->writeHeader(cacheItem->file);
188 d->inserting[
device] = cacheItem.release();
197#if defined(QNETWORKDISKCACHE_DEBUG)
201 const auto it =
d->inserting.constFind(
device);
203 qWarning() <<
"QNetworkDiskCache::insert() called on a device we don't know about" <<
device;
207 d->storeItem(
it.value());
209 d->inserting.erase(
it);
223 for (
uint i = 0;
i < 16 ;
i++) {
226 helper.
mkdir(subdir);
241 qWarning() <<
"QNetworkDiskCache: couldn't remove the cache file " <<
fileName;
247 if (!cacheItem->
file) {
265 delete std::exchange(cacheItem->
file,
nullptr);
276#if defined(QNETWORKDISKCACHE_DEBUG)
277 qDebug() <<
"QNetworkDiskCache::remove()" <<
url;
282 for (
auto it =
d->inserting.cbegin(),
end =
d->inserting.cend();
it !=
end; ++
it) {
286 d->inserting.erase(
it);
291 if (
d->lastItem.metaData.url() ==
url)
293 return d->removeFile(
d->cacheFileName(
url));
301#if defined(QNETWORKDISKCACHE_DEBUG)
302 qDebug() <<
"QNetworkDiskCache::removFile()" <<
file;
323#if defined(QNETWORKDISKCACHE_DEBUG)
324 qDebug() <<
"QNetworkDiskCache::metaData()" <<
url;
327 if (
d->lastItem.metaData.url() ==
url)
328 return d->lastItem.metaData;
339#if defined(QNETWORKDISKCACHE_DEBUG)
346 if (!
d->lastItem.read(&
file,
false)) {
351 return d->lastItem.metaData;
359#if defined(QNETWORKDISKCACHE_DEBUG)
360 qDebug() <<
"QNetworkDiskCache::data()" <<
url;
363 std::unique_ptr<QBuffer>
buffer;
366 if (
d->lastItem.metaData.url() ==
url &&
d->lastItem.data.isOpen()) {
368 buffer->setData(
d->lastItem.data.data());
374 if (!
d->lastItem.read(
file.data(),
true)) {
379 if (
d->lastItem.data.isOpen()) {
382 buffer->setData(
d->lastItem.data.data());
397#if defined(QNETWORKDISKCACHE_DEBUG)
403#if defined(QNETWORKDISKCACHE_DEBUG)
404 qDebug(
"QNetworkDiskCache::updateMetaData(), no device!");
411#if defined(QNETWORKDISKCACHE_DEBUG)
412 qDebug() <<
"QNetworkDiskCache::updateMetaData(), no new device!" <<
url;
417 while (!oldDevice->
atEnd()) {
433 return d->maximumCacheSize;
446 bool expireCache = (
size <
d->maximumCacheSize);
447 d->maximumCacheSize =
size;
449 d->currentCacheSize =
expire();
474 return d->currentCacheSize;
477 qWarning(
"QNetworkDiskCache::expire() The cache directory is not set");
489 std::chrono::milliseconds msecs;
493 std::vector<CacheItem> cacheItems;
495 while (
it.hasNext()) {
510 if (totalSize < goal)
513 auto byFileTime = [&](
const auto &
a,
const auto &
b) {
return a.msecs <
b.msecs; };
514 std::sort(cacheItems.begin(), cacheItems.end(), byFileTime);
516 [[maybe_unused]]
int removedFiles = 0;
517 for (
const CacheItem &cached : cacheItems) {
520 totalSize -= cached.size;
521 if (totalSize < goal)
524#if defined(QNETWORKDISKCACHE_DEBUG)
525 if (removedFiles > 0) {
526 qDebug() <<
"QNetworkDiskCache::expire()"
527 <<
"Removed:" << removedFiles
528 <<
"Kept:" << cacheItems.count() - removedFiles;
539#if defined(QNETWORKDISKCACHE_DEBUG)
540 qDebug(
"QNetworkDiskCache::clear()");
544 d->maximumCacheSize = 0;
545 d->currentCacheSize =
expire();
546 d->maximumCacheSize =
size;
588 for (
const auto &
header : headers) {
599 if (
type.startsWith(
"text/")
600 || (
type.startsWith(
"application/")
601 && (
type.endsWith(
"javascript") ||
type.endsWith(
"ecmascript"))))
606 if (sizeOk && typeOk)
624 out << static_cast<qint32>(
out.version());
661 if (streamVersion >
in.version())
663 in.setVersion(streamVersion);
677 if (!
device->fileName().endsWith(expectedFilename))
685#include "moc_qnetworkdiskcache.cpp"
IOBluetoothDevice * device
The QAbstractNetworkCache class provides the interface for cache implementations.
\inmodule QtCore \reentrant
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
QByteArray left(qsizetype len) const
Returns a byte array that contains the first len bytes of this byte array.
void writeCompressedData(QFileDevice *device) const
bool canCompress() const
We compress small text and JavaScript files.
bool read(QFileDevice *device, bool readData)
Returns false if the file is a cache file, but is an older version and should be removed otherwise tr...
QNetworkCacheMetaData metaData
void writeHeader(QFileDevice *device) const
static QByteArray hash(QByteArrayView data, Algorithm method)
Returns the hash of data using method.
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
qint64 toMSecsSinceEpoch() const
bool isValid() const
Returns true if this datetime represents a definite moment, otherwise false.
The QDirIterator class provides an iterator for directory entrylists.
bool mkdir(const QString &dirName) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool mkpath(const QString &dirPath) const
Creates the directory path dirPath.
qint64 size() const override
Returns the size of the file.
FileError error() const
Returns the file error status.
void close() override
Calls QFileDevice::flush() and closes the file.
\inmodule QtCore \reentrant
QDateTime birthTime() const
Returns the date and time when the file was created (born), in local time.
QString fileName() const
Returns the name of the file, excluding the path.
QDateTime metadataChangeTime() const
Returns the date and time when the file's metadata was last changed, in local time.
qint64 size() const
Returns the file size in bytes.
QString filePath() const
Returns the file name, including the path (which may be absolute or relative).
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
bool remove()
Removes the file specified by fileName().
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
\inmodule QtCore \reentrant
bool isOpen() const
Returns true if the device is open; otherwise returns false.
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i....
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
bool isEmpty() const noexcept
bool removeFile(const QString &file)
Put all of the misc file removing into one function to be extra safe.
QString cacheFileName(const QUrl &url) const
Generates fully qualified path of cached resource from a URL.
static QString uniqueFileName(const QUrl &url)
Given a URL, generates a unique enough filename (and subdirectory)
void prepareLayout()
Create subdirectories and other housekeeping on the filesystem.
void storeItem(QCacheItem *item)
The QNetworkDiskCache class provides a very basic disk cache.
void insert(QIODevice *device) override
\reimp
QNetworkCacheMetaData metaData(const QUrl &url) override
\reimp
void updateMetaData(const QNetworkCacheMetaData &metaData) override
\reimp
QIODevice * prepare(const QNetworkCacheMetaData &metaData) override
\reimp
void setCacheDirectory(const QString &cacheDir)
Sets the directory where cached files will be stored to cacheDir.
virtual qint64 expire()
Cleans the cache so that its size is under the maximum cache size.
void clear() override
\reimp
QString cacheDirectory() const
Returns the location where cached files will be stored.
QNetworkCacheMetaData fileMetaData(const QString &fileName) const
Returns the QNetworkCacheMetaData for the cache file fileName.
QNetworkDiskCache(QObject *parent=nullptr)
Creates a new disk cache.
~QNetworkDiskCache()
Destroys the cache object.
QIODevice * data(const QUrl &url) override
\reimp
void setMaximumCacheSize(qint64 size)
Sets the maximum size of the disk cache to be size.
qint64 maximumCacheSize() const
Returns the current maximum size for the disk cache.
bool remove(const QUrl &url) override
\reimp
qint64 cacheSize() const override
\reimp
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
bool commit()
Commits the changes to disk, if all previous writes were successful.
\macro QT_RESTRICTED_CAST_FROM_ASCII
QString first(qsizetype n) const
qlonglong toLongLong(bool *ok=nullptr, int base=10) const
Returns the string converted to a {long long} using base base, which is 10 by default and must be bet...
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
int compare(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setPassword(const QString &password, ParsingMode mode=DecodedMode)
Sets the URL's password to password.
void setFragment(const QString &fragment, ParsingMode mode=TolerantMode)
Sets the fragment of the URL to fragment.
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
QByteArray toEncoded(FormattingOptions options=FullyEncoded) const
Returns the encoded representation of the URL if it's valid; otherwise an empty QByteArray is returne...
QHash< int, QWidget * > hash
[35multi]
qDeleteAll(list.begin(), list.end())
QSet< QString >::iterator it
Combined button and popup list for selecting options.
QByteArray qCompress(const uchar *data, qsizetype nbytes, int compressionLevel)
Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, qsizetype nbytes)
static QString header(const QString &name)
#define MAX_COMPRESSION_SIZE
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble GLdouble GLdouble GLdouble q
GLsizei const GLchar *const * path
QFileInfo info(fileName)
[8]
QTextStream out(stdout)
[7]
QUrl url("example.com")
[constructor-url-reference]
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent