libosmscout  1.1.1
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 
40 namespace osmscout {
41 
42 namespace FileDownloaderConfig {
43 
44 static constexpr uint64_t BufferNetwork{1024*1024*1};
45 static constexpr std::chrono::seconds DownloadReadTimeout{60};
46 static constexpr std::chrono::seconds BackOffInitial{1};
47 static constexpr std::chrono::seconds BackOffMax{300};
48 static constexpr int MaxDownloadRetries{-1};
49 }
50 
56 class OSMSCOUT_CLIENT_QT_API FileDownloader : public QObject
57 {
58  Q_OBJECT
59 
60 #ifdef OSMSCOUT_CLIENT_QT_FILEDOWNLOADER_TEST
61 public: // make possible to modify internal state in test
62 #else
63 private:
64 #endif
65 
66  struct BackOff {
67  int downloadRetries{0};
68 
69  std::chrono::seconds backOffTime{FileDownloaderConfig::BackOffInitial};
70  QTimer restartTimer;
71 
72  bool scheduleRestart();
73  void recover();
74  };
75 
76  BackOff backOff;
77 
78  QNetworkAccessManager *manager;
79  QUrl url;
80  QString path;
81 
82  QNetworkReply *reply{nullptr};
83 
84  QFile file;
85 
86  bool isOk{true};
87  bool finishedSuccessfully{false};
88 
89  uint64_t downloaded{0};
90 
91  QTimer timeoutTimer;
92 
93 public:
94  explicit FileDownloader(QNetworkAccessManager *manager,
95  QString url,
96  QString path,
97  QObject *parent = nullptr);
98  ~FileDownloader() override;
99 
100  operator bool() const { return isOk; }
101  QString getFileName() const { return QFileInfo(path).fileName(); }
102  QString getFilePath() const { return path; }
103  uint64_t getBytesDownloaded() const;
104 
105 signals:
106  void downloadedBytes(uint64_t sz);
107  void writtenBytes(uint64_t sz);
108  void finished(QString path);
109  void error(QString error_text, bool recoverable);
110 
111 public slots:
112  void startDownload();
113 
114 protected slots:
115  void onNetworkReadyRead();
116  void onDownloaded();
117  void onNetworkError(QNetworkReply::NetworkError code);
118  void onTimeout();
119 
120 protected:
121  void onFinished();
122  void onError(const QString &err);
123 
124  bool restartDownload();
125 };
126 
127 
131 class OSMSCOUT_CLIENT_QT_API DownloadJob: public QObject
132 {
133  Q_OBJECT
134 
135 protected:
136  QList<FileDownloader*> jobs;
137  QNetworkAccessManager *webCtrl;
138 
139  QDir target;
140 
141  bool done{false};
142  bool started{false};
143  bool successful{false};
144  bool canceledByUser{false};
145 
146  uint64_t downloadedBytes{0};
147 
148  QString error;
149 
151 
152 signals:
153  void finished(); // successfully
154  void failed(QString error);
155  void canceled();
156  void downloadProgress();
157 
158 public slots:
159  void onJobFailed(QString errorMessage, bool recoverable);
160  void onJobFinished(QString path);
161  void onDownloadProgress(uint64_t);
162  void downloadNextFile();
163 
164 public:
165  DownloadJob(QNetworkAccessManager *webCtrl, QDir target, bool replaceExisting);
166  ~DownloadJob() override;
167 
168  DownloadJob(const DownloadJob&) = delete;
169  DownloadJob(DownloadJob&&) = delete;
170 
171  DownloadJob& operator=(const DownloadJob&) = delete;
172  DownloadJob& operator==(const DownloadJob&&) = delete;
173 
174  void start(const QString &serverBasePath, const QStringList &files);
175 
182  void cancel();
183 
184  virtual uint64_t expectedSize() const = 0;
185 
186  inline bool isDone() const
187  {
188  return done;
189  }
190 
191  inline bool isSuccessful() const
192  {
193  return successful;
194  }
195 
196  inline bool isDownloading() const
197  {
198  return started && !done;
199  }
200 
201  inline QString getError() const
202  {
203  return error;
204  }
205 
206  inline bool isReplaceExisting() const
207  {
208  return replaceExisting;
209  }
210 
211  inline QDir getDestinationDirectory() const
212  {
213  return target;
214  }
215 
216  double getProgress();
217  QString getDownloadingFile();
218 
219 protected:
223  void clearJobs();
224 
225 };
226 
227 }
228 
229 #endif // OSMSCOUT_CLIENT_QT_FILEDOWNLOADER_H
bool isSuccessful() const
Definition: FileDownloader.h:191
QString getFileName() const
Definition: FileDownloader.h:101
#define OSMSCOUT_CLIENT_QT_API
Definition: ClientQtImportExport.h:45
Definition: FileDownloader.h:131
QString getFilePath() const
Definition: FileDownloader.h:102
Downloads a file specified by URL.
Definition: FileDownloader.h:56
QList< FileDownloader * > jobs
Definition: FileDownloader.h:136
bool replaceExisting
Definition: FileDownloader.h:150
bool isDone() const
Definition: FileDownloader.h:186
static constexpr int MaxDownloadRetries
Maximal number of download retries before cancelling download.
Definition: FileDownloader.h:48
static constexpr std::chrono::seconds BackOffMax
Maximum back-off time.
Definition: FileDownloader.h:47
bool isDownloading() const
Definition: FileDownloader.h:196
bool isReplaceExisting() const
Definition: FileDownloader.h:206
QNetworkAccessManager * webCtrl
Definition: FileDownloader.h:137
Definition: Area.h:38
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
QString getError() const
Definition: FileDownloader.h:201
bool operator==(const MapView &a, const MapView &b)
Definition: InputHandler.h:222
QDir target
Definition: FileDownloader.h:139
QString error
Definition: FileDownloader.h:148
QDir getDestinationDirectory() const
Definition: FileDownloader.h:211
static constexpr std::chrono::seconds BackOffInitial
Initial back-off time.
Definition: FileDownloader.h:46