Kgapi

nickname.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 "nickname.h"
11
12#include "fieldmetadata.h"
13#include "peopleservice.h"
14
15#include <QJsonArray>
16#include <QJsonObject>
17#include <QJsonValue>
18#include <QSharedData>
19
20#include <algorithm>
21
22namespace KGAPI2::People
23{
24class Nickname::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 value == other.value && type == other.type && metadata == other.metadata;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 QString value{};
45 Nickname::Type type{};
46 FieldMetadata metadata{};
47};
48
50 : d(new Private)
51{
52}
53
54Nickname::Nickname(const Nickname &) = default;
55Nickname::Nickname(Nickname &&) noexcept = default;
56Nickname &Nickname::operator=(const Nickname &) = default;
57Nickname &Nickname::operator=(Nickname &&) noexcept = default;
58Nickname::~Nickname() = default;
59
60bool Nickname::operator==(const Nickname &other) const
61{
62 return *d == *other.d;
63}
64
65bool Nickname::operator!=(const Nickname &other) const
66{
67 return !(*this == other);
68}
69
71{
72 return d->value;
73}
74
75void Nickname::setValue(const QString &value)
76{
77 d->value = value;
78}
79Nickname::Nickname::Type Nickname::type() const
80{
81 return d->type;
82}
83
85{
86 d->type = value;
87}
89{
90 return d->metadata;
91}
92
94{
95 d->metadata = value;
96}
97
98Nickname Nickname::fromJSON(const QJsonObject &obj)
99{
100 Nickname nickname;
101
102 if(!obj.isEmpty()) {
103 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
104 nickname.setMetadata(FieldMetadata::fromJSON(metadata));
105 nickname.setValue(obj.value(QStringLiteral("value")).toString());
106
107 const auto type = obj.value(QStringLiteral("type")).toString();
108 if (type == QLatin1StringView("MAIDEN_NAME")) {
110 } else if (type == QLatin1StringView("INITIALS")) {
112 } else if (type == QLatin1StringView("GPLUS")) {
114 } else if (type == QLatin1StringView("OTHER_NAME")) {
116 } else if (type == QLatin1StringView("ALTERNATE_NAME")) {
118 } else if (type == QLatin1StringView("SHORT_NAME")) {
120 } else {
122 }
123 }
124
125 return nickname;
126}
127
128QList<Nickname> Nickname::fromJSONArray(const QJsonArray &data)
129{
130 QList<Nickname> nicknames;
131
132 for(const auto &nickname : data) {
133 if(nickname.isObject()) {
134 const auto objectifiedNickname = nickname.toObject();
135 nicknames.append(fromJSON(objectifiedNickname));
136 }
137 }
138
139 return nicknames;
140}
141
142QJsonValue Nickname::toJSON() const
143{
144 QJsonObject obj;
145
146 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
147 switch (d->type) {
148 case Type::DEFAULT:
149 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("DEFAULT"));
150 break;
152 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("MAIDEN_NAME"));
153 break;
154 case Type::INITIALS:
155 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("INITIALS"));
156 break;
157 case Type::GPLUS:
158 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("GPLUS"));
159 break;
160 case Type::OTHER_NAME:
161 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OTHER_NAME"));
162 break;
164 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("ALTERNATE_NAME"));
165 break;
166 case Type::SHORT_NAME:
167 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("SHORT_NAME"));
168 break;
169 }
170 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
171 return obj;
172}
173
174} // namespace KGAPI2::People
Metadata about a field.
A person's nickname.
Definition nickname.h:34
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition nickname.cpp:93
void setType(Nickname::Type value)
Sets value of the type property.
Definition nickname.cpp:84
FieldMetadata metadata() const
Metadata about the nickname.
Definition nickname.cpp:88
void setValue(const QString &value)
Sets value of the value property.
Definition nickname.cpp:75
QString value() const
The nickname.
Definition nickname.cpp:70
Nickname::Type type() const
The type of the nickname.
Definition nickname.cpp:79
@ SHORT_NAME
A shorter version of the person's name.
@ MAIDEN_NAME
Maiden name or birth family name. Used when the person's family name has changed as a result of marri...
@ DEFAULT
Generic nickname.
@ GPLUS
Google+ profile nickname.
@ ALTERNATE_NAME
Alternate name person is known by.
@ OTHER_NAME
A professional affiliation or other name; for example, Dr. Smith.
Nickname()
Constructs a new Nickname.
Definition nickname.cpp:49
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QString toString() const const
void append(QList< T > &&value)
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.