File indexing completed on 2024-05-12 11:56:10
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 #include "httpjob.h" 0008 0009 #include "httpworker.h" 0010 #include "knewstuffcore_debug.h" 0011 0012 #include <QTimer> 0013 0014 using namespace KNSCore; 0015 0016 class KNSCore::HttpJobPrivate 0017 { 0018 public: 0019 QUrl source; 0020 LoadType loadType = Reload; 0021 JobFlags flags = DefaultFlags; 0022 }; 0023 0024 HTTPJob::HTTPJob(const QUrl &source, LoadType loadType, JobFlags flags, QObject *parent) 0025 : KJob(parent) 0026 , d(new HttpJobPrivate) 0027 { 0028 d->source = source; 0029 d->loadType = loadType; 0030 d->flags = flags; 0031 } 0032 0033 HTTPJob::HTTPJob(QObject *parent) 0034 : KJob(parent) 0035 , d(new HttpJobPrivate) 0036 { 0037 } 0038 0039 HTTPJob::~HTTPJob() = default; 0040 0041 void HTTPJob::start() 0042 { 0043 HTTPWorker *worker = new HTTPWorker(d->source, HTTPWorker::GetJob, this); 0044 connect(worker, &HTTPWorker::data, this, &HTTPJob::handleWorkerData); 0045 connect(worker, &HTTPWorker::completed, this, &HTTPJob::handleWorkerCompleted); 0046 connect(worker, &HTTPWorker::error, this, &HTTPJob::handleWorkerError); 0047 connect(worker, &HTTPWorker::httpError, this, &HTTPJob::httpError); 0048 worker->startRequest(); 0049 } 0050 0051 void HTTPJob::handleWorkerData(const QByteArray &data) 0052 { 0053 Q_EMIT HTTPJob::data(this, data); 0054 } 0055 0056 void HTTPJob::handleWorkerCompleted() 0057 { 0058 emitResult(); 0059 } 0060 0061 void KNSCore::HTTPJob::handleWorkerError(const QString &error) 0062 { 0063 setError(KJob::UserDefinedError); 0064 setErrorText(error); 0065 } 0066 0067 HTTPJob *HTTPJob::get(const QUrl &source, LoadType loadType, JobFlags flags, QObject *parent) 0068 { 0069 HTTPJob *job = new HTTPJob(source, loadType, flags, parent); 0070 QTimer::singleShot(0, job, &HTTPJob::start); 0071 return job; 0072 } 0073 0074 #include "moc_httpjob.cpp"