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

0001 /*
0002     SPDX-FileCopyrightText: 2017 Roman Gilg <subdiff@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "compositorcoloradaptor.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <QDBusInterface>
0012 #include <QDBusReply>
0013 
0014 namespace ColorCorrect
0015 {
0016 CompositorAdaptor::CompositorAdaptor(QObject *parent)
0017     : QObject(parent)
0018 {
0019     m_iface = new QDBusInterface(QStringLiteral("org.kde.KWin"),
0020                                  QStringLiteral("/ColorCorrect"),
0021                                  QStringLiteral("org.kde.kwin.ColorCorrect"),
0022                                  QDBusConnection::sessionBus(),
0023                                  this);
0024 
0025     const bool connected = m_iface->connection().connect(QStringLiteral("org.kde.KWin"),
0026                                                          QStringLiteral("/ColorCorrect"),
0027                                                          QStringLiteral("org.freedesktop.DBus.Properties"),
0028                                                          QStringLiteral("PropertiesChanged"),
0029                                                          this,
0030                                                          SLOT(handlePropertiesChanged(QString, QVariantMap, QStringList)));
0031     if (!connected) {
0032         setError(ErrorCode::ErrorCodeConnectionFailed);
0033         return;
0034     }
0035 
0036     QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
0037                                                           QStringLiteral("/ColorCorrect"),
0038                                                           QStringLiteral("org.freedesktop.DBus.Properties"),
0039                                                           QStringLiteral("GetAll"));
0040     message.setArguments({"org.kde.kwin.ColorCorrect"});
0041 
0042     QDBusPendingReply<QVariantMap> properties = m_iface->connection().asyncCall(message);
0043     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(properties, this);
0044 
0045     connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) {
0046         self->deleteLater();
0047 
0048         const QDBusPendingReply<QVariantMap> properties = *self;
0049         if (properties.isError()) {
0050             return;
0051         }
0052 
0053         updateProperties(properties.value());
0054     });
0055 }
0056 
0057 void CompositorAdaptor::setError(ErrorCode error)
0058 {
0059     if (m_error == error) {
0060         return;
0061     }
0062     m_error = error;
0063     switch (error) {
0064     case ErrorCode::ErrorCodeConnectionFailed:
0065         m_errorText = i18nc("Critical error message", "Failed to connect to the Window Manager");
0066         break;
0067     case ErrorCode::ErrorCodeBackendNoSupport:
0068         m_errorText = i18nc("Critical error message", "Rendering backend doesn't support Color Correction.");
0069         break;
0070     default:
0071         m_errorText = "";
0072     }
0073     Q_EMIT errorChanged();
0074     Q_EMIT errorTextChanged();
0075 }
0076 
0077 void CompositorAdaptor::updateProperties(const QVariantMap &properties)
0078 {
0079     const QVariant available = properties.value(QStringLiteral("available"));
0080     if (available.isValid()) {
0081         m_nightColorAvailable = available.toBool();
0082 
0083         if (!m_nightColorAvailable) {
0084             setError(ErrorCode::ErrorCodeBackendNoSupport);
0085             return;
0086         }
0087     }
0088 
0089     const QVariant running = properties.value(QStringLiteral("running"));
0090     if (running.isValid()) {
0091         if (m_running != running.toBool()) {
0092             m_running = running.toBool();
0093             Q_EMIT runningChanged();
0094         }
0095     }
0096 }
0097 
0098 void CompositorAdaptor::handlePropertiesChanged(const QString &interfaceName, const QVariantMap &changedProperties, const QStringList &invalidatedProperties)
0099 {
0100     Q_UNUSED(interfaceName)
0101     Q_UNUSED(invalidatedProperties)
0102 
0103     updateProperties(changedProperties);
0104 }
0105 
0106 void CompositorAdaptor::sendAutoLocationUpdate(double latitude, double longitude)
0107 {
0108     m_iface->call(QStringLiteral("nightColorAutoLocationUpdate"), latitude, longitude);
0109 }
0110 
0111 void CompositorAdaptor::preview(int temperature)
0112 {
0113     m_iface->call("preview", (uint)temperature);
0114 }
0115 
0116 void CompositorAdaptor::stopPreview()
0117 {
0118     m_iface->call("stopPreview");
0119 }
0120 }