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 
57 class NetworkRequestBase : public QObject {
58  Q_OBJECT
59 
60 public:
61  // Constructors / Destructor
62  NetworkRequestBase() = delete;
63  NetworkRequestBase(const NetworkRequestBase& other) = delete;
64  NetworkRequestBase(const QUrl& url) noexcept;
65  virtual ~NetworkRequestBase() noexcept;
66 
67  // Setters
68 
75  void setHeaderField(const QByteArray& name, const QByteArray& value) noexcept;
76 
86  void setExpectedReplyContentSize(qint64 bytes) noexcept;
87 
88  // Operator Overloadings
89  NetworkRequestBase& operator=(const NetworkRequestBase& rhs) = delete;
90 
91 public slots:
92 
102  void start() noexcept;
103 
113  void abort() noexcept;
114 
115 signals:
116 
120  void startRequested();
121 
131  void progressState(QString state);
132 
138  void progressPercent(int percent);
139 
147  void progress(qint64 bytesReceived, qint64 bytesTotal, int percent);
148 
152  void aborted();
153 
157  void succeeded();
158 
164  void errored(QString errorMsg);
165 
173  void finished(bool success);
174 
175 public: // Methods
176  virtual void prepareRequest() = 0;
177  virtual void finalizeRequest() = 0;
178  virtual void emitSuccessfullyFinishedSignals() noexcept = 0;
179  virtual void fetchNewData() noexcept = 0;
180 
181 private: // Methods
182  void executeRequest() noexcept;
183  void replyReadyReadSlot() noexcept;
184  void replyErrorSlot(QNetworkReply::NetworkError code) noexcept;
185  void replySslErrorsSlot(const QList<QSslError>& errors) noexcept;
186  void replyDownloadProgressSlot(qint64 bytesReceived,
187  qint64 bytesTotal) noexcept;
188  void replyFinishedSlot() noexcept;
189  void finalize(const QString& errorMsg = QString()) noexcept;
190  static QString formatFileSize(qint64 bytes) noexcept;
191  static QString getUserAgent() noexcept;
192 
193 protected: // Data
194  // from constructor
195  QUrl mUrl;
197 
198  // internal data
199  QList<QUrl> mRedirectedUrls;
200  QNetworkRequest mRequest;
201  QScopedPointer<QNetworkReply> mReply;
202  bool mStarted;
203  bool mAborted;
204  bool mErrored;
205  bool mFinished;
206 };
207 
208 /*******************************************************************************
209  * End of File
210  ******************************************************************************/
211 
212 } // namespace librepcb
213 
214 #endif // LIBREPCB_NETWORKREQUESTBASE_H
QList< QUrl > mRedirectedUrls
Definition: networkrequestbase.h:199
virtual void prepareRequest()=0
void aborted()
Request aborted signal (emited right before finished())
QUrl mUrl
Definition: networkrequestbase.h:195
qint64 mExpectedContentSize
Definition: networkrequestbase.h:196
bool mStarted
Definition: networkrequestbase.h:202
void replyDownloadProgressSlot(qint64 bytesReceived, qint64 bytesTotal) noexcept
Definition: networkrequestbase.cpp:180
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:164
bool mFinished
Definition: networkrequestbase.h:205
void setExpectedReplyContentSize(qint64 bytes) noexcept
Set the expected size of the requested content.
Definition: networkrequestbase.cpp:81
void executeRequest() noexcept
Definition: networkrequestbase.cpp:115
void start() noexcept
Start downloading the requested content.
Definition: networkrequestbase.cpp:87
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:57
void abort() noexcept
Abort downloading the requested content.
Definition: networkrequestbase.cpp:102
virtual void finalizeRequest()=0
virtual ~NetworkRequestBase() noexcept
Definition: networkrequestbase.cpp:62
QScopedPointer< QNetworkReply > mReply
Definition: networkrequestbase.h:201
static QString getUserAgent() noexcept
Definition: networkrequestbase.cpp:298
void startRequested()
Internal signal, don't use it from outside.
bool mAborted
Definition: networkrequestbase.h:203
static QString formatFileSize(qint64 bytes) noexcept
Definition: networkrequestbase.cpp:286
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:74
void succeeded()
Request succeeded signal (emited right before finished())
QNetworkRequest mRequest
Definition: networkrequestbase.h:200
void progress(qint64 bytesReceived, qint64 bytesTotal, int percent)
Reply content download progress signal (extended)
void replySslErrorsSlot(const QList< QSslError > &errors) noexcept
Definition: networkrequestbase.cpp:171
void finalize(const QString &errorMsg=QString()) noexcept
Definition: networkrequestbase.cpp:257
bool mErrored
Definition: networkrequestbase.h:204
void replyFinishedSlot() noexcept
Definition: networkrequestbase.cpp:199
void replyReadyReadSlot() noexcept
Definition: networkrequestbase.cpp:159