File indexing completed on 2024-05-12 17:09:55

0001 /*
0002     SPDX-FileCopyrightText: 2017 Roman Gilg <subdiff@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "locationupdater.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusMessage>
0011 
0012 #include <KPluginFactory>
0013 
0014 #include "../compositorcoloradaptor.h"
0015 #include "../geolocator.h"
0016 
0017 K_PLUGIN_CLASS_WITH_JSON(LocationUpdater, "colorcorrectlocationupdater.json")
0018 
0019 LocationUpdater::LocationUpdater(QObject *parent, const QList<QVariant> &)
0020     : KDEDModule(parent)
0021     , m_adaptor(new ColorCorrect::CompositorAdaptor(this))
0022 {
0023     m_configWatcher = KConfigWatcher::create(KSharedConfig::openConfig(QStringLiteral("kwinrc")));
0024     connect(m_configWatcher.data(), &KConfigWatcher::configChanged, this, &LocationUpdater::resetLocator);
0025     connect(m_adaptor, &ColorCorrect::CompositorAdaptor::runningChanged, this, &LocationUpdater::resetLocator);
0026     resetLocator();
0027 }
0028 
0029 void LocationUpdater::resetLocator()
0030 {
0031     KConfigGroup group(m_configWatcher->config(), QStringLiteral("NightColor"));
0032     auto mode = group.readEntry(QStringLiteral("Mode"), 0);
0033     if (m_adaptor->running() && mode == 0) {
0034         if (!m_locator) {
0035             m_locator = new ColorCorrect::Geolocator(this);
0036             connect(m_locator, &ColorCorrect::Geolocator::locationChanged, this, &LocationUpdater::sendLocation);
0037         }
0038     } else {
0039         delete m_locator;
0040         m_locator = nullptr;
0041         // if automatic location isn't enabled, there's no need to keep running
0042         // Night Color KCM will enable us again if user changes to automatic
0043         disableSelf();
0044     }
0045 }
0046 
0047 void LocationUpdater::sendLocation(double latitude, double longitude)
0048 {
0049     m_adaptor->sendAutoLocationUpdate(latitude, longitude);
0050 }
0051 
0052 void LocationUpdater::disableSelf()
0053 {
0054     QDBusConnection dbus = QDBusConnection::sessionBus();
0055     QDBusMessage unloadMsg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kded5"),
0056                                                             QStringLiteral("/kded"),
0057                                                             QStringLiteral("org.kde.kded5"),
0058                                                             QStringLiteral("unloadModule"));
0059     unloadMsg.setArguments({QVariant(QStringLiteral("colorcorrectlocationupdater"))});
0060     dbus.call(unloadMsg, QDBus::NoBlock);
0061 }
0062 
0063 #include "locationupdater.moc"