Changeset View
Changeset View
Standalone View
Standalone View
src/drive/drivesmodifyjob.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 | | ||||
24 | #include "drivesmodifyjob.h" | ||||
25 | #include "account.h" | ||||
26 | #include "driveservice.h" | ||||
27 | #include "drives.h" | ||||
28 | #include "utils.h" | ||||
29 | #include "../debug.h" | ||||
30 | | ||||
31 | #include <QNetworkRequest> | ||||
32 | #include <QNetworkReply> | ||||
33 | #include <QUrlQuery> | ||||
34 | | ||||
35 | | ||||
36 | namespace { | ||||
37 | static const QString UseDomainAdminAccessAttr = QStringLiteral("useDomainAdminAccess"); | ||||
38 | static const QString True = QStringLiteral("true"); | ||||
39 | static const QString False = QStringLiteral("false"); | ||||
40 | } | ||||
41 | using namespace KGAPI2; | ||||
42 | using namespace KGAPI2::Drive; | ||||
43 | | ||||
44 | class Q_DECL_HIDDEN DrivesModifyJob::Private | ||||
45 | { | ||||
46 | public: | ||||
47 | Private(DrivesModifyJob *parent); | ||||
48 | void processNext(); | ||||
49 | | ||||
50 | bool useDomainAdminAccess = false; | ||||
51 | | ||||
52 | DrivesList drives; | ||||
53 | | ||||
54 | private: | ||||
55 | DrivesModifyJob *const q; | ||||
56 | }; | ||||
57 | | ||||
58 | DrivesModifyJob::Private::Private(DrivesModifyJob *parent): | ||||
59 | q(parent) | ||||
60 | { | ||||
61 | } | ||||
62 | | ||||
63 | void DrivesModifyJob::setUseDomainAdminAccess(bool useDomainAdminAccess) | ||||
64 | { | ||||
65 | if (isRunning()) { | ||||
66 | qCWarning(KGAPIDebug) << "Can't modify useDomainAdminAccess property when job is running"; | ||||
67 | return; | ||||
68 | } | ||||
69 | | ||||
70 | d->useDomainAdminAccess = useDomainAdminAccess; | ||||
71 | } | ||||
72 | | ||||
73 | bool DrivesModifyJob::useDomainAdminAccess() const | ||||
74 | { | ||||
75 | return d->useDomainAdminAccess; | ||||
76 | } | ||||
77 | | ||||
78 | void DrivesModifyJob::Private::processNext() | ||||
79 | { | ||||
80 | if (drives.isEmpty()) { | ||||
81 | q->emitFinished(); | ||||
82 | return; | ||||
83 | } | ||||
84 | | ||||
85 | const DrivesPtr drive = drives.takeFirst(); | ||||
86 | QUrl url = DriveService::fetchDrivesUrl(drive->id()); | ||||
87 | | ||||
88 | QUrlQuery query(url); | ||||
89 | if (useDomainAdminAccess != false) { | ||||
90 | query.addQueryItem(UseDomainAdminAccessAttr, useDomainAdminAccess ? True : False); | ||||
91 | } | ||||
92 | url.setQuery(query); | ||||
93 | | ||||
94 | QNetworkRequest request(url); | ||||
95 | | ||||
96 | const QByteArray rawData = Drives::toJSON(drive); | ||||
97 | q->enqueueRequest(request, rawData, QStringLiteral("application/json")); | ||||
98 | } | ||||
99 | | ||||
100 | DrivesModifyJob::DrivesModifyJob(const DrivesPtr &drive, | ||||
101 | const AccountPtr &account, | ||||
102 | QObject *parent): | ||||
103 | ModifyJob(account, parent), | ||||
104 | d(new Private(this)) | ||||
105 | { | ||||
106 | d->drives << drive; | ||||
107 | } | ||||
108 | | ||||
109 | DrivesModifyJob::DrivesModifyJob(const DrivesList &drives, | ||||
110 | const AccountPtr &account, | ||||
111 | QObject *parent): | ||||
112 | ModifyJob(account, parent), | ||||
113 | d(new Private(this)) | ||||
114 | { | ||||
115 | d->drives << drives; | ||||
116 | } | ||||
117 | | ||||
118 | DrivesModifyJob::~DrivesModifyJob() = default; | ||||
119 | | ||||
120 | void DrivesModifyJob::start() | ||||
121 | { | ||||
122 | d->processNext(); | ||||
123 | } | ||||
124 | | ||||
125 | ObjectsList DrivesModifyJob::handleReplyWithItems(const QNetworkReply *reply, | ||||
126 | const QByteArray &rawData) | ||||
127 | { | ||||
128 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | ||||
129 | ContentType ct = Utils::stringToContentType(contentType); | ||||
130 | ObjectsList items; | ||||
131 | if (ct == KGAPI2::JSON) { | ||||
132 | items << Drives::fromJSON(rawData); | ||||
133 | } else { | ||||
134 | setError(KGAPI2::InvalidResponse); | ||||
135 | setErrorString(tr("Invalid response content type")); | ||||
136 | emitFinished(); | ||||
137 | return items; | ||||
138 | } | ||||
139 | | ||||
140 | // Enqueue next item or finish | ||||
141 | d->processNext(); | ||||
142 | | ||||
143 | return items; | ||||
144 | } | ||||
145 | | ||||
146 | |