KGuiAddons

kgeourihandler.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "kgeourihandler_p.h"
7
8#include <QUrl>
9#include <QUrlQuery>
10
11void KGeoUriHandler::setCoordinateTemplate(const QString &coordTmpl)
12{
13 m_coordTmpl = coordTmpl;
14}
15
16void KGeoUriHandler::setQueryTemplate(const QString &queryTmpl)
17{
18 m_queryTmpl = queryTmpl;
19}
20
21void KGeoUriHandler::setFallbackUrl(const QString &fallbackUrl)
22{
23 m_fallbackUrl = fallbackUrl;
24}
25
26static bool isValidCoordinate(double c, double limit)
27{
28 return c != 0.0 && c >= -limit && c <= limit;
29}
30
31QString KGeoUriHandler::handleUri(const QUrl &geoUri)
32{
33 const auto pathElems = geoUri.path().split(QLatin1Char(';'));
34 const auto coordElems = pathElems.isEmpty() ? QStringList() : pathElems.at(0).split(QLatin1Char(','));
35
36 const auto lat = coordElems.size() < 2 ? 0.0 : coordElems.at(0).toDouble();
37 const auto lon = coordElems.size() < 2 ? 0.0 : coordElems.at(1).toDouble();
38
39 const auto geoQuery = QUrlQuery(geoUri.query());
40 const auto query = geoQuery.queryItemValue(QStringLiteral("q"));
41
42 bool zoomValid = false;
43 int zoom = geoQuery.queryItemValue(QStringLiteral("z")).toInt(&zoomValid);
44 if (!zoomValid || zoom < 0 || zoom > 21) {
45 zoom = 18;
46 }
47
48 // unsupported coordinate reference system
49 if (!pathElems.isEmpty() && std::any_of(pathElems.begin() + 1, pathElems.end(), [](const auto &elem) {
50 return elem.startsWith(QLatin1String("crs="), Qt::CaseInsensitive) && !elem.endsWith(QLatin1String("=wgs84"), Qt::CaseInsensitive);
51 })) {
52 return m_fallbackUrl;
53 }
54
55 QString tmpl;
56 if (!query.isEmpty()) {
57 tmpl = m_queryTmpl;
58 } else if (isValidCoordinate(lat, 90.0) && isValidCoordinate(lon, 180.0)) {
59 tmpl = m_coordTmpl;
60 } else {
61 return m_fallbackUrl;
62 }
63
64 tmpl.replace(QLatin1String("<LAT>"), QString::number(lat));
65 tmpl.replace(QLatin1String("<LON>"), QString::number(lon));
66 tmpl.replace(QLatin1String("<Q>"), query);
67 tmpl.replace(QLatin1String("<Z>"), QString::number(zoom));
68 return tmpl;
69}
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QUrl geoUri(const QVariant &location)
QAction * zoom(const QObject *recvr, const char *slot, QObject *parent)
bool isEmpty() const const
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString path(ComponentFormattingOptions options) const const
QString query(ComponentFormattingOptions options) const const
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.