LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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#include <memory>
30
31/*******************************************************************************
32 * Namespace / Forward Declarations
33 ******************************************************************************/
34namespace librepcb {
35
36/*******************************************************************************
37 * Class NetworkRequestBase
38 ******************************************************************************/
39
57class NetworkRequestBase : public QObject {
58 Q_OBJECT
59
60public:
61 // Constructors / Destructor
63 NetworkRequestBase(const NetworkRequestBase& other) = delete;
64 NetworkRequestBase(const QUrl& url,
65 const QByteArray& postData = QByteArray()) noexcept;
66 virtual ~NetworkRequestBase() noexcept;
67
68 // Setters
69
76 void setHeaderField(const QByteArray& name, const QByteArray& value) noexcept;
77
87 void setCacheLoadControl(QNetworkRequest::CacheLoadControl value) noexcept;
88
98 void setExpectedReplyContentSize(qint64 bytes) noexcept;
99
110 void setMinimumCacheTime(int seconds) noexcept;
111
120 void useBrowserUserAgent() noexcept;
121
122 // Operator Overloadings
123 NetworkRequestBase& operator=(const NetworkRequestBase& rhs) = delete;
124
125public slots:
126
136 void start() noexcept;
137
147 void abort() noexcept;
148
149signals:
150
155
164 void progressState(QString state);
165
171 void progressPercent(int percent);
172
180 void progress(qint64 bytesReceived, qint64 bytesTotal, int percent);
181
185 void aborted();
186
190 void succeeded();
191
197 void errored(QString errorMsg);
198
206 void finished(bool success);
207
208public: // Methods
209 virtual void prepareRequest() = 0;
210 virtual void finalizeRequest() = 0;
212 QString contentType) noexcept = 0;
213 virtual void fetchNewData(QIODevice& device) noexcept = 0;
214
215private: // Methods
216 void executeRequest() noexcept;
217 void uploadProgressSlot(qint64 bytesSent, qint64 bytesTotal) noexcept;
218 void replyDownloadProgressSlot(qint64 bytesReceived,
219 qint64 bytesTotal) noexcept;
220 void replyReadyReadSlot() noexcept;
221 void replyErrorSlot(QNetworkReply::NetworkError code) noexcept;
222 void replySslErrorsSlot(const QList<QSslError>& errors) noexcept;
223 void replyFinishedSlot() noexcept;
224 void finalize(const QString& errorMsg,
225 const QString& contentType = QString()) noexcept;
226 static QString formatFileSize(qint64 bytes) noexcept;
227 static QString getUserAgent() noexcept;
228
229protected: // Data
230 // from constructor
231 QUrl mUrl;
232 QByteArray mPostData;
235
236 // internal data
237 QList<QUrl> mRedirectedUrls;
238 QNetworkRequest mRequest;
239 std::unique_ptr<QNetworkReply> mReply;
244};
245
246/*******************************************************************************
247 * End of File
248 ******************************************************************************/
249
250} // namespace librepcb
251
252#endif
Base class for network requests which are processed in the network access manager.
Definition networkrequestbase.h:57
QUrl mUrl
Definition networkrequestbase.h:231
void replyFinishedSlot() noexcept
Definition networkrequestbase.cpp:278
virtual void finalizeRequest()=0
std::unique_ptr< QNetworkReply > mReply
Definition networkrequestbase.h:239
void setMinimumCacheTime(int seconds) noexcept
Set the minimum time the request should be cached.
Definition networkrequestbase.cpp:104
void startRequested()
Internal signal, don't use it from outside.
void replyErrorSlot(QNetworkReply::NetworkError code) noexcept
Definition networkrequestbase.cpp:260
QByteArray mPostData
Definition networkrequestbase.h:232
qint64 mExpectedContentSize
Definition networkrequestbase.h:233
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:236
void useBrowserUserAgent() noexcept
Use a typical browser user agent for this request.
Definition networkrequestbase.cpp:110
void replyReadyReadSlot() noexcept
Definition networkrequestbase.cpp:254
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:399
void setCacheLoadControl(QNetworkRequest::CacheLoadControl value) noexcept
Set the cache load control attribute.
Definition networkrequestbase.cpp:91
void setExpectedReplyContentSize(qint64 bytes) noexcept
Set the expected size of the requested content.
Definition networkrequestbase.cpp:98
bool mErrored
Definition networkrequestbase.h:242
void executeRequest() noexcept
Definition networkrequestbase.cpp:146
void setHeaderField(const QByteArray &name, const QByteArray &value) noexcept
Set a HTTP header field for the network request.
Definition networkrequestbase.cpp:84
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:217
void replySslErrorsSlot(const QList< QSslError > &errors) noexcept
Definition networkrequestbase.cpp:267
QList< QUrl > mRedirectedUrls
Definition networkrequestbase.h:237
void finalize(const QString &errorMsg, const QString &contentType=QString()) noexcept
Definition networkrequestbase.cpp:336
static QString formatFileSize(qint64 bytes) noexcept
Definition networkrequestbase.cpp:387
void abort() noexcept
Abort downloading the requested content.
Definition networkrequestbase.cpp:133
bool mStarted
Definition networkrequestbase.h:240
bool mFinished
Definition networkrequestbase.h:243
void progressState(QString state)
Reply progress / state changed signal.
QNetworkRequest mRequest
Definition networkrequestbase.h:238
bool mAborted
Definition networkrequestbase.h:241
int mMinimumCacheTime
Definition networkrequestbase.h:234
void start() noexcept
Start downloading the requested content.
Definition networkrequestbase.cpp:118
Definition occmodel.cpp:76
Definition uuid.h:186