File indexing completed on 2024-04-28 03:56:24

0001 /*
0002     SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #ifndef HTTPWORKER_H
0008 #define HTTPWORKER_H
0009 
0010 #include <QNetworkReply>
0011 #include <QUrl>
0012 
0013 class QNetworkReply;
0014 namespace KNSCore
0015 {
0016 class HTTPWorkerPrivate;
0017 class HTTPWorker : public QObject
0018 {
0019     Q_OBJECT
0020 public:
0021     enum JobType {
0022         GetJob,
0023         DownloadJob, // Much the same as a get... except with a filesystem destination, rather than outputting data
0024     };
0025     explicit HTTPWorker(const QUrl &url, JobType jobType = GetJob, QObject *parent = nullptr);
0026     explicit HTTPWorker(const QUrl &source, const QUrl &destination, JobType jobType = DownloadJob, QObject *parent = nullptr);
0027     ~HTTPWorker() override;
0028 
0029     void startRequest();
0030 
0031     void setUrl(const QUrl &url);
0032 
0033     Q_SIGNAL void error(QString error);
0034     Q_SIGNAL void progress(qlonglong current, qlonglong total);
0035     Q_SIGNAL void completed();
0036     Q_SIGNAL void data(const QByteArray &data);
0037 
0038     /**
0039      * Fired in case there is a http error reported
0040      * In some instances this is useful information for our users, and we want to make sure we report this centrally
0041      * @param status The HTTP status code (fired in cases where it is perceived by QNetworkReply as an error)
0042      * @param rawHeaders The raw HTTP headers for the errored-out network request
0043      */
0044     Q_SIGNAL void httpError(int status, QList<QNetworkReply::RawHeaderPair> rawHeaders);
0045 
0046     Q_SLOT void handleReadyRead();
0047     Q_SLOT void handleFinished();
0048     Q_SLOT void handleData(const QByteArray &data);
0049 
0050 private:
0051     const std::unique_ptr<HTTPWorkerPrivate> d;
0052 };
0053 
0054 }
0055 
0056 #endif // HTTPWORKER_H