QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCCachedFileDownload.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <QtCore/QDateTime>
7#include <QtCore/QLoggingCategory>
8#include <QtCore/QObject>
9#include <QtCore/QString>
10#include <QtCore/QUrl>
11
12class QGCFileDownload;
13class QNetworkDiskCache;
14
15Q_DECLARE_LOGGING_CATEGORY(QGCCachedFileDownloadLog)
16
17
55class QGCCachedFileDownload : public QObject
56{
57 Q_OBJECT
58 Q_DISABLE_COPY_MOVE(QGCCachedFileDownload)
59
60
61 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged FINAL)
62
63
64 Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged FINAL)
65
66
67 Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged FINAL)
68
69
70 Q_PROPERTY(QUrl url READ url NOTIFY urlChanged FINAL)
71
72
73 Q_PROPERTY(QString localPath READ localPath NOTIFY localPathChanged FINAL)
74
75
76 Q_PROPERTY(bool fromCache READ isFromCache NOTIFY fromCacheChanged FINAL)
77
78
79 Q_PROPERTY(QString cacheDirectory READ cacheDirectory WRITE setCacheDirectory
80 NOTIFY cacheDirectoryChanged FINAL)
81
82
83 Q_PROPERTY(qint64 maxCacheSize READ maxCacheSize WRITE setMaxCacheSize
84 NOTIFY maxCacheSizeChanged FINAL)
85
86
87 Q_PROPERTY(qint64 cacheSize READ cacheSize NOTIFY cacheSizeChanged FINAL)
88
89public:
93 explicit QGCCachedFileDownload(const QString &cacheDirectory, QObject *parent = nullptr);
94
96 explicit QGCCachedFileDownload(QObject *parent = nullptr);
97
98 ~QGCCachedFileDownload() override;
99
100 // Property getters
101 qreal progress() const { return _progress; }
102 bool isRunning() const { return _running; }
103 QString errorString() const { return _errorString; }
104 QUrl url() const { return _url; }
105 QString localPath() const { return _localPath; }
106 bool isFromCache() const { return _fromCache; }
107 QString cacheDirectory() const;
108 qint64 maxCacheSize() const;
109 qint64 cacheSize() const;
110
111 // Property setters
112 void setCacheDirectory(const QString &directory);
113 void setMaxCacheSize(qint64 bytes);
114
119 Q_INVOKABLE bool isCached(const QString &url, int maxAgeSec = 0) const;
120
125 Q_INVOKABLE QString cachedPath(const QString &url) const;
126
130 Q_INVOKABLE int cacheAge(const QString &url) const;
131
133 QGCFileDownload *fileDownloader() const { return _fileDownload; }
134
136 QNetworkDiskCache *diskCache() const { return _diskCache; }
137
138public slots:
143 bool download(const QString &url, int maxCacheAgeSec);
144
148 bool downloadPreferCache(const QString &url);
149
153 bool downloadNoCache(const QString &url);
154
156 void cancel();
157
159 void clearCache();
160
164 bool removeFromCache(const QString &url);
165
166signals:
168 void progressChanged(qreal progress);
169
171 void runningChanged(bool running);
172
174 void errorStringChanged(const QString &errorString);
175
177 void urlChanged(const QUrl &url);
178
180 void localPathChanged(const QString &localPath);
181
183 void fromCacheChanged(bool fromCache);
184
186 void cacheDirectoryChanged(const QString &directory);
187
189 void maxCacheSizeChanged(qint64 bytes);
190
192 void cacheSizeChanged(qint64 bytes);
193
199 void finished(bool success, const QString &localPath,
200 const QString &errorMessage, bool fromCache);
201
203 void downloadProgress(qint64 bytesReceived, qint64 totalBytes);
204
205private slots:
206 void _onDownloadFinished(bool success, const QString &localPath, const QString &errorMessage);
207 void _onDownloadProgress(qint64 bytesReceived, qint64 totalBytes);
208
209private:
210 void _initializeCache(const QString &directory);
211 void _setRunning(bool running);
212 void _setProgress(qreal progress);
213 void _setErrorString(const QString &error);
214 void _setFromCache(bool fromCache);
215 void _updateCacheTimestamp(const QString &url);
216 QDateTime _getCacheTimestamp(const QString &url) const;
217 bool _startDownload(const QString &url, bool forceNetwork, bool preferCache);
218 void _emitFinished(bool success, const QString &path, const QString &error);
219
220 QGCFileDownload *_fileDownload = nullptr;
221 QNetworkDiskCache *_diskCache = nullptr;
222
223 QUrl _url;
224 QString _localPath;
225 QString _errorString;
226 QString _pendingUrl;
227
228 qreal _progress = 0.0;
229 int _maxCacheAgeSec = 0;
230 bool _running = false;
231 bool _fromCache = false;
232 bool _networkAttemptFailed = false;
233 bool _forceNetwork = false;
234 bool _cancelRequested = false;
235};
Q_DECLARE_LOGGING_CATEGORY(AndroidSerialLog)
QString errorString
Error error
void maxCacheSizeChanged(qint64 bytes)
Emitted when max cache size changes.
void urlChanged(const QUrl &url)
Emitted when URL changes.
void downloadProgress(qint64 bytesReceived, qint64 totalBytes)
Emitted during download with byte counts.
void localPathChanged(const QString &localPath)
Emitted when local path changes.
void cacheDirectoryChanged(const QString &directory)
Emitted when cache directory changes.
void cacheSizeChanged(qint64 bytes)
Emitted when cache size changes.
void progressChanged(qreal progress)
Emitted when download progress changes.
void errorStringChanged(const QString &errorString)
Emitted when error string changes.
void runningChanged(bool running)
Emitted when running state changes.
void finished(bool success, const QString &localPath, const QString &errorMessage, bool fromCache)
void fromCacheChanged(bool fromCache)
Emitted when fromCache state changes.