Changeset View
Changeset View
Standalone View
Standalone View
colorcorrection/colorcorrectdbusinterface.cpp
Show All 17 Lines | |||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | *********************************************************************/ | 19 | *********************************************************************/ | ||
20 | 20 | | |||
21 | #include "colorcorrectdbusinterface.h" | 21 | #include "colorcorrectdbusinterface.h" | ||
22 | #include "colorcorrectadaptor.h" | 22 | #include "colorcorrectadaptor.h" | ||
23 | 23 | | |||
24 | #include "manager.h" | 24 | #include "manager.h" | ||
25 | 25 | | |||
26 | #include <QDBusMessage> | ||||
27 | | ||||
26 | namespace KWin { | 28 | namespace KWin { | ||
27 | namespace ColorCorrect { | 29 | namespace ColorCorrect { | ||
28 | 30 | | |||
29 | ColorCorrectDBusInterface::ColorCorrectDBusInterface(Manager *parent) | 31 | ColorCorrectDBusInterface::ColorCorrectDBusInterface(Manager *parent) | ||
30 | : QObject(parent) | 32 | : QObject(parent) | ||
31 | , m_manager(parent) | 33 | , m_manager(parent) | ||
34 | , m_inhibitorWatcher(new QDBusServiceWatcher(this)) | ||||
32 | { | 35 | { | ||
36 | m_inhibitorWatcher->setConnection(QDBusConnection::sessionBus()); | ||||
37 | m_inhibitorWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration); | ||||
38 | connect(m_inhibitorWatcher, &QDBusServiceWatcher::serviceUnregistered, | ||||
39 | this, &ColorCorrectDBusInterface::removeInhibitorService); | ||||
40 | | ||||
41 | // Argh, all this code is just to send one innocent signal... | ||||
42 | connect(m_manager, &Manager::inhibitedChanged, this, [this] { | ||||
43 | QVariantMap changedProperties; | ||||
44 | changedProperties.insert(QStringLiteral("inhibited"), m_manager->isInhibited()); | ||||
45 | | ||||
46 | QDBusMessage message = QDBusMessage::createSignal( | ||||
47 | QStringLiteral("/ColorCorrect"), | ||||
48 | QStringLiteral("org.freedesktop.DBus.Properties"), | ||||
49 | QStringLiteral("PropertiesChanged") | ||||
50 | ); | ||||
51 | | ||||
52 | message.setArguments({ | ||||
53 | QStringLiteral("org.kde.kwin.ColorCorrect"), | ||||
54 | changedProperties, | ||||
55 | QStringList(), // invalidated_properties | ||||
56 | }); | ||||
57 | | ||||
58 | QDBusConnection::sessionBus().send(message); | ||||
59 | }); | ||||
60 | | ||||
33 | connect(m_manager, &Manager::configChange, this, &ColorCorrectDBusInterface::nightColorConfigChanged); | 61 | connect(m_manager, &Manager::configChange, this, &ColorCorrectDBusInterface::nightColorConfigChanged); | ||
34 | new ColorCorrectAdaptor(this); | 62 | new ColorCorrectAdaptor(this); | ||
35 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/ColorCorrect"), this); | 63 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/ColorCorrect"), this); | ||
36 | } | 64 | } | ||
37 | 65 | | |||
66 | bool ColorCorrectDBusInterface::isInhibited() const | ||||
67 | { | ||||
68 | return m_manager->isInhibited(); | ||||
69 | } | ||||
70 | | ||||
38 | QHash<QString, QVariant> ColorCorrectDBusInterface::nightColorInfo() | 71 | QHash<QString, QVariant> ColorCorrectDBusInterface::nightColorInfo() | ||
39 | { | 72 | { | ||
40 | return m_manager->info(); | 73 | return m_manager->info(); | ||
41 | } | 74 | } | ||
42 | 75 | | |||
43 | bool ColorCorrectDBusInterface::setNightColorConfig(QHash<QString, QVariant> data) | 76 | bool ColorCorrectDBusInterface::setNightColorConfig(QHash<QString, QVariant> data) | ||
44 | { | 77 | { | ||
45 | return m_manager->changeConfiguration(data); | 78 | return m_manager->changeConfiguration(data); | ||
46 | } | 79 | } | ||
47 | 80 | | |||
48 | void ColorCorrectDBusInterface::nightColorAutoLocationUpdate(double latitude, double longitude) | 81 | void ColorCorrectDBusInterface::nightColorAutoLocationUpdate(double latitude, double longitude) | ||
49 | { | 82 | { | ||
50 | m_manager->autoLocationUpdate(latitude, longitude); | 83 | m_manager->autoLocationUpdate(latitude, longitude); | ||
51 | } | 84 | } | ||
52 | 85 | | |||
86 | uint ColorCorrectDBusInterface::inhibit() | ||||
87 | { | ||||
88 | const QString serviceName = QDBusContext::message().service(); | ||||
89 | | ||||
90 | if (m_inhibitors.values(serviceName).isEmpty()) { | ||||
91 | m_inhibitorWatcher->addWatchedService(serviceName); | ||||
92 | } | ||||
93 | | ||||
94 | m_inhibitors.insertMulti(serviceName, ++m_lastInhibitionCookie); | ||||
95 | | ||||
96 | m_manager->inhibit(); | ||||
97 | | ||||
98 | return m_lastInhibitionCookie; | ||||
99 | } | ||||
100 | | ||||
101 | void ColorCorrectDBusInterface::uninhibit(uint cookie) | ||||
102 | { | ||||
103 | const QString serviceName = QDBusContext::message().service(); | ||||
104 | | ||||
105 | uninhibit(serviceName, cookie); | ||||
106 | } | ||||
107 | | ||||
108 | void ColorCorrectDBusInterface::uninhibit(const QString &serviceName, uint cookie) | ||||
109 | { | ||||
110 | const int removedCount = m_inhibitors.remove(serviceName, cookie); | ||||
111 | if (!removedCount) { | ||||
112 | return; | ||||
113 | } | ||||
114 | | ||||
115 | if (m_inhibitors.values(serviceName).isEmpty()) { | ||||
116 | m_inhibitorWatcher->removeWatchedService(serviceName); | ||||
117 | } | ||||
118 | | ||||
119 | m_manager->uninhibit(); | ||||
120 | } | ||||
121 | | ||||
122 | void ColorCorrectDBusInterface::removeInhibitorService(const QString &serviceName) | ||||
123 | { | ||||
124 | const auto cookies = m_inhibitors.values(serviceName); | ||||
125 | for (const uint &cookie : cookies) { | ||||
126 | uninhibit(serviceName, cookie); | ||||
127 | } | ||||
128 | } | ||||
129 | | ||||
53 | } | 130 | } | ||
54 | } | 131 | } |