File: nightlightinhibitor.cpp

package info (click to toggle)
powerdevil 4%3A6.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,680 kB
  • sloc: cpp: 13,284; xml: 1,911; python: 1,204; sh: 19; makefile: 10
file content (133 lines) | stat: -rw-r--r-- 3,592 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
 * SPDX-FileCopyrightText: 2024 Natalie Clarius <natalie.clarius@kde.org>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "nightlightinhibitor.h"

#include <memory>

#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QLoggingCategory>
#include <QQmlEngine>

Q_LOGGING_CATEGORY(NIGHTLIGHT_CONTROL, "org.kde.plasma.nightlightcontrol")

static const QString s_serviceName = QStringLiteral("org.kde.KWin.NightLight");
static const QString s_path = QStringLiteral("/org/kde/KWin/NightLight");
static const QString s_interface = QStringLiteral("org.kde.KWin.NightLight");

NightLightInhibitor::NightLightInhibitor(QObject *parent)
    : QObject(parent)
{
}

NightLightInhibitor::~NightLightInhibitor()
{
    uninhibit();
}

NightLightInhibitor *NightLightInhibitor::create(QQmlEngine *qmlEngine, QJSEngine *)
{
    return new NightLightInhibitor(qmlEngine);
}

bool NightLightInhibitor::isInhibited() const
{
    return m_state == Inhibited || m_state == Inhibiting || m_pendingUninhibit;
}

void NightLightInhibitor::toggleInhibition()
{
    if (isInhibited()) {
        uninhibit();
    } else {
        inhibit();
    }
}

void NightLightInhibitor::inhibit()
{
    if (m_state == Inhibited) {
        return;
    }

    m_pendingUninhibit = false;

    if (m_state == Inhibiting) {
        return;
    }

    QDBusMessage message = QDBusMessage::createMethodCall(s_serviceName, s_path, s_interface, QStringLiteral("inhibit"));

    QDBusPendingReply<uint> cookie = QDBusConnection::sessionBus().asyncCall(message);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(cookie, this);

    connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) {
        const bool wasPendingUninhibit = m_pendingUninhibit;
        m_pendingUninhibit = false;

        const QDBusPendingReply<uint> reply = *self;
        self->deleteLater();

        if (reply.isError()) {
            qCWarning(NIGHTLIGHT_CONTROL()) << "Could not inhibit Night Light:" << reply.error().message();
            m_state = Uninhibited;
            Q_EMIT inhibitedChanged();
            return;
        }

        m_cookie = reply.value();
        m_state = Inhibited;
        Q_EMIT inhibitedChanged();

        if (wasPendingUninhibit) {
            uninhibit();
        }
    });

    m_state = Inhibiting;
}

void NightLightInhibitor::uninhibit()
{
    if (m_state == Uninhibiting || m_state == Uninhibited) {
        return;
    }

    if (m_state == Inhibiting) {
        m_pendingUninhibit = true;
        return;
    }

    QDBusMessage message = QDBusMessage::createMethodCall(s_serviceName, s_path, s_interface, QStringLiteral("uninhibit"));
    message.setArguments({m_cookie});

    QDBusPendingReply<void> reply = QDBusConnection::sessionBus().asyncCall(message);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);

    connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) {
        self->deleteLater();

        if (m_state != Uninhibiting) {
            return;
        }

        const QDBusPendingReply<void> reply = *self;
        if (reply.isError()) {
            qCWarning(NIGHTLIGHT_CONTROL) << "Could not uninhibit Night Light:" << reply.error().message();
        }

        m_state = Uninhibited;
        Q_EMIT inhibitedChanged();
    });

    m_state = Uninhibiting;
}

#include "moc_nightlightinhibitor.cpp"