Commit 99eff1da authored by Volker Krause's avatar Volker Krause
Browse files

Add methods to obtain geo: URIs for place-like objects

This is code we have duplicated in some form in at least Itinerary and
the KMail itinerary plugin.
parent ae854ea6
Pipeline #128151 passed with stages
in 1 minute and 35 seconds
......@@ -129,6 +129,27 @@ private Q_SLOTS:
QCOMPARE(LocationUtil::isSameLocation(lhs, rhs, LocationUtil::Exact), exactEqual);
QCOMPARE(LocationUtil::isSameLocation(rhs, lhs, LocationUtil::Exact), exactEqual);
}
void testGeoUri()
{
Place p;
QVERIFY(LocationUtil::geoUri(p).isEmpty());
GeoCoordinates coord(45.5137, 9.21139);
p.setGeo(coord);
QCOMPARE(LocationUtil::geoUri(p), QUrl(QStringLiteral("geo:45.5137,9.21139")));
QCOMPARE(p.geoUri(), QUrl(QStringLiteral("geo:45.5137,9.21139")));
PostalAddress addr;
addr.setStreetAddress(QStringLiteral("Piazza della Scienza"));
addr.setAddressLocality(QStringLiteral("Milan"));
addr.setAddressCountry(QStringLiteral("IT"));
p.setAddress(addr);
QCOMPARE(LocationUtil::geoUri(p), QUrl(QStringLiteral("geo:45.5137,9.21139")));
p.setGeo({});
QCOMPARE(LocationUtil::geoUri(p), QUrl(QStringLiteral("geo:0,0?q=Piazza della Scienza, Milan, IT")));
}
};
QTEST_APPLESS_MAIN(LocationUtilTest)
......
......@@ -6,6 +6,7 @@
#include "organization.h"
#include "datatypes_p.h"
#include "locationutil.h"
#include <QUrl>
......@@ -44,6 +45,11 @@ KITINERARY_MAKE_PROPERTY(Organization, KItinerary::GeoCoordinates, geo, setGeo)
KITINERARY_MAKE_PROPERTY(Organization, QVariantList, potentialAction, setPotentialAction)
KITINERARY_MAKE_OPERATOR(Organization)
QUrl Organization::geoUri() const
{
return LocationUtil::geoUri(*this);
}
class AirlinePrivate : public OrganizationPrivate
{
KITINERARY_PRIVATE_GADGET(Airline)
......
......@@ -41,6 +41,13 @@ class KITINERARY_EXPORT Organization
KITINERARY_PROPERTY(KItinerary::PostalAddress, address, setAddress)
KITINERARY_PROPERTY(KItinerary::GeoCoordinates, geo, setGeo)
KITINERARY_PROPERTY(QVariantList, potentialAction, setPotentialAction)
/** @see LocationUtil::geoUri() */
Q_PROPERTY(QUrl geoUri READ geoUri STORED false)
public:
QUrl geoUri() const;
protected:
///@cond internal
QExplicitlySharedDataPointer<OrganizationPrivate> d;
......
......@@ -6,6 +6,7 @@
#include "place.h"
#include "datatypes_p.h"
#include "locationutil.h"
#include <cmath>
......@@ -91,6 +92,11 @@ KITINERARY_MAKE_PROPERTY(Place, QString, telephone, setTelephone)
KITINERARY_MAKE_PROPERTY(Place, QString, identifier, setIdentifier)
KITINERARY_MAKE_OPERATOR(Place)
QUrl Place::geoUri() const
{
return LocationUtil::geoUri(*this);
}
class AirportPrivate : public PlacePrivate
{
......
......@@ -10,6 +10,7 @@
#include "datatypes.h"
#include <QUrl>
namespace KItinerary {
......@@ -82,6 +83,13 @@ class KITINERARY_EXPORT Place
* @see http://schema.org/docs/datamodel.html#identifierBg
*/
KITINERARY_PROPERTY(QString, identifier, setIdentifier)
/** @see LocationUtil::geoUri() */
Q_PROPERTY(QUrl geoUri READ geoUri STORED false)
public:
QUrl geoUri() const;
protected:
///@cond internal
QExplicitlySharedDataPointer<PlacePrivate> d;
......
......@@ -16,6 +16,8 @@
#include <KItinerary/Visit>
#include <QDebug>
#include <QUrl>
#include <QUrlQuery>
#include <cmath>
......@@ -292,3 +294,40 @@ bool LocationUtil::isSameLocation(const QVariant &lhs, const QVariant &rhs, Loca
return isSameLocationName(name(lhs), name(rhs), accuracy);
}
QUrl LocationUtil::geoUri(const QVariant &location)
{
QUrl url;
url.setScheme(QStringLiteral("geo"));
const auto geo = LocationUtil::geo(location);
if (geo.isValid()) {
url.setPath(QString::number(geo.latitude()) + QLatin1Char(',') + QString::number(geo.longitude()));
return url;
}
const auto addr = LocationUtil::address(location);
if (!addr.isEmpty()) {
url.setPath(QStringLiteral("0,0"));
QStringList q;
if (!addr.streetAddress().isEmpty()) {
q.push_back(addr.streetAddress());
}
if (!addr.addressLocality().isEmpty()) {
q.push_back(addr.postalCode().isEmpty() ? addr.addressLocality() : (addr.postalCode() + QLatin1Char(' ') + addr.addressLocality()));
}
if (!addr.addressRegion().isEmpty()) {
q.push_back(addr.addressRegion());
}
if (!addr.addressCountry().isEmpty()) {
q.push_back(addr.addressCountry());
}
QUrlQuery query;
query.addQueryItem(QStringLiteral("q"), q.join(QLatin1String(", ")));
url.setQuery(query);
return url;
}
return {};
}
......@@ -9,6 +9,7 @@
#include "kitinerary_export.h"
class QString;
class QUrl;
class QVariant;
namespace KItinerary {
......@@ -69,6 +70,9 @@ enum Accuracy {
*/
bool KITINERARY_EXPORT isSameLocation(const QVariant &lhs, const QVariant &rhs, Accuracy accuracy = Exact);
/** Returns a geo: URI for the given location. */
QUrl KITINERARY_EXPORT geoUri(const QVariant &location);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment