Changeset View
Changeset View
Standalone View
Standalone View
src/drive/drivesfetchjob.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 "drivesfetchjob.h" | ||||
24 | #include "account.h" | ||||
25 | #include "drives.h" | ||||
26 | #include "../debug.h" | ||||
27 | #include "driveservice.h" | ||||
28 | #include "utils.h" | ||||
29 | | ||||
30 | #include <QNetworkReply> | ||||
31 | #include <QNetworkRequest> | ||||
32 | #include <QUrlQuery> | ||||
33 | | ||||
34 | namespace { | ||||
35 | static const QString MaxResultsAttr = QStringLiteral("maxResults"); | ||||
36 | static const QString UseDomainAdminAccessAttr = QStringLiteral("useDomainAdminAccess"); | ||||
37 | static const QString True = QStringLiteral("true"); | ||||
38 | static const QString False = QStringLiteral("false"); | ||||
39 | } | ||||
40 | using namespace KGAPI2; | ||||
41 | using namespace KGAPI2::Drive; | ||||
42 | | ||||
43 | class Q_DECL_HIDDEN DrivesFetchJob::Private | ||||
44 | { | ||||
45 | public: | ||||
46 | Private(DrivesFetchJob *parent); | ||||
47 | | ||||
48 | DrivesSearchQuery searchQuery; | ||||
49 | QString drivesId; | ||||
50 | | ||||
51 | int maxResults = 0; | ||||
52 | QVariant useDomainAdminAccess; | ||||
53 | | ||||
54 | QStringList fields; | ||||
55 | | ||||
56 | private: | ||||
57 | DrivesFetchJob *const q; | ||||
58 | }; | ||||
59 | | ||||
60 | DrivesFetchJob::Private::Private(DrivesFetchJob *parent): | ||||
61 | q(parent) | ||||
62 | { | ||||
63 | } | ||||
64 | | ||||
65 | DrivesFetchJob::DrivesFetchJob(const QString &drivesId, | ||||
66 | const AccountPtr &account, | ||||
67 | QObject *parent): | ||||
68 | FetchJob(account, parent), | ||||
69 | d(new Private(this)) | ||||
70 | { | ||||
71 | d->drivesId = drivesId; | ||||
72 | } | ||||
73 | | ||||
74 | DrivesFetchJob::DrivesFetchJob(const DrivesSearchQuery &query, | ||||
75 | const AccountPtr &account, QObject *parent): | ||||
76 | FetchJob(account, parent), | ||||
77 | d(new Private(this)) | ||||
78 | { | ||||
79 | d->searchQuery = query; | ||||
80 | } | ||||
81 | | ||||
82 | DrivesFetchJob::DrivesFetchJob(const AccountPtr &account, QObject *parent): | ||||
83 | FetchJob(account, parent), | ||||
84 | d(new Private(this)) | ||||
85 | { | ||||
86 | } | ||||
87 | | ||||
88 | DrivesFetchJob::~DrivesFetchJob() = default; | ||||
89 | | ||||
90 | void DrivesFetchJob::setMaxResults(int maxResults) | ||||
91 | { | ||||
92 | if (isRunning()) { | ||||
93 | qCWarning(KGAPIDebug) << "Can't modify maxResults property when job is running"; | ||||
94 | return; | ||||
95 | } | ||||
96 | | ||||
97 | d->maxResults = maxResults; | ||||
98 | } | ||||
99 | | ||||
100 | int DrivesFetchJob::maxResults() const | ||||
101 | { | ||||
102 | return d->maxResults; | ||||
103 | } | ||||
104 | | ||||
105 | void DrivesFetchJob::setUseDomainAdminAccess(bool useDomainAdminAccess) | ||||
106 | { | ||||
107 | if (isRunning()) { | ||||
108 | qCWarning(KGAPIDebug) << "Can't modify useDomainAdminAccess property when job is running"; | ||||
109 | return; | ||||
110 | } | ||||
111 | | ||||
112 | d->useDomainAdminAccess = useDomainAdminAccess; | ||||
113 | } | ||||
114 | | ||||
115 | bool DrivesFetchJob::useDomainAdminAccess() const | ||||
116 | { | ||||
117 | return d->useDomainAdminAccess.toBool(); | ||||
118 | } | ||||
119 | | ||||
120 | void DrivesFetchJob::setFields(const QStringList &fields) | ||||
121 | { | ||||
122 | if (isRunning()) { | ||||
123 | qCWarning(KGAPIDebug) << "Called setFields() on running job. Ignoring."; | ||||
124 | return; | ||||
125 | } | ||||
126 | | ||||
127 | d->fields = fields; | ||||
128 | } | ||||
129 | | ||||
130 | QStringList DrivesFetchJob::fields() const | ||||
131 | { | ||||
132 | return d->fields; | ||||
133 | } | ||||
134 | | ||||
135 | void DrivesFetchJob::start() | ||||
136 | { | ||||
137 | QUrl url; | ||||
138 | if (d->drivesId.isEmpty()) { | ||||
139 | url = DriveService::fetchDrivesUrl(); | ||||
140 | applyRequestParameters(url); | ||||
141 | } else { | ||||
142 | url = DriveService::fetchDrivesUrl(d->drivesId); | ||||
143 | if (!d->fields.isEmpty()) { | ||||
144 | // Deserializing requires kind attribute, always force add it | ||||
145 | if (!d->fields.contains(Drives::Fields::Kind)) { | ||||
146 | d->fields << Drives::Fields::Kind; | ||||
147 | } | ||||
148 | Job::setFields(d->fields); | ||||
149 | } | ||||
150 | } | ||||
151 | | ||||
152 | QNetworkRequest request(url); | ||||
153 | enqueueRequest(request); | ||||
154 | } | ||||
155 | | ||||
156 | | ||||
157 | ObjectsList DrivesFetchJob::handleReplyWithItems(const QNetworkReply *reply, | ||||
158 | const QByteArray &rawData) | ||||
159 | { | ||||
160 | FeedData feedData; | ||||
161 | feedData.requestUrl = reply->url(); | ||||
162 | | ||||
163 | ObjectsList items; | ||||
164 | QString itemId; | ||||
165 | | ||||
166 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | ||||
167 | ContentType ct = Utils::stringToContentType(contentType); | ||||
168 | if (ct == KGAPI2::JSON) { | ||||
169 | if (d->drivesId.isEmpty()) { | ||||
170 | items << Drives::fromJSONFeed(rawData, feedData); | ||||
171 | } else { | ||||
172 | items << Drives::fromJSON(rawData); | ||||
173 | } | ||||
174 | } else { | ||||
175 | setError(KGAPI2::InvalidResponse); | ||||
176 | setErrorString(tr("Invalid response content type")); | ||||
177 | emitFinished(); | ||||
178 | return items; | ||||
179 | } | ||||
180 | | ||||
181 | if (feedData.nextPageUrl.isValid()) { | ||||
182 | // Reapply query options | ||||
183 | applyRequestParameters(feedData.nextPageUrl); | ||||
184 | QNetworkRequest request(feedData.nextPageUrl); | ||||
185 | enqueueRequest(request); | ||||
186 | } | ||||
187 | | ||||
188 | return items; | ||||
189 | } | ||||
190 | | ||||
191 | | ||||
192 | void DrivesFetchJob::applyRequestParameters(QUrl &url) { | ||||
193 | QUrlQuery query(url); | ||||
194 | if (d->maxResults != 0) { | ||||
195 | query.addQueryItem(MaxResultsAttr, QString::number(d->maxResults)); | ||||
196 | } | ||||
197 | if (!d->useDomainAdminAccess.isNull()) { | ||||
198 | query.addQueryItem(UseDomainAdminAccessAttr, d->useDomainAdminAccess.toBool() ? True : False); | ||||
199 | } | ||||
200 | if (!d->searchQuery.isEmpty()) { | ||||
201 | query.addQueryItem(QStringLiteral("q"), d->searchQuery.serialize()); | ||||
202 | } | ||||
203 | if (!d->fields.isEmpty()) { | ||||
204 | // Deserializing requires kind attribute, always force add it | ||||
205 | if (!d->fields.contains(Drives::Fields::Kind)) { | ||||
206 | d->fields << Drives::Fields::Kind; | ||||
207 | } | ||||
208 | QString itemFields = Job::buildSubfields(Drives::Fields::Items, d->fields); | ||||
209 | Job::setFields({ | ||||
210 | Drives::Fields::Kind, | ||||
211 | Drives::Fields::NextPageToken, | ||||
212 | itemFields | ||||
213 | }); | ||||
214 | } | ||||
215 | url.setQuery(query); | ||||
216 | } |