LibrePCB Developers Documentation
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_CORE_NETWORKREQUESTBASE_H
21#define LIBREPCB_CORE_NETWORKREQUESTBASE_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include <QtCore>
27#include <QtNetwork>
28
29/*******************************************************************************
30 * Namespace / Forward Declarations
31 ******************************************************************************/
32namespace librepcb {
33
34/*******************************************************************************
35 * Class NetworkRequestBase
36 ******************************************************************************/
37
55class NetworkRequestBase : public QObject {
56 Q_OBJECT
57
58public:
59 // Constructors / Destructor
61 NetworkRequestBase(const NetworkRequestBase& other) = delete;
62 NetworkRequestBase(const QUrl& url,
63 const QByteArray& postData = QByteArray()) noexcept;
64 virtual ~NetworkRequestBase() noexcept;
65
66 // Setters
67
74 void setHeaderField(const QByteArray& name, const QByteArray& value) noexcept;
75
85 void setCacheLoadControl(QNetworkRequest::CacheLoadControl value) noexcept;
86
96 void setExpectedReplyContentSize(qint64 bytes) noexcept;
97
108 void setMinimumCacheTime(int seconds) noexcept;
109
118 void useBrowserUserAgent() noexcept;
119
120 // Operator Overloadings
121 NetworkRequestBase& operator=(const NetworkRequestBase& rhs) = delete;
122
123public slots:
124
134 void start() noexcept;
135
145 void abort() noexcept;
146
147signals:
148
153
162 void progressState(QString state);
163
169 void progressPercent(int percent);
170
178 void progress(qint64 bytesReceived, qint64 bytesTotal, int percent);
179
183 void aborted();
184
188 void succeeded();
189
195 void errored(QString errorMsg);
196
204 void finished(bool success);
205
206public: // Methods
207 virtual void prepareRequest() = 0;
208 virtual void finalizeRequest() = 0;
210 QString contentType) noexcept = 0;
211 virtual void fetchNewData(QIODevice& device) noexcept = 0;
212
213private: // Methods
214 void executeRequest() noexcept;
215 void uploadProgressSlot(qint64 bytesSent, qint64 bytesTotal) noexcept;
216 void replyDownloadProgressSlot(qint64 bytesReceived,
217 qint64 bytesTotal) noexcept;
218 void replyReadyReadSlot() noexcept;
219 void replyErrorSlot(QNetworkReply::NetworkError code) noexcept;
220 void replySslErrorsSlot(const QList<QSslError>& errors) noexcept;
221 void replyFinishedSlot() noexcept;
222 void finalize(const QString& errorMsg,
223 const QString& contentType = QString()) noexcept;
224 static QString formatFileSize(qint64 bytes) noexcept;
225 static QString getUserAgent() noexcept;
226
227protected: // Data
228 // from constructor
229 QUrl mUrl;
230 QByteArray mPostData;
233
234 // internal data
235 QList<QUrl> mRedirectedUrls;
236 QNetworkRequest mRequest;
237 QScopedPointer<QNetworkReply> mReply;
242};
243
244/*******************************************************************************
245 * End of File
246 ******************************************************************************/
247
248} // namespace librepcb
249
250#endif
Base class for network requests which are processed in the network access manager.
Definition: networkrequestbase.h:55
QUrl mUrl
Definition: networkrequestbase.h:229
void replyFinishedSlot() noexcept
Definition: networkrequestbase.cpp:287
virtual void finalizeRequest()=0
void setMinimumCacheTime(int seconds) noexcept
Set the minimum time the request should be cached.
Definition: networkrequestbase.cpp:106
void startRequested()
Internal signal, don't use it from outside.
void replyErrorSlot(QNetworkReply::NetworkError code) noexcept
Definition: networkrequestbase.cpp:269
QByteArray mPostData
Definition: networkrequestbase.h:230
qint64 mExpectedContentSize
Definition: networkrequestbase.h:231
void aborted()
Request aborted signal (emitted right before finished())
NetworkRequestBase(const NetworkRequestBase &other)=delete
void errored(QString errorMsg)
Request errored signal (emitted right before finished())
void replyDownloadProgressSlot(qint64 bytesReceived, qint64 bytesTotal) noexcept
Definition: networkrequestbase.cpp:245
void useBrowserUserAgent() noexcept
Use a typical browser user agent for this request.
Definition: networkrequestbase.cpp:112
QScopedPointer< QNetworkReply > mReply
Definition: networkrequestbase.h:237
void replyReadyReadSlot() noexcept
Definition: networkrequestbase.cpp:263
virtual void prepareRequest()=0
void finished(bool success)
Request finished signal.
virtual void fetchNewData(QIODevice &device) noexcept=0
void progressPercent(int percent)
Reply content download progress signal (simple)
virtual void emitSuccessfullyFinishedSignals(QString contentType) noexcept=0
static QString getUserAgent() noexcept
Definition: networkrequestbase.cpp:408
void setCacheLoadControl(QNetworkRequest::CacheLoadControl value) noexcept
Set the cache load control attribute.
Definition: networkrequestbase.cpp:93
void setExpectedReplyContentSize(qint64 bytes) noexcept
Set the expected size of the requested content.
Definition: networkrequestbase.cpp:100
bool mErrored
Definition: networkrequestbase.h:240
void executeRequest() noexcept
Definition: networkrequestbase.cpp:148
void setHeaderField(const QByteArray &name, const QByteArray &value) noexcept
Set a HTTP header field for the network request.
Definition: networkrequestbase.cpp:86
void succeeded()
Request succeeded signal (emitted right before finished())
void progress(qint64 bytesReceived, qint64 bytesTotal, int percent)
Reply content download progress signal (extended)
void uploadProgressSlot(qint64 bytesSent, qint64 bytesTotal) noexcept
Definition: networkrequestbase.cpp:226
void replySslErrorsSlot(const QList< QSslError > &errors) noexcept
Definition: networkrequestbase.cpp:276
QList< QUrl > mRedirectedUrls
Definition: networkrequestbase.h:235
void finalize(const QString &errorMsg, const QString &contentType=QString()) noexcept
Definition: networkrequestbase.cpp:345
static QString formatFileSize(qint64 bytes) noexcept
Definition: networkrequestbase.cpp:396
void abort() noexcept
Abort downloading the requested content.
Definition: networkrequestbase.cpp:135
bool mStarted
Definition: networkrequestbase.h:238
bool mFinished
Definition: networkrequestbase.h:241
void progressState(QString state)
Reply progress / state changed signal.
QNetworkRequest mRequest
Definition: networkrequestbase.h:236
bool mAborted
Definition: networkrequestbase.h:239
int mMinimumCacheTime
Definition: networkrequestbase.h:232
void start() noexcept
Start downloading the requested content.
Definition: networkrequestbase.cpp:120
Definition: occmodel.cpp:77