File indexing completed on 2024-04-28 16:54:30

0001 /*
0002     SPDX-FileCopyrightText: 2017 Roman Gilg <subdiff@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "geolocator.h"
0008 #include <cmath>
0009 
0010 namespace ColorCorrect
0011 {
0012 Geolocator::Geolocator(QObject *parent)
0013     : QObject(parent)
0014 {
0015     m_engine = dataEngine(QStringLiteral("geolocation"));
0016 
0017     if (m_engine && m_engine->isValid()) {
0018         m_engine->connectSource(QStringLiteral("location"), this);
0019     }
0020 }
0021 
0022 void Geolocator::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
0023 {
0024     Q_UNUSED(source);
0025 
0026     if (!m_engine) {
0027         return;
0028     }
0029 
0030     if (data.isEmpty()) {
0031         return;
0032     }
0033 
0034     auto readGeoDouble = [](const Plasma::DataEngine::Data &data, const QString &key) -> double {
0035         const Plasma::DataEngine::Data::ConstIterator it = data.find(key);
0036 
0037         if (it == data.end()) {
0038             return qQNaN();
0039         }
0040 
0041         bool ok = false;
0042         double result = it.value().toDouble(&ok);
0043         if (!ok) {
0044             result = qQNaN();
0045         }
0046         return result;
0047     };
0048 
0049     double lat = readGeoDouble(data, QStringLiteral("latitude"));
0050     double lng = readGeoDouble(data, QStringLiteral("longitude"));
0051     if (std::isnan(lat) || std::isnan(lng)) {
0052         return;
0053     }
0054 
0055     bool isChanged = false;
0056     if (m_latitude != lat) {
0057         m_latitude = lat;
0058         isChanged = true;
0059         Q_EMIT latitudeChanged();
0060     }
0061     if (m_longitude != lng) {
0062         m_longitude = lng;
0063         isChanged = true;
0064         Q_EMIT longitudeChanged();
0065     }
0066     if (isChanged) {
0067         Q_EMIT locationChanged(m_latitude, m_longitude);
0068     }
0069 }
0070 
0071 }