KNewStuff

httpjob.cpp
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(worker, &HTTPWorker::data, this, &HTTPJob::handleWorkerData);
45 connect(worker, &HTTPWorker::completed, this, &HTTPJob::handleWorkerCompleted);
46 connect(worker, &HTTPWorker::error, this, &HTTPJob::handleWorkerError);
47 connect(worker, &HTTPWorker::httpError, this, &HTTPJob::httpError);
48 worker->startRequest();
49}
50
51void HTTPJob::handleWorkerData(const QByteArray &data)
52{
53 Q_EMIT HTTPJob::data(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(0, job, &HTTPJob::start);
71 return job;
72}
73
74#include "moc_httpjob.cpp"
void emitResult()
DefaultFlags
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.