Enroute Flight Navigation
A navigation app for VFR pilots
Downloadable_SingleFile.h
1/***************************************************************************
2 * Copyright (C) 2019-2025 by Stefan Kebekus *
3 * stefan.kebekus@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#pragma once
22
23#include <QDateTime>
24#include <QFile>
25#include <QFileInfo>
26#include <QNetworkReply>
27#include <QPointer>
28#include <QQmlEngine>
29#include <QSaveFile>
30
31#include "Downloadable_Abstract.h"
32
33namespace DataManagement
34{
35
50
52{
53 Q_OBJECT
54 QML_ELEMENT
55 QML_UNCREATABLE("")
56
57public:
84 explicit Downloadable_SingleFile(QUrl url, const QString& localFileName, const QGeoRectangle& bBox = {}, QObject* parent = nullptr);
85
92
93
94
95 //
96 // Properties
97 //
98
99 // Repeated from Downloadable_Abstract to keep QML happy
100 Q_PROPERTY(bool downloading READ downloading NOTIFY downloadingChanged)
101 Q_PROPERTY(bool hasFile READ hasFile BINDABLE bindableHasFile NOTIFY hasFileChanged)
102
103
109
111 Q_PROPERTY(QString fileName READ fileName CONSTANT)
112
118 Q_PROPERTY(QByteArray fileContent READ fileContent NOTIFY fileContentChanged)
119
127
135
137 Q_PROPERTY(QUrl url READ url CONSTANT)
138
139
140
141 //
142 // Getter Methods
143 //
144
149 [[nodiscard]] auto description() -> QString override;
150
155 [[nodiscard]] auto downloading() -> bool override { return !m_networkReplyDownloadFile.isNull(); }
156
161 [[nodiscard]] auto downloadProgress() const -> int { return m_downloadProgress; }
162
167 [[nodiscard]] auto fileName() const -> QString { return m_fileName; }
168
173 [[nodiscard]] auto fileContent() const -> QByteArray;
174
179 [[nodiscard]] auto files() -> QStringList override;
180
185 [[nodiscard]] auto infoText() -> QString override;
186
191 [[nodiscard]] auto remoteFileDate() const -> QDateTime { return m_remoteFileDate; }
192
197 [[nodiscard]] auto remoteFileSize() -> qint64 override { return m_remoteFileSize; }
198
203 [[nodiscard]] auto updateSize() -> Units::ByteSize override;
204
209 [[nodiscard]] auto url() const -> QUrl { return m_url; }
210
211
212
213 //
214 // Setter Methods
215 //
216
221 void setRemoteFileDate(const QDateTime &date);
222
227 void setRemoteFileSize(qint64 size);
228
229
230
231 //
232 // Methods
233 //
234
241 Q_INVOKABLE void deleteFiles() override;
242
277 Q_INVOKABLE void startDownload() override;
278
291 Q_INVOKABLE void startInfoDownload();
292
299 Q_INVOKABLE void stopDownload() override;
300
302 Q_INVOKABLE void update() override;
303
304
305signals:
314 void downloadProgressChanged(int percentage);
315
324
325private:
326 Q_DISABLE_COPY_MOVE(Downloadable_SingleFile)
327
328 // Called when an error occurs during the download of the remote file, this
329 // method deletes QTemporaryFile _tmpFile, emits the signal error() and
330 // deletes _networkReplyDownload by calling deleteLater. Connected to
331 // &QNetworkReply::error of _networkReplyDownload.
332 void downloadFileErrorReceiver(QNetworkReply::NetworkError code);
333
334 // Called once download of the remote file is finished, this method
335 // overwrites the local file. It deletes the QTemporaryFile _tmpFile, and
336 // deletes _networkReplyDownload by calling deleteLater. Connected to
337 // &QNetworkReply::finished of _networkReplyDownload.
338 void downloadFileFinished();
339
340 // Called during the download of the remote file, this method emits the
341 // signal downloadProgress(). Connected to &QNetworkReply::downloadProgress
342 // of _networkReplyDownload.
343 void downloadFileProgressReceiver(qint64 bytesReceived, qint64 bytesTotal);
344
345 // Called during the download of the remote file, this method reads all the
346 // data that has been downloaded so far and stores it in the QTemporaryFile
347 // _tmpFile. Connected to &QNetworkReply::readyRead of
348 // _networkReplyDownload.
349 void downloadFilePartialDataReceiver();
350
351 // Called once download of the remote file header data is finished, this
352 // method updates the properties remoteFileDate and remoteFileSize, and
353 // _networkReplyDownloadHeader by calling deleteLater. Connected to
354 // &QNetworkReply::finished of _networkReplyDownloadHeader.
355 void downloadHeaderFinished();
356
357 // This member holds the download progress.
358 int m_downloadProgress{0};
359
360 // NetworkReply for downloading of remote file data. Set to nullptr when no
361 // download is in progress.
362 QPointer<QNetworkReply> m_networkReplyDownloadFile;
363
364 // NetworkReply for downloading of remote file header. Set to nullptr when
365 // no download is in progress.
366 QPointer<QNetworkReply> m_networkReplyDownloadHeader;
367
368 // Temporary file for storing partiall data when downloading the remote
369 // file. Set to nullptr when no download is in progress.
370 QPointer<QSaveFile> m_saveFile;
371
372 // URL of the remote file, as set in the constructor
373 QUrl m_url;
374
375 // Name of the local file, as set in the constructor
376 QString m_fileName;
377
378 // Modification date of the remote file, set directly via a setter method or
379 // by calling downloadRemoteFileInfo().
380 QDateTime m_remoteFileDate;
381
382 // Size of the remote file, set directly via a setter method or by calling
383 // downloadRemoteFileInfo().
384 qint64 m_remoteFileSize{-1};
385};
386
387} // namespace DataManagement
void downloadingChanged()
Notifier signal.
void remoteFileSizeChanged()
Notifier signal.
Downloadable_Abstract(QObject *parent=nullptr)
Standard constructor.
void hasFileChanged()
Notifier signal.
QBindable< bool > bindableHasFile() const
Getter method for the property with the same name.
void fileContentChanged()
Indicates that the content of a local file (or several local files) has changed.
void downloadProgressChanged(int percentage)
Download progress.
auto remoteFileSize() -> qint64 override
Getter function for the property with the same name.
auto fileContent() const -> QByteArray
Getter function for the property with the same name.
auto fileName() const -> QString
Getter function for the property with the same name.
Q_INVOKABLE void stopDownload() override
Stops download process.
~Downloadable_SingleFile() override
Standard destructor.
QByteArray fileContent
Content of the downloaded file.
auto infoText() -> QString override
Implementation of pure virtual getter method from Downloadable_Abstract.
Q_INVOKABLE void startInfoDownload()
Contacts the server and downloads information about the remote file.
QDateTime remoteFileDate
Modification date of the remote file.
auto downloadProgress() const -> int
Getter function for the property with the same name.
Downloadable_SingleFile(QUrl url, const QString &localFileName, const QGeoRectangle &bBox={}, QObject *parent=nullptr)
Standard constructor.
void setRemoteFileSize(qint64 size)
Setter function for the property with the same name.
QString fileName
File name, as set in the constructor.
QUrl url
URL, as set in the constructor.
Q_INVOKABLE void update() override
Implementation of pure virtual method from Downloadable_Abstract.
void remoteFileDateChanged()
Notifier signal for the properties remoteFileDate and remoteFileSize.
qint64 remoteFileSize
Size of the remote file.
auto updateSize() -> Units::ByteSize override
Implementation of pure virtual getter method from Downloadable_Abstract.
Q_INVOKABLE void startDownload() override
Initiate a download.
auto url() const -> QUrl
Getter function for the property with the same name.
auto description() -> QString override
Implementation of pure virtual getter method from Downloadable_Abstract.
auto files() -> QStringList override
Implementation of pure virtual getter method from Downloadable_Abstract.
void setRemoteFileDate(const QDateTime &date)
Setter function for the property with the same name.
Q_INVOKABLE void deleteFiles() override
Implementation of pure virtual method from Downloadable_Abstract.
Convenience class for size_t.
Definition ByteSize.h:37