| 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 "keybindingsmanager.h" |
| 24 |
#include <QGSettings/QGSettings> |
| 25 |
#include <QDir> |
| 26 |
#include <QSettings> |
| 27 |
|
| 28 |
#define KYSDK_COMPONET_NAME "kysdk-keybindings" |
| 29 |
|
| 30 |
KeybindingsManager *KeybindingsManager::mKeybindingsManager = nullptr; |
| 31 |
KeybindingsManager::KeybindingsManager(QObject *parent) |
| 32 |
: QObject(parent), |
| 33 |
mDconfClient(nullptr) |
| 34 |
{ |
| 35 |
start(); |
| 36 |
} |
| 37 |
|
| 38 |
KeybindingsManager::~KeybindingsManager() |
| 39 |
{ |
| 40 |
if (mDconfClient) |
| 41 |
{ |
| 42 |
dconf_client_unwatch_fast(mDconfClient, KYSDK_KEYBINDINGS_DIR); |
| 43 |
dconf_client_unwatch_sync(mDconfClient, KYSDK_KEYBINDINGS_DIR); |
| 44 |
g_object_unref(mDconfClient); |
| 45 |
mDconfClient = nullptr; |
| 46 |
} |
| 47 |
clearShortcutList(); |
| 48 |
} |
| 49 |
|
| 50 |
KeybindingsManager *KeybindingsManager::getKeybindingsManager() |
| 51 |
{ |
| 52 |
if (mKeybindingsManager == nullptr) |
| 53 |
mKeybindingsManager = new KeybindingsManager(); |
| 54 |
return mKeybindingsManager; |
| 55 |
} |
| 56 |
|
| 57 |
bool KeybindingsManager::start() |
| 58 |
{ |
| 59 |
clearKglobalShortcutAll(); |
| 60 |
registerShortcutAll(); |
| 61 |
if (!mDconfClient) |
| 62 |
{ |
| 63 |
mDconfClient = dconf_client_new(); |
| 64 |
dconf_client_watch_fast(mDconfClient, KYSDK_KEYBINDINGS_DIR); |
| 65 |
dconf_client_watch_sync(mDconfClient, KYSDK_KEYBINDINGS_DIR); |
| 66 |
g_signal_connect(mDconfClient, "changed", G_CALLBACK(bindingsCallback), this); |
| 67 |
} |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
void KeybindingsManager::stop() |
| 73 |
{ |
| 74 |
if (mDconfClient) |
| 75 |
{ |
| 76 |
dconf_client_unwatch_fast(mDconfClient, KYSDK_KEYBINDINGS_DIR); |
| 77 |
dconf_client_unwatch_sync(mDconfClient, KYSDK_KEYBINDINGS_DIR); |
| 78 |
g_object_unref(mDconfClient); |
| 79 |
mDconfClient = nullptr; |
| 80 |
} |
| 81 |
clearShortcutList(); |
| 82 |
} |
| 83 |
|
| 84 |
void KeybindingsManager::bindingsCallback(DConfClient *client, gchar *prefix, const gchar **changes, gchar *tag, KeybindingsManager *manager) |
| 85 |
{ |
| 86 |
Q_UNUSED(client) |
| 87 |
Q_UNUSED(changes) |
| 88 |
Q_UNUSED(tag) |
| 89 |
|
| 90 |
if (strncmp(KYSDK_KEYBINDINGS_DIR, prefix, strlen(KYSDK_KEYBINDINGS_DIR))) |
| 91 |
{ |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
// 卸载所有自定义快捷键 |
| 96 |
manager->unRegisterShortcutAll(); |
| 97 |
// 重新注册已定义的热键 |
| 98 |
manager->registerShortcutAll(); |
| 99 |
qDebug() << prefix; |
| 100 |
} |
| 101 |
|
| 102 |
void KeybindingsManager::registerShortcutAll() |
| 103 |
{ |
| 104 |
KShortcutUtil util; |
| 105 |
QStringList existsDirs = util.listExistsShortcutPath(KYSDK_KEYBINDINGS_DIR); |
| 106 |
for (QString dir : existsDirs) |
| 107 |
{ |
| 108 |
QString gsettingsPath = QString("%1%2").arg(KYSDK_KEYBINDINGS_DIR).arg(dir); |
| 109 |
QGSettings setting(KYSDK_KEYBINDING_SCHEMA, gsettingsPath.toLatin1()); |
| 110 |
QString name = setting.get(NAME_KEY).toString(); |
| 111 |
QString binding = setting.get(BINDING_KEY).toString(); |
| 112 |
QString action = setting.get(ACTION_KEY).toString(); |
| 113 |
Keybind *key = new Keybind(name, binding, action, gsettingsPath, KYSDK_COMPONET_NAME, this); |
| 114 |
mKeybinds.append(key); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void KeybindingsManager::unRegisterShortcutAll() |
| 119 |
{ |
| 120 |
if (mKeybinds.isEmpty()) |
| 121 |
return; |
| 122 |
for (Keybind *key : mKeybinds) |
| 123 |
{ |
| 124 |
KGlobalAccel::self()->removeAllShortcuts(key->action()); |
| 125 |
} |
| 126 |
qDeleteAll(mKeybinds); |
| 127 |
mKeybinds.clear(); |
| 128 |
} |
| 129 |
|
| 130 |
void KeybindingsManager::clearShortcutList() |
| 131 |
{ |
| 132 |
if (mKeybinds.isEmpty()) |
| 133 |
{ |
| 134 |
return; |
| 135 |
} |
| 136 |
qDeleteAll(mKeybinds); |
| 137 |
mKeybinds.clear(); |
| 138 |
} |
| 139 |
|
| 140 |
void KeybindingsManager::clearKglobalShortcutAll() |
| 141 |
{ |
| 142 |
QString path = QDir::homePath() + QStringLiteral("/.config/kglobalshortcutsrc"); |
| 143 |
qDebug() << path; |
| 144 |
QSettings settings(path, QSettings::IniFormat); |
| 145 |
settings.beginGroup(KYSDK_COMPONET_NAME); |
| 146 |
QStringList strList = settings.allKeys(); |
| 147 |
for (QString str : strList) |
| 148 |
{ |
| 149 |
QStringList list = settings.value(str).toStringList(); |
| 150 |
if (list.contains("kdkshortcut")) |
| 151 |
{ |
| 152 |
continue; |
| 153 |
} |
| 154 |
QAction action; |
| 155 |
action.setObjectName(str.toLatin1().data()); |
| 156 |
action.setProperty("componentName", KYSDK_COMPONET_NAME); |
| 157 |
QList<QKeySequence> seq = QKeySequence::listFromString(list.at(0)); |
| 158 |
KGlobalAccel::self()->setDefaultShortcut(&action, seq); |
| 159 |
KGlobalAccel::self()->setShortcut(&action, seq); |
| 160 |
KGlobalAccel::self()->removeAllShortcuts(&action); |
| 161 |
} |
| 162 |
settings.endGroup(); |
| 163 |
} |