1 /*
2  * geolocation.cpp
3  * Copyright (C) 2006  Remko Troncon
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #include <QDomDocument>
22 #include <QDomElement>
23 #include <QApplication>
24 
25 #include "geolocation.h"
26 
GeoLocation()27 GeoLocation::GeoLocation()
28 {
29 }
30 
GeoLocation(const QDomElement & el)31 GeoLocation::GeoLocation(const QDomElement& el)
32 {
33 	fromXml(el);
34 }
35 
toXml(QDomDocument & doc)36 QDomElement GeoLocation::toXml(QDomDocument& doc)
37 {
38 	QDomElement geoloc = doc.createElement(PEP_GEOLOC_TN);
39 	geoloc.setAttribute("xmlns", PEP_GEOLOC_NS);
40 
41 	if (alt_.hasValue()) {
42 		QDomElement e = doc.createElement("alt");
43 		e.appendChild(doc.createTextNode(QString::number(alt_.value())));
44 		geoloc.appendChild(e);
45 	}
46 	if (bearing_.hasValue()) {
47 		QDomElement e = doc.createElement("bearing");
48 		e.appendChild(doc.createTextNode(QString::number(bearing_.value())));
49 		geoloc.appendChild(e);
50 	}
51 	if (error_.hasValue()) {
52 		QDomElement e = doc.createElement("error");
53 		e.appendChild(doc.createTextNode(QString::number(error_.value())));
54 		geoloc.appendChild(e);
55 	}
56 	if (lat_.hasValue()) {
57 		QDomElement e = doc.createElement("lat");
58 		e.appendChild(doc.createTextNode(QString::number(lat_.value())));
59 		geoloc.appendChild(e);
60 	}
61 	if (lon_.hasValue()) {
62 		QDomElement e = doc.createElement("lon");
63 		e.appendChild(doc.createTextNode(QString::number(lon_.value())));
64 		geoloc.appendChild(e);
65 	}
66 	if (!datum_.isEmpty()) {
67 		QDomElement e = doc.createElement("datum");
68 		e.appendChild(doc.createTextNode(datum_));
69 		geoloc.appendChild(e);
70 	}
71 	if (!description_.isEmpty()) {
72 		QDomElement e = doc.createElement("description");
73 		e.appendChild(doc.createTextNode(description_));
74 		geoloc.appendChild(e);
75 	}
76 	if (!country_.isEmpty()) {
77 		QDomElement e = doc.createElement("country");
78 		e.appendChild(doc.createTextNode(country_));
79 		geoloc.appendChild(e);
80 	}
81 	if (!region_.isEmpty()) {
82 		QDomElement e = doc.createElement("region");
83 		e.appendChild(doc.createTextNode(region_));
84 		geoloc.appendChild(e);
85 	}
86 	if (!locality_.isEmpty()) {
87 		QDomElement e = doc.createElement("locality");
88 		e.appendChild(doc.createTextNode(locality_));
89 		geoloc.appendChild(e);
90 	}
91 	if (!area_.isEmpty()) {
92 		QDomElement e = doc.createElement("area");
93 		e.appendChild(doc.createTextNode(area_));
94 		geoloc.appendChild(e);
95 	}
96 	if (!street_.isEmpty()) {
97 		QDomElement e = doc.createElement("street");
98 		e.appendChild(doc.createTextNode(street_));
99 		geoloc.appendChild(e);
100 	}
101 	if (!building_.isEmpty()) {
102 		QDomElement e = doc.createElement("building");
103 		e.appendChild(doc.createTextNode(building_));
104 		geoloc.appendChild(e);
105 	}
106 	if (!floor_.isEmpty()) {
107 		QDomElement e = doc.createElement("floor");
108 		e.appendChild(doc.createTextNode(floor_));
109 		geoloc.appendChild(e);
110 	}
111 	if (!room_.isEmpty()) {
112 		QDomElement e = doc.createElement("room");
113 		e.appendChild(doc.createTextNode(room_));
114 		geoloc.appendChild(e);
115 	}
116 	if (!postalcode_.isEmpty()) {
117 		QDomElement e = doc.createElement("postalcode");
118 		e.appendChild(doc.createTextNode(postalcode_));
119 		geoloc.appendChild(e);
120 	}
121 	if (!text_.isEmpty()) {
122 		QDomElement e = doc.createElement("text");
123 		e.appendChild(doc.createTextNode(text_));
124 		geoloc.appendChild(e);
125 	}
126 
127 	return geoloc;
128 }
129 
fromXml(const QDomElement & e)130 void GeoLocation::fromXml(const QDomElement& e)
131 {
132 	if (e.tagName() != PEP_GEOLOC_TN)
133 		return;
134 
135 	for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
136 		QDomElement m = n.toElement();
137 		if (m.isNull()) {
138 			continue;
139 		}
140 		if (m.tagName() == "alt")
141 			alt_ = Maybe<float>(m.text().toFloat());
142 		else if (m.tagName() == "bearing")
143 			bearing_ = Maybe<float>(m.text().toFloat());
144 		else if (m.tagName() == "error")
145 			error_ = Maybe<float>(m.text().toFloat());
146 		else if (m.tagName() == "lat")
147 			lat_ = Maybe<float>(m.text().toFloat());
148 		else if (m.tagName() == "lon")
149 			lon_ = Maybe<float>(m.text().toFloat());
150 		else if (m.tagName() == "datum")
151 			datum_ = m.text();
152 		else if (m.tagName() == "description")
153 			description_ = m.text();
154 		else if (m.tagName() == "country")
155 			country_ = m.text();
156 		else if (m.tagName() == "region")
157 			region_ = m.text();
158 		else if (m.tagName() == "locality")
159 			locality_ = m.text();
160 		else if (m.tagName() == "area")
161 			area_ = m.text();
162 		else if (m.tagName() == "street")
163 			street_ = m.text();
164 		else if (m.tagName() == "building")
165 			building_ = m.text();
166 		else if (m.tagName() == "floor")
167 			floor_ = m.text();
168 		else if (m.tagName() == "room")
169 			room_ = m.text();
170 		else if (m.tagName() == "postalcode")
171 			postalcode_ = m.text();
172 		else if (m.tagName() == "text")
173 			text_ = m.text();
174 	}
175 }
176 
setAlt(float alt)177 void GeoLocation::setAlt(float alt)
178 {
179 	alt_ = Maybe<float>(alt);
180 }
setBearing(float bearing)181 void GeoLocation::setBearing(float bearing)
182 {
183 	bearing_ = Maybe<float>(bearing);
184 }
185 
setError(float error)186 void GeoLocation::setError(float error)
187 {
188 	error_ = Maybe<float>(error);
189 }
190 
setLat(float lat)191 void GeoLocation::setLat(float lat)
192 {
193 	lat_ = Maybe<float>(lat);
194 }
195 
setLon(float lon)196 void GeoLocation::setLon(float lon)
197 {
198 	lon_ = Maybe<float>(lon);
199 }
200 
setDatum(const QString & datum)201 void GeoLocation::setDatum(const QString& datum)
202 {
203 	datum_ = datum;
204 }
205 
setDescription(const QString & description)206 void GeoLocation::setDescription(const QString& description)
207 {
208 	description_ = description;
209 }
210 
alt() const211 const Maybe<float>& GeoLocation::alt() const
212 {
213 	return alt_;
214 }
215 
bearing() const216 const Maybe<float>& GeoLocation::bearing() const
217 {
218 	return bearing_;
219 }
220 
error() const221 const Maybe<float>& GeoLocation::error() const
222 {
223 	return error_;
224 }
225 
lat() const226 const Maybe<float>& GeoLocation::lat() const
227 {
228 	return lat_;
229 }
230 
lon() const231 const Maybe<float>& GeoLocation::lon() const
232 {
233 	return lon_;
234 }
235 
datum() const236 const QString& GeoLocation::datum() const
237 {
238 	return datum_;
239 }
240 
description() const241 const QString& GeoLocation::description() const
242 {
243 	return description_;
244 }
245 
country() const246 const QString& GeoLocation::country() const
247 {
248 	return country_;
249 }
250 
region() const251 const QString& GeoLocation::region() const
252 {
253 	return region_;
254 }
255 
locality() const256 const QString& GeoLocation::locality() const
257 {
258 	return locality_;
259 }
260 
area() const261 const QString& GeoLocation::area() const
262 {
263 	return area_;
264 }
265 
street() const266 const QString& GeoLocation::street() const
267 {
268 	return street_;
269 }
270 
building() const271 const QString& GeoLocation::building() const
272 {
273 	return building_;
274 }
275 
floor() const276 const QString& GeoLocation::floor() const
277 {
278 	return floor_;
279 }
280 
room() const281 const QString& GeoLocation::room() const
282 {
283 	return room_;
284 }
285 
postalcode() const286 const QString& GeoLocation::postalcode() const
287 {
288 	return postalcode_;
289 }
290 
text() const291 const QString& GeoLocation::text() const
292 {
293 	return text_;
294 }
295 
isNull() const296 bool GeoLocation::isNull() const
297 {
298 	return !lat_.hasValue() && !lon_.hasValue() && !alt_.hasValue() && !bearing_.hasValue() && !error_.hasValue() && country_.isNull() && region_.isNull() && locality_.isNull()
299 		&& area_.isNull() && street_.isNull() && building_.isNull() && floor_.isNull() && room_.isNull() && postalcode_.isNull() && text_.isNull() && description_.isNull() && datum_.isNull();
300 }
301 
operator ==(const GeoLocation & o) const302 bool GeoLocation::operator==(const GeoLocation& o) const
303 {
304 	// FIXME
305 	bool equal = true;
306 	equal = equal && (lat_.hasValue() ? lat_.value() == o.lat().value() : !o.lat().hasValue());
307 	equal = equal && (lon_.hasValue() ? lon_.value() == o.lon().value() : !o.lon().hasValue());
308 	equal = equal && (alt_.hasValue() ? alt_.value() == o.alt().value() : !o.alt().hasValue());
309 	equal = equal && (bearing_.hasValue() ? bearing_.value() == o.bearing().value() : !o.bearing().hasValue());
310 	equal = equal && (error_.hasValue() ? error_.value() == o.error().value() : !o.error().hasValue());
311 	equal = equal && country() == o.country() && region() == o.region() && locality() == o.locality()
312 			&& area() == o.area() && street() == o.street() && datum() == o.datum() && building() == o.building()
313 			&& floor() == o.floor() && room() == o.room() && postalcode() == o.postalcode() && text() == o.text()
314 			&& description() == o.description();
315 	return equal;
316 }
317 
operator !=(const GeoLocation & o) const318 bool GeoLocation::operator!=(const GeoLocation& o) const
319 {
320 	return !((*this) == o);
321 }
322 
setCountry(const QString & s)323 void GeoLocation::setCountry(const QString& s)
324 {
325 	country_ = s;
326 }
327 
setRegion(const QString & s)328 void GeoLocation::setRegion(const QString& s)
329 {
330 	region_ = s;
331 }
332 
setLocality(const QString & s)333 void GeoLocation::setLocality(const QString& s)
334 {
335 	locality_ = s;
336 }
337 
setArea(const QString & s)338 void GeoLocation::setArea(const QString& s)
339 {
340 	area_ = s;
341 }
342 
setStreet(const QString & s)343 void GeoLocation::setStreet(const QString& s)
344 {
345 	street_ = s;
346 }
347 
setBuilding(const QString & s)348 void GeoLocation::setBuilding(const QString& s)
349 {
350 	building_ = s;
351 }
352 
setFloor(const QString & s)353 void GeoLocation::setFloor(const QString& s)
354 {
355 	floor_ = s;
356 }
357 
setRoom(const QString & s)358 void GeoLocation::setRoom(const QString& s)
359 {
360 	room_ = s;
361 }
362 
setPostalcode(const QString & s)363 void GeoLocation::setPostalcode(const QString& s)
364 {
365 	postalcode_ = s;
366 }
367 
setText(const QString & s)368 void GeoLocation::setText(const QString& s)
369 {
370 	text_ = s;
371 }
372 
toString() const373 QString GeoLocation::toString() const
374 {
375 	QString loc;
376 
377 	if(alt_.hasValue() || lon_.hasValue() || lat_.hasValue()) {
378 
379 		loc += QObject::tr("Latitude/Longitude/Altitude: ");
380 
381 		if(lat_.hasValue())
382 			loc += QString::number(lat_.value()) + "/";
383 		else
384 			loc += "0/";
385 
386 		if(lon_.hasValue())
387 			loc += QString::number(lon_.value()) + "/";
388 		else
389 			loc += "0/";
390 
391 		if(alt_.hasValue())
392 			loc += QString::number(alt_.value());
393 		else
394 			loc += "0";
395 
396 	}
397 
398 	if(bearing_.hasValue())
399 		loc += QObject::tr("\nBearing: ") + QString::number(bearing_.value());
400 
401 	if(error_.hasValue())
402 		loc += QObject::tr("\nError: ") + QString::number(error_.value());
403 
404 	if (!datum().isEmpty())
405 		loc += QObject::tr("\nDatum: ") + datum();
406 
407 	if (!country().isEmpty())
408 		loc += QObject::tr("\nCountry: ") + country();
409 
410 	if (!postalcode().isEmpty())
411 		loc += QObject::tr("\nPostalcode: ") + postalcode();
412 
413 	if (!region().isEmpty())
414 		loc += QObject::tr("\nRegion: ") + region();
415 
416 	if (!locality().isEmpty())
417 		loc += QObject::tr("\nLocality: ") + locality();
418 
419 	if (!area().isEmpty())
420 		loc += QObject::tr("\nArea: ") + area();
421 
422 	if (!street().isEmpty())
423 		loc += QObject::tr("\nStreet: ") + street();
424 
425 	if (!building().isEmpty() && (!area().isEmpty() || !street().isEmpty()))
426 		loc += ", " + building();
427 
428 	if (!floor().isEmpty())
429 		loc += QObject::tr("\nFloor: ") + floor();
430 
431 	if (!room().isEmpty())
432 		loc += QObject::tr("\nRoom: ") + room();
433 
434 	if (!description().isEmpty())
435 		loc += QObject::tr("\nDescription: ") + description();
436 
437 	if (!text().isEmpty())
438 		loc += "\n" + text();
439 
440 	return loc;
441 }
442