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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
* -*- coding: utf-8 -*-
*
* Copyright (C) 2023 KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QDir>
#include <QProcess>
#include "keybindings-wayland-manager.h"
#include "clib-syslog.h"
#define GSETTINGS_KEYBINDINGS_DIR "/org/ukui/desktop/keybindings/"
#define CUSTOM_KEYBINDING_SCHEMA "org.ukui.control-center.keybinding"
#define ACTION "action"
#define BINDING "binding"
#define NAME "name"
#define CUSTOM_COMPONET_NAME "usd_keybindings"
KeybindingsWaylandManager *KeybindingsWaylandManager::m_keybinding = nullptr;
KeybindingsWaylandManager::KeybindingsWaylandManager()
{
}
KeybindingsWaylandManager::~KeybindingsWaylandManager()
{
if (m_dconfClient) {
dconf_client_unwatch_fast(m_dconfClient, GSETTINGS_KEYBINDINGS_DIR);
dconf_client_unwatch_sync(m_dconfClient, GSETTINGS_KEYBINDINGS_DIR);
g_object_unref (m_dconfClient);
m_dconfClient = nullptr;
}
clearShortcutList();
}
KeybindingsWaylandManager *KeybindingsWaylandManager::KeybindingsWaylandManagerNew()
{
if(nullptr == m_keybinding)
m_keybinding = new KeybindingsWaylandManager();
return m_keybinding;
}
void KeybindingsWaylandManager::bindings_callback (DConfClient *client,
gchar *prefix,
const gchar **changes,
gchar *tag,
KeybindingsWaylandManager *manager)
{
Q_UNUSED(client)
Q_UNUSED(changes)
Q_UNUSED(tag)
if (strncmp(GSETTINGS_KEYBINDINGS_DIR,prefix,strlen(GSETTINGS_KEYBINDINGS_DIR))) {
return;
}
//卸载所有自定义快捷键
manager->unRegisterShortcutAll();
//重新注册已定义的热键
manager->registerShortcutAll();
qDebug()<<prefix;
}
bool KeybindingsWaylandManager::start()
{
USD_LOG(LOG_DEBUG,"-- Keybindings Wayland Manager Start --");
//清除kglobalaceel 的配置
clearKglobalShortcutAll();
registerShortcutAll();
if(!m_dconfClient){
m_dconfClient = dconf_client_new ();
dconf_client_watch_fast (m_dconfClient, GSETTINGS_KEYBINDINGS_DIR);
dconf_client_watch_sync (m_dconfClient, GSETTINGS_KEYBINDINGS_DIR);
g_signal_connect (m_dconfClient, "changed", G_CALLBACK (bindings_callback), this);
}
return true;
}
QStringList KeybindingsWaylandManager::getCustomShortcutPath()
{
int len;
gchar ** childs;
QStringList path;
DConfClient * client = dconf_client_new();
childs = dconf_client_list (client, GSETTINGS_KEYBINDINGS_DIR, &len);
g_object_unref (client);
for (int i = 0; childs[i] != NULL; i++){
if (dconf_is_rel_dir (childs[i], NULL)){
gchar * val = g_strdup (childs[i]);
path<<val;
}
}
g_strfreev (childs);
return path;
}
void KeybindingsWaylandManager::registerShortcutAll()
{
QStringList customPaths = getCustomShortcutPath();
for (QString path : customPaths) {
QString customPath = QString(GSETTINGS_KEYBINDINGS_DIR) + path;
GSettings* settings;
settings = g_settings_new_with_path (CUSTOM_KEYBINDING_SCHEMA, customPath.toLatin1().data());
if(settings){
QString path = customPath;
QString action = QString(g_settings_get_string (settings, ACTION));
QString binding = QString(g_settings_get_string (settings, BINDING));
QString name = QString(g_settings_get_string (settings, NAME));
USD_LOG(LOG_DEBUG,"keybindings name : %s key : %s action : %s",
name.toLocal8Bit().data(),binding.toLatin1().data(),action.toLatin1().data());
ShortCutKeyBind* bind = new ShortCutKeyBind(path,name,binding,action,CUSTOM_COMPONET_NAME);
m_shortcutList.append(bind);
g_object_unref (settings);
}
}
}
void KeybindingsWaylandManager::clearKglobalShortcutAll()
{
QString path = QDir::homePath() + QStringLiteral("/.config/kglobalshortcutsrc");
qDebug()<<path;
QSettings settings(path,QSettings::IniFormat);
settings.beginGroup(CUSTOM_COMPONET_NAME);
QStringList strList = settings.allKeys();
for(QString str : strList){
QStringList list = settings.value(str).toStringList();
if(list.contains("ukui-settings-daemon")){
continue;
}
QAction action;
action.setObjectName(str.toLatin1().data());
action.setProperty("componentName",CUSTOM_COMPONET_NAME);
QList<QKeySequence> seq = QKeySequence::listFromString(list.at(0));
KGlobalAccel::self()->setDefaultShortcut(&action, seq);
KGlobalAccel::self()->setShortcut(&action, seq);
KGlobalAccel::self()->removeAllShortcuts(&action);
}
settings.endGroup();
}
void KeybindingsWaylandManager::unRegisterShortcutAll()
{
if(m_shortcutList.isEmpty()){
return;
}
for(ShortCutKeyBind* bind : m_shortcutList){
KGlobalAccel::self()->removeAllShortcuts(bind->action());
}
qDeleteAll(m_shortcutList);
m_shortcutList.clear();
}
void KeybindingsWaylandManager::clearShortcutList()
{
if(m_shortcutList.isEmpty()){
return;
}
qDeleteAll(m_shortcutList);
m_shortcutList.clear();
}
void KeybindingsWaylandManager::stop()
{
USD_LOG(LOG_DEBUG,"Stopping keybindings manager");
if (m_dconfClient) {
dconf_client_unwatch_fast(m_dconfClient, GSETTINGS_KEYBINDINGS_DIR);
dconf_client_unwatch_sync(m_dconfClient, GSETTINGS_KEYBINDINGS_DIR);
g_object_unref (m_dconfClient);
m_dconfClient = nullptr;
}
clearShortcutList();
}
/*********************ShortCutKeyBind***********************/
ShortCutKeyBind::ShortCutKeyBind(QString settingsPath, QString actionName, QString bindKey, QString execName,QString componetName,QObject* parent)
:m_settingsPath(settingsPath),
m_actionName(actionName),
m_bindKey(bindKey),
m_execName(execName),
m_componentName(componetName),
QObject(parent)
{
m_action = new QAction(this);
m_action->setObjectName(m_actionName);
m_action->setProperty("componentName", m_componentName);
setUp();
}
ShortCutKeyBind::~ShortCutKeyBind()
{
}
void ShortCutKeyBind::setUp()
{
setShortcut();
}
void ShortCutKeyBind::setShortcut()
{
QList<QKeySequence> seq = listFromString();
KGlobalAccel::self()->setDefaultShortcut(m_action, seq);
KGlobalAccel::self()->setShortcut(m_action, seq);
connect(m_action, &QAction::triggered,this,[this]() {
USD_LOG(LOG_DEBUG,"shortcut action name %s",m_execName.toLatin1().data());
parsingDesktop(m_execName);
});
}
QList<QKeySequence> ShortCutKeyBind::listFromString()
{
m_bindKey.replace("<","");
m_bindKey.replace(">","+");
if(m_bindKey.contains("Win")){
m_bindKey.replace("Win","Meta");
}
return QKeySequence::listFromString(m_bindKey);
}
void ShortCutKeyBind::parsingDesktop(QString exec)
{
if(exec.contains("desktop")){
QSettings setting(exec,QSettings::IniFormat);
setting.beginGroup("Desktop Entry");
QString name = setting.value("Name").toString();
QString type = setting.value("Type").toString();
QString exec = setting.value("Exec").toString();
setting.endGroup();
Q_UNUSED(name)
Q_UNUSED(type)
QProcess process;
process.startDetached(exec);
}
}
|