Kgapi

contactgroup.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
3 * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 * SPDX-License-Identifier: LGPL-3.0-only
7 * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
8 */
9
10#include "contactgroup.h"
11
12#include "contactgroupmetadata.h"
13#include "groupclientdata.h"
14#include "peopleservice.h"
15
16#include <QJsonArray>
17#include <QJsonObject>
18#include <QJsonValue>
19#include <QSharedData>
20
21#include <algorithm>
22
23namespace KGAPI2::People
24{
25class ContactGroup::Private
26{
27public:
28 explicit Private() = default;
29 Private(const Private &) = default;
30 Private(Private &&) noexcept = delete;
31 Private &operator=(const Private &) = delete;
32 Private &operator=(Private &&) noexcept = delete;
33 ~Private() = default;
34
35 bool operator==(const Private &other) const
36 {
37 return formattedName == other.formattedName && memberCount == other.memberCount && etag == other.etag && groupType == other.groupType
38 && clientData == other.clientData && name == other.name && metadata == other.metadata && resourceName == other.resourceName
39 && memberResourceNames == other.memberResourceNames;
40 }
41
42 bool operator!=(const Private &other) const
43 {
44 return !(*this == other);
45 }
46
47 QString formattedName{};
48 int memberCount{};
49 QString etag{};
50 ContactGroup::GroupType groupType{};
51 QList<GroupClientData> clientData{};
52 QString name{};
53 ContactGroupMetadata metadata{};
54 QString resourceName{};
55 QList<QString> memberResourceNames{};
56};
57
59 : d(new Private)
60{
61}
62
64
65bool ContactGroup::operator==(const ContactGroup &other) const
66{
67 return *d == *other.d;
68}
69
70bool ContactGroup::operator!=(const ContactGroup &other) const
71{
72 return !(*this == other);
73}
74
76{
77 return d->formattedName;
78}
80{
81 return d->memberCount;
82}
84{
85 return d->etag;
86}
87
89{
90 d->etag = value;
91}
92ContactGroup::ContactGroup::GroupType ContactGroup::groupType() const
93{
94 return d->groupType;
95}
97{
98 return d->clientData;
99}
100
102{
103 d->clientData = value;
104}
105
107{
108 d->clientData.push_back(value);
109}
110
112{
113 d->clientData.removeOne(value);
114}
115
117{
118 d->clientData.clear();
119}
120
122{
123 return d->name;
124}
125
127{
128 d->name = value;
129}
131{
132 return d->metadata;
133}
135{
136 return d->resourceName;
137}
138
140{
141 d->resourceName = value;
142}
144{
145 return d->memberResourceNames;
146}
147
148ContactGroupPtr ContactGroup::fromJSON(const QJsonObject &obj)
149{
150 auto contactGroup = new ContactGroup;
151
152 if (!obj.isEmpty()) {
153 contactGroup->d->resourceName = obj.value(QStringLiteral("resourceName")).toString();
154 contactGroup->d->etag = obj.value(QStringLiteral("etag")).toString();
155
156 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
157 contactGroup->d->metadata = ContactGroupMetadata::fromJSON(metadata);
158
159 const auto groupType = obj.value(QStringLiteral("groupType"));
160 contactGroup->d->groupType = ContactGroup::GroupType(groupType.toInt());
161
162 contactGroup->d->name = obj.value(QStringLiteral("name")).toString();
163 contactGroup->d->formattedName = obj.value(QStringLiteral("formattedName")).toString();
164
165 const auto memberResourceNames = obj.value(QStringLiteral("memberResourceNames")).toArray();
166 std::transform(memberResourceNames.cbegin(),
168 std::back_inserter(contactGroup->d->memberResourceNames),
169 [](const QJsonValue &jsonMemberResourceName) {
170 return jsonMemberResourceName.toString();
171 });
172
173 contactGroup->d->memberCount = obj.value(QStringLiteral("memberCount")).toInt();
174
175 const auto clientData = obj.value(QStringLiteral("clientData")).toArray();
176 contactGroup->d->clientData = GroupClientData::fromJSONArray(clientData);
177 }
178
179 return ContactGroupPtr(contactGroup);
180}
181
182QJsonValue ContactGroup::toJSON() const
183{
184 QJsonObject obj;
185
186 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedName", d->formattedName);
187 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "memberCount", d->memberCount);
188 PeopleUtils::addValueToJsonObjectIfValid(obj, "etag", d->etag);
189 /* Output only
190 switch (d->groupType) {
191 case GroupType::GROUP_TYPE_UNSPECIFIED:
192 PeopleUtils::addValueToJsonObjectIfValid(obj, "groupType", QStringLiteral("GROUP_TYPE_UNSPECIFIED"));
193 break;
194 case GroupType::USER_CONTACT_GROUP:
195 PeopleUtils::addValueToJsonObjectIfValid(obj, "groupType", QStringLiteral("USER_CONTACT_GROUP"));
196 break;
197 case GroupType::SYSTEM_CONTACT_GROUP:
198 PeopleUtils::addValueToJsonObjectIfValid(obj, "groupType", QStringLiteral("SYSTEM_CONTACT_GROUP"));
199 break;
200 }*/
201 if (!d->clientData.isEmpty()) {
202 QJsonArray arr;
203 std::transform(d->clientData.cbegin(), d->clientData.cend(), std::back_inserter(arr), [](const auto &val) {
204 return val.toJSON();
205 });
206 PeopleUtils::addValueToJsonObjectIfValid(obj, "clientData", std::move(arr));
207 }
208 PeopleUtils::addValueToJsonObjectIfValid(obj, "name", d->name);
209 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
210 PeopleUtils::addValueToJsonObjectIfValid(obj, "resourceName", d->resourceName);
211 /* Output only
212 if (!d->memberResourceNames.isEmpty()) {
213 QJsonArray arr;
214 std::transform(d->memberResourceNames.cbegin(), d->memberResourceNames.cend(), std::back_inserter(arr), [](const auto &val) {
215 return val;
216 });
217 PeopleUtils::addValueToJsonObjectIfValid(obj, "memberResourceNames", std::move(arr));
218 }*/
219 return obj;
220}
221
222} // namespace KGAPI2::People
The metadata about a contact group.
QString resourceName() const
The resource name for the contact group, assigned by the server.
int memberCount() const
Output only.
void setClientData(const QList< GroupClientData > &value)
Sets value of the clientData property.
void setName(const QString &value)
Sets value of the name property.
QString etag() const
The HTTP entity tag of the resource.
ContactGroup()
Constructs a new ContactGroup.
void removeGroupClientData(const GroupClientData &value)
Removes the given value from the list of clientData if it exists.
QList< QString > memberResourceNames() const
Output only.
QString name() const
The contact group name set by the group owner or a system provided name for system groups.
void setEtag(const QString &value)
Sets value of the etag property.
ContactGroupMetadata metadata() const
Output only.
ContactGroup::GroupType groupType() const
Output only.
void setResourceName(const QString &value)
Sets value of the resourceName property.
void clearClientData()
Clears the list of clientData.
QList< GroupClientData > clientData() const
The group's client data.
QString formattedName() const
Output only.
void addGroupClientData(const GroupClientData &value)
Appends the given value to the list of clientData.
Arbitrary client data that is populated by clients.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
int toInt(int defaultValue) const const
QJsonObject toObject() const const
QString toString() const const
const_iterator cbegin() const const
const_iterator cend() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:55:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.