Kgapi

people/location.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 "location.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{
24
25class Location::Private : public QSharedData
26{
27public:
28 explicit Private() = default;
29 Private(const Private &) = default;
30 Private(Private &&) noexcept = delete;
31 Private &operator=(const Private &) = delete;
32 Private &operator=(Private &&) noexcept = delete;
33 ~Private() = default;
34
35 bool operator==(const Private &other) const
36 {
37 return deskCode == other.deskCode && floor == other.floor && buildingId == other.buildingId && type == other.type && current == other.current
38 && value == other.value && metadata == other.metadata && floorSection == other.floorSection;
39 }
40
41 bool operator!=(const Private &other) const
42 {
43 return !(*this == other);
44 }
45
46 QString deskCode{};
47 QString floor{};
48 QString buildingId{};
49 QString type{};
50 bool current{};
51 QString value{};
52 FieldMetadata metadata{};
53 QString floorSection{};
54};
55
57 : d(new Private)
58{
59}
60
61Location::Location(const Location &) = default;
62Location::Location(Location &&) noexcept = default;
63Location &Location::operator=(const Location &) = default;
64Location &Location::operator=(Location &&) noexcept = default;
65Location::~Location() = default;
66
67bool Location::operator==(const Location &other) const
68{
69 return *d == *other.d;
70}
71
72bool Location::operator!=(const Location &other) const
73{
74 return !(*this == other);
75}
76
78{
79 return d->deskCode;
80}
81
83{
84 d->deskCode = value;
85}
87{
88 return d->floor;
89}
90
91void Location::setFloor(const QString &value)
92{
93 d->floor = value;
94}
96{
97 return d->buildingId;
98}
99
101{
102 d->buildingId = value;
103}
105{
106 return d->type;
107}
108
109void Location::setType(const QString &value)
110{
111 d->type = value;
112}
114{
115 return d->current;
116}
117
118void Location::setCurrent(bool value)
119{
120 d->current = value;
121}
123{
124 return d->value;
125}
126
127void Location::setValue(const QString &value)
128{
129 d->value = value;
130}
132{
133 return d->metadata;
134}
135
137{
138 d->metadata = value;
139}
141{
142 return d->floorSection;
143}
144
146{
147 d->floorSection = value;
148}
149
150Location Location::fromJSON(const QJsonObject &obj)
151{
152 Location location;
153
154 if(!obj.isEmpty()) {
155 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
156 location.setMetadata(FieldMetadata::fromJSON(metadata));
157 location.setValue(obj.value(QStringLiteral("value")).toString());
158 location.setType(obj.value(QStringLiteral("type")).toString());
159 location.setCurrent(obj.value(QStringLiteral("current")).toBool());
160 location.setBuildingId(obj.value(QStringLiteral("buildingId")).toString());
161 location.setFloor(obj.value(QStringLiteral("floor")).toString());
162 location.setFloorSection(obj.value(QStringLiteral("floorSection")).toString());
163 location.setDeskCode(obj.value(QStringLiteral("deskCode")).toString());
164 }
165
166 return location;
167}
168
169QList<Location> Location::fromJSONArray(const QJsonArray &data)
170{
171 QList<Location> locations;
172
173 for(const auto &location : data) {
174 if(location.isObject()) {
175 const auto objectifiedLocation = location.toObject();
176 locations.append(fromJSON(objectifiedLocation));
177 }
178 }
179
180 return locations;
181}
182
183QJsonValue Location::toJSON() const
184{
185 QJsonObject obj;
186
187 PeopleUtils::addValueToJsonObjectIfValid(obj, "deskCode", d->deskCode);
188 PeopleUtils::addValueToJsonObjectIfValid(obj, "floor", d->floor);
189 PeopleUtils::addValueToJsonObjectIfValid(obj, "buildingId", d->buildingId);
190 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
191 PeopleUtils::addValueToJsonObjectIfValid(obj, "current", d->current);
192 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
193 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
194 PeopleUtils::addValueToJsonObjectIfValid(obj, "floorSection", d->floorSection);
195 return obj;
196}
197
198} // namespace KGAPI2::People
Represents a single record about geographical location provided by Google Latitude service.
Metadata about a field.
A person's location.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
void setCurrent(bool value)
Sets value of the current property.
bool current() const
Whether the location is the current location.
QString deskCode() const
The individual desk location.
void setDeskCode(const QString &value)
Sets value of the deskCode property.
QString floor() const
The floor name or number.
QString buildingId() const
The building identifier.
void setType(const QString &value)
Sets value of the type property.
QString value() const
The free-form value of the location.
Location()
Constructs a new Location.
void setFloorSection(const QString &value)
Sets value of the floorSection property.
QString floorSection() const
The floor section in floor_name.
FieldMetadata metadata() const
Metadata about the location.
void setValue(const QString &value)
Sets value of the value property.
QString type() const
The type of the location.
void setFloor(const QString &value)
Sets value of the floor property.
void setBuildingId(const QString &value)
Sets value of the buildingId property.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
void append(QList< T > &&value)
void setValue(QVariant &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.