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#include "httpjob.h"
8
9#include "httpworker.h"
10#include "knewstuffcore_debug.h"
11
12#include <QTimer>
13
14using namespace KNSCore;
15
16class KNSCore::HttpJobPrivate
17{
18public:
19 QUrl source;
20 LoadType loadType = Reload;
21 JobFlags flags = DefaultFlags;
22};
23
24HTTPJob::HTTPJob(const QUrl &source, LoadType loadType, JobFlags flags, QObject *parent)
25 : KJob(parent)
26 , d(new HttpJobPrivate)
27{
28 d->source = source;
29 d->loadType = loadType;
30 d->flags = flags;
31}
32
33HTTPJob::HTTPJob(QObject *parent)
34 : KJob(parent)
35 , d(new HttpJobPrivate)
36{
37}
38
39HTTPJob::~HTTPJob() = default;
40
41void HTTPJob::start()
42{
43 HTTPWorker *worker = new HTTPWorker(d->source, HTTPWorker::GetJob, this);
44 connect(sender: worker, signal: &HTTPWorker::data, context: this, slot: &HTTPJob::handleWorkerData);
45 connect(sender: worker, signal: &HTTPWorker::completed, context: this, slot: &HTTPJob::handleWorkerCompleted);
46 connect(sender: worker, signal: &HTTPWorker::error, context: this, slot: &HTTPJob::handleWorkerError);
47 connect(sender: worker, signal: &HTTPWorker::httpError, context: this, slot: &HTTPJob::httpError);
48 worker->startRequest();
49}
50
51void HTTPJob::handleWorkerData(const QByteArray &data)
52{
53 Q_EMIT HTTPJob::data(job: this, data);
54}
55
56void HTTPJob::handleWorkerCompleted()
57{
58 emitResult();
59}
60
61void KNSCore::HTTPJob::handleWorkerError(const QString &error)
62{
63 setError(KJob::UserDefinedError);
64 setErrorText(error);
65}
66
67HTTPJob *HTTPJob::get(const QUrl &source, LoadType loadType, JobFlags flags, QObject *parent)
68{
69 HTTPJob *job = new HTTPJob(source, loadType, flags, parent);
70 QTimer::singleShot(interval: 0, receiver: job, slot: &HTTPJob::start);
71 return job;
72}
73
74#include "moc_httpjob.cpp"
75

source code of knewstuff/src/core/jobs/httpjob.cpp