Kgapi

contactgroupcreatejob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 *
7 */
8
9#include "contactgroupcreatejob.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 ContactGroupCreateJob::Private
26{
27public:
28 explicit Private(ContactGroupCreateJob *parent);
29 void processNextContactGroup();
30
31 QueueHelper<ContactGroupPtr> groups;
32 ContactGroupPtr lastGroup;
33
34private:
35 ContactGroupCreateJob * const q;
36};
37
38ContactGroupCreateJob::Private::Private(ContactGroupCreateJob *parent)
39 : q(parent)
40{
41}
42
43void ContactGroupCreateJob::Private::processNextContactGroup()
44{
45 if (groups.atEnd()) {
46 q->emitFinished();
47 return;
48 }
49
50 const auto group = groups.current();
51 static const auto createUrl = PeopleService::createContactGroupUrl();
52 QNetworkRequest request(createUrl);
53 request.setRawHeader("Host", "people.googleapis.com");
54
55 const auto groupJson = group->toJSON().toObject();
56 const QJsonObject rootObject {
57 { QStringLiteral("contactGroup"), groupJson },
58 { QStringLiteral("readGroupFields"), PeopleService::allContactGroupRecentlyCreatedAvailableFields() }
59 };
60 const auto rawData = QJsonDocument(rootObject).toJson();
61 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
62}
63
64
65ContactGroupCreateJob::ContactGroupCreateJob(const ContactGroupList &contactGroups, const AccountPtr& account, QObject* parent)
66 : CreateJob(account, parent)
67 , d(std::make_unique<Private>(this))
68{
69 d->groups = contactGroups;
70}
71
72ContactGroupCreateJob::ContactGroupCreateJob(const ContactGroupPtr &contactGroup, const AccountPtr &account, QObject* parent)
73 : CreateJob(account, parent)
74 , d(std::make_unique<Private>(this))
75{
76 d->groups << contactGroup;
77}
78
79ContactGroupCreateJob::~ContactGroupCreateJob() = default;
80
81void ContactGroupCreateJob::start()
82{
83 d->processNextContactGroup();
84}
85
86ObjectsList ContactGroupCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
87{
88 const auto contentTypeString = reply->header(QNetworkRequest::ContentTypeHeader).toString();
89 const auto contentType = Utils::stringToContentType(contentTypeString);
90
91 if (contentType != KGAPI2::JSON) {
93 setErrorString(tr("Invalid response content type"));
94 emitFinished();
95 return {};
96 }
97
98 ObjectsList items;
99 const auto json = QJsonDocument::fromJson(rawData);
100 if (json.isObject()) {
101 const auto group = ContactGroup::fromJSON(json.object());
102 items << group;
103 }
104
105 d->groups.currentProcessed();
106 d->processNextContactGroup();
107
108 return items;
109}
110
111}
112
113#include "moc_contactgroupcreatejob.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 4 2024 16:36:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.