[dataengines/geolocation] Use wifi data if available

Adds a dependency to NetworkManager and queries the available wifi
access points to include in the request to the location service.

REVIEW: 122736
wilder-5.14
Martin Gräßlin 11 years ago
parent 8787afce5d
commit 78c97f5b42
  1. 2
      CMakeLists.txt
  2. 3
      dataengines/geolocation/CMakeLists.txt
  3. 15
      dataengines/geolocation/geolocation.cpp
  4. 3
      dataengines/geolocation/geolocation.h
  5. 41
      dataengines/geolocation/location_ip.cpp

@ -13,7 +13,7 @@ set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR})
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
Plasma DocTools Runner JsEmbed NotifyConfig Su NewStuff Wallet KCMUtils Plasma DocTools Runner JsEmbed NotifyConfig Su NewStuff Wallet KCMUtils
IdleTime Declarative TextWidgets KDELibs4Support Crash GlobalAccel) IdleTime Declarative TextWidgets KDELibs4Support Crash GlobalAccel NetworkManagerQt)
find_package(KF5XmlRpcClient REQUIRED) find_package(KF5XmlRpcClient REQUIRED)
# WARNING PlasmaQuick provides unversioned CMake config # WARNING PlasmaQuick provides unversioned CMake config

@ -31,6 +31,7 @@ target_link_libraries(plasma_engine_geolocation
KF5::Plasma KF5::Plasma
KF5::CoreAddons KF5::CoreAddons
KF5::KIOCore KF5::KIOCore
KF5::NetworkManagerQt
KF5::Service KF5::Service
KF5::Solid) KF5::Solid)
kcoreaddons_desktop_to_json(plasma_engine_geolocation plasma-dataengine-geolocation.desktop) kcoreaddons_desktop_to_json(plasma_engine_geolocation plasma-dataengine-geolocation.desktop)
@ -43,7 +44,7 @@ install(FILES plasma-geolocationprovider.desktop DESTINATION ${KDE_INSTALL_KSERV
set(plasma_geolocation_ip_SRCS location_ip.cpp) set(plasma_geolocation_ip_SRCS location_ip.cpp)
add_library(plasma-geolocation-ip MODULE ${plasma_geolocation_ip_SRCS}) add_library(plasma-geolocation-ip MODULE ${plasma_geolocation_ip_SRCS})
target_link_libraries(plasma-geolocation-ip plasma-geolocation-interface KF5::KIOCore) target_link_libraries(plasma-geolocation-ip plasma-geolocation-interface KF5::KIOCore KF5::NetworkManagerQt)
install(FILES plasma-geolocation-ip.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) install(FILES plasma-geolocation-ip.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR})
install(TARGETS plasma-geolocation-ip DESTINATION ${KDE_INSTALL_PLUGINDIR}) install(TARGETS plasma-geolocation-ip DESTINATION ${KDE_INSTALL_PLUGINDIR})

@ -22,20 +22,27 @@
#include <QDebug> #include <QDebug>
#include <QNetworkConfigurationManager> #include <QNetworkConfigurationManager>
#include <KServiceTypeTrader> #include <KServiceTypeTrader>
#include <NetworkManagerQt/Manager>
static const char SOURCE[] = "location"; static const char SOURCE[] = "location";
Geolocation::Geolocation(QObject* parent, const QVariantList& args) Geolocation::Geolocation(QObject* parent, const QVariantList& args)
: Plasma::DataEngine(parent, args) : Plasma::DataEngine(parent, args)
, m_networkManager(new QNetworkConfigurationManager(this))
{ {
Q_UNUSED(args) Q_UNUSED(args)
setMinimumPollingInterval(500); setMinimumPollingInterval(500);
connect(m_networkManager, SIGNAL(onlineStateChanged(bool)), connect(NetworkManager::notifier(), &NetworkManager::Notifier::networkingEnabledChanged, this, &Geolocation::networkStatusChanged);
this, SLOT(networkStatusChanged(bool))); connect(NetworkManager::notifier(), &NetworkManager::Notifier::wirelessEnabledChanged, this, &Geolocation::networkStatusChanged);
m_updateTimer.setInterval(100); m_updateTimer.setInterval(100);
m_updateTimer.setSingleShot(true); m_updateTimer.setSingleShot(true);
connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(actuallySetData())); connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(actuallySetData()));
m_networkChangedTimer.setInterval(100);
m_networkChangedTimer.setSingleShot(true);
connect(&m_networkChangedTimer, &QTimer::timeout, this,
[this] {
updatePlugins(GeolocationProvider::NetworkConnected);
}
);
init(); init();
} }
@ -111,7 +118,7 @@ void Geolocation::networkStatusChanged(bool isOnline)
{ {
qDebug() << "network status changed"; qDebug() << "network status changed";
if (isOnline) { if (isOnline) {
updatePlugins(GeolocationProvider::NetworkConnected); m_networkChangedTimer.start();
} }
} }

