Changeset View
Changeset View
Standalone View
Standalone View
applets/nightcolor/plugin/monitor.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 "monitor.h" | ||||
19 | #include "monitor_p.h" | ||||
20 | | ||||
21 | #include <QDBusConnection> | ||||
22 | #include <QDBusMessage> | ||||
23 | #include <QDBusPendingCallWatcher> | ||||
24 | #include <QDBusPendingReply> | ||||
25 | #include <QDBusServiceWatcher> | ||||
26 | | ||||
27 | static const QString s_serviceName = QStringLiteral("org.kde.KWin"); | ||||
28 | static const QString s_nightColorPath = QStringLiteral("/ColorCorrect"); | ||||
29 | static const QString s_nightColorInterface = QStringLiteral("org.kde.kwin.ColorCorrect"); | ||||
30 | static const QString s_propertiesInterface = QStringLiteral("org.freedesktop.DBus.Properties"); | ||||
31 | | ||||
32 | MonitorPrivate::MonitorPrivate(QObject *parent) | ||||
33 | : QObject(parent) | ||||
34 | { | ||||
35 | QDBusServiceWatcher *watcher = new QDBusServiceWatcher(s_serviceName, | ||||
36 | QDBusConnection::sessionBus(), | ||||
37 | QDBusServiceWatcher::WatchForOwnerChange, | ||||
38 | this); | ||||
39 | connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &MonitorPrivate::handleServiceRegistered); | ||||
40 | connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &MonitorPrivate::handleServiceUnregistered); | ||||
41 | | ||||
42 | handleServiceRegistered(); | ||||
43 | } | ||||
44 | | ||||
45 | MonitorPrivate::~MonitorPrivate() | ||||
46 | { | ||||
47 | } | ||||
48 | | ||||
49 | void MonitorPrivate::handleServiceRegistered() | ||||
50 | { | ||||
51 | QDBusConnection bus = QDBusConnection::sessionBus(); | ||||
52 | | ||||
53 | const bool connected = bus.connect(s_serviceName, s_nightColorPath, s_propertiesInterface, | ||||
54 | QStringLiteral("PropertiesChanged"), | ||||
55 | this, SLOT(handlePropertiesChanged(QString,QVariantMap,QStringList))); | ||||
56 | if (!connected) { | ||||
57 | return; | ||||
58 | } | ||||
59 | | ||||
60 | QDBusMessage message = QDBusMessage::createMethodCall(s_serviceName, | ||||
61 | s_nightColorPath, | ||||
62 | s_propertiesInterface, | ||||
63 | QStringLiteral("GetAll")); | ||||
64 | message.setArguments({ s_nightColorInterface }); | ||||
65 | | ||||
66 | QDBusPendingReply<QVariantMap> properties = bus.asyncCall(message); | ||||
67 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(properties, this); | ||||
68 | | ||||
69 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { | ||||
70 | self->deleteLater(); | ||||
71 | | ||||
72 | const QDBusPendingReply<QVariantMap> properties = *self; | ||||
73 | if (properties.isError()) { | ||||
74 | return; | ||||
75 | } | ||||
76 | | ||||
77 | updateProperties(properties.value()); | ||||
78 | }); | ||||
79 | } | ||||
80 | | ||||
81 | void MonitorPrivate::handleServiceUnregistered() | ||||
82 | { | ||||
83 | QDBusConnection bus = QDBusConnection::sessionBus(); | ||||
84 | | ||||
85 | bus.disconnect(s_serviceName, s_nightColorPath, s_propertiesInterface, | ||||
86 | QStringLiteral("PropertiesChanged"), | ||||
87 | this, SLOT(handlePropertiesChanged(QString,QVariantMap,QStringList))); | ||||
88 | | ||||
89 | setAvailable(false); | ||||
90 | } | ||||
91 | | ||||
92 | void MonitorPrivate::handlePropertiesChanged(const QString &interfaceName, | ||||
93 | const QVariantMap &changedProperties, | ||||
94 | const QStringList &invalidatedProperties) | ||||
95 | { | ||||
96 | Q_UNUSED(interfaceName) | ||||
97 | Q_UNUSED(invalidatedProperties) | ||||
98 | | ||||
99 | updateProperties(changedProperties); | ||||
100 | } | ||||
101 | | ||||
102 | int MonitorPrivate::currentTemperature() const | ||||
103 | { | ||||
104 | return m_currentTemperature; | ||||
105 | } | ||||
106 | | ||||
107 | void MonitorPrivate::updateProperties(const QVariantMap &properties) | ||||
108 | { | ||||
109 | const QVariant available = properties.value(QStringLiteral("available")); | ||||
110 | if (available.isValid()) { | ||||
111 | setAvailable(available.toBool()); | ||||
112 | } | ||||
113 | | ||||
114 | const QVariant enabled = properties.value(QStringLiteral("enabled")); | ||||
115 | if (enabled.isValid()) { | ||||
116 | setEnabled(enabled.toBool()); | ||||
117 | } | ||||
118 | | ||||
119 | const QVariant running = properties.value(QStringLiteral("running")); | ||||
120 | if (running.isValid()) { | ||||
121 | setRunning(running.toBool()); | ||||
122 | } | ||||
123 | | ||||
124 | const QVariant currentTemperature = properties.value(QStringLiteral("currentTemperature")); | ||||
125 | if (currentTemperature.isValid()) { | ||||
126 | setCurrentTemperature(currentTemperature.toInt()); | ||||
127 | } | ||||
128 | } | ||||
129 | | ||||
130 | void MonitorPrivate::setCurrentTemperature(int temperature) | ||||
131 | { | ||||
132 | if (m_currentTemperature == temperature) { | ||||
133 | return; | ||||
134 | } | ||||
135 | m_currentTemperature = temperature; | ||||
136 | emit currentTemperatureChanged(); | ||||
137 | } | ||||
138 | | ||||
139 | bool MonitorPrivate::isAvailable() const | ||||
140 | { | ||||
141 | return m_isAvailable; | ||||
142 | } | ||||
143 | | ||||
144 | void MonitorPrivate::setAvailable(bool available) | ||||
145 | { | ||||
146 | if (m_isAvailable == available) { | ||||
147 | return; | ||||
148 | } | ||||
149 | m_isAvailable = available; | ||||
150 | emit availableChanged(); | ||||
151 | } | ||||
152 | | ||||
153 | bool MonitorPrivate::isEnabled() const | ||||
154 | { | ||||
155 | return m_isEnabled; | ||||
156 | } | ||||
157 | | ||||
158 | void MonitorPrivate::setEnabled(bool enabled) | ||||
159 | { | ||||
160 | if (m_isEnabled == enabled) { | ||||
161 | return; | ||||
162 | } | ||||
163 | m_isEnabled = enabled; | ||||
164 | emit enabledChanged(); | ||||
165 | } | ||||
166 | | ||||
167 | bool MonitorPrivate::isRunning() const | ||||
168 | { | ||||
169 | return m_isRunning; | ||||
170 | } | ||||
171 | | ||||
172 | void MonitorPrivate::setRunning(bool running) | ||||
173 | { | ||||
174 | if (m_isRunning == running) { | ||||
175 | return; | ||||
176 | } | ||||
177 | m_isRunning = running; | ||||
178 | emit runningChanged(); | ||||
179 | } | ||||
180 | | ||||
181 | Monitor::Monitor(QObject *parent) | ||||
182 | : QObject(parent) | ||||
183 | , d(new MonitorPrivate(this)) | ||||
184 | { | ||||
185 | connect(d, &MonitorPrivate::availableChanged, this, &Monitor::availableChanged); | ||||
186 | connect(d, &MonitorPrivate::enabledChanged, this, &Monitor::enabledChanged); | ||||
187 | connect(d, &MonitorPrivate::runningChanged, this, &Monitor::runningChanged); | ||||
188 | connect(d, &MonitorPrivate::currentTemperatureChanged, this, &Monitor::currentTemperatureChanged); | ||||
189 | } | ||||
190 | | ||||
191 | Monitor::~Monitor() | ||||
192 | { | ||||
193 | } | ||||
194 | | ||||
195 | bool Monitor::isAvailable() const | ||||
196 | { | ||||
197 | return d->isAvailable(); | ||||
198 | } | ||||
199 | | ||||
200 | bool Monitor::isEnabled() const | ||||
201 | { | ||||
202 | return d->isEnabled(); | ||||
203 | } | ||||
204 | | ||||
205 | bool Monitor::isRunning() const | ||||
206 | { | ||||
207 | return d->isRunning(); | ||||
208 | } | ||||
209 | | ||||
210 | int Monitor::currentTemperature() const | ||||
211 | { | ||||
212 | return d->currentTemperature(); | ||||
213 | } |