Kgapi

address.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 "address.h"
11
12#include "fieldmetadata.h"
13#include "peopleservice.h"
14
15#include <QJsonValue>
16#include <QSharedData>
17
18#include <algorithm>
19
20namespace KGAPI2::People
21{
22class Address::Private : public QSharedData
23{
24public:
25 explicit Private() = default;
26 Private(const Private &) = default;
27 Private(Private &&) noexcept = delete;
28 Private &operator=(const Private &) = delete;
29 Private &operator=(Private &&) noexcept = delete;
30 ~Private() = default;
31
32 bool operator==(const Private &other) const
33 {
34 return formattedType == other.formattedType && city == other.city && metadata == other.metadata && countryCode == other.countryCode
35 && postalCode == other.postalCode && poBox == other.poBox && type == other.type && formattedValue == other.formattedValue
36 && extendedAddress == other.extendedAddress && region == other.region && streetAddress == other.streetAddress && country == other.country;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 QString formattedType{};
45 QString city{};
46 FieldMetadata metadata{};
47 QString countryCode{};
48 QString postalCode{};
49 QString poBox{};
50 QString type{};
51 QString formattedValue{};
52 QString extendedAddress{};
53 QString region{};
54 QString streetAddress{};
55 QString country{};
56};
57
59 : d(new Private)
60{
61}
62
63Address::Address(const Address &) = default;
64Address::Address(Address &&) noexcept = default;
65Address &Address::operator=(const Address &) = default;
66Address &Address::operator=(Address &&) noexcept = default;
67Address::~Address() = default;
68
69bool Address::operator==(const Address &other) const
70{
71 return *d == *other.d;
72}
73
74bool Address::operator!=(const Address &other) const
75{
76 return !(*this == other);
77}
78
80{
81 return d->formattedType;
82}
84{
85 return d->city;
86}
87
88void Address::setCity(const QString &value)
89{
90 d->city = value;
91}
93{
94 return d->metadata;
95}
96
98{
99 d->metadata = value;
100}
102{
103 return d->countryCode;
104}
105
107{
108 d->countryCode = value;
109}
111{
112 return d->postalCode;
113}
114
116{
117 d->postalCode = value;
118}
120{
121 return d->poBox;
122}
123
124void Address::setPoBox(const QString &value)
125{
126 d->poBox = value;
127}
129{
130 return d->type;
131}
132
133void Address::setType(const QString &value)
134{
135 d->type = value;
136}
138{
139 return d->formattedValue;
140}
141
143{
144 d->formattedValue = value;
145}
147{
148 return d->extendedAddress;
149}
150
152{
153 d->extendedAddress = value;
154}
156{
157 return d->region;
158}
159
160void Address::setRegion(const QString &value)
161{
162 d->region = value;
163}
165{
166 return d->streetAddress;
167}
168
170{
171 d->streetAddress = value;
172}
174{
175 return d->country;
176}
177
178void Address::setCountry(const QString &value)
179{
180 d->country = value;
181}
182
183Address Address::fromJSON(const QJsonObject &obj)
184{
185 Address address;
186
187 if(!obj.isEmpty()) {
188 address.setMetadata(FieldMetadata::fromJSON(obj.value(QStringLiteral("metadata")).toObject()));
189 address.setFormattedValue(obj.value(QStringLiteral("formattedValue")).toString());
190 address.setType(obj.value(QStringLiteral("type")).toString());
191 address.setPoBox(obj.value(QStringLiteral("poBox")).toString());
192 address.setStreetAddress(obj.value(QStringLiteral("streetAddress")).toString());
193 address.setExtendedAddress(obj.value(QStringLiteral("extendedAddress")).toString());
194 address.setCity(obj.value(QStringLiteral("city")).toString());
195 address.setRegion(obj.value(QStringLiteral("region")).toString());
196 address.setPostalCode(obj.value(QStringLiteral("postalCode")).toString());
197 address.setCountry(obj.value(QStringLiteral("country")).toString());
198 address.setCountryCode(obj.value(QStringLiteral("countryCode")).toString());
199 }
200
201 return address;
202}
203
204QList<Address> Address::fromJSONArray(const QJsonArray &data)
205{
206 QList<People::Address> addresses;
207
208 for(const auto &address : data) {
209 if(address.isObject()) {
210 const auto objectifiedAddress = address.toObject();
211 addresses.append(fromJSON(objectifiedAddress));
212 }
213 }
214
215 return addresses;
216}
217
218QJsonValue Address::toJSON() const
219{
220 QJsonObject obj;
221
222 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType", d->formattedType);
223 PeopleUtils::addValueToJsonObjectIfValid(obj, "city", d->city);
224 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
225 PeopleUtils::addValueToJsonObjectIfValid(obj, "countryCode", d->countryCode);
226 PeopleUtils::addValueToJsonObjectIfValid(obj, "postalCode", d->postalCode);
227 PeopleUtils::addValueToJsonObjectIfValid(obj, "poBox", d->poBox);
228 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
229 PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedValue", d->formattedValue);
230 PeopleUtils::addValueToJsonObjectIfValid(obj, "extendedAddress", d->extendedAddress);
231 PeopleUtils::addValueToJsonObjectIfValid(obj, "region", d->region);
232 PeopleUtils::addValueToJsonObjectIfValid(obj, "streetAddress", d->streetAddress);
233 PeopleUtils::addValueToJsonObjectIfValid(obj, "country", d->country);
234 return obj;
235}
236
237} // namespace KGAPI2::People
A person's physical address.
Definition address.h:35
QString city() const
The city of the address.
Definition address.cpp:83
QString countryCode() const
The ISO 3166-1 alpha-2 country code of the address.
Definition address.cpp:101
QString type() const
The type of the address.
Definition address.cpp:128
void setCity(const QString &value)
Sets value of the city property.
Definition address.cpp:88
void setPoBox(const QString &value)
Sets value of the poBox property.
Definition address.cpp:124
QString postalCode() const
The postal code of the address.
Definition address.cpp:110
void setRegion(const QString &value)
Sets value of the region property.
Definition address.cpp:160
QString poBox() const
The P.O.
Definition address.cpp:119
void setType(const QString &value)
Sets value of the type property.
Definition address.cpp:133
void setExtendedAddress(const QString &value)
Sets value of the extendedAddress property.
Definition address.cpp:151
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition address.cpp:97
void setCountry(const QString &value)
Sets value of the country property.
Definition address.cpp:178
Address()
Constructs a new Address.
Definition address.cpp:58
QString region() const
The region of the address; for example, the state or province.
Definition address.cpp:155
QString extendedAddress() const
The extended address of the address; for example, the apartment number.
Definition address.cpp:146
QString formattedValue() const
The unstructured value of the address.
Definition address.cpp:137
FieldMetadata metadata() const
Metadata about the address.
Definition address.cpp:92
QString streetAddress() const
The street address.
Definition address.cpp:164
QString country() const
The country of the address.
Definition address.cpp:173
void setStreetAddress(const QString &value)
Sets value of the streetAddress property.
Definition address.cpp:169
void setFormattedValue(const QString &value)
Sets value of the formattedValue property.
Definition address.cpp:142
void setCountryCode(const QString &value)
Sets value of the countryCode property.
Definition address.cpp:106
void setPostalCode(const QString &value)
Sets value of the postalCode property.
Definition address.cpp:115
QString formattedType() const
Output only.
Definition address.cpp:79
Metadata about a field.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) 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.