File: globalaccel.cpp

package info (click to toggle)
kmenuedit 4%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,768 kB
  • sloc: cpp: 3,882; xml: 161; makefile: 3; sh: 2
file content (51 lines) | stat: -rw-r--r-- 1,856 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
/*
 * SPDX-FileCopyrightText: 2019 David Redondo <kde@david-redondo.de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "globalaccel.h"

#include <QAction>
#include <QFile>
#include <QStandardPaths>

#include <KGlobalAccel>
#include <KLocalizedString>

void GlobalAccel::changeMenuEntryShortcut(const KService::Ptr &service, const QKeySequence &shortcut)
{
    if (!service) {
        return;
    }

    const QString desktopFile = QStringLiteral("%1.desktop").arg(service->desktopEntryName());

    // if the component doesn't exist already, and we don't want any shortcuts, don't create one
    if (!KGlobalAccel::isComponentActive(desktopFile) && shortcut.isEmpty()) {
        return;
    }

    if (!KGlobalAccel::isComponentActive(desktopFile)) {
        const QString destination = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kglobalaccel/") + desktopFile;
        QFile::copy(service->entryPath(), destination);
    }
    QAction action(i18n("Launch %1", service->name()));
    action.setProperty("componentName", desktopFile);
    action.setProperty("componentDisplayName", service->name());
    action.setObjectName(QStringLiteral("_launch"));
    // Make sure that the action is marked active
    KGlobalAccel::self()->setShortcut(&action, {shortcut});
    action.setProperty("isConfigurationAction", true);
    KGlobalAccel::self()->setShortcut(&action, {shortcut}, KGlobalAccel::NoAutoloading);
}

QKeySequence GlobalAccel::getMenuEntryShortcut(const KService::Ptr &service)
{
    const QString desktopFile = QStringLiteral("%1.desktop").arg(service->desktopEntryName());
    const QList<QKeySequence> shortcut = KGlobalAccel::self()->globalShortcut(desktopFile, QStringLiteral("_launch"));
    if (!shortcut.isEmpty()) {
        return shortcut[0];
    }
    return QKeySequence();
}