Kgapi

name.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 "name.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 <KContacts/Addressee>
21
22#include <algorithm>
23
24namespace KGAPI2::People
25{
26class Name::Private : public QSharedData
27{
28public:
29 explicit Private() = default;
30 Private(const Private &) = default;
31 Private(Private &&) noexcept = delete;
32 Private &operator=(const Private &) = delete;
33 Private &operator=(Private &&) noexcept = delete;
34 ~Private() = default;
35
36 bool operator==(const Private &other) const
37 {
38 return unstructuredName == other.unstructuredName && familyName == other.familyName && phoneticMiddleName == other.phoneticMiddleName
39 && middleName == other.middleName && honorificPrefix == other.honorificPrefix && givenName == other.givenName
40 && phoneticGivenName == other.phoneticGivenName && phoneticHonorificPrefix == other.phoneticHonorificPrefix && displayName == other.displayName
41 && displayNameLastFirst == other.displayNameLastFirst && phoneticFamilyName == other.phoneticFamilyName && honorificSuffix == other.honorificSuffix
42 && phoneticHonorificSuffix == other.phoneticHonorificSuffix && phoneticFullName == other.phoneticFullName && metadata == other.metadata;
43 }
44
45 bool operator!=(const Private &other) const
46 {
47 return !(*this == other);
48 }
49
50 QString unstructuredName{};
51 QString familyName{};
52 QString phoneticMiddleName{};
53 QString middleName{};
54 QString honorificPrefix{};
55 QString givenName{};
56 QString phoneticGivenName{};
57 QString phoneticHonorificPrefix{};
58 QString displayName{};
59 QString displayNameLastFirst{};
60 QString phoneticFamilyName{};
61 QString honorificSuffix{};
62 QString phoneticHonorificSuffix{};
63 QString phoneticFullName{};
64 FieldMetadata metadata{};
65};
66
68 : d(new Private)
69{
70}
71
72Name::Name(const Name &) = default;
73Name::Name(Name &&) noexcept = default;
74Name &Name::operator=(const Name &) = default;
75Name &Name::operator=(Name &&) noexcept = default;
76Name::~Name() = default;
77
78bool Name::operator==(const Name &other) const
79{
80 return *d == *other.d;
81}
82
83bool Name::operator!=(const Name &other) const
84{
85 return !(*this == other);
86}
87
89{
90 return d->unstructuredName;
91}
92
94{
95 d->unstructuredName = value;
96}
98{
99 return d->familyName;
100}
101
102void Name::setFamilyName(const QString &value)
103{
104 d->familyName = value;
105}
107{
108 return d->phoneticMiddleName;
109}
110
112{
113 d->phoneticMiddleName = value;
114}
116{
117 return d->middleName;
118}
119
120void Name::setMiddleName(const QString &value)
121{
122 d->middleName = value;
123}
125{
126 return d->honorificPrefix;
127}
128
130{
131 d->honorificPrefix = value;
132}
134{
135 return d->givenName;
136}
137
138void Name::setGivenName(const QString &value)
139{
140 d->givenName = value;
141}
143{
144 return d->phoneticGivenName;
145}
146
148{
149 d->phoneticGivenName = value;
150}
152{
153 return d->phoneticHonorificPrefix;
154}
155
157{
158 d->phoneticHonorificPrefix = value;
159}
161{
162 return d->displayName;
163}
165{
166 return d->displayNameLastFirst;
167}
169{
170 return d->phoneticFamilyName;
171}
172
174{
175 d->phoneticFamilyName = value;
176}
178{
179 return d->honorificSuffix;
180}
181
183{
184 d->honorificSuffix = value;
185}
187{
188 return d->phoneticHonorificSuffix;
189}
190
192{
193 d->phoneticHonorificSuffix = value;
194}
196{
197 return d->phoneticFullName;
198}
199
201{
202 d->phoneticFullName = value;
203}
205{
206 return d->metadata;
207}
208
210{
211 d->metadata = value;
212}
213
214Name Name::fromJSON(const QJsonObject &obj)
215{
216 Name name;
217
218 if(!obj.isEmpty()) {
219 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
220 name.d->metadata = FieldMetadata::fromJSON(metadata);
221 name.d->displayName = obj.value(QStringLiteral("displayName")).toString();
222 name.d->displayNameLastFirst = obj.value(QStringLiteral("displayNameLastFirst")).toString();
223 name.d->unstructuredName = obj.value(QStringLiteral("unstructuredName")).toString();
224 name.d->familyName = obj.value(QStringLiteral("familyName")).toString();
225 name.d->givenName = obj.value(QStringLiteral("givenName")).toString();
226 name.d->middleName = obj.value(QStringLiteral("middleName")).toString();
227 name.d->honorificPrefix = obj.value(QStringLiteral("honorificPrefix")).toString();
228 name.d->honorificSuffix = obj.value(QStringLiteral("honorificSuffix")).toString();
229 name.d->phoneticFullName = obj.value(QStringLiteral("phoneticFullName")).toString();
230 name.d->phoneticFamilyName = obj.value(QStringLiteral("phoneticFamilyName")).toString();
231 name.d->phoneticGivenName = obj.value(QStringLiteral("phoneticGivenName")).toString();
232 name.d->phoneticMiddleName = obj.value(QStringLiteral("phoneticMiddleName")).toString();
233 name.d->phoneticHonorificPrefix = obj.value(QStringLiteral("phoneticHonorificPrefix")).toString();
234 name.d->phoneticHonorificSuffix = obj.value(QStringLiteral("phoneticHonorificSuffix")).toString();
235 }
236
237 return name;
238}
239
240QList<Name> Name::fromJSONArray(const QJsonArray &data)
241{
242 QList<Name> names;
243
244 for(const auto &name : data) {
245 if(name.isObject()) {
246 const auto objectifiedName = name.toObject();
247 names.append(fromJSON(objectifiedName));
248 }
249 }
250
251 return names;
252}
253
254QJsonValue Name::toJSON() const
255{
256 QJsonObject obj;
257
258 PeopleUtils::addValueToJsonObjectIfValid(obj, "unstructuredName", d->unstructuredName);
259 PeopleUtils::addValueToJsonObjectIfValid(obj, "familyName", d->familyName);
260 PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticMiddleName", d->phoneticMiddleName);
261 PeopleUtils::addValueToJsonObjectIfValid(obj, "middleName", d->middleName);
262 PeopleUtils::addValueToJsonObjectIfValid(obj, "honorificPrefix", d->honorificPrefix);
263 PeopleUtils::addValueToJsonObjectIfValid(obj, "givenName", d->givenName);
264 PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticGivenName", d->phoneticGivenName);
265 PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticHonorificPrefix", d->phoneticHonorificPrefix);
266 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "displayName", d->displayName);
267 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "displayNameLastFirst", d->displayNameLastFirst);
268 PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticFamilyName", d->phoneticFamilyName);
269 PeopleUtils::addValueToJsonObjectIfValid(obj, "honorificSuffix", d->honorificSuffix);
270 PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticHonorificSuffix", d->phoneticHonorificSuffix);
271 PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticFullName", d->phoneticFullName);
272 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
273 return obj;
274}
275
276Name Name::fromKContactsAddressee(const KContacts::Addressee &addressee)
277{
278 Name name;
279 name.setFamilyName(addressee.familyName());
280 name.setGivenName(addressee.givenName());
281 name.setHonorificPrefix(addressee.prefix());
282 name.setHonorificSuffix(addressee.suffix());
283 name.setUnstructuredName(addressee.formattedName());
284 return name;
285}
286
287void Name::applyToKContactsAddressee(KContacts::Addressee &addressee) const
288{
289 addressee.setName(unstructuredName());
290 addressee.setFamilyName(familyName());
291 addressee.setGivenName(givenName());
292 addressee.setPrefix(honorificPrefix());
293 addressee.setSuffix(honorificSuffix());
294 addressee.setFormattedName(displayName());
295}
296
297} // namespace KGAPI2::People
QString familyName() const
void setPrefix(const QString &prefix)
QString prefix() const
void setSuffix(const QString &suffix)
QString suffix() const
void setGivenName(const QString &givenName)
QString formattedName() const
void setName(const QString &name)
QString givenName() const
void setFormattedName(const QString &formattedName)
void setFamilyName(const QString &familyName)
Metadata about a field.
A person's name.
Definition name.h:38
QString displayName() const
Output only.
Definition name.cpp:160
QString familyName() const
The family name.
Definition name.cpp:97
QString givenName() const
The given name.
Definition name.cpp:133
void setHonorificSuffix(const QString &value)
Sets value of the honorificSuffix property.
Definition name.cpp:182
QString honorificSuffix() const
The honorific suffixes, such as Jr.
Definition name.cpp:177
QString displayNameLastFirst() const
Output only.
Definition name.cpp:164
QString unstructuredName() const
The free form name value.
Definition name.cpp:88
QString phoneticFamilyName() const
The family name spelled as it sounds.
Definition name.cpp:168
void setPhoneticGivenName(const QString &value)
Sets value of the phoneticGivenName property.
Definition name.cpp:147
QString middleName() const
The middle name(s).
Definition name.cpp:115
void setPhoneticFamilyName(const QString &value)
Sets value of the phoneticFamilyName property.
Definition name.cpp:173
QString phoneticHonorificSuffix() const
The honorific suffixes spelled as they sound.
Definition name.cpp:186
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition name.cpp:209
void setUnstructuredName(const QString &value)
Sets value of the unstructuredName property.
Definition name.cpp:93
QString phoneticHonorificPrefix() const
The honorific prefixes spelled as they sound.
Definition name.cpp:151
QString honorificPrefix() const
The honorific prefixes, such as Mrs.
Definition name.cpp:124
void setHonorificPrefix(const QString &value)
Sets value of the honorificPrefix property.
Definition name.cpp:129
void setPhoneticMiddleName(const QString &value)
Sets value of the phoneticMiddleName property.
Definition name.cpp:111
QString phoneticMiddleName() const
The middle name(s) spelled as they sound.
Definition name.cpp:106
QString phoneticFullName() const
The full name spelled as it sounds.
Definition name.cpp:195
void setPhoneticHonorificPrefix(const QString &value)
Sets value of the phoneticHonorificPrefix property.
Definition name.cpp:156
void setPhoneticHonorificSuffix(const QString &value)
Sets value of the phoneticHonorificSuffix property.
Definition name.cpp:191
Name()
Constructs a new Name.
Definition name.cpp:67
FieldMetadata metadata() const
Metadata about the name.
Definition name.cpp:204
void setFamilyName(const QString &value)
Sets value of the familyName property.
Definition name.cpp:102
void setMiddleName(const QString &value)
Sets value of the middleName property.
Definition name.cpp:120
QString phoneticGivenName() const
The given name spelled as it sounds.
Definition name.cpp:142
void setGivenName(const QString &value)
Sets value of the givenName property.
Definition name.cpp:138
void setPhoneticFullName(const QString &value)
Sets value of the phoneticFullName property.
Definition name.cpp:200
QString name(StandardShortcut id)
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 Fri May 3 2024 11:50:41 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.