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
110 // Operator Overloadings
111 NetworkRequestBase& operator=(const NetworkRequestBase& rhs) = delete;
112
113public slots:
114
124 void start() noexcept;
125
135 void abort() noexcept;
136
137signals:
138
143
152 void progressState(QString state);
153
159 void progressPercent(int percent);
160
168 void progress(qint64 bytesReceived, qint64 bytesTotal, int percent);
169
173 void aborted();
174
178 void succeeded();
179
185 void errored(QString errorMsg);
186
194 void finished(bool success);
195
196public: // Methods
197 virtual void prepareRequest() = 0;
198 virtual void finalizeRequest() = 0;
199 virtual void emitSuccessfullyFinishedSignals() noexcept = 0;
200 virtual void fetchNewData(QIODevice& device) noexcept = 0;
201
202private: // Methods
203 void executeRequest() noexcept;
204 void uploadProgressSlot(qint64 bytesSent, qint64 bytesTotal) noexcept;
205 void replyDownloadProgressSlot(qint64 bytesReceived,
206 qint64 bytesTotal) noexcept;
207 void replyReadyReadSlot() noexcept;
208 void replyErrorSlot(QNetworkReply::NetworkError code) noexcept;
209 void replySslErrorsSlot(const QList<QSslError>& errors) noexcept;
210 void replyFinishedSlot() noexcept;
211 void finalize(const QString& errorMsg = QString()) noexcept;
212 static QString formatFileSize(qint64 bytes) noexcept;
213 static QString getUserAgent() noexcept;
214
215protected: // Data
216 // from constructor
217 QUrl mUrl;
218 QByteArray mPostData;
221
222 // internal data
223 QList<QUrl> mRedirectedUrls;
224 QNetworkRequest mRequest;
225 QScopedPointer<QNetworkReply> mReply;
230};
231
232/*******************************************************************************
233 * End of File
234 ******************************************************************************/
235
236} // namespace librepcb
237
238#endif
Base class for network requests which are processed in the network access manager.
Definition: networkrequestbase.h:55
QUrl mUrl
Definition: networkrequestbase.h:217
void replyFinishedSlot() noexcept
Definition: networkrequestbase.cpp:277
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:259
QByteArray mPostData
Definition: networkrequestbase.h:218
qint64 mExpectedContentSize
Definition: networkrequestbase.h:219
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:235
QScopedPointer< QNetworkReply > mReply
Definition: networkrequestbase.h:225
void replyReadyReadSlot() noexcept
Definition: networkrequestbase.cpp:253
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)
void finalize(const QString &errorMsg=QString()) noexcept
Definition: networkrequestbase.cpp:333
static QString getUserAgent() noexcept
Definition: networkrequestbase.cpp:395
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:228
void executeRequest() noexcept
Definition: networkrequestbase.cpp:140
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:216
void replySslErrorsSlot(const QList< QSslError > &errors) noexcept
Definition: networkrequestbase.cpp:266
QList< QUrl > mRedirectedUrls
Definition: networkrequestbase.h:223
static QString formatFileSize(qint64 bytes) noexcept
Definition: networkrequestbase.cpp:383
void abort() noexcept
Abort downloading the requested content.
Definition: networkrequestbase.cpp:127
bool mStarted
Definition: networkrequestbase.h:226
virtual void emitSuccessfullyFinishedSignals() noexcept=0
bool mFinished
Definition: networkrequestbase.h:229
void progressState(QString state)
Reply progress / state changed signal.
QNetworkRequest mRequest
Definition: networkrequestbase.h:224
bool mAborted
Definition: networkrequestbase.h:227
int mMinimumCacheTime
Definition: networkrequestbase.h:220
void start() noexcept
Start downloading the requested content.
Definition: networkrequestbase.cpp:112
Definition: occmodel.cpp:77