libosmscout 1.1.1
Loading...
Searching...
No Matches
FileDownloader.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_CLIENT_QT_FILEDOWNLOADER_H
2#define OSMSCOUT_CLIENT_QT_FILEDOWNLOADER_H
3
4/*
5 OSMScout - a Qt backend for libosmscout and libosmscout-map
6 Copyright (C) 2017 Rinigus
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <QObject>
24#include <QString>
25#include <QNetworkAccessManager>
26#include <QNetworkReply>
27#include <QFile>
28#include <QUrl>
29#include <QProcess>
30#include <QByteArray>
31#include <QTime>
32#include <QFileInfo>
33#include <QTimer>
34#include <QDir>
35
37
38#include <chrono>
39
40namespace osmscout {
41
43
44static constexpr uint64_t BufferNetwork{1024*1024*1};
45static constexpr std::chrono::seconds DownloadReadTimeout{60};
46static constexpr std::chrono::seconds BackOffInitial{1};
47static constexpr std::chrono::seconds BackOffMax{300};
48static constexpr int MaxDownloadRetries{-1};
49static constexpr char const *TemporaryFileSuffix{".download"};
50}
51
58{
59 Q_OBJECT
60
61#ifdef OSMSCOUT_CLIENT_QT_FILEDOWNLOADER_TEST
62public: // make possible to modify internal state in test
63#else
64private:
65#endif
66
67 struct BackOff {
68 int downloadRetries{0};
69
70 std::chrono::seconds backOffTime{FileDownloaderConfig::BackOffInitial};
71 QTimer restartTimer;
72
73 bool scheduleRestart();
74 void recover();
75 };
76
77 BackOff backOff;
78
79 QNetworkAccessManager *manager;
80 QUrl url;
81 QString path;
82
83 QNetworkReply *reply{nullptr};
84
85 QFile file;
86
87 bool isOk{true};
88 bool finishedSuccessfully{false};
89
90 uint64_t downloaded{0};
91
92 QTimer timeoutTimer;
93
94public:
95 explicit FileDownloader(QNetworkAccessManager *manager,
96 QString url,
97 QString path,
98 QObject *parent = nullptr);
99 ~FileDownloader() override;
100
101 explicit operator bool() const { return isOk; }
102 QString getFileName() const { return QFileInfo(path).fileName(); }
103 QString getFilePath() const { return path; }
104 uint64_t getBytesDownloaded() const;
105
106signals:
107 void downloadedBytes(uint64_t sz);
108 void writtenBytes(uint64_t sz);
109 void finished(QString path);
110 void error(QString error_text, bool recoverable);
111
112public slots:
114
115protected slots:
118 void onNetworkError(QNetworkReply::NetworkError code);
119 void onTimeout();
120
121protected:
123 void onError(const QString &err);
124
126};
127
128
133{
134 Q_OBJECT
135
136protected:
137 QList<FileDownloader*> jobs;
138 QNetworkAccessManager *webCtrl;
139
140 QDir target;
141
142 bool done{false};
143 bool started{false};
144 bool successful{false};
145 bool canceledByUser{false};
146
147 uint64_t downloadedBytes{0};
148
149 QString error;
150
152
153signals:
154 void finished(); // successfully
155 void failed(QString error);
156 void canceled();
158
159public slots:
160 void onJobFailed(QString errorMessage, bool recoverable);
161 void onJobFinished(QString path);
162 void onDownloadProgress(uint64_t);
164
165public:
166 DownloadJob(QNetworkAccessManager *webCtrl, QDir target, bool replaceExisting);
167 ~DownloadJob() override;
168
169 DownloadJob(const DownloadJob&) = delete;
171
174
175 void start(const QString &serverBasePath, const QStringList &files);
176
183 void cancel();
184
185 virtual uint64_t expectedSize() const = 0;
186
187 bool isDone() const
188 {
189 return done;
190 }
191
192 bool isSuccessful() const
193 {
194 return successful;
195 }
196
197 bool isDownloading() const
198 {
199 return started && !done;
200 }
201
202 QString getError() const
203 {
204 return error;
205 }
206
207 bool isReplaceExisting() const
208 {
209 return replaceExisting;
210 }
211
213 {
214 return target;
215 }
216
217 double getProgress();
219
220protected:
224 void clearJobs();
225
226};
227
228}
229
230#endif // OSMSCOUT_CLIENT_QT_FILEDOWNLOADER_H
#define OSMSCOUT_CLIENT_QT_API
Definition ClientQtImportExport.h:45
QString getError() const
Definition FileDownloader.h:202
DownloadJob(const DownloadJob &)=delete
QList< FileDownloader * > jobs
Definition FileDownloader.h:137
bool isSuccessful() const
Definition FileDownloader.h:192
bool isReplaceExisting() const
Definition FileDownloader.h:207
DownloadJob(QNetworkAccessManager *webCtrl, QDir target, bool replaceExisting)
QNetworkAccessManager * webCtrl
Definition FileDownloader.h:138
void onJobFailed(QString errorMessage, bool recoverable)
bool done
Definition FileDownloader.h:142
QString getDownloadingFile()
bool isDone() const
Definition FileDownloader.h:187
QString error
Definition FileDownloader.h:149
DownloadJob & operator=(const DownloadJob &)=delete
void onJobFinished(QString path)
bool isDownloading() const
Definition FileDownloader.h:197
DownloadJob & operator==(const DownloadJob &&)=delete
bool successful
Definition FileDownloader.h:144
QDir getDestinationDirectory() const
Definition FileDownloader.h:212
bool started
Definition FileDownloader.h:143
QDir target
Definition FileDownloader.h:140
void failed(QString error)
DownloadJob(DownloadJob &&)=delete
void start(const QString &serverBasePath, const QStringList &files)
uint64_t downloadedBytes
Definition FileDownloader.h:147
bool canceledByUser
Definition FileDownloader.h:145
virtual uint64_t expectedSize() const =0
void onDownloadProgress(uint64_t)
bool replaceExisting
Definition FileDownloader.h:151
void writtenBytes(uint64_t sz)
void finished(QString path)
void error(QString error_text, bool recoverable)
void onNetworkError(QNetworkReply::NetworkError code)
FileDownloader(QNetworkAccessManager *manager, QString url, QString path, QObject *parent=nullptr)
uint64_t getBytesDownloaded() const
void downloadedBytes(uint64_t sz)
QString getFilePath() const
Definition FileDownloader.h:103
void onError(const QString &err)
bool restartDownload()
Restart download if download retries are not used up.
QString getFileName() const
Definition FileDownloader.h:102
Definition FileDownloader.h:42
static constexpr std::chrono::seconds DownloadReadTimeout
Download read timeout in seconds.
Definition FileDownloader.h:45
static constexpr uint64_t BufferNetwork
Size of network ring buffer.
Definition FileDownloader.h:44
static constexpr std::chrono::seconds BackOffMax
Maximum back-off time.
Definition FileDownloader.h:47
static constexpr char const * TemporaryFileSuffix
suffix of file being downloaded
Definition FileDownloader.h:49
static constexpr int MaxDownloadRetries
Maximal number of download retries before cancelling download.
Definition FileDownloader.h:48
static constexpr std::chrono::seconds BackOffInitial
Initial back-off time.
Definition FileDownloader.h:46
Definition Area.h:39