File indexing completed on 2024-05-12 05:22:17
0001 /* 0002 * This file is part of LibKGAPI library 0003 * 0004 * SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si> 0005 * 0006 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "drivescreatejob.h" 0010 #include "drives.h" 0011 #include "driveservice.h" 0012 #include "utils.h" 0013 0014 #include <QNetworkReply> 0015 #include <QNetworkRequest> 0016 #include <QUrlQuery> 0017 0018 namespace 0019 { 0020 static const QString RequestIdParam = QStringLiteral("requestId"); 0021 } 0022 using namespace KGAPI2; 0023 using namespace KGAPI2::Drive; 0024 0025 class Q_DECL_HIDDEN DrivesCreateJob::Private 0026 { 0027 public: 0028 Private(DrivesCreateJob *parent); 0029 void processNext(); 0030 0031 DrivesList drives; 0032 QString requestId; 0033 0034 private: 0035 DrivesCreateJob *const q; 0036 }; 0037 0038 DrivesCreateJob::Private::Private(DrivesCreateJob *parent) 0039 : q(parent) 0040 { 0041 } 0042 0043 void DrivesCreateJob::Private::processNext() 0044 { 0045 if (drives.isEmpty()) { 0046 q->emitFinished(); 0047 return; 0048 } 0049 0050 const DrivesPtr drive = drives.takeFirst(); 0051 0052 QUrl url = DriveService::fetchDrivesUrl(); 0053 0054 QUrlQuery query(url); 0055 if (!requestId.isEmpty()) { 0056 query.addQueryItem(RequestIdParam, requestId); 0057 } 0058 url.setQuery(query); 0059 0060 QNetworkRequest request(url); 0061 0062 const QByteArray rawData = Drives::toJSON(drive); 0063 q->enqueueRequest(request, rawData, QStringLiteral("application/json")); 0064 } 0065 0066 DrivesCreateJob::DrivesCreateJob(const QString &requestId, const DrivesPtr &drive, const AccountPtr &account, QObject *parent) 0067 : CreateJob(account, parent) 0068 , d(new Private(this)) 0069 { 0070 d->requestId = requestId; 0071 d->drives << drive; 0072 } 0073 0074 DrivesCreateJob::DrivesCreateJob(const QString &requestId, const DrivesList &drives, const AccountPtr &account, QObject *parent) 0075 : CreateJob(account, parent) 0076 , d(new Private(this)) 0077 { 0078 d->requestId = requestId; 0079 d->drives = drives; 0080 } 0081 0082 DrivesCreateJob::~DrivesCreateJob() = default; 0083 0084 QString DrivesCreateJob::requestId() const 0085 { 0086 return d->requestId; 0087 } 0088 0089 void DrivesCreateJob::start() 0090 { 0091 d->processNext(); 0092 } 0093 0094 ObjectsList DrivesCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) 0095 { 0096 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); 0097 ContentType ct = Utils::stringToContentType(contentType); 0098 ObjectsList items; 0099 if (ct == KGAPI2::JSON) { 0100 items << Drives::fromJSON(rawData); 0101 } else { 0102 setError(KGAPI2::InvalidResponse); 0103 setErrorString(tr("Invalid response content type")); 0104 emitFinished(); 0105 return items; 0106 } 0107 0108 // Enqueue next item or finish 0109 d->processNext(); 0110 0111 return items; 0112 } 0113 0114 #include "moc_drivescreatejob.cpp"