| 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 "kshortcututil.h" |
| 24 |
#include <QDBusInterface> |
| 25 |
#include <QDBusReply> |
| 26 |
#include <QGSettings> |
| 27 |
|
| 28 |
KShortcutUtil::KShortcutUtil() |
| 29 |
{ |
| 30 |
} |
| 31 |
|
| 32 |
QStringList KShortcutUtil::listExistsShortcutPath(const char *dir) |
| 33 |
{ |
| 34 |
int len; |
| 35 |
DConfClient *client = dconf_client_new(); |
| 36 |
char **childs = dconf_client_list(client, dir, &len); |
| 37 |
g_object_unref(client); |
| 38 |
|
| 39 |
QStringList vals; |
| 40 |
for (int i = 0; childs[i] != nullptr; i++) |
| 41 |
{ |
| 42 |
if (dconf_is_rel_dir(childs[i], nullptr)) |
| 43 |
{ |
| 44 |
char *val = g_strdup(childs[i]); |
| 45 |
|
| 46 |
vals.append(val); |
| 47 |
} |
| 48 |
} |
| 49 |
g_strfreev(childs); |
| 50 |
return vals; |
| 51 |
} |
| 52 |
|
| 53 |
QString KShortcutUtil::findFreePath(const char *dir) |
| 54 |
{ |
| 55 |
int i = 0; |
| 56 |
QString d; |
| 57 |
QStringList existsdirs; |
| 58 |
|
| 59 |
existsdirs = listExistsShortcutPath(dir); |
| 60 |
|
| 61 |
for (; i < MAX_CUSTOM_SHORTCUTS; i++) |
| 62 |
{ |
| 63 |
d = QString("custom%1/").arg(i); |
| 64 |
if (!existsdirs.contains(d)) |
| 65 |
{ |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (i == MAX_CUSTOM_SHORTCUTS) |
| 71 |
{ |
| 72 |
return ""; |
| 73 |
} |
| 74 |
|
| 75 |
return QString("%1%2").arg(KYSDK_KEYBINDINGS_DIR).arg(QString(d)); |
| 76 |
} |
| 77 |
|
| 78 |
QString KShortcutUtil::findTargetPath(QString name, const char *dir) |
| 79 |
{ |
| 80 |
QString result; |
| 81 |
QStringList existsdirs; |
| 82 |
existsdirs = listExistsShortcutPath(dir); |
| 83 |
|
| 84 |
DConfClient *client = dconf_client_new(); |
| 85 |
for (QString dir : existsdirs) |
| 86 |
{ |
| 87 |
QString gsettingsPath = QString("%1%2name").arg(KYSDK_KEYBINDINGS_DIR).arg(dir); |
| 88 |
GVariant *variant = dconf_client_read(client, gsettingsPath.toLatin1().constData()); |
| 89 |
if (NULL != variant) |
| 90 |
{ |
| 91 |
gsize size = g_variant_get_size(variant); |
| 92 |
const char *value = g_variant_get_string(variant, &size); |
| 93 |
if (0 == name.compare(value)) |
| 94 |
{ |
| 95 |
g_variant_unref(variant); |
| 96 |
result = QString("%1%2").arg(KYSDK_KEYBINDINGS_DIR).arg(dir); |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
g_object_unref(client); |
| 102 |
return result; |
| 103 |
} |
| 104 |
|
| 105 |
QList<KeyEntry> KShortcutUtil::getKysdkShortcuts() |
| 106 |
{ |
| 107 |
QList<KeyEntry> result; |
| 108 |
QStringList existsPath = listExistsShortcutPath(KYSDK_KEYBINDINGS_DIR); |
| 109 |
for (QString dir : existsPath) |
| 110 |
{ |
| 111 |
QString strFullPath = QString("%1%2").arg(KYSDK_KEYBINDINGS_DIR).arg(dir); |
| 112 |
|
| 113 |
const QByteArray ba(KYSDK_KEYBINDING_SCHEMA); |
| 114 |
const QByteArray bba(strFullPath.toLatin1().data()); |
| 115 |
QGSettings settings(ba, bba); |
| 116 |
QString pathStr = strFullPath; |
| 117 |
QString actionStr = settings.get(ACTION_KEY).toString(); |
| 118 |
QString bindingStr = settings.get(BINDING_KEY).toString(); |
| 119 |
QString nameStr = settings.get(NAME_KEY).toString(); |
| 120 |
|
| 121 |
KeyEntry customKeyEntry; |
| 122 |
customKeyEntry.gsSchema = KYSDK_KEYBINDING_SCHEMA; |
| 123 |
customKeyEntry.gsPath = pathStr; |
| 124 |
customKeyEntry.name = nameStr; |
| 125 |
customKeyEntry.binding = bindingStr; |
| 126 |
customKeyEntry.action = actionStr; |
| 127 |
result.append(customKeyEntry); |
| 128 |
} |
| 129 |
return result; |
| 130 |
} |