LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
networkrequestbase.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_NETWORKREQUESTBASE_H
21 #define LIBREPCB_NETWORKREQUESTBASE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../exceptions.h"
27 
28 #include <QtCore>
29 #include <QtNetwork>
30 
31 /*******************************************************************************
32  * Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 
36 /*******************************************************************************
37  * Class NetworkRequestBase
38  ******************************************************************************/
39 
60 class NetworkRequestBase : public QObject {
61  Q_OBJECT
62 
63 public:
64  // Constructors / Destructor
65  NetworkRequestBase() = delete;
66  NetworkRequestBase(const NetworkRequestBase& other) = delete;
67  NetworkRequestBase(const QUrl& url) noexcept;
68  virtual ~NetworkRequestBase() noexcept;
69 
70  // Setters
71 
78  void setHeaderField(const QByteArray& name, const QByteArray& value) noexcept;
79 
89  void setExpectedReplyContentSize(qint64 bytes) noexcept;
90 
91  // Operator Overloadings
92  NetworkRequestBase& operator=(const NetworkRequestBase& rhs) = delete;
93 
94 public slots:
95 
105  void start() noexcept;
106 
116  void abort() noexcept;
117 
118 signals:
119 
123  void startRequested();
124 
134  void progressState(QString state);
135 
141  void progressPercent(int percent);
142 
150  void progress(qint64 bytesReceived, qint64 bytesTotal, int percent);
151 
155  void aborted();
156 
160  void succeeded();
161 
167  void errored(QString errorMsg);
168 
176  void finished(bool success);
177 
178 public: // Methods
179  virtual void prepareRequest() = 0;
180  virtual void finalizeRequest() = 0;
181  virtual void emitSuccessfullyFinishedSignals() noexcept = 0;
182  virtual void fetchNewData() noexcept = 0;
183 
184 private: // Methods
185  void executeRequest() noexcept;
186  void replyReadyReadSlot() noexcept;
187  void replyErrorSlot(QNetworkReply::NetworkError code) noexcept;
188  void replySslErrorsSlot(const QList<QSslError>& errors) noexcept;
189  void replyDownloadProgressSlot(qint64 bytesReceived,
190  qint64 bytesTotal) noexcept;
191  void replyFinishedSlot() noexcept;
192  void finalize(const QString& errorMsg = QString()) noexcept;
193  static QString formatFileSize(qint64 bytes) noexcept;
194 
195 protected: // Data
196  // from constructor
197  QUrl mUrl;
199 
200  // internal data
201  QList<QUrl> mRedirectedUrls;
202  QNetworkRequest mRequest;
203  QScopedPointer<QNetworkReply> mReply;
204  bool mStarted;
205  bool mAborted;
206  bool mErrored;
207  bool mFinished;
208 };
209 
210 /*******************************************************************************
211  * End of File
212  ******************************************************************************/
213 
214 } // namespace librepcb
215 
216 #endif // LIBREPCB_NETWORKREQUESTBASE_H
QList< QUrl > mRedirectedUrls
Definition: networkrequestbase.h:201
virtual void prepareRequest()=0
void aborted()
Request aborted signal (emited right before finished())
QUrl mUrl
Definition: networkrequestbase.h:197
qint64 mExpectedContentSize
Definition: networkrequestbase.h:198
bool mStarted
Definition: networkrequestbase.h:204
void replyDownloadProgressSlot(qint64 bytesReceived, qint64 bytesTotal) noexcept
Definition: networkrequestbase.cpp:181
void progressState(QString state)
Reply progress / state changed signal.
void errored(QString errorMsg)
Request errored signal (emited right before finished())
virtual void fetchNewData() noexcept=0
void replyErrorSlot(QNetworkReply::NetworkError code) noexcept
Definition: networkrequestbase.cpp:165
bool mFinished
Definition: networkrequestbase.h:207
void setExpectedReplyContentSize(qint64 bytes) noexcept
Set the expected size of the requested content.
Definition: networkrequestbase.cpp:82
void executeRequest() noexcept
Definition: networkrequestbase.cpp:116
void start() noexcept
Start downloading the requested content.
Definition: networkrequestbase.cpp:88
void progressPercent(int percent)
Reply content download progress signal (simple)
void finished(bool success)
Request finished signal.
Base class for network requests which are processed in the network access manager.
Definition: networkrequestbase.h:60
void abort() noexcept
Abort downloading the requested content.
Definition: networkrequestbase.cpp:103
virtual void finalizeRequest()=0
virtual ~NetworkRequestBase() noexcept
Definition: networkrequestbase.cpp:63
QScopedPointer< QNetworkReply > mReply
Definition: networkrequestbase.h:203
void startRequested()
Internal signal, don't use it from outside.
bool mAborted
Definition: networkrequestbase.h:205
static QString formatFileSize(qint64 bytes) noexcept
Definition: networkrequestbase.cpp:287
NetworkRequestBase & operator=(const NetworkRequestBase &rhs)=delete
virtual void emitSuccessfullyFinishedSignals() noexcept=0
void setHeaderField(const QByteArray &name, const QByteArray &value) noexcept
Set a HTTP header field for the network request.
Definition: networkrequestbase.cpp:75
void succeeded()
Request succeeded signal (emited right before finished())
QNetworkRequest mRequest
Definition: networkrequestbase.h:202
void progress(qint64 bytesReceived, qint64 bytesTotal, int percent)
Reply content download progress signal (extended)
void replySslErrorsSlot(const QList< QSslError > &errors) noexcept
Definition: networkrequestbase.cpp:172
void finalize(const QString &errorMsg=QString()) noexcept
Definition: networkrequestbase.cpp:258
bool mErrored
Definition: networkrequestbase.h:206
void replyFinishedSlot() noexcept
Definition: networkrequestbase.cpp:200
void replyReadyReadSlot() noexcept
Definition: networkrequestbase.cpp:160