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 | |
14 | using namespace KNSCore; |
15 | |
16 | class KNSCore::HttpJobPrivate |
17 | { |
18 | public: |
19 | QUrl source; |
20 | LoadType loadType = Reload; |
21 | JobFlags flags = DefaultFlags; |
22 | }; |
23 | |
24 | HTTPJob::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 | |
33 | HTTPJob::HTTPJob(QObject *parent) |
34 | : KJob(parent) |
35 | , d(new HttpJobPrivate) |
36 | { |
37 | } |
38 | |
39 | HTTPJob::~HTTPJob() = default; |
40 | |
41 | void 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 | |
51 | void HTTPJob::handleWorkerData(const QByteArray &data) |
52 | { |
53 | Q_EMIT HTTPJob::data(job: this, data); |
54 | } |
55 | |
56 | void HTTPJob::handleWorkerCompleted() |
57 | { |
58 | emitResult(); |
59 | } |
60 | |
61 | void KNSCore::HTTPJob::handleWorkerError(const QString &error) |
62 | { |
63 | setError(KJob::UserDefinedError); |
64 | setErrorText(error); |
65 | } |
66 | |
67 | HTTPJob *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 | |