Kgapi

contactgroupmodifyjob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "contactgroupmodifyjob.h"
10#include "contactgroup.h"
11#include "peopleservice.h"
12#include "private/queuehelper_p.h"
13#include "utils.h"
14
15#include <QNetworkRequest>
16#include <QNetworkReply>
17#include <QUrlQuery>
18#include <QJsonDocument>
19#include <QJsonObject>
20#include <QJsonArray>
21
22namespace KGAPI2::People
23{
24
25class Q_DECL_HIDDEN ContactGroupModifyJob::Private
26{
27public:
28 explicit Private(ContactGroupModifyJob *parent);
29 void processNextContactGroup();
30
31 QueueHelper<ContactGroupPtr> groups;
32 ContactGroupPtr lastGroup;
33
34private:
35 ContactGroupModifyJob * const q;
36};
37
38ContactGroupModifyJob::Private::Private(ContactGroupModifyJob *parent)
39 : q(parent)
40{
41}
42
43void ContactGroupModifyJob::Private::processNextContactGroup()
44{
45 if (groups.atEnd()) {
46 q->emitFinished();
47 return;
48 }
49
50 const auto group = groups.current();
51 const auto modifyUrl = PeopleService::updateContactGroupUrl(group->resourceName());
52 QNetworkRequest request(modifyUrl);
53 request.setRawHeader("Host", "people.googleapis.com");
54
55 const auto groupJson = group->toJSON().toObject();
56 const QJsonObject rootObject {
57 { QStringLiteral("contactGroup"), groupJson }
58 };
59 const auto rawData = QJsonDocument(rootObject).toJson();
60 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
61}
62
63ContactGroupModifyJob::ContactGroupModifyJob(const ContactGroupList &contactGroups, const AccountPtr &account, QObject* parent)
64 : ModifyJob(account, parent)
65 , d(std::make_unique<Private>(this))
66{
67 d->groups = contactGroups;
68}
69
70ContactGroupModifyJob::ContactGroupModifyJob(const ContactGroupPtr &group, const AccountPtr &account, QObject* parent)
71 : ModifyJob(account, parent)
72 , d(std::make_unique<Private>(this))
73{
74 d->groups << group;
75}
76
77ContactGroupModifyJob::~ContactGroupModifyJob() = default;
78
79void ContactGroupModifyJob::start()
80{
81 d->processNextContactGroup();
82}
83
84ObjectsList ContactGroupModifyJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
85{
86 const auto contentTypeString = reply->header(QNetworkRequest::ContentTypeHeader).toString();
87 const auto contentType = Utils::stringToContentType(contentTypeString);
88
89 if (contentType != KGAPI2::JSON) {
91 setErrorString(tr("Invalid response content type"));
92 emitFinished();
93 return {};
94 }
95
96 ObjectsList items;
97 const auto json = QJsonDocument::fromJson(rawData);
98 if (json.isObject()) {
99 const auto group = ContactGroup::fromJSON(json.object());
100 items << group;
101 }
102
103 d->groups.currentProcessed();
104 d->processNextContactGroup();
105
106 return items;
107}
108
109}
110
111#include "moc_contactgroupmodifyjob.cpp"
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QByteArray toJson(JsonFormat format) const const
QVariant header(QNetworkRequest::KnownHeaders header) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.