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 HTTPJOB_H
0008 #define HTTPJOB_H
0009 
0010 #include "jobbase.h"
0011 
0012 #include <QNetworkReply>
0013 #include <QUrl>
0014 
0015 #include <memory>
0016 
0017 namespace KNSCore
0018 {
0019 class HttpJobPrivate;
0020 class HTTPJob : public KJob
0021 {
0022     Q_OBJECT
0023 public:
0024     explicit HTTPJob(const QUrl &source, LoadType loadType = Reload, JobFlags flags = DefaultFlags, QObject *parent = nullptr);
0025     explicit HTTPJob(QObject *parent = nullptr);
0026     ~HTTPJob() override;
0027 
0028     Q_SLOT void start() override;
0029 
0030     static HTTPJob *get(const QUrl &source, LoadType loadType = Reload, JobFlags flags = DefaultFlags, QObject *parent = nullptr);
0031 
0032 Q_SIGNALS:
0033     /**
0034      * Data from the worker has arrived.
0035      * @param job the job that emitted this signal
0036      * @param data data received from the worker.
0037      *
0038      * End of data (EOD) has been reached if data.size() == 0, however, you
0039      * should not be certain of data.size() == 0 ever happening (e.g. in case
0040      * of an error), so you should rely on result() instead.
0041      */
0042     void data(KJob *job, const QByteArray &data);
0043 
0044     /**
0045      * Fired in case there is a http error reported
0046      * In some instances this is useful information for our users, and we want to make sure we report this centrally
0047      * @param status The HTTP status code (fired in cases where it is perceived by QNetworkReply as an error)
0048      * @param rawHeaders The raw HTTP headers for the errored-out network request
0049      */
0050     void httpError(int status, QList<QNetworkReply::RawHeaderPair> rawHeaders);
0051 
0052 protected Q_SLOTS:
0053     void handleWorkerData(const QByteArray &data);
0054     void handleWorkerCompleted();
0055     void handleWorkerError(const QString &error);
0056 
0057 private:
0058     const std::unique_ptr<HttpJobPrivate> d;
0059 };
0060 
0061 }
0062 
0063 #endif // HTTPJOB_H