keyboardlayout.cpp 2.64 KB
Newer Older
1
/*
2 3 4 5
 * 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
6 7 8
 */

#include "keyboardlayout.h"
9
#include "keyboard_layout_interface.h"
10

11
#include <QDBusInterface>
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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); }


30 31
KeyboardLayout::KeyboardLayout(QObject* parent)
    : QObject(parent)
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
32
    , mIface(nullptr)
33
{
34
    mIface = new OrgKdeKeyboardLayoutsInterface(QStringLiteral("org.kde.keyboard"),
35
                                QStringLiteral("/Layouts"),
36 37 38 39
                                QDBusConnection::sessionBus(),
                                this);
    if (!mIface->isValid()) {
          delete mIface;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
40
          mIface = nullptr;
41 42 43
          return;
    }

44
    connect(mIface, &OrgKdeKeyboardLayoutsInterface::currentLayoutChanged,
45
            this, &KeyboardLayout::onCurrentLayoutChanged);
46
    connect(mIface, &OrgKdeKeyboardLayoutsInterface::layoutListChanged,
47
            this, &KeyboardLayout::onLayoutListChanged);
48 49
    connect(this, &KeyboardLayout::currentLayoutChanged,
            this, &KeyboardLayout::requestDBusData<CurrentLayoutDisplayName>);
50

51 52 53
    requestDBusData<CurrentLayout>();
    requestDBusData<CurrentLayoutShortName>();
    requestDBusData<Layouts>();
54 55 56 57 58 59
}

KeyboardLayout::~KeyboardLayout()
{
}

60
void KeyboardLayout::onCurrentLayoutChanged(const QString &newLayout)
61
{
62
    mCurrentLayout = newLayout;
63

64 65
    requestDBusData<CurrentLayoutShortName>();
    requestDBusData<CurrentLayoutDisplayName>();
66 67
}

68
void KeyboardLayout::onLayoutListChanged()
69
{
70 71 72
    requestDBusData<CurrentLayout>();
    requestDBusData<CurrentLayoutShortName>();
    requestDBusData<Layouts>();
73 74 75 76
}

void KeyboardLayout::setCurrentLayout(const QString &layout)
{
77
    if (mIface) mIface->setLayout(layout);
78
}