2022-04-25 10:21:41 +03:00
|
|
|
/* 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_geoplugin.h"
|
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
#include <KJob>
|
2023-07-15 03:04:05 +03:00
|
|
|
#include <KIO/StoredTransferJob>
|
2022-04-25 10:21:41 +03:00
|
|
|
#include <QJsonDocument>
|
|
|
|
|
|
|
|
geoPlugin::geoPlugin(QObject* parent, const QVariantList& args)
|
2023-07-15 03:04:05 +03:00
|
|
|
: GeolocationProvider(parent, args)
|
2022-04-25 10:21:41 +03:00
|
|
|
{
|
2023-07-15 09:30:24 +03:00
|
|
|
setObjectName("location_geoplugin");
|
2022-04-25 10:21:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void geoPlugin::update()
|
|
|
|
{
|
2023-07-15 03:04:05 +03:00
|
|
|
kDebug() << "Fetching http://www.geoplugin.net/json.gp";
|
|
|
|
KIO::StoredTransferJob *datajob = KIO::storedGet(
|
|
|
|
KUrl("http://www.geoplugin.net/json.gp"),
|
|
|
|
KIO::NoReload, KIO::HideProgressInfo
|
|
|
|
);
|
|
|
|
datajob->setAutoDelete(false);
|
|
|
|
connect(datajob, SIGNAL(result(KJob*)), this, SLOT(result(KJob*)));
|
2022-04-25 10:21:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void geoPlugin::result(KJob* job)
|
|
|
|
{
|
|
|
|
Plasma::DataEngine::Data outd;
|
|
|
|
|
2023-07-15 03:04:05 +03:00
|
|
|
if (job->error() == KJob::NoError) {
|
|
|
|
KIO::StoredTransferJob *datajob = qobject_cast<KIO::StoredTransferJob*>(job);
|
|
|
|
const QJsonDocument jsondoc = QJsonDocument::fromJson(datajob->data());
|
|
|
|
const QVariantMap jsonmap = jsondoc.toVariant().toMap();
|
|
|
|
// for reference:
|
|
|
|
// http://www.geoplugin.com/quickstart
|
2023-07-15 03:18:35 +03:00
|
|
|
outd["accuracy"] = jsonmap["geoplugin_locationAccuracyRadius"];
|
|
|
|
outd["country"] = jsonmap["geoplugin_countryName"];
|
2023-07-15 03:04:05 +03:00
|
|
|
outd["country code"] = jsonmap["geoplugin_countryCode"];
|
|
|
|
outd["city"] = jsonmap["geoplugin_city"];
|
|
|
|
outd["latitude"] = jsonmap["geoplugin_latitude"];
|
|
|
|
outd["longitude"] = jsonmap["geoplugin_longitude"];
|
|
|
|
outd["ip"] = jsonmap["geoplugin_request"];
|
|
|
|
// qDebug() << Q_FUNC_INFO << outd;
|
2022-04-25 10:21:41 +03:00
|
|
|
} else {
|
2023-07-15 03:04:05 +03:00
|
|
|
kWarning() << "geoplugin job error" << job->errorString();
|
2022-04-25 10:21:41 +03:00
|
|
|
}
|
2023-07-15 03:04:05 +03:00
|
|
|
job->deleteLater();
|
2022-04-25 10:21:41 +03:00
|
|
|
|
|
|
|
setData(outd);
|
|
|
|
}
|
|
|
|
|
|
|
|
K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(geoplugin, geoPlugin)
|
|
|
|
|
|
|
|
#include "moc_location_geoplugin.cpp"
|