mirror of
https://bitbucket.org/smil3y/kde-workspace.git
synced 2025-02-23 10:22:49 +00:00
plasma: implement Mozilla Location Service provider for geolocation data engine
for reference: https://github.com/fluxer/katana/issues/26 Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
910a7588f9
commit
269e1224e9
4 changed files with 192 additions and 0 deletions
|
@ -68,3 +68,11 @@ kde4_add_plugin(plasma-geolocation-geoplugin ${plasma_geolocation_geoplugin_SRCS
|
|||
target_link_libraries(plasma-geolocation-geoplugin plasma-geolocation-interface)
|
||||
install(FILES plasma-geolocation-geoplugin.desktop DESTINATION ${KDE4_SERVICES_INSTALL_DIR})
|
||||
install(TARGETS plasma-geolocation-geoplugin DESTINATION ${KDE4_PLUGIN_INSTALL_DIR})
|
||||
|
||||
# -------------------------------------------------------------------------------------------------
|
||||
|
||||
set(plasma_geolocation_mozilla_SRCS location_mozilla.cpp)
|
||||
kde4_add_plugin(plasma-geolocation-mozilla ${plasma_geolocation_mozilla_SRCS})
|
||||
target_link_libraries(plasma-geolocation-mozilla plasma-geolocation-interface)
|
||||
install(FILES plasma-geolocation-mozilla.desktop DESTINATION ${KDE4_SERVICES_INSTALL_DIR})
|
||||
install(TARGETS plasma-geolocation-mozilla DESTINATION ${KDE4_PLUGIN_INSTALL_DIR})
|
||||
|
|
130
plasma/dataengines/geolocation/location_mozilla.cpp
Normal file
130
plasma/dataengines/geolocation/location_mozilla.cpp
Normal file
|
@ -0,0 +1,130 @@
|
|||
/* This file is part of the KDE project
|
||||
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License version 2, as published by the Free Software Foundation.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public License
|
||||
along with this library; see the file COPYING.LIB. If not, write to
|
||||
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "location_mozilla.h"
|
||||
|
||||
#include <KDebug>
|
||||
#include <KJob>
|
||||
#include <KIO/TransferJob>
|
||||
#include <QJsonDocument>
|
||||
|
||||
static const QString s_geolocateurl = QString::fromLatin1("https://location.services.mozilla.com/v1/geolocate?key=b9255e08-838f-4d4e-b270-bec2cc89719b");
|
||||
static const QString s_regionurl = QString::fromLatin1("https://location.services.mozilla.com/v1/country?key=b9255e08-838f-4d4e-b270-bec2cc89719b");
|
||||
|
||||
class Mozilla::Private
|
||||
{
|
||||
public:
|
||||
QByteArray payload;
|
||||
|
||||
void populateGeoDataEngineData()
|
||||
{
|
||||
const QJsonDocument jsondoc = QJsonDocument::fromJson(payload);
|
||||
const QVariantMap jsonmap = jsondoc.toVariant().toMap();
|
||||
const QVariantMap jsonlocationmap = jsonmap["location"].toMap();
|
||||
// for reference:
|
||||
// https://ichnaea.readthedocs.io/en/latest/api/geolocate.html
|
||||
outd["accuracy"] = jsonmap["accuracy"];
|
||||
outd["latitude"] = jsonlocationmap["lat"];
|
||||
outd["longitude"] = jsonlocationmap["lng"];
|
||||
//qDebug() << Q_FUNC_INFO << jsonmap << jsonlocationmap;
|
||||
}
|
||||
|
||||
void populateRegionDataEngineData()
|
||||
{
|
||||
const QJsonDocument jsondoc = QJsonDocument::fromJson(payload);
|
||||
const QVariantMap jsonmap = jsondoc.toVariant().toMap();
|
||||
// for reference:
|
||||
// https://ichnaea.readthedocs.io/en/latest/api/region.html
|
||||
outd["country code"] = jsonmap["country_code"];
|
||||
outd["country"] = jsonmap["country_name"];
|
||||
// qDebug() << Q_FUNC_INFO << jsonmap;
|
||||
}
|
||||
|
||||
Plasma::DataEngine::Data outd;
|
||||
};
|
||||
|
||||
Mozilla::Mozilla(QObject* parent, const QVariantList& args)
|
||||
: GeolocationProvider(parent, args), d(new Private())
|
||||
{
|
||||
setUpdateTriggers(SourceEvent | NetworkConnected);
|
||||
}
|
||||
|
||||
Mozilla::~Mozilla()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void Mozilla::update()
|
||||
{
|
||||
d->outd.clear();
|
||||
d->payload.clear();
|
||||
KIO::TransferJob *datajob = KIO::get(KUrl(s_geolocateurl), KIO::NoReload, KIO::HideProgressInfo);
|
||||
if (datajob) {
|
||||
kDebug() << "Fetching" << s_geolocateurl;
|
||||
connect(datajob, SIGNAL(data(KIO::Job*,QByteArray)), this, SLOT(readData(KIO::Job*,QByteArray)));
|
||||
connect(datajob, SIGNAL(result(KJob*)), this, SLOT(resultGeo(KJob*)));
|
||||
} else {
|
||||
kDebug() << "Could not create geo job";
|
||||
}
|
||||
}
|
||||
|
||||
void Mozilla::readData(KIO::Job* job, const QByteArray &data)
|
||||
{
|
||||
Q_UNUSED(job)
|
||||
|
||||
if (data.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
d->payload.append(data);
|
||||
}
|
||||
|
||||
void Mozilla::resultGeo(KJob* job)
|
||||
{
|
||||
if (job && !job->error()) {
|
||||
d->populateGeoDataEngineData();
|
||||
} else {
|
||||
kDebug() << "error" << job->errorString();
|
||||
}
|
||||
|
||||
d->payload.clear();
|
||||
KIO::TransferJob *datajob = KIO::get(KUrl(s_regionurl), KIO::NoReload, KIO::HideProgressInfo);
|
||||
if (datajob) {
|
||||
kDebug() << "Fetching" << s_regionurl;
|
||||
connect(datajob, SIGNAL(data(KIO::Job*,QByteArray)), this, SLOT(readData(KIO::Job*,QByteArray)));
|
||||
connect(datajob, SIGNAL(result(KJob*)), this, SLOT(resultRegion(KJob*)));
|
||||
} else {
|
||||
kDebug() << "Could not create region job";
|
||||
setData(d->outd);
|
||||
// qDebug() << Q_FUNC_INFO << d->outd;
|
||||
}
|
||||
}
|
||||
|
||||
void Mozilla::resultRegion(KJob* job)
|
||||
{
|
||||
if (job && !job->error()) {
|
||||
d->populateRegionDataEngineData();
|
||||
} else {
|
||||
kDebug() << "error" << job->errorString();
|
||||
}
|
||||
setData(d->outd);
|
||||
// qDebug() << Q_FUNC_INFO << d->outd;
|
||||
}
|
||||
|
||||
K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(mozilla, Mozilla)
|
||||
|
||||
#include "moc_location_mozilla.cpp"
|
45
plasma/dataengines/geolocation/location_mozilla.h
Normal file
45
plasma/dataengines/geolocation/location_mozilla.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/* This file is part of the KDE project
|
||||
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License version 2, as published by the Free Software Foundation.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public License
|
||||
along with this library; see the file COPYING.LIB. If not, write to
|
||||
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef MOZILLA_H
|
||||
#define MOZILLA_H
|
||||
|
||||
#include "geolocationprovider.h"
|
||||
|
||||
#include <KIO/Job>
|
||||
|
||||
class Mozilla : public GeolocationProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Mozilla(QObject *parent = 0, const QVariantList &args = QVariantList());
|
||||
~Mozilla();
|
||||
|
||||
virtual void update();
|
||||
|
||||
protected slots:
|
||||
void readData(KIO::Job *, const QByteArray &data);
|
||||
void resultGeo(KJob* job);
|
||||
void resultRegion(KJob* job);
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *const d;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,9 @@
|
|||
[Desktop Entry]
|
||||
Name=Geolocation Mozilla Location Service
|
||||
Comment=Geolocation from IP address via Mozilla Location Service.
|
||||
X-KDE-ServiceTypes=Plasma/GeolocationProvider
|
||||
X-KDE-ParentApp=geolocation
|
||||
Type=Service
|
||||
Icon=applications-internet
|
||||
X-KDE-Library=plasma-geolocation-mozilla
|
||||
X-KDE-PluginInfo-Name=mozilla
|
Loading…
Add table
Reference in a new issue