Kgapi

membership.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 "membership.h"
11
12#include "contactgroupmembership.h"
13#include "domainmembership.h"
14#include "fieldmetadata.h"
15#include "peopleservice.h"
16
17#include <QJsonArray>
18#include <QJsonObject>
19#include <QJsonValue>
20#include <QSharedData>
21
22#include <algorithm>
23
24namespace KGAPI2::People
25{
26
27struct MembershipDefinition
28{
29 FieldMetadata metadata;
30 ContactGroupMembership contactGroupMembership;
31 DomainMembership domainMembership;
32};
33
34class Membership::Private : public QSharedData
35{
36public:
37 explicit Private() = default;
38 Private(const Private &) = default;
39 Private(Private &&) noexcept = delete;
40 Private &operator=(const Private &) = delete;
41 Private &operator=(Private &&) noexcept = delete;
42 ~Private() = default;
43
44 bool operator==(const Private &other) const
45 {
46 return domainMembership == other.domainMembership && contactGroupMembership == other.contactGroupMembership && metadata == other.metadata;
47 }
48
49 bool operator!=(const Private &other) const
50 {
51 return !(*this == other);
52 }
53
54 DomainMembership domainMembership{};
55 ContactGroupMembership contactGroupMembership{};
56 FieldMetadata metadata{};
57};
58
60 : d(new Private)
61{
62}
63
64Membership::Membership(const MembershipDefinition &definition)
65 : d(new Private)
66{
67 d->metadata = definition.metadata;
68 d->contactGroupMembership = definition.contactGroupMembership;
69 d->domainMembership = definition.domainMembership;
70}
71
72Membership::Membership(const Membership &) = default;
73Membership::Membership(Membership &&) noexcept = default;
74Membership &Membership::operator=(const Membership &) = default;
75Membership &Membership::operator=(Membership &&) noexcept = default;
76Membership::~Membership() = default;
77
78bool Membership::operator==(const Membership &other) const
79{
80 return *d == *other.d;
81}
82
83bool Membership::operator!=(const Membership &other) const
84{
85 return !(*this == other);
86}
87
89{
90 return d->domainMembership;
91}
93{
94 return d->contactGroupMembership;
95}
96
98{
99 d->contactGroupMembership = value;
100}
102{
103 return d->metadata;
104}
105
107{
108 d->metadata = value;
109}
110
111Membership Membership::fromJSON(const QJsonObject &obj)
112{
113 if(obj.isEmpty()) {
114 return Membership();
115 }
116
117 MembershipDefinition definition;
118
119 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
120 definition.metadata = FieldMetadata::fromJSON(metadata);
121
122 const auto contactGroupMembership = obj.value(QStringLiteral("contactGroupMembership")).toObject();
123 definition.contactGroupMembership = ContactGroupMembership::fromJSON(contactGroupMembership);
124
125 const auto domainMembership = obj.value(QStringLiteral("domainMembership")).toObject();
126 definition.domainMembership = DomainMembership::fromJSON(domainMembership);
127
128 return Membership(definition);
129}
130
131QList<Membership> Membership::fromJSONArray(const QJsonArray &data)
132{
133 QList<Membership> memberships;
134
135 for(const auto &membership : data) {
136 if(membership.isObject()) {
137 const auto objectifiedMembership = membership.toObject();
138 memberships.append(fromJSON(objectifiedMembership));
139 }
140 }
141
142 return memberships;
143}
144
145QJsonValue Membership::toJSON() const
146{
147 QJsonObject obj;
148
149 if (!d->contactGroupMembership.contactGroupResourceName().isEmpty()) {
150 PeopleUtils::addValueToJsonObjectIfValid(obj, "contactGroupMembership", d->contactGroupMembership.toJSON());
151 } else if (d->domainMembership.inViewerDomain()) {
152 PeopleUtils::addValueToJsonObjectIfValid(obj, "domainMembership", d->domainMembership.toJSON());
153 }
154 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
155 return obj;
156}
157
158} // namespace KGAPI2::People
A Google contact group membership.
A Google Workspace Domain membership.
Metadata about a field.
A person's membership in a group.
Definition membership.h:36
ContactGroupMembership contactGroupMembership() const
The contact group membership.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Membership()
Constructs a new Membership.
void setContactGroupMembership(const ContactGroupMembership &value)
Sets value of the contactGroupMembership property.
DomainMembership domainMembership() const
Output only.
FieldMetadata metadata() const
Metadata about the membership.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
void append(QList< T > &&value)
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.