| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef HTTPWORKER_H |
| 8 | #define HTTPWORKER_H |
| 9 | |
| 10 | #include <QNetworkReply> |
| 11 | #include <QUrl> |
| 12 | |
| 13 | class QNetworkReply; |
| 14 | namespace KNSCore |
| 15 | { |
| 16 | class HTTPWorkerPrivate; |
| 17 | class HTTPWorker : public QObject |
| 18 | { |
| 19 | Q_OBJECT |
| 20 | public: |
| 21 | enum JobType { |
| 22 | GetJob, |
| 23 | DownloadJob, // Much the same as a get... except with a filesystem destination, rather than outputting data |
| 24 | }; |
| 25 | explicit HTTPWorker(const QUrl &url, JobType jobType = GetJob, QObject *parent = nullptr); |
| 26 | explicit HTTPWorker(const QUrl &source, const QUrl &destination, JobType jobType = DownloadJob, QObject *parent = nullptr); |
| 27 | ~HTTPWorker() override; |
| 28 | |
| 29 | void startRequest(); |
| 30 | |
| 31 | void setUrl(const QUrl &url); |
| 32 | |
| 33 | Q_SIGNAL void error(QString error); |
| 34 | Q_SIGNAL void progress(qlonglong current, qlonglong total); |
| 35 | Q_SIGNAL void completed(); |
| 36 | Q_SIGNAL void data(const QByteArray &data); |
| 37 | |
| 38 | /** |
| 39 | * Fired in case there is a http error reported |
| 40 | * In some instances this is useful information for our users, and we want to make sure we report this centrally |
| 41 | * @param status The HTTP status code (fired in cases where it is perceived by QNetworkReply as an error) |
| 42 | * @param rawHeaders The raw HTTP headers for the errored-out network request |
| 43 | */ |
| 44 | Q_SIGNAL void httpError(int status, QList<QNetworkReply::RawHeaderPair> ); |
| 45 | |
| 46 | Q_SLOT void handleReadyRead(); |
| 47 | Q_SLOT void handleFinished(); |
| 48 | Q_SLOT void handleData(const QByteArray &data); |
| 49 | |
| 50 | private: |
| 51 | const std::unique_ptr<HTTPWorkerPrivate> d; |
| 52 | }; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | #endif // HTTPWORKER_H |
| 57 | |