| 1 |
/* |
| 2 |
* libkysdk-shortcut's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2025, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: ShaoShuai Tian <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "keybind.h" |
| 24 |
#include <QProcess> |
| 25 |
#include <QSettings> |
| 26 |
|
| 27 |
Keybind::Keybind(QString name, QString key, QString action, QString settingsPath, QString componentName, QObject *parent) |
| 28 |
: QObject(parent), |
| 29 |
mActionName(name), |
| 30 |
mBindKey(key), |
| 31 |
mActionCmd(action), |
| 32 |
mSettingsPath(settingsPath), |
| 33 |
mComponentName(componentName) |
| 34 |
{ |
| 35 |
mAction = new QAction(this); |
| 36 |
mAction->setObjectName(mActionName); |
| 37 |
mAction->setProperty("componentName", mComponentName); |
| 38 |
setUp(); |
| 39 |
} |
| 40 |
|
| 41 |
void Keybind::reConnectAction() |
| 42 |
{ |
| 43 |
disconnect(mAction, &QAction::triggered, this, &Keybind::onActionTriggered); |
| 44 |
setShortcut(); |
| 45 |
} |
| 46 |
|
| 47 |
void Keybind::setUp() |
| 48 |
{ |
| 49 |
setShortcut(); |
| 50 |
} |
| 51 |
|
| 52 |
void Keybind::setShortcut() |
| 53 |
{ |
| 54 |
QList<QKeySequence> seq = listFromString(); |
| 55 |
KGlobalAccel::self()->setDefaultShortcut(mAction, seq); |
| 56 |
KGlobalAccel::self()->setShortcut(mAction, seq); |
| 57 |
connect(mAction, &QAction::triggered, this, &Keybind::onActionTriggered); |
| 58 |
} |
| 59 |
|
| 60 |
void Keybind::parsingDesktop(QString exec) |
| 61 |
{ |
| 62 |
QStringList exelist; |
| 63 |
if (exec.contains(".desktop")) |
| 64 |
{ |
| 65 |
QSettings setting(exec, QSettings::IniFormat); |
| 66 |
setting.beginGroup("Desktop Entry"); |
| 67 |
QString exec = setting.value("Exec").toString(); |
| 68 |
exelist = exec.split(' '); |
| 69 |
setting.endGroup(); |
| 70 |
if (0 == exelist.count()) |
| 71 |
{ |
| 72 |
qDebug() << "error %s can't find Exec!" << exec.toLatin1().data(); |
| 73 |
return; |
| 74 |
} |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
exelist = exec.split(' '); |
| 79 |
} |
| 80 |
qDebug() << exelist[0]; |
| 81 |
QProcess process; |
| 82 |
process.startDetached(exelist[0]); |
| 83 |
} |
| 84 |
|
| 85 |
QList<QKeySequence> Keybind::listFromString() |
| 86 |
{ |
| 87 |
mBindKey.replace("<", ""); |
| 88 |
mBindKey.replace(">", "+"); |
| 89 |
if (mBindKey.contains("Win")) |
| 90 |
{ |
| 91 |
mBindKey.replace("Win", "Meta"); |
| 92 |
} |
| 93 |
return QKeySequence::listFromString(mBindKey); |
| 94 |
} |
| 95 |
|
| 96 |
void Keybind::onActionTriggered() |
| 97 |
{ |
| 98 |
qDebug() << "shortcut action name " << mActionCmd.toLatin1().data(); |
| 99 |
parsingDesktop(mActionCmd); |
| 100 |
} |