diff --git a/dataengines/geolocation/CMakeLists.txt b/dataengines/geolocation/CMakeLists.txt index e80c31d92..1f7da7701 100644 --- a/dataengines/geolocation/CMakeLists.txt +++ b/dataengines/geolocation/CMakeLists.txt @@ -1,77 +1,78 @@ if(NOT CMAKE_VERSION VERSION_LESS "3.10.0") # CMake 3.9+ warns about automoc on files without Q_OBJECT, and doesn't know about other macros. # 3.10+ lets us provide more macro names that require automoc. list(APPEND CMAKE_AUTOMOC_MACRO_NAMES K_EXPORT_PLASMA_GEOLOCATIONPROVIDER) endif() set(plasma_geolocation_interface_SRCS geolocationprovider.cpp) add_library(plasma-geolocation-interface SHARED ${plasma_geolocation_interface_SRCS}) target_link_libraries(plasma-geolocation-interface PUBLIC Qt5::Core Qt5::Network KF5::Plasma PRIVATE KF5::KIOCore ) set_target_properties(plasma-geolocation-interface PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) install(TARGETS plasma-geolocation-interface ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES geolocationprovider.h ${CMAKE_CURRENT_BINARY_DIR}/geolocation_export.h DESTINATION ${KDE_INSTALL_INCLUDEDIR}/plasma/geolocation COMPONENT Devel) #install(FILES includes/Interface # DESTINATION ${KDE_INSTALL_INCLUDEDIR}/KDE/Plasma/Geolocation # COMPONENT Devel) # ------------------------------------------------------------------------------------------------- add_library(plasma_engine_geolocation MODULE geolocation.cpp) target_compile_definitions(plasma_engine_geolocation PRIVATE -DQT_NO_KEYWORDS) generate_export_header(plasma_engine_geolocation EXPORT_FILE_NAME "geolocation_export.h" BASE_NAME "GEOLOCATION") target_link_libraries(plasma_engine_geolocation plasma-geolocation-interface KF5::Plasma KF5::CoreAddons KF5::KIOCore KF5::NetworkManagerQt KF5::Service KF5::Solid) kcoreaddons_desktop_to_json(plasma_engine_geolocation plasma-dataengine-geolocation.desktop) install(TARGETS plasma_engine_geolocation DESTINATION ${KDE_INSTALL_PLUGINDIR}/plasma/dataengine) install(FILES plasma-dataengine-geolocation.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) install(FILES plasma-geolocationprovider.desktop DESTINATION ${KDE_INSTALL_KSERVICETYPES5DIR}) # ------------------------------------------------------------------------------------------------- set(plasma_geolocation_ip_SRCS location_ip.cpp) +ecm_qt_declare_logging_category(plasma_geolocation_ip_SRCS HEADER geolocdebug.h IDENTIFIER DATAENGINE_GEOLOCATION CATEGORY_NAME org.kde.plasma.dataengine.geolocation) add_library(plasma-geolocation-ip MODULE ${plasma_geolocation_ip_SRCS}) target_compile_definitions(plasma-geolocation-ip PRIVATE -DQT_NO_KEYWORDS) target_link_libraries(plasma-geolocation-ip plasma-geolocation-interface KF5::KIOCore KF5::NetworkManagerQt) install(FILES plasma-geolocation-ip.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) install(TARGETS plasma-geolocation-ip DESTINATION ${KDE_INSTALL_PLUGINDIR}) # ------------------------------------------------------------------------------------------------- set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH}) find_package(libgps) set_package_properties(libgps PROPERTIES DESCRIPTION "GPS support for geolocation" URL "https://www.berlios.de/software/gpsd/" TYPE OPTIONAL ) if(LIBGPS_FOUND) include_directories(${LIBGPS_INCLUDES} ${LIBGPS_INCLUDE_DIR}) set(plasma_geolocation_gps_SRCS location_gps.cpp) ecm_qt_declare_logging_category(plasma_geolocation_gps_SRCS HEADER geolocdebug.h IDENTIFIER DATAENGINE_GEOLOCATION CATEGORY_NAME org.kde.plasma.dataengine.geolocation) add_library(plasma-geolocation-gps MODULE ${plasma_geolocation_gps_SRCS}) target_link_libraries(plasma-geolocation-gps plasma-geolocation-interface ${LIBGPS_LIBRARIES}) install(FILES plasma-geolocation-gps.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) install(TARGETS plasma-geolocation-gps DESTINATION ${KDE_INSTALL_PLUGINDIR}) endif() # ------------------------------------------------------------------------------------------------- diff --git a/dataengines/geolocation/location_ip.cpp b/dataengines/geolocation/location_ip.cpp index 837d40517..ab3ad4147 100644 --- a/dataengines/geolocation/location_ip.cpp +++ b/dataengines/geolocation/location_ip.cpp @@ -1,197 +1,197 @@ /* * Copyright (C) 2009 Petri Damstén * - Original Implementation. * 2009 Andrew Coles * - Extension to iplocationtools engine. * 2015 Martin Gräßlin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "location_ip.h" -#include +#include "geolocdebug.h" #include #include #include #include #include #include #include #include #include #include class Ip::Private : public QObject { Q_OBJECT public: Private(Ip *q) : q(q) {} void readGeoLocation(KJob *job) { m_geoLocationResolved = true; if (job && job->error()) { - qDebug() << "error" << job->errorString(); + qCCritical(DATAENGINE_GEOLOCATION) << "error: " << job->errorString(); m_geoLocationPayload.clear(); checkUpdateData(); return; } const QJsonObject json = QJsonDocument::fromJson(m_geoLocationPayload).object(); m_geoLocationPayload.clear(); auto accuracyIt = json.find(QStringLiteral("accuracy")); if (accuracyIt != json.end()) { m_data[QStringLiteral("accuracy")] = (*accuracyIt).toDouble(); } else { m_data[QStringLiteral("accuracy")] = 40000; } auto locationIt = json.find(QStringLiteral("location")); if (locationIt != json.end()) { QJsonObject location = (*locationIt).toObject(); m_data[QStringLiteral("latitude")] = location.value(QStringLiteral("lat")).toDouble(); m_data[QStringLiteral("longitude")] = location.value(QStringLiteral("lng")).toDouble(); } checkUpdateData(); } void clear() { m_geoLocationPayload.clear(); m_countryPayload.clear(); m_countryResolved = false; m_geoLocationResolved = false; m_data.clear(); } void geoLocationData(KIO::Job *job, const QByteArray &data) { Q_UNUSED(job) if (data.isEmpty()) { return; } m_geoLocationPayload.append(data); } void countryData(KIO::Job *job, const QByteArray &data) { Q_UNUSED(job) if (data.isEmpty()) { return; } m_countryPayload.append(data); } void readCountry(KJob *job) { m_countryResolved = true; if (job && job->error()) { - qDebug() << "error" << job->errorString(); + qCCritical(DATAENGINE_GEOLOCATION) << "error: " << job->errorString(); m_countryPayload.clear(); checkUpdateData(); return; } const QJsonObject json = QJsonDocument::fromJson(m_countryPayload).object(); m_countryPayload.clear(); m_data[QStringLiteral("country")] = json.value(QStringLiteral("country_name")).toString(); m_data[QStringLiteral("country code")] = json.value(QStringLiteral("country_code")).toString(); checkUpdateData(); } private: void checkUpdateData() { if (!m_countryResolved || !m_geoLocationResolved) { return; } q->setData(m_data); } Ip *q; QByteArray m_geoLocationPayload; QByteArray m_countryPayload; bool m_countryResolved = false; bool m_geoLocationResolved = false; Plasma::DataEngine::Data m_data; }; Ip::Ip(QObject* parent, const QVariantList& args) : GeolocationProvider(parent, args), d(new Private(this)) { setUpdateTriggers(SourceEvent | NetworkConnected); } Ip::~Ip() { 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 wifi = qSharedPointerDynamicCast(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() { d->clear(); if (!NetworkManager::isNetworkingEnabled()) { 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"); KIO::TransferJob *datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/geolocate?key=%1").arg(apiKey)), postData, KIO::HideProgressInfo); datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json")); - qDebug() << "Fetching https://location.services.mozilla.com/v1/geolocate"; + qCDebug(DATAENGINE_GEOLOCATION) << "Fetching https://location.services.mozilla.com/v1/geolocate"; connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::geoLocationData); connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readGeoLocation); datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/country?key=%1").arg(apiKey)), postData, KIO::HideProgressInfo); datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json")); connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::countryData); connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readCountry); } K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(ip, Ip) #include "location_ip.moc"