aboutsummaryrefslogtreecommitdiff
path: root/src/gui/kstandardshortcutwatcher.cpp
blob: 1be5e503af4a156126779d9a611d8b8f030818c6 (plain)
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
/*
    SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>

    SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "kstandardshortcutwatcher.h"

#include "kconfigwatcher.h"
#include "kstandardshortcut_p.h"

namespace KStandardShortcut
{
class StandardShortcutWatcherPrivate
{
public:
    KConfigWatcher::Ptr watcher = KConfigWatcher::create(KSharedConfig::openConfig());
};

StandardShortcutWatcher::StandardShortcutWatcher(QObject *parent)
    : QObject(parent)
    , d(std::make_unique<StandardShortcutWatcherPrivate>())
{
    connect(d->watcher.get(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &keys) {
        if (group.name() != QStringLiteral("Shortcuts")) {
            return;
        }
        for (const auto &key : keys) {
            const StandardShortcut shortcut = KStandardShortcut::findByName(QString::fromUtf8(key));
            if (shortcut != KStandardShortcut::AccelNone) {
                initialize(shortcut);
                Q_EMIT shortcutChanged(shortcut, KStandardShortcut::shortcut(shortcut));
            }
        }
    });
}

StandardShortcutWatcher::~StandardShortcutWatcher() = default;

StandardShortcutWatcher *shortcutWatcher()
{
    static StandardShortcutWatcher watcher;
    return &watcher;
}

}