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 | QDBusMessage message = QDBusMessage::createSignal( | ||||
44 | QStringLiteral("/org/freedesktop/Notifications"), | ||||
45 | QStringLiteral("org.freedesktop.DBus.Properties"), | ||||
46 | QStringLiteral("PropertiesChanged") | ||||
47 | ); | ||||
48 | | ||||
49 | const QVariantMap changedProperties { | ||||
50 | { QStringLiteral("inhibited"), m_manager->isInhibited() }, | ||||
51 | }; | ||||
52 | | ||||
53 | message.setArguments({ | ||||
54 | QStringLiteral("org.kde.kwin.ColorCorrect"), | ||||
55 | changedProperties, | ||||
56 | QStringList(), // invalidated_properties | ||||
57 | }); | ||||
58 | | ||||
59 | QDBusConnection::sessionBus().send(message); | ||||
60 | }); | ||||
61 | | ||||
33 | connect(m_manager, &Manager::configChange, this, &ColorCorrectDBusInterface::nightColorConfigChanged); | 62 | connect(m_manager, &Manager::configChange, this, &ColorCorrectDBusInterface::nightColorConfigChanged); | ||
34 | new ColorCorrectAdaptor(this); | 63 | new ColorCorrectAdaptor(this); | ||
35 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/ColorCorrect"), this); | 64 | QDBusConnection::sessionBus().registerObject(QStringLiteral("/ColorCorrect"), this); | ||
36 | } | 65 | } | ||
37 | 66 | | |||
67 | bool ColorCorrectDBusInterface::isInhibited() const | ||||
68 | { | ||||
69 | return m_manager->isInhibited(); | ||||
70 | } | ||||
71 | | ||||
38 | QHash<QString, QVariant> ColorCorrectDBusInterface::nightColorInfo() | 72 | QHash<QString, QVariant> ColorCorrectDBusInterface::nightColorInfo() | ||
39 | { | 73 | { | ||
40 | return m_manager->info(); | 74 | return m_manager->info(); | ||
41 | } | 75 | } | ||
42 | 76 | | |||
43 | bool ColorCorrectDBusInterface::setNightColorConfig(QHash<QString, QVariant> data) | 77 | bool ColorCorrectDBusInterface::setNightColorConfig(QHash<QString, QVariant> data) | ||
44 | { | 78 | { | ||
45 | return m_manager->changeConfiguration(data); | 79 | return m_manager->changeConfiguration(data); | ||
46 | } | 80 | } | ||
47 | 81 | | |||
48 | void ColorCorrectDBusInterface::nightColorAutoLocationUpdate(double latitude, double longitude) | 82 | void ColorCorrectDBusInterface::nightColorAutoLocationUpdate(double latitude, double longitude) | ||
49 | { | 83 | { | ||
50 | m_manager->autoLocationUpdate(latitude, longitude); | 84 | m_manager->autoLocationUpdate(latitude, longitude); | ||
51 | } | 85 | } | ||
52 | 86 | | |||
87 | uint ColorCorrectDBusInterface::inhibit() | ||||
88 | { | ||||
89 | const QString serviceName = QDBusContext::message().service(); | ||||
90 | | ||||
91 | if (m_inhibitors.values(serviceName).isEmpty()) { | ||||
92 | m_inhibitorWatcher->addWatchedService(serviceName); | ||||
93 | } | ||||
94 | | ||||
95 | m_inhibitors.insertMulti(serviceName, ++m_lastInhibitionCookie); | ||||
96 | | ||||
97 | m_manager->inhibit(); | ||||
98 | | ||||
99 | return m_lastInhibitionCookie; | ||||
100 | } | ||||
101 | | ||||
102 | void ColorCorrectDBusInterface::uninhibit(uint cookie) | ||||
103 | { | ||||
104 | const QString serviceName = QDBusContext::message().service(); | ||||
105 | | ||||
106 | uninhibit(serviceName, cookie); | ||||
107 | } | ||||
108 | | ||||
109 | void ColorCorrectDBusInterface::uninhibit(const QString &serviceName, uint cookie) | ||||
110 | { | ||||
111 | const int removedCount = m_inhibitors.remove(serviceName, cookie); | ||||
112 | if (!removedCount) { | ||||
113 | return; | ||||
114 | } | ||||
115 | | ||||
116 | if (m_inhibitors.values(serviceName).isEmpty()) { | ||||
117 | m_inhibitors.remove(serviceName); | ||||
118 | m_inhibitorWatcher->removeWatchedService(serviceName); | ||||
119 | } | ||||
120 | | ||||
121 | m_manager->uninhibit(); | ||||
122 | } | ||||
123 | | ||||
124 | void ColorCorrectDBusInterface::removeInhibitorService(const QString &serviceName) | ||||
125 | { | ||||
126 | const auto cookies = m_inhibitors.values(serviceName); | ||||
127 | for (const uint &cookie : cookies) { | ||||
128 | uninhibit(serviceName, cookie); | ||||
129 | } | ||||
130 | } | ||||
131 | | ||||
53 | } | 132 | } | ||
54 | } | 133 | } |