KNewStuff

httpjob.cpp
1 /*
2  Copyright (C) 2016 Dan Leinir Turthra Jensen <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "httpjob.h"
19 
20 #include "knewstuffcore_debug.h"
21 #include "httpworker.h"
22 
23 #include <QTimer>
24 
25 using namespace KNSCore;
26 
27 class HTTPJob::Private
28 {
29 public:
30  Private()
31  : loadType(Reload)
32  , flags(DefaultFlags)
33  {}
34  QUrl source;
35  LoadType loadType;
36  JobFlags flags;
37 };
38 
39 HTTPJob::HTTPJob(const QUrl& source, LoadType loadType, JobFlags flags, QObject* parent)
40  : KJob(parent)
41  , d(new Private)
42 {
43  d->source = source;
44  d->loadType = loadType;
45  d->flags = flags;
46 }
47 
48 HTTPJob::HTTPJob(QObject* parent)
49  : KJob(parent)
50  , d(new Private)
51 {
52 }
53 
54 HTTPJob::~HTTPJob()
55 {
56  delete d;
57 }
58 
59 void HTTPJob::start()
60 {
61  HTTPWorker* worker = new HTTPWorker(d->source, HTTPWorker::GetJob, this);
62  connect(worker, &HTTPWorker::data, this, &HTTPJob::handleWorkerData);
63  connect(worker, &HTTPWorker::completed, this, &HTTPJob::handleWorkerCompleted);
64  connect(worker, &HTTPWorker::error, this, &HTTPJob::handleWorkerError);
65  worker->startRequest();
66 }
67 
68 void HTTPJob::handleWorkerData(const QByteArray& data)
69 {
70  emit HTTPJob::data(this, data);
71 }
72 
73 void HTTPJob::handleWorkerCompleted()
74 {
75  emitResult();
76 }
77 
78 void KNSCore::HTTPJob::handleWorkerError(const QString& error)
79 {
80  setError(KJob::UserDefinedError);
81  setErrorText(error);
82 }
83 
84 HTTPJob* HTTPJob::get(const QUrl& source, LoadType loadType, JobFlags flags, QObject* parent)
85 {
86  HTTPJob* job = new HTTPJob(source, loadType, flags, parent);
87  QTimer::singleShot(0, job, &HTTPJob::start);
88  return job;
89 }
Contains the core functionality for handling interaction with NewStuff providers. ...
DefaultFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.