QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCFileDownload.h
Go to the documentation of this file.
1#pragma once
2
5
6#include "QGCNetworkHelper.h"
7
8#include <QtCore/QLoggingCategory>
9#include <QtCore/QObject>
10#include <QtCore/QUrl>
11#include <QtNetwork/QNetworkReply>
12#include <QtNetwork/QNetworkRequest>
13
14class QNetworkAccessManager;
15class QAbstractNetworkCache;
17class QFile;
18
19Q_DECLARE_LOGGING_CATEGORY(QGCFileDownloadLog)
20
21
62class QGCFileDownload : public QObject
63{
64 Q_OBJECT
65 Q_DISABLE_COPY_MOVE(QGCFileDownload)
66
67
68 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged FINAL)
69
70
71 Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged FINAL)
72
73
74 Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged FINAL)
75
76
77 Q_PROPERTY(QUrl url READ url NOTIFY urlChanged FINAL)
78
79
80 Q_PROPERTY(QString localPath READ localPath NOTIFY localPathChanged FINAL)
81
82
83 Q_PROPERTY(qint64 totalBytes READ totalBytes NOTIFY totalBytesChanged FINAL)
84
85
86 Q_PROPERTY(qint64 bytesReceived READ bytesReceived NOTIFY bytesReceivedChanged FINAL)
87
88
89 Q_PROPERTY(bool autoDecompress READ autoDecompress WRITE setAutoDecompress NOTIFY autoDecompressChanged FINAL)
90
91
92 Q_PROPERTY(QString outputPath READ outputPath WRITE setOutputPath NOTIFY outputPathChanged FINAL)
93
94
95 Q_PROPERTY(QString expectedHash READ expectedHash WRITE setExpectedHash NOTIFY expectedHashChanged FINAL)
96
97public:
99 enum class State {
100 Idle,
101 Downloading,
102 Decompressing,
103 Verifying,
104 Completed,
105 Failed,
106 Cancelled
107 };
108 Q_ENUM(State)
109
110 explicit QGCFileDownload(QObject *parent = nullptr);
111 ~QGCFileDownload() override;
112
113 // Property getters
114 qreal progress() const { return _progress; }
115 bool isRunning() const {
116 return _state == State::Downloading || _state == State::Decompressing
117 || _state == State::Verifying;
118 }
119 QString errorString() const { return _errorString; }
120 QUrl url() const { return _url; }
121 QString localPath() const { return _localPath; }
122 qint64 totalBytes() const { return _totalBytes; }
123 qint64 bytesReceived() const { return _bytesReceived; }
124 bool autoDecompress() const { return _autoDecompress; }
125 QString outputPath() const { return _outputPath; }
126 QString expectedHash() const { return _expectedHash; }
127 State state() const { return _state; }
128 bool lastResultFromCache() const { return _lastResultFromCache; }
129
130 // Property setters
131 void setAutoDecompress(bool enabled);
132 void setOutputPath(const QString &path);
133 void setExpectedHash(const QString &hash);
134
136 void setCache(QAbstractNetworkCache *cache);
137
139 void setTimeout(int timeoutMs);
140
142 QNetworkAccessManager *networkManager() const { return _networkManager; }
143
144public slots:
148 bool start(const QString &remoteUrl);
149
154 bool start(const QString &remoteUrl, const QGCNetworkHelper::RequestConfig &config);
155
157 void cancel();
158
159signals:
161 void progressChanged(qreal progress);
162
164 void runningChanged(bool running);
165
167 void errorStringChanged(const QString &errorString);
168
170 void urlChanged(const QUrl &url);
171
173 void localPathChanged(const QString &localPath);
174
176 void totalBytesChanged(qint64 totalBytes);
177
179 void bytesReceivedChanged(qint64 bytesReceived);
180
182 void autoDecompressChanged(bool autoDecompress);
183
185 void outputPathChanged(const QString &outputPath);
186
188 void expectedHashChanged(const QString &expectedHash);
189
191 void stateChanged(State state);
192
197 void finished(bool success, const QString &localPath, const QString &errorMessage);
198
200 void downloadProgress(qint64 bytesReceived, qint64 totalBytes);
201
203 void decompressionProgress(qreal progress);
204
205private slots:
206 void _onDownloadProgress(qint64 bytesReceived, qint64 totalBytes);
207 void _onDownloadFinished();
208 void _onDownloadError(QNetworkReply::NetworkError code);
209 void _onDecompressionFinished(bool success);
210 void _onReadyRead();
211
212private:
213 void _setState(State newState);
214 void _setProgress(qreal progress);
215 void _setErrorString(const QString &error);
216 void _cleanup();
217 void _emitFinished(bool success, const QString &localPath, const QString &errorMessage);
218 bool _writeReplyData(const QByteArray &data);
219 bool _failForWriteError(const QString &context);
220 QString _generateOutputPath(const QString &remoteUrl) const;
221 bool _verifyHash();
222 void _startDecompression();
223
224 QNetworkAccessManager *_networkManager = nullptr;
225 QNetworkReply *_currentReply = nullptr;
226 QGCCompressionJob *_decompressionJob = nullptr;
227 QFile *_outputFile = nullptr;
228
229 QUrl _url;
230 QString _localPath;
231 QString _outputPath;
232 QString _expectedHash;
233 QString _errorString;
234 QString _compressedFilePath;
235
236 qreal _progress = 0.0;
237 qint64 _totalBytes = -1;
238 qint64 _bytesReceived = 0;
240
241 State _state = State::Idle;
242 bool _autoDecompress = false;
243 bool _finishEmitted = false;
244 bool _lastResultFromCache = false;
245};
Q_DECLARE_LOGGING_CATEGORY(AndroidSerialLog)
QString errorString
Error error
void finished(bool success, const QString &localPath, const QString &errorMessage)
void localPathChanged(const QString &localPath)
Emitted when local path changes.
void runningChanged(bool running)
Emitted when running state changes.
void totalBytesChanged(qint64 totalBytes)
Emitted when total bytes changes.
void stateChanged(State state)
Emitted when state changes.
void autoDecompressChanged(bool autoDecompress)
Emitted when auto-decompress setting changes.
void bytesReceivedChanged(qint64 bytesReceived)
Emitted when bytes received changes.
void errorStringChanged(const QString &errorString)
Emitted when error string changes.
void progressChanged(qreal progress)
Emitted when download progress changes.
void expectedHashChanged(const QString &expectedHash)
Emitted when expected hash setting changes.
void downloadProgress(qint64 bytesReceived, qint64 totalBytes)
Emitted during download with byte counts.
void outputPathChanged(const QString &outputPath)
Emitted when output path setting changes.
void urlChanged(const QUrl &url)
Emitted when URL changes.
void decompressionProgress(qreal progress)
Emitted during decompression (0.0 to 1.0)
constexpr int kDefaultTimeoutMs
Default request timeout in milliseconds.
Common request configuration options.