Changeset View
Changeset View
Standalone View
Standalone View
helpers/virtualkeyboard/sni.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org> | ||||
6 | | ||||
7 | This program is free software; you can redistribute it and/or modify | ||||
8 | it under the terms of the GNU General Public License as published by | ||||
9 | the Free Software Foundation; either version 2 of the License, or | ||||
10 | (at your option) any later version. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | #include "sni.h" | ||||
21 | | ||||
22 | #include <QDBusConnection> | ||||
23 | #include <QDBusMessage> | ||||
24 | #include <QDBusPendingReply> | ||||
25 | | ||||
26 | #include <KLocalizedString> | ||||
27 | #include <KStatusNotifierItem> | ||||
28 | | ||||
29 | static const QString s_service = QStringLiteral("org.kde.KWin"); | ||||
30 | static const QString s_object = QStringLiteral("/VirtualKeyboard"); | ||||
31 | static const QString s_interface = QStringLiteral("org.kde.kwin.VirtualKeyboard"); | ||||
32 | | ||||
33 | Sni::Sni(QObject *parent) | ||||
34 | : QObject(parent) | ||||
35 | , m_sni(new KStatusNotifierItem(QStringLiteral("kwin-virtual-keyboard"), this)) | ||||
36 | { | ||||
37 | m_sni->setStandardActionsEnabled(false); | ||||
38 | m_sni->setCategory(KStatusNotifierItem::Hardware); | ||||
39 | m_sni->setStatus(KStatusNotifierItem::Passive); | ||||
40 | m_sni->setTitle(i18n("Virtual Keyboard")); | ||||
41 | connect(m_sni, &KStatusNotifierItem::activateRequested, this, | ||||
42 | [this] { | ||||
43 | const QString method = m_enabled ? QStringLiteral("disable") : QStringLiteral("enable"); | ||||
44 | const QDBusMessage message = QDBusMessage::createMethodCall(s_service, s_object, s_interface, method); | ||||
45 | QDBusConnection::sessionBus().asyncCall(message); | ||||
46 | } | ||||
47 | ); | ||||
48 | QDBusConnection::sessionBus().connect(s_service, s_object, s_interface, QStringLiteral("enabledChanged"), this, SLOT(updateEnabled())); | ||||
49 | updateEnabled(); | ||||
50 | update(); | ||||
51 | } | ||||
52 | | ||||
53 | Sni::~Sni() = default; | ||||
54 | | ||||
55 | void Sni::updateEnabled() | ||||
56 | { | ||||
57 | const QDBusMessage message = QDBusMessage::createMethodCall(s_service, s_object, s_interface, QStringLiteral("isEnabled")); | ||||
58 | const QDBusPendingReply<bool> reply = QDBusConnection::sessionBus().asyncCall(message); | ||||
59 | | ||||
60 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); | ||||
61 | connect(watcher, &QDBusPendingCallWatcher::finished, this, | ||||
62 | [this](QDBusPendingCallWatcher *self) { | ||||
63 | QDBusPendingReply<bool> reply = *self; | ||||
64 | self->deleteLater(); | ||||
65 | if (!reply.isValid()) { | ||||
66 | return; | ||||
67 | } | ||||
68 | const bool enabled = reply.value(); | ||||
69 | if (m_enabled == enabled) { | ||||
70 | return; | ||||
71 | } | ||||
72 | m_enabled = enabled; | ||||
73 | update(); | ||||
74 | } | ||||
75 | ); | ||||
76 | } | ||||
77 | | ||||
78 | void Sni::update() | ||||
79 | { | ||||
80 | if (m_enabled) { | ||||
81 | m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-on")); | ||||
82 | m_sni->setToolTipTitle(i18n("Virtual Keyboard is enabled.")); | ||||
83 | } else { | ||||
84 | m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-off")); | ||||
85 | m_sni->setToolTipTitle(i18n("Virtual Keyboard is disabled.")); | ||||
86 | } | ||||
87 | } |