1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
/*
SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
SPDX-FileCopyrightText: 2020 Andrey Butirsky <butirsky@gmail.com>
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "keyboardlayout.h"
#include "keyboard_layout_interface.h"
#include <QDBusInterface>
template<>
inline void KeyboardLayout::requestDBusData<KeyboardLayout::Layout>()
{
if (mIface)
requestDBusData(mIface->getLayout(), mLayout, &KeyboardLayout::layoutChanged);
}
template<>
inline void KeyboardLayout::requestDBusData<KeyboardLayout::LayoutsList>()
{
if (mIface)
requestDBusData(mIface->getLayoutsList(), mLayoutsList, &KeyboardLayout::layoutsListChanged);
}
KeyboardLayout::KeyboardLayout(QObject *parent)
: QObject(parent)
, mIface(nullptr)
{
LayoutNames::registerMetaType();
mIface = new OrgKdeKeyboardLayoutsInterface(QStringLiteral("org.kde.keyboard"), QStringLiteral("/Layouts"), QDBusConnection::sessionBus(), this);
if (!mIface->isValid()) {
delete mIface;
mIface = nullptr;
return;
}
connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutChanged, this, [this](uint index) {
mLayout = index;
Q_EMIT layoutChanged();
});
connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutListChanged, this, [this]() {
requestDBusData<LayoutsList>();
requestDBusData<Layout>();
});
Q_EMIT mIface->OrgKdeKeyboardLayoutsInterface::layoutListChanged();
}
KeyboardLayout::~KeyboardLayout()
{
}
void KeyboardLayout::switchToNextLayout()
{
if (mIface)
mIface->switchToNextLayout();
}
void KeyboardLayout::switchToPreviousLayout()
{
if (mIface)
mIface->switchToPreviousLayout();
}
void KeyboardLayout::setLayout(uint index)
{
if (mIface)
mIface->setLayout(index);
}
template<class T>
void KeyboardLayout::requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)())
{
connect(new QDBusPendingCallWatcher(pendingReply, this), &QDBusPendingCallWatcher::finished, this, [this, &out, notify](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<T> reply = *watcher;
if (reply.isError()) {
qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
}
out = reply.value();
Q_EMIT(this->*notify)();
watcher->deleteLater();
});
}
#include "moc_keyboardlayout.cpp"
|