keyboardlayout.h 2.5 KB
Newer Older
1
/*
2 3 4
 * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
 * SPDX-FileCopyrightText: 2020 Andrey Butirsky <butirsky@gmail.com>
 * SPDX-License-Identifier: LGPL-2.1-or-later
5 6 7 8 9
 */

#ifndef KEYBOARDLAYOUT_H
#define KEYBOARDLAYOUT_H

10 11 12
#include <QDBusReply>

#include "debug.h"
13

14
class OrgKdeKeyboardLayoutsInterface;
15 16 17 18 19 20 21
class QDBusPendingCallWatcher;

class KeyboardLayout : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString currentLayout
22
               MEMBER mCurrentLayout
23 24
               WRITE setCurrentLayout
               NOTIFY currentLayoutChanged)
25

26
    Q_PROPERTY(QString currentLayoutDisplayName
27 28
               MEMBER mCurrentLayoutDisplayName
               NOTIFY currentLayoutDisplayNameChanged)
29 30

    Q_PROPERTY(QString currentLayoutShortName
31 32
               MEMBER mCurrentLayoutShortName
               NOTIFY currentLayoutShortNameChanged)
33

34
    Q_PROPERTY(QStringList layouts
35
               MEMBER mLayouts
36 37 38
               NOTIFY layoutsChanged)

public:
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
39
    explicit KeyboardLayout(QObject *parent = nullptr);
40
    ~KeyboardLayout() override;
41 42

Q_SIGNALS:
43
    void currentLayoutChanged();
44 45
    void currentLayoutDisplayNameChanged();
    void currentLayoutShortNameChanged();
46 47
    void layoutsChanged();

48 49
private:
    void setCurrentLayout(const QString &layout);
50

51 52
    enum DBusData {CurrentLayout, CurrentLayoutDisplayName, CurrentLayoutShortName, Layouts};

53
    template<class T>
54 55 56 57 58 59
    void requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)());
    template<DBusData>
    inline void requestDBusData();

    void onCurrentLayoutChanged(const QString &newLayout);
    void onLayoutListChanged();
60 61 62

    QStringList mLayouts;
    QString mCurrentLayout;
63 64
    QString mCurrentLayoutDisplayName;
    QString mCurrentLayoutShortName;
65
    OrgKdeKeyboardLayoutsInterface *mIface;
66 67
};

68
template<class T>
69
void KeyboardLayout::requestDBusData(QDBusPendingReply<T> pendingReply, T &out, void (KeyboardLayout::*notify)())
70
{
71 72 73 74 75 76 77 78 79 80 81 82 83 84
    const QDBusPendingCallWatcher * const watcher = new QDBusPendingCallWatcher(pendingReply, this);
    connect(watcher, &QDBusPendingCallWatcher::finished, this,
        [this, &out, notify](QDBusPendingCallWatcher *watcher)
        {
            QDBusPendingReply<T> reply = *watcher;
            if (reply.isError()) {
                qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
            } else {
                out = reply.value();
                emit (this->*notify)();
            }
            watcher->deleteLater();
        }
    );
85
}
86 87

#endif // KEYBOARDLAYOUT_H