Changeset View
Changeset View
Standalone View
Standalone View
src/drive/teamdrivecreatejob.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 "teamdrivecreatejob.h" | ||||
24 | #include "account.h" | ||||
25 | #include "driveservice.h" | ||||
26 | #include "teamdrive.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 TeamdriveCreateJob::Private | ||||
41 | { | ||||
42 | public: | ||||
43 | Private(TeamdriveCreateJob *parent); | ||||
44 | void processNext(); | ||||
45 | | ||||
46 | TeamdrivesList teamdrives; | ||||
47 | QString requestId; | ||||
48 | | ||||
49 | private: | ||||
50 | TeamdriveCreateJob *const q; | ||||
51 | }; | ||||
52 | | ||||
53 | TeamdriveCreateJob::Private::Private(TeamdriveCreateJob *parent): | ||||
54 | q(parent) | ||||
55 | { | ||||
56 | } | ||||
57 | | ||||
58 | void TeamdriveCreateJob::Private::processNext() | ||||
59 | { | ||||
60 | if (teamdrives.isEmpty()) { | ||||
61 | q->emitFinished(); | ||||
62 | return; | ||||
63 | } | ||||
64 | | ||||
65 | const TeamdrivePtr teamdrive = teamdrives.takeFirst(); | ||||
66 | | ||||
67 | QUrl url = DriveService::fetchTeamdrivesUrl(); | ||||
68 | | ||||
69 | QNetworkRequest request; | ||||
70 | request.setRawHeader("Authorization", "Bearer " + q->account()->accessToken().toLatin1()); | ||||
71 | QUrlQuery query(url); | ||||
72 | if (!requestId.isEmpty()) { | ||||
73 | query.addQueryItem(RequestIdParam, requestId); | ||||
74 | } | ||||
75 | url.setQuery(query); | ||||
76 | request.setUrl(url); | ||||
77 | | ||||
78 | const QByteArray rawData = Teamdrive::toJSON(teamdrive); | ||||
79 | q->enqueueRequest(request, rawData, QStringLiteral("application/json")); | ||||
80 | } | ||||
81 | | ||||
82 | TeamdriveCreateJob::TeamdriveCreateJob(const QString &requestId, | ||||
83 | const TeamdrivePtr &teamdrive, | ||||
84 | const AccountPtr &account, | ||||
85 | QObject *parent): | ||||
86 | CreateJob(account, parent), | ||||
87 | d(new Private(this)) | ||||
88 | { | ||||
89 | d->requestId = requestId; | ||||
90 | d->teamdrives << teamdrive; | ||||
91 | } | ||||
92 | | ||||
93 | TeamdriveCreateJob::TeamdriveCreateJob(const QString &requestId, | ||||
94 | const TeamdrivesList &teamdrives, | ||||
95 | const AccountPtr &account, | ||||
96 | QObject *parent): | ||||
97 | CreateJob(account, parent), | ||||
98 | d(new Private(this)) | ||||
99 | { | ||||
100 | d->requestId = requestId; | ||||
101 | d->teamdrives = teamdrives; | ||||
102 | } | ||||
103 | | ||||
104 | TeamdriveCreateJob::~TeamdriveCreateJob() = default; | ||||
105 | | ||||
106 | QString TeamdriveCreateJob::requestId() const | ||||
107 | { | ||||
108 | return d->requestId; | ||||
109 | } | ||||
110 | | ||||
111 | void TeamdriveCreateJob::start() | ||||
112 | { | ||||
113 | d->processNext(); | ||||
114 | } | ||||
115 | | ||||
116 | ObjectsList TeamdriveCreateJob::handleReplyWithItems(const QNetworkReply *reply, | ||||
117 | const QByteArray &rawData) | ||||
118 | { | ||||
119 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | ||||
120 | ContentType ct = Utils::stringToContentType(contentType); | ||||
121 | ObjectsList items; | ||||
122 | if (ct == KGAPI2::JSON) { | ||||
123 | items << Teamdrive::fromJSON(rawData); | ||||
124 | } else { | ||||
125 | setError(KGAPI2::InvalidResponse); | ||||
126 | setErrorString(tr("Invalid response content type")); | ||||
127 | emitFinished(); | ||||
128 | } | ||||
129 | | ||||
130 | // Enqueue next item or finish | ||||
131 | d->processNext(); | ||||
132 | | ||||
133 | return items; | ||||
134 | } | ||||
135 | | ||||
136 | |