Kgapi

gender.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 "gender.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 Gender::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 metadata == other.metadata && formattedValue == other.formattedValue && value == other.value && addressMeAs == other.addressMeAs;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 FieldMetadata metadata{};
45 QString formattedValue{};
46 QString value{};
47 QString addressMeAs{};
48};
49
51 : d(new Private)
52{
53}
54
55Gender::Gender(const Gender &) = default;
56Gender::Gender(Gender &&) noexcept = default;
57Gender &Gender::operator=(const Gender &) = default;
58Gender &Gender::operator=(Gender &&) noexcept = default;
59Gender::~Gender() = default;
60
61bool Gender::operator==(const Gender &other) const
62{
63 return *d == *other.d;
64}
65
66bool Gender::operator!=(const Gender &other) const
67{
68 return !(*this == other);
69}
70
72{
73 return d->metadata;
74}
75
77{
78 d->metadata = value;
79}
81{
82 return d->formattedValue;
83}
85{
86 return d->value;
87}
88
89void Gender::setValue(const QString &value)
90{
91 d->value = value;
92}
94{
95 return d->addressMeAs;
96}
97
99{
100 d->addressMeAs = value;
101}
102
103Gender Gender::fromJSON(const QJsonObject &obj)
104{
105 Gender gender;
106
107 if(!obj.isEmpty()) {
108 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
109 gender.d->metadata = FieldMetadata::fromJSON(metadata);
110 gender.d->value = obj.value(QStringLiteral("value")).toString();
111 gender.d->formattedValue = obj.value(QStringLiteral("formattedValue")).toString();
112 gender.d->addressMeAs = obj.value(QStringLiteral("addressMeAs")).toString();
113 }
114
115 return gender;
116}
117
118QList<Gender> Gender::fromJSONArray(const QJsonArray &data)
119{
120 QList<Gender> genders;
121
122 for(const auto &gender : data) {
123 if(gender.isObject()) {
124 const auto objectifiedGender = gender.toObject();
125 genders.append(fromJSON(objectifiedGender));
126 }
127 }
128
129 return genders;
130}
131
132QJsonValue Gender::toJSON() const
133{
134 QJsonObject obj;
135
136 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
137 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedValue", d->formattedValue);
138 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
139 PeopleUtils::addValueToJsonObjectIfValid(obj, "addressMeAs", d->addressMeAs);
140 return obj;
141}
142
143} // namespace KGAPI2::People
Metadata about a field.
A person's gender.
Definition gender.h:34
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition gender.cpp:76
void setAddressMeAs(const QString &value)
Sets value of the addressMeAs property.
Definition gender.cpp:98
QString addressMeAs() const
Free form text field for pronouns that should be used to address the person.
Definition gender.cpp:93
QString formattedValue() const
Output only.
Definition gender.cpp:80
Gender()
Constructs a new Gender.
Definition gender.cpp:50
QString value() const
The gender for the person.
Definition gender.cpp:84
void setValue(const QString &value)
Sets value of the value property.
Definition gender.cpp:89
FieldMetadata metadata() const
Metadata about the gender.
Definition gender.cpp:71
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 4 2024 16:36:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.