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/QObject>
8#include <QtCore/QString>
9#include <QtCore/QUrl>
10
11class QGCFileDownload;
12class QNetworkDiskCache;
13
53class QGCCachedFileDownload : public QObject
54{
55 Q_OBJECT
56 Q_DISABLE_COPY_MOVE(QGCCachedFileDownload)
57
58
59 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged FINAL)
60
61
62 Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged FINAL)
63
64
65 Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged FINAL)
66
67
68 Q_PROPERTY(QUrl url READ url NOTIFY urlChanged FINAL)
69
70
71 Q_PROPERTY(QString localPath READ localPath NOTIFY localPathChanged FINAL)
72
73
74 Q_PROPERTY(bool fromCache READ isFromCache NOTIFY fromCacheChanged FINAL)
75
76
77 Q_PROPERTY(QString cacheDirectory READ cacheDirectory WRITE setCacheDirectory
78 NOTIFY cacheDirectoryChanged FINAL)
79
80
81 Q_PROPERTY(qint64 maxCacheSize READ maxCacheSize WRITE setMaxCacheSize
82 NOTIFY maxCacheSizeChanged FINAL)
83
84
85 Q_PROPERTY(qint64 cacheSize READ cacheSize NOTIFY cacheSizeChanged FINAL)
86
87public:
91 explicit QGCCachedFileDownload(const QString &cacheDirectory, QObject *parent = nullptr);
92
94 explicit QGCCachedFileDownload(QObject *parent = nullptr);
95
96 ~QGCCachedFileDownload() override;
97
98 // Property getters
99 qreal progress() const { return _progress; }
100 bool isRunning() const { return _running; }
101 QString errorString() const { return _errorString; }
102 QUrl url() const { return _url; }
103 QString localPath() const { return _localPath; }
104 bool isFromCache() const { return _fromCache; }
105 QString cacheDirectory() const;
106 qint64 maxCacheSize() const;
107 qint64 cacheSize() const;
108
109 // Property setters
110 void setCacheDirectory(const QString &directory);
111 void setMaxCacheSize(qint64 bytes);
112
117 Q_INVOKABLE bool isCached(const QString &url, int maxAgeSec = 0) const;
118
123 Q_INVOKABLE QString cachedPath(const QString &url) const;
124
128 Q_INVOKABLE int cacheAge(const QString &url) const;
129
131 QGCFileDownload *fileDownloader() const { return _fileDownload; }
132
134 QNetworkDiskCache *diskCache() const { return _diskCache; }
135
136public slots:
141 bool download(const QString &url, int maxCacheAgeSec);
142
146 bool downloadPreferCache(const QString &url);
147
151 bool downloadNoCache(const QString &url);
152
154 void cancel();
155
157 void clearCache();
158
162 bool removeFromCache(const QString &url);
163
164signals:
167
169 void runningChanged(bool running);
170
172 void errorStringChanged(const QString &errorString);
173
175 void urlChanged(const QUrl &url);
176
178 void localPathChanged(const QString &localPath);
179
181 void fromCacheChanged(bool fromCache);
182
184 void cacheDirectoryChanged(const QString &directory);
185
187 void maxCacheSizeChanged(qint64 bytes);
188
190 void cacheSizeChanged(qint64 bytes);
191
197 void finished(bool success, const QString &localPath,
198 const QString &errorMessage, bool fromCache);
199
201 void downloadProgress(qint64 bytesReceived, qint64 totalBytes);
202
203private slots:
204 void _onDownloadFinished(bool success, const QString &localPath, const QString &errorMessage);
205 void _onDownloadProgress(qint64 bytesReceived, qint64 totalBytes);
206
207private:
208 void _initializeCache(const QString &directory);
209 void _setRunning(bool running);
210 void _setProgress(qreal progress);
211 void _setErrorString(const QString &error);
212 void _setFromCache(bool fromCache);
213 void _updateCacheTimestamp(const QString &url);
214 QDateTime _getCacheTimestamp(const QString &url) const;
215 bool _startDownload(const QString &url, bool forceNetwork, bool preferCache);
216 void _emitFinished(bool success, const QString &path, const QString &error);
217
218 QGCFileDownload *_fileDownload = nullptr;
219 QNetworkDiskCache *_diskCache = nullptr;
220
221 QUrl _url;
222 QString _localPath;
223 QString _errorString;
224 QString _pendingUrl;
225
226 qreal _progress = 0.0;
227 int _maxCacheAgeSec = 0;
228 bool _running = false;
229 bool _fromCache = false;
230 bool _networkAttemptFailed = false;
231 bool _forceNetwork = false;
232 bool _cancelRequested = false;
233};
Error error
Cached file download with time-based expiration.
void maxCacheSizeChanged(qint64 bytes)
Emitted when max cache size changes.
bool downloadPreferCache(const QString &url)
void urlChanged(const QUrl &url)
Emitted when URL changes.
Q_INVOKABLE bool isCached(const QString &url, int maxAgeSec=0) const
void cancel()
Cancel current download.
bool removeFromCache(const QString &url)
void downloadProgress(qint64 bytesReceived, qint64 totalBytes)
Emitted during download with byte counts.
void localPathChanged(const QString &localPath)
Emitted when local path changes.
Q_INVOKABLE QString cachedPath(const QString &url) const
void setCacheDirectory(const QString &directory)
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 clearCache()
Clear all cached files.
QNetworkDiskCache * diskCache() const
Get the disk cache (for advanced configuration)
void errorStringChanged(const QString &errorString)
Emitted when error string changes.
bool downloadNoCache(const QString &url)
bool download(const QString &url, int maxCacheAgeSec)
void setMaxCacheSize(qint64 bytes)
QGCFileDownload * fileDownloader() const
Get the underlying file downloader (for advanced configuration)
void runningChanged(bool running)
Emitted when running state changes.
Q_INVOKABLE int cacheAge(const QString &url) const
void finished(bool success, const QString &localPath, const QString &errorMessage, bool fromCache)
void fromCacheChanged(bool fromCache)
Emitted when fromCache state changes.
File download with progress, decompression, and hash verification.