1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
// SPDX-FileCopyrightText: 2022 Cochise César <cochisecesar@zoho.com>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppGeolocItem.h"
#include "QXmppConstants_p.h"
#include "QXmppUtils.h"
#include <QDomElement>
#include <QXmlStreamWriter>
/// \cond
class QXmppGeolocItemPrivate : public QSharedData
{
public:
std::optional<double> accuracy;
QString country;
QString locality;
std::optional<double> latitude;
std::optional<double> longitude;
};
/// \endcond
///
/// \class QXmppGeolocItem
///
/// This class represents a PubSub item for \xep{0080, User Location}.
///
/// \since QXmpp 1.5
///
///
/// Default constructor
///
QXmppGeolocItem::QXmppGeolocItem()
: d(new QXmppGeolocItemPrivate)
{
}
/// Copy-constructor.
QXmppGeolocItem::QXmppGeolocItem(const QXmppGeolocItem &other) = default;
/// Move-constructor.
QXmppGeolocItem::QXmppGeolocItem(QXmppGeolocItem &&) = default;
QXmppGeolocItem::~QXmppGeolocItem() = default;
/// Assignment operator.
QXmppGeolocItem &QXmppGeolocItem::operator=(const QXmppGeolocItem &other) = default;
/// Move-assignment operator.
QXmppGeolocItem &QXmppGeolocItem::operator=(QXmppGeolocItem &&) = default;
///
/// Returns the horizontal GPS error in meters.
///
std::optional<double> QXmppGeolocItem::accuracy() const
{
return d->accuracy;
}
///
/// Sets the horizontal GPS error.
///
void QXmppGeolocItem::setAccuracy(std::optional<double> accuracy)
{
d->accuracy = std::move(accuracy);
}
///
/// Returns the country.
///
QString QXmppGeolocItem::country() const
{
return d->country;
}
///
/// Sets the country.
///
void QXmppGeolocItem::setCountry(QString country)
{
d->country = std::move(country);
}
///
/// Returns the latitude in decimal degrees.
///
std::optional<double> QXmppGeolocItem::latitude() const
{
return d->latitude;
}
///
/// Sets the latitude.
///
void QXmppGeolocItem::setLatitude(std::optional<double> lat)
{
if (lat && (*lat > 90 || *lat < -90)) {
d->latitude.reset();
return;
}
d->latitude = std::move(lat);
}
///
/// Returns the locality such as a town or a city.
///
QString QXmppGeolocItem::locality() const
{
return d->locality;
}
///
/// Sets the locality.
///
void QXmppGeolocItem::setLocality(QString locality)
{
d->locality = std::move(locality);
}
///
/// Returns the longitude in decimal degrees.
///
std::optional<double> QXmppGeolocItem::longitude() const
{
return d->longitude;
}
///
/// Sets the longitude.
///
void QXmppGeolocItem::setLongitude(std::optional<double> lon)
{
if (lon && (*lon > 180 || *lon < -180)) {
d->longitude.reset();
return;
}
d->longitude = std::move(lon);
}
///
/// Returns true, if the element is a valid \xep{0080}: User Location PubSub item.
///
bool QXmppGeolocItem::isItem(const QDomElement &itemElement)
{
auto isPayloadValid = [](const QDomElement &payload) -> bool {
return payload.tagName() == QStringLiteral("geoloc") &&
payload.namespaceURI() == ns_geoloc;
};
return QXmppPubSubBaseItem::isItem(itemElement, isPayloadValid);
}
/// \cond
std::optional<double> parseOptDouble(const QDomElement &element)
{
bool ok = false;
if (auto val = element.text().toDouble(&ok); ok) {
return val;
}
return {};
}
void QXmppGeolocItem::parsePayload(const QDomElement &tune)
{
for (auto child = tune.firstChildElement(); !child.isNull(); child = child.nextSiblingElement()) {
const auto tagName = child.tagName();
if (tagName == QStringLiteral("accuracy")) {
d->accuracy = parseOptDouble(child);
} else if (tagName == QStringLiteral("country")) {
d->country = child.text();
} else if (tagName == QStringLiteral("lat")) {
setLatitude(parseOptDouble(child));
} else if (tagName == QStringLiteral("locality")) {
d->locality = child.text();
} else if (tagName == QStringLiteral("lon")) {
setLongitude(parseOptDouble(child));
}
}
}
auto writeTextEl(QXmlStreamWriter *writer, const QString &name, const std::optional<double> &val)
{
if (val.has_value()) {
writer->writeTextElement(name, QString::number(*val));
}
}
auto writeTextEl(QXmlStreamWriter *writer, const QString &name, const QString &val)
{
helperToXmlAddTextElement(writer, name, val);
}
void QXmppGeolocItem::serializePayload(QXmlStreamWriter *writer) const
{
writer->writeStartElement(QStringLiteral("geoloc"));
writer->writeDefaultNamespace(ns_geoloc);
writeTextEl(writer, QStringLiteral("accuracy"), d->accuracy);
writeTextEl(writer, QStringLiteral("country"), d->country);
writeTextEl(writer, QStringLiteral("lat"), d->latitude);
writeTextEl(writer, QStringLiteral("locality"), d->locality);
writeTextEl(writer, QStringLiteral("lon"), d->longitude);
writer->writeEndElement();
}
/// \endcond
|