@ -25,7 +25,6 @@
#include "geolocationprovider.h" #include "geolocationprovider.h"
class GeolocationProvider; class GeolocationProvider;
class QNetworkConfigurationManager;
class Geolocation : public Plasma::DataEngine class Geolocation : public Plasma::DataEngine
{ {
@ -51,9 +50,9 @@ class Geolocation : public Plasma::DataEngine
private: private:
Data m_data; Data m_data;
EntryAccuracy m_accuracy; EntryAccuracy m_accuracy;
QNetworkConfigurationManager *m_networkManager;
QList<GeolocationProvider *> m_plugins; QList<GeolocationProvider *> m_plugins;
QTimer m_updateTimer; QTimer m_updateTimer;
QTimer m_networkChangedTimer;
}; };
#endif #endif

@ -27,6 +27,9 @@
#include <KJob> #include <KJob>
#include <KIO/Job> #include <KIO/Job>
#include <KIO/TransferJob> #include <KIO/TransferJob>
#include <KSharedConfig>
#include <NetworkManagerQt/Manager>
#include <NetworkManagerQt/WirelessDevice>
class Ip::Private : public QObject { class Ip::Private : public QObject {
Q_OBJECT Q_OBJECT
@ -131,11 +134,45 @@ Ip::~Ip()
delete d; delete d;
} }
static QJsonArray accessPoints()
{
QJsonArray wifiAccessPoints;
const KConfigGroup config = KSharedConfig::openConfig()->group(QStringLiteral("org.kde.plasma.geolocation.ip"));
if (!NetworkManager::isWirelessEnabled() || !config.readEntry("Wifi", false)) {
return wifiAccessPoints;
}
for (const auto &device : NetworkManager::networkInterfaces()) {
QSharedPointer<NetworkManager::WirelessDevice> wifi = qSharedPointerDynamicCast<NetworkManager::WirelessDevice>(device);
if (!wifi) {
continue;
}
for (const auto &network : wifi->networks()) {
const QString &ssid = network->ssid();
if (ssid.isEmpty() || ssid.endsWith(QLatin1String("_nomap"))) {
// skip hidden SSID and networks with "_nomap"
continue;
}
for (const auto &accessPoint : network->accessPoints()) {
wifiAccessPoints.append(QJsonObject{{QStringLiteral("macAddress"), accessPoint->hardwareAddress()}});
}
}
}
return wifiAccessPoints;
}
void Ip::update() void Ip::update()
{ {
d->clear(); d->clear();
// TODO: add wifi data if available if (!NetworkManager::isNetworkingEnabled()) {
const QByteArray postData = QByteArrayLiteral("{}"); setData(Plasma::DataEngine::Data());
return;
}
const QJsonArray wifiAccessPoints = accessPoints();
QJsonObject request;
if (wifiAccessPoints.count() >= 2) {
request.insert(QStringLiteral("wifiAccessPoints"), wifiAccessPoints);
}
const QByteArray postData = QJsonDocument(request).toJson(QJsonDocument::Compact);
const QString apiKey = QStringLiteral("60e8eae6-3988-4ada-ad48-2cfddddf216b"); const QString apiKey = QStringLiteral("60e8eae6-3988-4ada-ad48-2cfddddf216b");
KIO::TransferJob *datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/geolocate?key=%1").arg(apiKey)), KIO::TransferJob *datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/geolocate?key=%1").arg(apiKey)),
postData, postData,

Loading…
Cancel
Save