Kgapi

source.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 "source.h"
11
12#include "peopleservice.h"
13#include "profilemetadata.h"
14
15#include <QJsonArray>
16#include <QJsonObject>
17#include <QJsonValue>
18#include <QSharedData>
19
20#include <algorithm>
21
22namespace KGAPI2::People
23{
24class Source::Private : public QSharedData
25{
26public:
27 explicit Private() = default;
28 Private(const Private &) = default;
29 Private(Private &&) noexcept = delete;
30 Private &operator=(const Private &) = delete;
31 Private &operator=(Private &&) noexcept = delete;
32 ~Private() = default;
33
34 bool operator==(const Private &other) const
35 {
36 return etag == other.etag && profileMetadata == other.profileMetadata && id == other.id && updateTime == other.updateTime && type == other.type;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 QString etag{};
45 ProfileMetadata profileMetadata{};
46 QString id{};
47 QString updateTime{};
48 Source::Type type{};
49};
50
52 : d(new Private)
53{
54}
55
56Source::Source(const Source &) = default;
57Source::Source(Source &&) noexcept = default;
58Source &Source::operator=(const Source &) = default;
59Source &Source::operator=(Source &&) noexcept = default;
60Source::~Source() = default;
61
62bool Source::operator==(const Source &other) const
63{
64 return *d == *other.d;
65}
66
67bool Source::operator!=(const Source &other) const
68{
69 return !(*this == other);
70}
71
73{
74 return d->etag;
75}
76
77void Source::setEtag(const QString &value)
78{
79 d->etag = value;
80}
82{
83 return d->profileMetadata;
84}
86{
87 return d->id;
88}
89
90void Source::setId(const QString &value)
91{
92 d->id = value;
93}
95{
96 return d->updateTime;
97}
98Source::Source::Type Source::type() const
99{
100 return d->type;
101}
102
104{
105 d->type = value;
106}
107
108Source Source::fromJSON(const QJsonObject &obj)
109{
110 Source source;
111
112 if(!obj.isEmpty()) {
113 const auto typeEnumString = obj.value(QStringLiteral("type"));
114
115 if (typeEnumString == QLatin1StringView("ACCOUNT")) {
116 source.d->type = Type::ACCOUNT;
117 } else if (typeEnumString == QLatin1StringView("PROFILE")) {
118 source.d->type = Type::PROFILE;
119 } else if (typeEnumString == QLatin1StringView("DOMAIN_PROFILE")) {
120 source.d->type = Type::DOMAIN_PROFILE;
121 } else if (typeEnumString == QLatin1StringView("CONTACT")) {
122 source.d->type = Type::CONTACT;
123 } else if (typeEnumString == QLatin1StringView("OTHER_CONTACT")) {
124 source.d->type = Type::OTHER_CONTACT;
125 } else if (typeEnumString == QLatin1StringView("DOMAIN_CONTACT")) {
126 source.d->type = Type::DOMAIN_CONTACT;
127 } else {
128 source.d->type = Type::SOURCE_TYPE_UNSPECIFIED;
129 }
130
131 source.d->id = obj.value(QStringLiteral("id")).toString();
132 source.d->etag = obj.value(QStringLiteral("etag")).toString();
133 source.d->updateTime = obj.value(QStringLiteral("id")).toString();
134 source.d->profileMetadata = ProfileMetadata::fromJSON(obj.value(QStringLiteral("profileMetadata")).toObject());
135 }
136
137 return source;
138}
139
140QJsonValue Source::toJSON() const
141{
142 QJsonObject obj;
143
144 PeopleUtils::addValueToJsonObjectIfValid(obj, "etag", d->etag);
145 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "profileMetadata", d->profileMetadata.toJSON());
146 PeopleUtils::addValueToJsonObjectIfValid(obj, "id", d->id);
147 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "updateTime", d->updateTime);
148 switch (d->type) {
150 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("SOURCE_TYPE_UNSPECIFIED"));
151 break;
152 case Type::ACCOUNT:
153 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("ACCOUNT"));
154 break;
155 case Type::PROFILE:
156 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("PROFILE"));
157 break;
159 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("DOMAIN_PROFILE"));
160 break;
161 case Type::CONTACT:
162 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("CONTACT"));
163 break;
165 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OTHER_CONTACT"));
166 break;
168 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("DOMAIN_CONTACT"));
169 break;
170 }
171 return obj;
172}
173
174} // namespace KGAPI2::People
The metadata about a profile.
The source of a field.
Definition source.h:33
QString etag() const
Only populated in person.metadata.sources.
Definition source.cpp:72
Source::Type type() const
The source type.
Definition source.cpp:98
QString id() const
The unique identifier within the source type generated by the server.
Definition source.cpp:85
QString updateTime() const
Output only.
Definition source.cpp:94
ProfileMetadata profileMetadata() const
Output only.
Definition source.cpp:81
Source()
Constructs a new Source.
Definition source.cpp:51
@ SOURCE_TYPE_UNSPECIFIED
Unspecified.
@ DOMAIN_CONTACT
Google Workspace domain shared contact.
@ OTHER_CONTACT
Google "Other contact".
@ DOMAIN_PROFILE
Google Workspace domain profile.
void setEtag(const QString &value)
Sets value of the etag property.
Definition source.cpp:77
void setType(Source::Type value)
Sets value of the type property.
Definition source.cpp:103
void setId(const QString &value)
Sets value of the id property.
Definition source.cpp:90
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:54:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.