1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QNETWORKDISKCACHE_P_H |
5 | #define QNETWORKDISKCACHE_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
19 | #include "private/qabstractnetworkcache_p.h" |
20 | |
21 | #include <qbuffer.h> |
22 | #include <qhash.h> |
23 | #include <qsavefile.h> |
24 | |
25 | QT_REQUIRE_CONFIG(networkdiskcache); |
26 | |
27 | QT_BEGIN_NAMESPACE |
28 | |
29 | class QCacheItem |
30 | { |
31 | public: |
32 | QCacheItem() = default; |
33 | ~QCacheItem() |
34 | { |
35 | reset(); |
36 | } |
37 | |
38 | QNetworkCacheMetaData metaData; |
39 | QBuffer data; |
40 | QSaveFile *file = nullptr; |
41 | inline qint64 size() const |
42 | { return file ? file->size() : data.size(); } |
43 | |
44 | inline void reset() { |
45 | metaData = QNetworkCacheMetaData(); |
46 | data.close(); |
47 | delete file; |
48 | file = nullptr; |
49 | } |
50 | void writeHeader(QFileDevice *device) const; |
51 | void writeCompressedData(QFileDevice *device) const; |
52 | bool read(QFileDevice *device, bool readData); |
53 | |
54 | bool canCompress() const; |
55 | }; |
56 | |
57 | class QNetworkDiskCachePrivate : public QAbstractNetworkCachePrivate |
58 | { |
59 | public: |
60 | QNetworkDiskCachePrivate() |
61 | : QAbstractNetworkCachePrivate() |
62 | , maximumCacheSize(1024 * 1024 * 50) |
63 | , currentCacheSize(-1) |
64 | {} |
65 | |
66 | static QString uniqueFileName(const QUrl &url); |
67 | QString cacheFileName(const QUrl &url) const; |
68 | bool removeFile(const QString &file); |
69 | void storeItem(QCacheItem *item); |
70 | void prepareLayout(); |
71 | static quint32 crc32(const char *data, uint len); |
72 | |
73 | mutable QCacheItem lastItem; |
74 | QString cacheDirectory; |
75 | QString dataDirectory; |
76 | qint64 maximumCacheSize; |
77 | qint64 currentCacheSize; |
78 | |
79 | QHash<QIODevice*, QCacheItem*> inserting; |
80 | Q_DECLARE_PUBLIC(QNetworkDiskCache) |
81 | }; |
82 | |
83 | QT_END_NAMESPACE |
84 | |
85 | #endif // QNETWORKDISKCACHE_P_H |
86 |