File indexing completed on 2024-05-12 17:08:56

0001 /*
0002     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0004     SPDX-FileCopyrightText: 2020 Andrey Butirsky <butirsky@gmail.com>
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "keyboardlayout.h"
0009 #include "keyboard_layout_interface.h"
0010 
0011 #include <QDBusInterface>
0012 
0013 template<>
0014 inline void KeyboardLayout::requestDBusData<KeyboardLayout::Layout>()
0015 {
0016     if (mIface)
0017         requestDBusData(mIface->getLayout(), mLayout, &KeyboardLayout::layoutChanged);
0018 }
0019 
0020 template<>
0021 inline void KeyboardLayout::requestDBusData<KeyboardLayout::LayoutsList>()
0022 {
0023     if (mIface)
0024         requestDBusData(mIface->getLayoutsList(), mLayoutsList, &KeyboardLayout::layoutsListChanged);
0025 }
0026 
0027 KeyboardLayout::KeyboardLayout(QObject *parent)
0028     : QObject(parent)
0029     , mIface(nullptr)
0030 {
0031     LayoutNames::registerMetaType();
0032 
0033     mIface = new OrgKdeKeyboardLayoutsInterface(QStringLiteral("org.kde.keyboard"), QStringLiteral("/Layouts"), QDBusConnection::sessionBus(), this);
0034     if (!mIface->isValid()) {
0035         delete mIface;
0036         mIface = nullptr;
0037         return;
0038     }
0039 
0040     connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutChanged, this, [this](uint index) {
0041         mLayout = index;
0042         Q_EMIT layoutChanged();
0043     });
0044 
0045     connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutListChanged, this, [this]() {
0046         requestDBusData<LayoutsList>();
0047         requestDBusData<Layout>();
0048     });
0049 
0050     Q_EMIT mIface->OrgKdeKeyboardLayoutsInterface::layoutListChanged();
0051 }
0052 
0053 KeyboardLayout::~KeyboardLayout()
0054 {
0055 }
0056 
0057 void KeyboardLayout::switchToNextLayout()
0058 {
0059     if (mIface)
0060         mIface->switchToNextLayout();
0061 }
0062 
0063 void KeyboardLayout::switchToPreviousLayout()
0064 {
0065     if (mIface)
0066         mIface->switchToPreviousLayout();
0067 }
0068 
0069 void KeyboardLayout::setLayout(uint index)
0070 {
0071     if (mIface)
0072         mIface->setLayout(index);
0073 }
0074 
0075 template<class T>
0076 void KeyboardLayout::requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)())
0077 {
0078     connect(new QDBusPendingCallWatcher(pendingReply, this), &QDBusPendingCallWatcher::finished, this, [this, &out, notify](QDBusPendingCallWatcher *watcher) {
0079         QDBusPendingReply<T> reply = *watcher;
0080         if (reply.isError()) {
0081             qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
0082         }
0083         out = reply.value();
0084         Q_EMIT(this->*notify)();
0085 
0086         watcher->deleteLater();
0087     });
0088 }
0089 
0090 #include "moc_keyboardlayout.cpp"