Commit 5c70dc21 authored by Andrey Butirsky's avatar Andrey Butirsky
Browse files

Keyboard Layout plugin: bring back async DBus API

Sync DBus API is more clean and precise for this plugin needs.
But we have demand of async API to not block main plasmashell thread.
parent 2fb63d85
/* /*
* Copyright (C) 2014 Daniel Vratil <dvratil@redhat.com> * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
* Copyright (C) 2019 David Edmundson <davidedmundson@kde.org> * SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
* * SPDX-FileCopyrightText: 2020 Andrey Butirsky <butirsky@gmail.com>
* This library is free software; you can redistribute it and/or * SPDX-License-Identifier: LGPL-2.1-or-later
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/ */
#include "keyboardlayout.h" #include "keyboardlayout.h"
...@@ -23,6 +10,23 @@ ...@@ -23,6 +10,23 @@
#include <QDBusInterface> #include <QDBusInterface>
template<>
void KeyboardLayout::requestDBusData<KeyboardLayout::CurrentLayout>()
{ if (mIface) requestDBusData(mIface->getCurrentLayout(), mCurrentLayout, &KeyboardLayout::currentLayoutChanged); }
template<>
void KeyboardLayout::requestDBusData<KeyboardLayout::CurrentLayoutDisplayName>()
{ if (mIface) requestDBusData(mIface->getLayoutDisplayName(mCurrentLayout), mCurrentLayoutDisplayName, &KeyboardLayout::currentLayoutDisplayNameChanged); }
template<>
void KeyboardLayout::requestDBusData<KeyboardLayout::CurrentLayoutShortName>()
{ if (mIface) requestDBusData(mIface->getCurrentLayoutShortName(), mCurrentLayoutShortName, &KeyboardLayout::currentLayoutShortNameChanged); }
template<>
void KeyboardLayout::requestDBusData<KeyboardLayout::Layouts>()
{ if (mIface) requestDBusData(mIface->getLayoutsList(), mLayouts, &KeyboardLayout::layoutsChanged); }
KeyboardLayout::KeyboardLayout(QObject* parent) KeyboardLayout::KeyboardLayout(QObject* parent)
: QObject(parent) : QObject(parent)
, mIface(nullptr) , mIface(nullptr)
...@@ -41,9 +45,12 @@ KeyboardLayout::KeyboardLayout(QObject* parent) ...@@ -41,9 +45,12 @@ KeyboardLayout::KeyboardLayout(QObject* parent)
this, &KeyboardLayout::onCurrentLayoutChanged); this, &KeyboardLayout::onCurrentLayoutChanged);
connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutListChanged, connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutListChanged,
this, &KeyboardLayout::onLayoutListChanged); this, &KeyboardLayout::onLayoutListChanged);
connect(this, &KeyboardLayout::currentLayoutChanged,
this, &KeyboardLayout::requestDBusData<CurrentLayoutDisplayName>);
mCurrentLayout = callDBus(mIface->getCurrentLayout()); requestDBusData<CurrentLayout>();
mLayouts = callDBus(mIface->getLayoutsList()); requestDBusData<CurrentLayoutShortName>();
requestDBusData<Layouts>();
} }
KeyboardLayout::~KeyboardLayout() KeyboardLayout::~KeyboardLayout()
...@@ -53,30 +60,19 @@ KeyboardLayout::~KeyboardLayout() ...@@ -53,30 +60,19 @@ KeyboardLayout::~KeyboardLayout()
void KeyboardLayout::onCurrentLayoutChanged(const QString &newLayout) void KeyboardLayout::onCurrentLayoutChanged(const QString &newLayout)
{ {
mCurrentLayout = newLayout; mCurrentLayout = newLayout;
Q_EMIT currentLayoutChanged();
}
void KeyboardLayout::onLayoutListChanged()
{
if (mIface) {
mLayouts = callDBus(mIface->getLayoutsList());
Q_EMIT layoutsChanged();
}
}
QString KeyboardLayout::currentLayoutShortName() const requestDBusData<CurrentLayoutShortName>();
{ requestDBusData<CurrentLayoutDisplayName>();
return mIface ? callDBus(mIface->getCurrentLayoutShortName()) : QString();
} }
QString KeyboardLayout::currentLayoutDisplayName() const void KeyboardLayout::onLayoutListChanged()
{ {
return mIface ? callDBus(mIface->getLayoutDisplayName(mCurrentLayout)) : QString(); requestDBusData<CurrentLayout>();
requestDBusData<CurrentLayoutShortName>();
requestDBusData<Layouts>();
} }
void KeyboardLayout::setCurrentLayout(const QString &layout) void KeyboardLayout::setCurrentLayout(const QString &layout)
{ {
if (mIface) { if (mIface) mIface->setLayout(layout);
mIface->setLayout(layout);
}
} }
...@@ -20,15 +20,16 @@ class KeyboardLayout : public QObject ...@@ -20,15 +20,16 @@ class KeyboardLayout : public QObject
Q_PROPERTY(QString currentLayout Q_PROPERTY(QString currentLayout
MEMBER mCurrentLayout MEMBER mCurrentLayout
WRITE setCurrentLayout) WRITE setCurrentLayout
NOTIFY currentLayoutChanged)
Q_PROPERTY(QString currentLayoutDisplayName Q_PROPERTY(QString currentLayoutDisplayName
READ currentLayoutDisplayName MEMBER mCurrentLayoutDisplayName
NOTIFY currentLayoutChanged) NOTIFY currentLayoutDisplayNameChanged)
Q_PROPERTY(QString currentLayoutShortName Q_PROPERTY(QString currentLayoutShortName
READ currentLayoutShortName MEMBER mCurrentLayoutShortName
NOTIFY currentLayoutChanged) NOTIFY currentLayoutShortNameChanged)
Q_PROPERTY(QStringList layouts Q_PROPERTY(QStringList layouts
MEMBER mLayouts MEMBER mLayouts
...@@ -40,34 +41,47 @@ public: ...@@ -40,34 +41,47 @@ public:
Q_SIGNALS: Q_SIGNALS:
void currentLayoutChanged(); void currentLayoutChanged();
void currentLayoutDisplayNameChanged();
void currentLayoutShortNameChanged();
void layoutsChanged(); void layoutsChanged();
private: private:
void onCurrentLayoutChanged(const QString &newLayout);
void onLayoutListChanged();
QString currentLayoutShortName() const;
QString currentLayoutDisplayName() const;
void setCurrentLayout(const QString &layout); void setCurrentLayout(const QString &layout);
enum DBusData {CurrentLayout, CurrentLayoutDisplayName, CurrentLayoutShortName, Layouts};
template<class T> template<class T>
static T callDBus(QDBusPendingReply<T> pendingReply); void requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)());
template<DBusData>
inline void requestDBusData();
void onCurrentLayoutChanged(const QString &newLayout);
void onLayoutListChanged();
QStringList mLayouts; QStringList mLayouts;
QString mCurrentLayout; QString mCurrentLayout;
QString mCurrentLayoutDisplayName;
QString mCurrentLayoutShortName;
OrgKdeKeyboardLayoutsInterface *mIface; OrgKdeKeyboardLayoutsInterface *mIface;
}; };
template<class T> template<class T>
T KeyboardLayout::callDBus(QDBusPendingReply<T> pendingReply) void KeyboardLayout::requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)())
{ {
pendingReply.waitForFinished(); const QDBusPendingCallWatcher * const watcher = new QDBusPendingCallWatcher(pendingReply, this);
if (pendingReply.isError()) { connect(watcher, &QDBusPendingCallWatcher::finished, this,
qCWarning(KEYBOARD_LAYOUT) << pendingReply.error().message(); [this, &out, notify](QDBusPendingCallWatcher *watcher)
return {}; {
} else { QDBusPendingReply<T> reply = *watcher;
return pendingReply.value(); if (reply.isError()) {
} qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
} else {
out = reply.value();
emit (this->*notify)();
}
watcher->deleteLater();
}
);
} }
#endif // KEYBOARDLAYOUT_H #endif // KEYBOARDLAYOUT_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment