Kirigami2

virtualkeyboardwatcher.cpp
1 /*
2  * SPDX-FileCopyrightText: 2018 Marco Martin <[email protected]>
3  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 
8 #include "virtualkeyboardwatcher.h"
9 
10 #ifdef KIRIGAMI_ENABLE_DBUS
11 #include "virtualkeyboard_interface.h"
12 #include <QDBusConnection>
13 #include <QDBusPendingCallWatcher>
14 #endif
15 
16 #include "loggingcategory.h"
17 
18 namespace Kirigami
19 {
20 Q_GLOBAL_STATIC(VirtualKeyboardWatcher, virtualKeyboardWatcherSelf)
21 
22 class Q_DECL_HIDDEN VirtualKeyboardWatcher::Private
23 {
24 public:
25  Private(VirtualKeyboardWatcher *qq)
26  : q(qq)
27  {
28  }
29 
30  VirtualKeyboardWatcher *q;
31 
32 #ifdef KIRIGAMI_ENABLE_DBUS
33  void getAllProperties();
34  void getProperty(const QString &propertyName);
35  void updateWillShowOnActive();
36 
37  OrgKdeKwinVirtualKeyboardInterface *keyboardInterface = nullptr;
38  OrgFreedesktopDBusPropertiesInterface *propertiesInterface = nullptr;
39 
40  QDBusPendingCallWatcher *willShowOnActiveCall = nullptr;
41 #endif
42 
43  bool available = false;
44  bool enabled = false;
45  bool active = false;
46  bool visible = false;
47  bool willShowOnActive = false;
48 
49  static const QString serviceName;
50  static const QString objectName;
51  static const QString interfaceName;
52 };
53 
54 const QString VirtualKeyboardWatcher::Private::serviceName = QStringLiteral("org.kde.KWin");
55 const QString VirtualKeyboardWatcher::Private::objectName = QStringLiteral("/VirtualKeyboard");
56 const QString VirtualKeyboardWatcher::Private::interfaceName = QStringLiteral("org.kde.kwin.VirtualKeyboard");
57 
58 VirtualKeyboardWatcher::VirtualKeyboardWatcher(QObject *parent)
59  : QObject(parent)
60  , d(std::make_unique<Private>(this))
61 {
62 #ifdef KIRIGAMI_ENABLE_DBUS
63  d->keyboardInterface = new OrgKdeKwinVirtualKeyboardInterface(Private::serviceName, Private::objectName, QDBusConnection::sessionBus(), this);
64  d->propertiesInterface = new OrgFreedesktopDBusPropertiesInterface(Private::serviceName, Private::objectName, QDBusConnection::sessionBus(), this);
65 
66  connect(d->keyboardInterface, &OrgKdeKwinVirtualKeyboardInterface::availableChanged, this, [this]() {
67  d->getProperty(QStringLiteral("available"));
68  });
69  connect(d->keyboardInterface, &OrgKdeKwinVirtualKeyboardInterface::enabledChanged, this, [this]() {
70  d->getProperty(QStringLiteral("enabled"));
71  });
72  connect(d->keyboardInterface, &OrgKdeKwinVirtualKeyboardInterface::activeChanged, this, [this]() {
73  d->getProperty(QStringLiteral("active"));
74  });
75  connect(d->keyboardInterface, &OrgKdeKwinVirtualKeyboardInterface::visibleChanged, this, [this]() {
76  d->getProperty(QStringLiteral("visible"));
77  });
78 
79  d->getAllProperties();
80 #endif
81 }
82 
83 VirtualKeyboardWatcher::~VirtualKeyboardWatcher() = default;
84 
85 bool VirtualKeyboardWatcher::available() const
86 {
87  return d->available;
88 }
89 
90 bool VirtualKeyboardWatcher::enabled() const
91 {
92  return d->enabled;
93 }
94 
95 void VirtualKeyboardWatcher::setEnabled(bool newEnabled)
96 {
97  if (newEnabled == d->enabled) {
98  return;
99  }
100 
101  d->enabled = newEnabled;
102 
103 #ifdef KIRIGAMI_ENABLE_DBUS
104  d->propertiesInterface->Set(Private::interfaceName, QStringLiteral("enabled"), QDBusVariant(newEnabled));
105 #else
106  Q_EMIT enabledChanged();
107 #endif
108 }
109 
110 bool VirtualKeyboardWatcher::active() const
111 {
112  return d->active;
113 }
114 
115 void VirtualKeyboardWatcher::setActive(bool newActive)
116 {
117  if (newActive == d->active) {
118  return;
119  }
120 
121  d->active = newActive;
122 
123 #ifdef KIRIGAMI_ENABLE_DBUS
124  d->propertiesInterface->Set(Private::interfaceName, QStringLiteral("active"), QDBusVariant(newActive));
125 #else
126  Q_EMIT activeChanged();
127 #endif
128 }
129 
130 bool VirtualKeyboardWatcher::visible() const
131 {
132  return d->visible;
133 }
134 
135 bool VirtualKeyboardWatcher::willShowOnActive() const
136 {
137 #ifdef KIRIGAMI_ENABLE_DBUS
138  d->updateWillShowOnActive();
139 #endif
140  return d->willShowOnActive;
141 }
142 
143 VirtualKeyboardWatcher *VirtualKeyboardWatcher::self()
144 {
145  return virtualKeyboardWatcherSelf();
146 }
147 
148 #ifdef KIRIGAMI_ENABLE_DBUS
149 
150 void VirtualKeyboardWatcher::Private::updateWillShowOnActive()
151 {
152  if (willShowOnActiveCall) {
153  return;
154  }
155 
156  willShowOnActiveCall = new QDBusPendingCallWatcher(keyboardInterface->willShowOnActive(), q);
157  connect(willShowOnActiveCall, &QDBusPendingCallWatcher::finished, q, [this](auto call) {
158  QDBusPendingReply<bool> reply = *call;
159  if (reply.isError()) {
160  qCDebug(KirigamiLog) << reply.error().message();
161  } else {
162  if (reply.value() != willShowOnActive) {
163  willShowOnActive = reply.value();
164  Q_EMIT q->willShowOnActiveChanged();
165  }
166  }
167  call->deleteLater();
168  willShowOnActiveCall = nullptr;
169  });
170 }
171 
172 void VirtualKeyboardWatcher::Private::getAllProperties()
173 {
174  auto call = new QDBusPendingCallWatcher(propertiesInterface->GetAll(interfaceName), q);
175  connect(call, &QDBusPendingCallWatcher::finished, q, [this](auto call) {
176  QDBusPendingReply<QVariantMap> reply = *call;
177  if (reply.isError()) {
178  qCDebug(KirigamiLog) << reply.error().message();
179  } else {
180  auto value = reply.value();
181  available = value.value(QStringLiteral("available")).toBool();
182  enabled = value.value(QStringLiteral("enabled")).toBool();
183  active = value.value(QStringLiteral("active")).toBool();
184  visible = value.value(QStringLiteral("visible")).toBool();
185  }
186  call->deleteLater();
187 
188  Q_EMIT q->availableChanged();
189  Q_EMIT q->enabledChanged();
190  Q_EMIT q->activeChanged();
191  Q_EMIT q->visibleChanged();
192  });
193 }
194 
195 void VirtualKeyboardWatcher::Private::getProperty(const QString &propertyName)
196 {
197  auto call = new QDBusPendingCallWatcher(propertiesInterface->Get(interfaceName, propertyName), q);
198  connect(call, &QDBusPendingCallWatcher::finished, q, [this, propertyName](auto call) {
199  QDBusPendingReply<QDBusVariant> reply = *call;
200  if (reply.isError()) {
201  qCDebug(KirigamiLog) << reply.error().message();
202  } else {
203  auto value = reply.value();
204  if (propertyName == QStringLiteral("available")) {
205  available = value.variant().toBool();
206  Q_EMIT q->availableChanged();
207  } else if (propertyName == QStringLiteral("enabled")) {
208  enabled = value.variant().toBool();
209  Q_EMIT q->enabledChanged();
210  } else if (propertyName == QStringLiteral("active")) {
211  active = value.variant().toBool();
212  Q_EMIT q->activeChanged();
213  } else if (propertyName == QStringLiteral("visible")) {
214  visible = value.variant().toBool();
215  Q_EMIT q->visibleChanged();
216  }
217  }
218  call->deleteLater();
219  });
220 }
221 
222 #endif
223 }
224 
225 #include "moc_virtualkeyboardwatcher.cpp"
void finished(QDBusPendingCallWatcher *self)
bool isError() const const
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
QDBusConnection sessionBus()
QCA_EXPORT QVariant getProperty(const QString &name)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Dec 2 2023 04:01:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.