Changeset View
Changeset View
Standalone View
Standalone View
src/drive/drivescreatejob.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * This file is part of LibKGAPI library | ||||
3 | * | ||||
4 | * Copyright (C) 2019 David Barchiesi <david@barchie.si> | ||||
5 | * | ||||
6 | * This library is free software; you can redistribute it and/or | ||||
7 | * modify it under the terms of the GNU Lesser General Public | ||||
8 | * License as published by the Free Software Foundation; either | ||||
9 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
10 | * later version accepted by the membership of KDE e.V. (or its | ||||
11 | * successor approved by the membership of KDE e.V.), which shall | ||||
12 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
13 | * | ||||
14 | * This library is distributed in the hope that it will be useful, | ||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
17 | * GNU Lesser General Public License for more details. | ||||
18 | * | ||||
19 | * You should have received a copy of the GNU Lesser General Public | ||||
20 | * License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||||
21 | */ | ||||
22 | | ||||
23 | #include "drivescreatejob.h" | ||||
24 | #include "account.h" | ||||
25 | #include "driveservice.h" | ||||
26 | #include "drives.h" | ||||
27 | #include "utils.h" | ||||
28 | | ||||
29 | #include <QNetworkRequest> | ||||
30 | #include <QNetworkReply> | ||||
31 | #include <QUrlQuery> | ||||
32 | | ||||
33 | | ||||
34 | namespace { | ||||
35 | static const QString RequestIdParam = QStringLiteral("requestId"); | ||||
36 | } | ||||
37 | using namespace KGAPI2; | ||||
38 | using namespace KGAPI2::Drive; | ||||
39 | | ||||
40 | class Q_DECL_HIDDEN DrivesCreateJob::Private | ||||
41 | { | ||||
42 | public: | ||||
43 | Private(DrivesCreateJob *parent); | ||||
44 | void processNext(); | ||||
45 | | ||||
46 | DrivesList drives; | ||||
47 | QString requestId; | ||||
48 | | ||||
49 | private: | ||||
50 | DrivesCreateJob *const q; | ||||
51 | }; | ||||
52 | | ||||
53 | DrivesCreateJob::Private::Private(DrivesCreateJob *parent): | ||||
54 | q(parent) | ||||
55 | { | ||||
56 | } | ||||
57 | | ||||
58 | void DrivesCreateJob::Private::processNext() | ||||
59 | { | ||||
60 | if (drives.isEmpty()) { | ||||
61 | q->emitFinished(); | ||||
62 | return; | ||||
63 | } | ||||
64 | | ||||
65 | const DrivesPtr drive = drives.takeFirst(); | ||||
66 | | ||||
67 | QUrl url = DriveService::fetchDrivesUrl(); | ||||
68 | | ||||
69 | QUrlQuery query(url); | ||||
70 | if (!requestId.isEmpty()) { | ||||
71 | query.addQueryItem(RequestIdParam, requestId); | ||||
72 | } | ||||
73 | url.setQuery(query); | ||||
74 | | ||||
75 | QNetworkRequest request(url); | ||||
76 | | ||||
77 | const QByteArray rawData = Drives::toJSON(drive); | ||||
78 | q->enqueueRequest(request, rawData, QStringLiteral("application/json")); | ||||
79 | } | ||||
80 | | ||||
81 | DrivesCreateJob::DrivesCreateJob(const QString &requestId, | ||||
82 | const DrivesPtr &drive, | ||||
83 | const AccountPtr &account, | ||||
84 | QObject *parent): | ||||
85 | CreateJob(account, parent), | ||||
86 | d(new Private(this)) | ||||
87 | { | ||||
88 | d->requestId = requestId; | ||||
89 | d->drives << drive; | ||||
90 | } | ||||
91 | | ||||
92 | DrivesCreateJob::DrivesCreateJob(const QString &requestId, | ||||
93 | const DrivesList &drives, | ||||
94 | const AccountPtr &account, | ||||
95 | QObject *parent): | ||||
96 | CreateJob(account, parent), | ||||
97 | d(new Private(this)) | ||||
98 | { | ||||
99 | d->requestId = requestId; | ||||
100 | d->drives = drives; | ||||
101 | } | ||||
102 | | ||||
103 | DrivesCreateJob::~DrivesCreateJob() = default; | ||||
104 | | ||||
105 | QString DrivesCreateJob::requestId() const | ||||
106 | { | ||||
107 | return d->requestId; | ||||
108 | } | ||||
109 | | ||||
110 | void DrivesCreateJob::start() | ||||
111 | { | ||||
112 | d->processNext(); | ||||
113 | } | ||||
114 | | ||||
115 | ObjectsList DrivesCreateJob::handleReplyWithItems(const QNetworkReply *reply, | ||||
116 | const QByteArray &rawData) | ||||
117 | { | ||||
118 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | ||||
119 | ContentType ct = Utils::stringToContentType(contentType); | ||||
120 | ObjectsList items; | ||||
121 | if (ct == KGAPI2::JSON) { | ||||
122 | items << Drives::fromJSON(rawData); | ||||
123 | } else { | ||||
124 | setError(KGAPI2::InvalidResponse); | ||||
125 | setErrorString(tr("Invalid response content type")); | ||||
126 | emitFinished(); | ||||
127 | return items; | ||||
128 | } | ||||
129 | | ||||
130 | // Enqueue next item or finish | ||||
131 | d->processNext(); | ||||
132 | | ||||
133 | return items; | ||||
134 | } | ||||
135 | | ||||
136 | |