Changeset View
Changeset View
Standalone View
Standalone View
applets/nightcolor/plugin/inhibitor.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 Vlad Zahorodnii <vladzzag@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
16 | */ | ||||
17 | | ||||
18 | #include "inhibitor.h" | ||||
19 | | ||||
20 | #include <QDBusConnection> | ||||
21 | #include <QDBusMessage> | ||||
22 | #include <QDBusPendingCallWatcher> | ||||
23 | #include <QDBusPendingReply> | ||||
24 | #include <QLoggingCategory> | ||||
25 | | ||||
26 | Q_LOGGING_CATEGORY(NIGHTCOLOR_CONTROL, "org.kde.plasma.nightcolorcontrol") | ||||
27 | | ||||
28 | static const QString s_serviceName = QStringLiteral("org.kde.KWin"); | ||||
29 | static const QString s_path = QStringLiteral("/ColorCorrect"); | ||||
30 | static const QString s_interface = QStringLiteral("org.kde.kwin.ColorCorrect"); | ||||
31 | | ||||
32 | class Inhibitor::Private | ||||
33 | { | ||||
34 | public: | ||||
35 | uint cookie = 0; | ||||
36 | State state = Uninhibited; | ||||
37 | bool pendingUninhibit = false; | ||||
38 | }; | ||||
39 | | ||||
40 | Inhibitor::Inhibitor(QObject *parent) | ||||
41 | : QObject(parent) | ||||
42 | , d(new Private) | ||||
43 | { | ||||
44 | } | ||||
45 | | ||||
46 | Inhibitor::~Inhibitor() | ||||
47 | { | ||||
48 | uninhibit(); | ||||
49 | } | ||||
50 | | ||||
51 | Inhibitor::State Inhibitor::state() const | ||||
52 | { | ||||
53 | return d->state; | ||||
54 | } | ||||
55 | | ||||
56 | void Inhibitor::inhibit() | ||||
57 | { | ||||
58 | if (d->state == Inhibited) { | ||||
59 | return; | ||||
60 | } | ||||
61 | | ||||
62 | d->pendingUninhibit = false; | ||||
63 | | ||||
64 | if (d->state == Inhibiting) { | ||||
65 | return; | ||||
66 | } | ||||
67 | | ||||
68 | QDBusMessage message = QDBusMessage::createMethodCall(s_serviceName, | ||||
69 | s_path, | ||||
70 | s_interface, | ||||
71 | QStringLiteral("inhibit")); | ||||
72 | | ||||
73 | QDBusPendingReply<uint> cookie = QDBusConnection::sessionBus().asyncCall(message); | ||||
74 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(cookie, this); | ||||
75 | | ||||
76 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { | ||||
77 | const bool wasPendingUninhibit = d->pendingUninhibit; | ||||
78 | d->pendingUninhibit = false; | ||||
79 | | ||||
80 | const QDBusPendingReply<uint> reply = *self; | ||||
81 | self->deleteLater(); | ||||
82 | | ||||
83 | if (reply.isError()) { | ||||
84 | qCWarning(NIGHTCOLOR_CONTROL()) << "Could not inhibit Night Color:" << reply.error().message(); | ||||
85 | d->state = Uninhibited; | ||||
86 | emit stateChanged(); | ||||
87 | return; | ||||
88 | } | ||||
89 | | ||||
90 | d->cookie = reply.value(); | ||||
91 | d->state = Inhibited; | ||||
92 | emit stateChanged(); | ||||
93 | | ||||
94 | if (wasPendingUninhibit) { | ||||
95 | uninhibit(); | ||||
96 | } | ||||
97 | }); | ||||
98 | | ||||
99 | d->state = Inhibiting; | ||||
100 | emit stateChanged(); | ||||
101 | } | ||||
102 | | ||||
103 | void Inhibitor::uninhibit() | ||||
104 | { | ||||
105 | if (d->state == Uninhibiting || d->state == Uninhibited) { | ||||
106 | return; | ||||
107 | } | ||||
108 | | ||||
109 | if (d->state == Inhibiting) { | ||||
110 | d->pendingUninhibit = true; | ||||
111 | return; | ||||
112 | } | ||||
113 | | ||||
114 | QDBusMessage message = QDBusMessage::createMethodCall(s_serviceName, | ||||
115 | s_path, | ||||
116 | s_interface, | ||||
117 | QStringLiteral("uninhibit")); | ||||
118 | message.setArguments({ d->cookie }); | ||||
119 | | ||||
120 | QDBusPendingReply<void> reply = QDBusConnection::sessionBus().asyncCall(message); | ||||
121 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); | ||||
122 | | ||||
123 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { | ||||
124 | self->deleteLater(); | ||||
125 | | ||||
126 | if (d->state != Uninhibiting) { | ||||
127 | return; | ||||
128 | } | ||||
129 | | ||||
130 | const QDBusPendingReply<void> reply = *self; | ||||
131 | if (reply.isError()) { | ||||
132 | qCWarning(NIGHTCOLOR_CONTROL) << "Could not uninhibit Night Color:" << reply.error().message(); | ||||
133 | } | ||||
134 | | ||||
135 | d->state = Uninhibited; | ||||
136 | emit stateChanged(); | ||||
137 | }); | ||||
138 | | ||||
139 | d->state = Uninhibiting; | ||||
140 | emit stateChanged(); | ||||
141 | } |