File: keyboardlayout.cpp

package info (click to toggle)
plasma-workspace 4%3A5.8.6-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 54,996 kB
  • sloc: cpp: 70,006; sh: 868; xml: 867; python: 46; makefile: 16
file content (162 lines) | stat: -rw-r--r-- 4,867 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2014  Daniel Vratil <dvratil@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * 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 <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>

#include <QDebug>
#include "debug.h"

KeyboardLayout::KeyboardLayout(QObject* parent)
    : QObject(parent)
    , mIface(0)
{
    mIface = new QDBusInterface(QStringLiteral("org.kde.kded5"),
                                QStringLiteral("/modules/keyboard"),
                                QStringLiteral("org.kde.KeyboardLayouts"),
                                QDBusConnection::sessionBus(),
                                this);
    if (!mIface->isValid()) {
          delete mIface;
          mIface = 0;
          return;
    }

    connect(mIface, SIGNAL(currentLayoutChanged(QString)),
            this, SLOT(setCurrentLayout(QString)));
    connect(mIface, SIGNAL(layoutListChanged()),
            this, SLOT(requestLayoutsList()));

    requestCurrentLayout();
    requestLayoutsList();

}

KeyboardLayout::~KeyboardLayout()
{
}

void KeyboardLayout::requestCurrentLayout()
{
    if (!mIface) {
        return;
    }

    QDBusPendingCall pendingLayout = mIface->asyncCall(QStringLiteral("getCurrentLayout"));
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingLayout, this);
    connect(watcher, &QDBusPendingCallWatcher::finished,
            this, &KeyboardLayout::onCurrentLayoutReceived);
}

void KeyboardLayout::onCurrentLayoutReceived(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<QString> reply = *watcher;
    if (reply.isError()) {
        qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
    } else {
        mCurrentLayout = reply.value();
        requestCurrentLayoutDisplayName();
        Q_EMIT currentLayoutChanged(mCurrentLayout);
    }
    watcher->deleteLater();
}

void KeyboardLayout::requestCurrentLayoutDisplayName()
{
    QDBusPendingCall pendingDisplayName = mIface->asyncCallWithArgumentList(QStringLiteral("getLayoutDisplayName"), {mCurrentLayout});
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingDisplayName, this);
    connect(watcher, &QDBusPendingCallWatcher::finished, this, &KeyboardLayout::onCurrentLayoutDisplayNameReceived);
}

void KeyboardLayout::onCurrentLayoutDisplayNameReceived(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<QString> reply = *watcher;
    if (reply.isError()) {
        qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
    } else {
        mCurrentLayoutDisplayName = reply.value();
        Q_EMIT currentLayoutDisplayNameChanged(mCurrentLayoutDisplayName);
    }
    watcher->deleteLater();
}

QString KeyboardLayout::currentLayoutDisplayName() const
{
    return mCurrentLayoutDisplayName;
}

void KeyboardLayout::requestLayoutsList()
{
    if (!mIface) {
        return;
    }

    QDBusPendingCall pendingLayout = mIface->asyncCall(QStringLiteral("getLayoutsList"));
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingLayout, this);
    connect(watcher, &QDBusPendingCallWatcher::finished,
            this, &KeyboardLayout::onLayoutsListReceived);
}


void KeyboardLayout::onLayoutsListReceived(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<QStringList> reply = *watcher;
    if (reply.isError()) {
        qCWarning(KEYBOARD_LAYOUT) << reply.error().message();
    } else {
        mLayouts = reply.value();
        qCDebug(KEYBOARD_LAYOUT) << "Layouts list changed: " << mLayouts;
        Q_EMIT layoutsChanged();
    }
    watcher->deleteLater();
}

QString KeyboardLayout::currentLayout() const
{
    return mCurrentLayout;
}

void KeyboardLayout::setCurrentLayout(const QString &layout)
{
    if (!mIface) {
        return;
    }
    if (mCurrentLayout == layout) {
        return;
    }

    if (!mLayouts.contains(layout)) {
        qCWarning(KEYBOARD_LAYOUT) << "No such layout" << layout;
        return;
    }

    mCurrentLayout = layout;
    requestCurrentLayoutDisplayName();
    mIface->asyncCall(QStringLiteral("setLayout"), layout);
    Q_EMIT currentLayoutChanged(layout);
}


QStringList KeyboardLayout::layouts() const
{
    return mLayouts;
}