Changeset View
Changeset View
Standalone View
Standalone View
src/drive/driveshidejob.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 "driveshidejob.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 | using namespace KGAPI2; | ||||
35 | using namespace KGAPI2::Drive; | ||||
36 | | ||||
37 | class Q_DECL_HIDDEN DrivesHideJob::Private | ||||
38 | { | ||||
39 | public: | ||||
40 | Private(DrivesHideJob *parent); | ||||
41 | void processNext(); | ||||
42 | | ||||
43 | bool hide = false; | ||||
44 | | ||||
45 | DrivesList drives; | ||||
46 | | ||||
47 | private: | ||||
48 | DrivesHideJob *const q; | ||||
49 | }; | ||||
50 | | ||||
51 | DrivesHideJob::Private::Private(DrivesHideJob *parent): | ||||
52 | q(parent) | ||||
53 | { | ||||
54 | } | ||||
55 | | ||||
56 | void DrivesHideJob::Private::processNext() | ||||
57 | { | ||||
58 | if (drives.isEmpty()) { | ||||
59 | q->emitFinished(); | ||||
60 | return; | ||||
61 | } | ||||
62 | | ||||
63 | const DrivesPtr drive = drives.takeFirst(); | ||||
64 | | ||||
65 | QUrl url = DriveService::hideDrivesUrl(drive->id(), hide); | ||||
66 | | ||||
67 | QNetworkRequest request(url); | ||||
68 | | ||||
69 | // A hide request doesn't have a body | ||||
70 | q->enqueueRequest(request, nullptr, QStringLiteral("application/json")); | ||||
71 | } | ||||
72 | | ||||
73 | DrivesHideJob::DrivesHideJob(const DrivesPtr &drive, | ||||
74 | bool hide, | ||||
75 | const AccountPtr &account, | ||||
76 | QObject *parent): | ||||
77 | CreateJob(account, parent), | ||||
78 | d(new Private(this)) | ||||
79 | { | ||||
80 | d->drives << drive; | ||||
81 | d->hide = hide; | ||||
82 | } | ||||
83 | | ||||
84 | DrivesHideJob::DrivesHideJob(const DrivesList &drives, | ||||
85 | bool hide, | ||||
86 | const AccountPtr &account, | ||||
87 | QObject *parent): | ||||
88 | CreateJob(account, parent), | ||||
89 | d(new Private(this)) | ||||
90 | { | ||||
91 | d->drives = drives; | ||||
92 | d->hide = hide; | ||||
93 | } | ||||
94 | | ||||
95 | DrivesHideJob::~DrivesHideJob() = default; | ||||
96 | | ||||
97 | void DrivesHideJob::start() | ||||
98 | { | ||||
99 | d->processNext(); | ||||
100 | } | ||||
101 | | ||||
102 | ObjectsList DrivesHideJob::handleReplyWithItems(const QNetworkReply *reply, | ||||
103 | const QByteArray &rawData) | ||||
104 | { | ||||
105 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | ||||
106 | ContentType ct = Utils::stringToContentType(contentType); | ||||
107 | ObjectsList items; | ||||
108 | if (ct == KGAPI2::JSON) { | ||||
109 | items << Drives::fromJSON(rawData); | ||||
110 | } else { | ||||
111 | setError(KGAPI2::InvalidResponse); | ||||
112 | setErrorString(tr("Invalid response content type")); | ||||
113 | emitFinished(); | ||||
114 | return items; | ||||
115 | } | ||||
116 | | ||||
117 | // Enqueue next item or finish | ||||
118 | d->processNext(); | ||||
119 | | ||||
120 | return items; | ||||
121 | } | ||||
122 | | ||||
123 | |