Debian Codegraph @

kysdk-shortcut/libkyshortcut/kglobalshortcut.cpp

raw
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 "kglobalshortcut.h"
24 #include "../utils/kshortcututil.h"
25 #include "libkyshortcut.h"
26
27 #include <KGlobalAccel>
28 #include <KStandardShortcut>
29 #include <QGSettings>
30 #include <QDebug>
31 #include <QProcess>
32 #include <QDBusInterface>
33 #include <QDBusReply>
34 #include <QFile>
35 #include <QDBusArgument>
36 #include <QRegularExpression>
37
38 #define KDE_GLOBALACCEL_SERVER "org.kde.kglobalaccel"
39 #define KDE_GLOBALACCEL_PATH "/kglobalaccel"
40 #define KDE_GLOBALACCEL_INTERFACE "org.kde.KGlobalAccel"
41 #define KDE_ALL_COMPONENTS_METHOD "allComponents"
42 #define KDE_GLOBALACCEL_COMPONENT_INTERFACE "org.kde.kglobalaccel.Component"
43 #define KDE_ALL_SHORTCUT_INFOS_METHOD "allShortcutInfos"
44 #define KYSDK_COMPONET_NAME "kysdk-keybindings"
45
46 // 定义结构体来匹配返回的数据类型
47 struct DShortcutInfo
48 {
49 QString contextUniqueName;
50 QString contextFriendlyName;
51 QString componentUniqueName;
52 QString componentFriendlyName;
53 QString uniqueName;
54 QString friendlyName;
55 QList<int> keys;
56 QList<int> defaultKeys;
57 };
58
59 // 重载 QDBusArgument 的 >> 操作符以解析结构体
60 const QDBusArgument &operator>>(const QDBusArgument &argument, DShortcutInfo &myStruct)
61 {
62 argument.beginStructure();
63 argument >> myStruct.contextUniqueName >> myStruct.contextFriendlyName >> myStruct.componentUniqueName >> myStruct.componentFriendlyName >> myStruct.uniqueName >> myStruct.friendlyName >> myStruct.keys >> myStruct.defaultKeys;
64 argument.endStructure();
65 return argument;
66 }
67
68 kdk::KGlobalShortcut::KGlobalShortcut(QObject *parent) : QObject(parent)
69 {
70 }
71
72 int kdk::KGlobalShortcut::creatGlobalShortcut(QString name, QString key, QString action)
73 {
74 if (name.isEmpty() || name.isNull())
75 return KYSDK_SHORTCUT_NAME_ERROR;
76
77 KShortcutUtil util;
78 QList<KeyEntry> kysdkEntries = util.getKysdkShortcuts();
79 for (KeyEntry &entry : kysdkEntries)
80 {
81 if (0 == entry.name.compare(name))
82 {
83 return KYSDK_SHORTCUT_EXISTED;
84 }
85 }
86
87 key = convertKeyWithSeqString(key);
88 int ret = checkShortcutIsAvailable(key);
89 if (KYSDK_SUCCESS != ret)
90 {
91 return ret;
92 }
93
94 if (!isActionAvailable(action))
95 {
96 return KYSDK_SHORTCUT_ACTION_ERROR;
97 }
98
99 QString usdKey = convertKeyWithUsdString(key);
100
101 QString availablepath = util.findFreePath(KYSDK_KEYBINDINGS_DIR);
102 QGSettings setting(KYSDK_KEYBINDING_SCHEMA, availablepath.toLatin1(), this);
103 setting.set(NAME_KEY, name);
104 setting.set(BINDING_KEY, usdKey);
105 setting.set(ACTION_KEY, action);
106
107 // 创建快捷键
108 return KYSDK_SUCCESS;
109 }
110
111 int kdk::KGlobalShortcut::deleteGlobalShortcut(QString name)
112 {
113 KShortcutUtil util;
114 QString targetpath = util.findTargetPath(name, KYSDK_KEYBINDINGS_DIR);
115 if (targetpath.isEmpty())
116 return -1;
117
118 QProcess p(0);
119 QStringList args;
120
121 char *fullpath = targetpath.toLatin1().data();
122 QString cmd = "dconf";
123
124 args.append("reset");
125 args.append("-f");
126 args.append(fullpath);
127 p.execute(cmd, args);
128 p.waitForFinished(-1);
129 return KYSDK_SUCCESS;
130 }
131
132 int kdk::KGlobalShortcut::globalShortcutIsAvailable(QString key)
133 {
134 key = convertKeyWithSeqString(key);
135 return checkShortcutIsAvailable(key);
136 }
137
138 int kdk::KGlobalShortcut::setGlobalShortcut(QString name, QString key, QString action)
139 {
140 if (name.isEmpty() || name.isNull())
141 return KYSDK_SHORTCUT_NAME_ERROR;
142
143 KShortcutUtil util;
144 QList<KeyEntry> kysdkEntries = util.getKysdkShortcuts();
145 for (KeyEntry &entry : kysdkEntries)
146 {
147 // 修改快捷键
148 if (0 == entry.name.compare(name))
149 {
150 QGSettings setting(KYSDK_KEYBINDING_SCHEMA, entry.gsPath.toLatin1(), this);
151 if (key.length() > 0)
152 {
153 key = convertKeyWithSeqString(key);
154 if (checkShortcutIsAvailable(key))
155 return KYSDK_SHORTCUT_KEY_ERROR;
156 key = convertKeyWithUsdString(key);
157 setting.set(BINDING_KEY, key);
158 }
159 if (action.length() > 0)
160 {
161 if (!isActionAvailable(action))
162 return KYSDK_SHORTCUT_ACTION_ERROR;
163 setting.set(ACTION_KEY, action);
164 }
165 return KYSDK_SUCCESS;
166 }
167 }
168
169 // 创建快捷键
170 key = convertKeyWithSeqString(key);
171 int ret = checkShortcutIsAvailable(key);
172 if (KYSDK_SUCCESS != ret)
173 {
174 return ret;
175 }
176
177 if (!isActionAvailable(action))
178 {
179 return KYSDK_SHORTCUT_ACTION_ERROR;
180 }
181
182 QString usdKey = convertKeyWithUsdString(key);
183
184 QString availablepath = util.findFreePath(KYSDK_KEYBINDINGS_DIR);
185 QGSettings setting(KYSDK_KEYBINDING_SCHEMA, availablepath.toLatin1(), this);
186 setting.set(NAME_KEY, name);
187 setting.set(BINDING_KEY, usdKey);
188 setting.set(ACTION_KEY, action);
189
190 // 创建快捷键
191 return KYSDK_SUCCESS;
192 }
193
194 KYSDKGlobalShortcutInfoList *kdk::KGlobalShortcut::getGlobalShortcuts()
195 {
196 KYSDKGlobalShortcutInfoList *list = nullptr;
197 KYSDKGlobalShortcutInfoList *prev = nullptr, *curr = nullptr;
198
199 QDBusInterface interface(KDE_GLOBALACCEL_SERVER, KDE_GLOBALACCEL_PATH, KDE_GLOBALACCEL_INTERFACE);
200
201 QDBusReply<QList<QDBusObjectPath>> reply = interface.call(KDE_ALL_COMPONENTS_METHOD);
202 if (!reply.isValid())
203 {
204 qDebug() << reply.error().message();
205 return nullptr;
206 }
207
208 QList<QDBusObjectPath> componentPaths = reply.value();
209 for (QDBusObjectPath componentPath : componentPaths)
210 {
211 QDBusInterface componentInterface(KDE_GLOBALACCEL_SERVER, componentPath.path(), KDE_GLOBALACCEL_COMPONENT_INTERFACE);
212
213 QDBusReply<QDBusArgument> componentReply = componentInterface.call(KDE_ALL_SHORTCUT_INFOS_METHOD, "default");
214 if (!componentReply.isValid())
215 {
216 qDebug() << componentReply.error().name() << componentReply.error().message();
217 return nullptr;
218 }
219 const QDBusArgument argument = componentReply.value();
220
221 QList<DShortcutInfo> shortcuts;
222 argument.beginArray();
223 while (!argument.atEnd())
224 {
225 DShortcutInfo myStruct;
226 argument >> myStruct;
227 shortcuts.append(myStruct);
228 }
229 argument.endArray();
230
231 for (DShortcutInfo shortcut : shortcuts)
232 {
233 curr = (KYSDKGlobalShortcutInfoList *)calloc(1, sizeof(KYSDKGlobalShortcutInfoList));
234 if (nullptr == list)
235 {
236 list = curr;
237 }
238 else
239 prev->next = curr;
240
241 curr->data = (KYSDKGlobalShortcutInfo *)calloc(1, sizeof(KYSDKGlobalShortcutInfo));
242 curr->data->compenteName = qstrdup(shortcut.componentUniqueName.toLatin1().data());
243 curr->data->name = qstrdup(shortcut.uniqueName.toLatin1().data());
244 int i = 0;
245 for (int key : shortcut.keys)
246 {
247 curr->data->keys[i++] = key;
248 }
249
250 prev = curr;
251 }
252 }
253
254 KShortcutUtil util;
255 QList<KeyEntry> kysdkEntries = util.getKysdkShortcuts();
256 for (KeyEntry &entry : kysdkEntries)
257 {
258 curr = (KYSDKGlobalShortcutInfoList *)calloc(1, sizeof(KYSDKGlobalShortcutInfoList));
259 if (nullptr == list)
260 {
261 list = curr;
262 }
263 else
264 prev->next = curr;
265
266 curr->data = (KYSDKGlobalShortcutInfo *)calloc(1, sizeof(KYSDKGlobalShortcutInfo));
267 curr->data->compenteName = qstrdup(KYSDK_COMPONET_NAME);
268 curr->data->name = qstrdup(entry.name.toLatin1().data());
269 curr->data->keys[0] = keyToNumber(entry.binding);
270 curr->data->action = qstrdup(entry.action.toLatin1().data());
271
272 prev = curr;
273 }
274 return list;
275 }
276
277 KYSDKGlobalShortcutInfoList *kdk::KGlobalShortcut::getGlobalShortcutByKey(QString key)
278 {
279 KYSDKGlobalShortcutInfoList *list = nullptr;
280 KYSDKGlobalShortcutInfoList *prev = nullptr, *curr = nullptr;
281
282 key = convertKeyWithSeqString(key);
283 QKeySequence seq = QKeySequence::fromString(key);
284 if (false == isKeyAvailable(seq))
285 {
286 return nullptr;
287 }
288
289 QList<KGlobalShortcutInfo> shortcuts = KGlobalAccel::globalShortcutsByKey(seq);
290 for (KGlobalShortcutInfo shortcut : shortcuts)
291 {
292 curr = (KYSDKGlobalShortcutInfoList *)calloc(1, sizeof(KYSDKGlobalShortcutInfoList));
293 if (nullptr == list)
294 {
295 list = curr;
296 }
297 else
298 prev->next = curr;
299
300 curr->data = (KYSDKGlobalShortcutInfo *)calloc(1, sizeof(KYSDKGlobalShortcutInfo));
301 curr->data->compenteName = qstrdup(shortcut.componentUniqueName().toLatin1().data());
302 curr->data->name = qstrdup(shortcut.contextUniqueName().toLatin1().data());
303 if (0 == qstrcmp(curr->data->compenteName,"kysdk-keybindings"))
304 {
305 KShortcutUtil util;
306 QString schema_path = util.findTargetPath(curr->data->name, KYSDK_KEYBINDINGS_DIR);
307 QGSettings setting(KYSDK_KEYBINDING_SCHEMA, schema_path.toLatin1(), this);
308 curr->data->action = qstrdup(setting.get(ACTION_KEY).toByteArray().data());
309 }
310 int i = 0;
311 for (QKeySequence key : shortcut.keys())
312 {
313 curr->data->keys[i++] = key[0];
314 }
315
316 prev = curr;
317 }
318
319 return list;
320 }
321
322 QString kdk::KGlobalShortcut::numberToKey(uint number)
323 {
324 QKeySequence seq(number);
325 QString key = seq.toString();
326 if (key.contains("Meta"))
327 {
328 key.replace("Meta", "Win");
329 }
330 if (key.contains("Print", Qt::CaseInsensitive))
331 {
332 key.replace("Print", "PrtSc", Qt::CaseInsensitive);
333 }
334 return key;
335 }
336
337 uint kdk::KGlobalShortcut::keyToNumber(QString key)
338 {
339 key = convertKeyWithSeqString(key);
340 QKeySequence seq(key);
341 return seq[0];
342 }
343
344 bool kdk::KGlobalShortcut::isKeyAvailable(const QKeySequence seq)
345 {
346 QString key = seq.toString();
347 if (!key.contains("+"))
348 {
349 return false;
350 }
351 else if (key.contains("Num") ||
352 key.contains("Space") ||
353 key.contains("Meta") ||
354 key.contains("Ins") ||
355 key.contains("Home") ||
356 key.contains("PgUp") ||
357 key.contains("Del") ||
358 key.contains("End") ||
359 key.contains("PgDown") ||
360 key.contains("Print") ||
361 key.contains("Backspace") ||
362 key.contains("ScrollLock") ||
363 key.contains("Return") ||
364 key.contains("Enter") ||
365 key.contains("Tab") ||
366 key.contains("CapsLock") ||
367 key.contains("Left") ||
368 key.contains("Right") ||
369 key.contains("Up") ||
370 key.contains("Down") ||
371 key.contains("Clear Grab"))
372 {
373 return false;
374 }
375 else
376 {
377 QStringList keys = key.split("+");
378 if (keys.count() >= 4)
379 {
380 return false;
381 }
382 else
383 {
384 QString key = keys.at(keys.count() - 1);
385 if (!key.contains(QRegularExpression("[A-Z]")) && !key.contains(QRegularExpression("[a-z]")) && !key.contains(QRegularExpression("[0-9]")))
386 {
387 return false;
388 }
389 }
390 }
391
392 return true;
393 }
394
395 bool kdk::KGlobalShortcut::isActionAvailable(const QString &action)
396 {
397 return QFile::exists(action);
398 }
399
400 QString kdk::KGlobalShortcut::convertKeyWithSeqString(QString key)
401 {
402 key.replace(" ", "");
403 if (key.contains("Win"))
404 {
405 key.replace("Win", "Meta");
406 }
407 if (key.contains("Start"))
408 {
409 key.replace("Start", "Meta");
410 }
411 if (key.contains("PrtSc", Qt::CaseInsensitive))
412 {
413 key.replace("PrtSc", "Print", Qt::CaseInsensitive);
414 }
415 if (key.contains("Control", Qt::CaseInsensitive))
416 {
417 key.replace("Control", "Ctrl", Qt::CaseInsensitive);
418 }
419 if (key.contains("↑"))
420 {
421 key.replace("↑", "Up");
422 }
423 if (key.contains("→"))
424 {
425 key.replace("→", "Right");
426 }
427 if (key.contains("←"))
428 {
429 key.replace("←", "Left");
430 }
431 if (key.contains("↓"))
432 {
433 key.replace("↓", "Down");
434 }
435
436 if (key.contains("<"))
437 {
438 key.replace("<", "");
439 }
440
441 if (key.contains(">"))
442 {
443 key.replace(">", "+");
444 }
445 return key;
446 }
447
448 QString kdk::KGlobalShortcut::convertKeyWithUsdString(QString key)
449 {
450 key.replace(" ", "");
451 if (key.contains("Meta"))
452 {
453 key.replace("Meta", "Win");
454 }
455 if (key.contains("Start"))
456 {
457 key.replace("Start", "Win");
458 }
459 if (key.contains("Print", Qt::CaseInsensitive))
460 {
461 key.replace("Print", "PrtSc", Qt::CaseInsensitive);
462 }
463
464 if (key.contains("+"))
465 {
466 QStringList keys = key.split("+");
467 if (keys.count() == 2)
468 {
469 QString upper = keys.at(1);
470 QString usdKey = "<" + keys.at(0) + ">" + upper.toUpper();
471
472 return usdKey;
473 }
474 else if (keys.count() == 3)
475 {
476 QString upper = keys.at(2);
477 QString usdKey = "<" + keys.at(0) + ">" + "<" + keys.at(1) + ">" + upper.toUpper();
478
479 return usdKey;
480 }
481 else if (keys.count() == 4)
482 {
483 QString upper = keys.at(2);
484 QString usdKey = "<" + keys.at(0) + ">" + "<" + keys.at(1) + ">" + "<" + keys.at(2) + ">" + upper.toUpper();
485
486 return usdKey;
487 }
488 }
489
490 return key;
491 }
492
493 int kdk::KGlobalShortcut::checkShortcutIsAvailable(QString key)
494 {
495 int ret = KYSDK_SUCCESS;
496
497 QKeySequence seq = QKeySequence::fromString(key);
498 if (false == isKeyAvailable(seq))
499 {
500 return KYSDK_SHORTCUT_KEY_ERROR;
501 }
502
503 do
504 {
505 ret = conflictWithGlobalShortcuts(seq);
506 if (KYSDK_SUCCESS != ret)
507 break;
508
509 ret = conflictWithStandardShortcuts(seq);
510 if (KYSDK_SUCCESS != ret)
511 break;
512 } while (0);
513
514 return ret;
515 }
516
517 int kdk::KGlobalShortcut::conflictWithGlobalShortcuts(const QKeySequence &keySequence)
518 {
519 QList<KGlobalShortcutInfo> shortcuts = KGlobalAccel::globalShortcutsByKey(keySequence);
520
521 if (!shortcuts.isEmpty())
522 {
523 KGlobalShortcutInfo &sfd = shortcuts.first();
524 QString componentUniqueName = sfd.componentUniqueName();
525 QString uniqueName = sfd.uniqueName();
526 qDebug() << componentUniqueName << ":" << uniqueName;
527 return KYSDK_SHORTCUT_EXISTED;
528 }
529 return KYSDK_SUCCESS ;
530 }
531
532 int kdk::KGlobalShortcut::conflictWithStandardShortcuts(const QKeySequence &keySequence)
533 {
534 KStandardShortcut::StandardShortcut ssc = KStandardShortcut::find(keySequence);
535 if (ssc != KStandardShortcut::AccelNone)
536 {
537 return KYSDK_SHORTCUT_EXISTED;
538 }
539 return KYSDK_SUCCESS;
540 }
541
542 int kdk::KGlobalShortcut::conflictWithKysdkShortcuts(const QKeySequence &keySequence)
543 {
544 QString kysdkKey = convertKeyWithUsdString(keySequence.toString());
545
546 KShortcutUtil util;
547 QList<KeyEntry> customEntries = util.getKysdkShortcuts();
548 for (KeyEntry &entry : customEntries)
549 {
550 QString key = entry.binding;
551 if (!kysdkKey.compare(key, Qt::CaseInsensitive))
552 {
553 return KYSDK_SHORTCUT_EXISTED;
554 }
555 }
556
557 return KYSDK_SUCCESS;
558 }