| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include <DNativeSettings> |
| 6 |
|
| 7 |
#include <QGuiApplication> |
| 8 |
#include <QCommandLineParser> |
| 9 |
#include <QCommandLineOption> |
| 10 |
#include <QDebug> |
| 11 |
#include <QColor> |
| 12 |
#include <QProcess> |
| 13 |
|
| 14 |
DGUI_USE_NAMESPACE |
| 15 |
|
| 16 |
int main(int argc, char *argv[]) |
| 17 |
{ |
| 18 |
// 禁用输入法,防止输入法插件乱输出内容 |
| 19 |
qunsetenv("QT_IM_MODULE"); |
| 20 |
qputenv("QT_QPA_PLATFORM", "dxcb"); |
| 21 |
QGuiApplication app(argc, argv); |
| 22 |
qunsetenv("QT_QPA_PLATFORM"); |
| 23 |
|
| 24 |
QCommandLineParser parser; |
| 25 |
QCommandLineOption option_window({"w", "window"}, "resource id of window to examine"); |
| 26 |
QCommandLineOption option_window_leader("window-leader", "use leader window of the window"); |
| 27 |
QCommandLineOption option_select_window("select", "auto select a window on screen"); |
| 28 |
QCommandLineOption option_domain({"p", "domain"}, "domain of settings property"); |
| 29 |
QCommandLineOption option_set("set", "set a settings item to a given value. Can only and must specify a value(eg: [-s|-i|-c])"); |
| 30 |
QCommandLineOption option_string({"s", "string"}, "set a string value of a settings item"); |
| 31 |
QCommandLineOption option_int({"i", "int"}, "set a int value of a settings item"); |
| 32 |
QCommandLineOption option_color({"c", "color"}, "set a color value of a settings item"); |
| 33 |
QCommandLineOption option_remove({"r", "remove"}, "remove a settings item"); |
| 34 |
|
| 35 |
option_window.setValueName("id"); |
| 36 |
option_window.setDefaultValue("0"); |
| 37 |
option_domain.setValueName("domain"); |
| 38 |
option_domain.setDefaultValue(QString()); |
| 39 |
option_set.setValueName("key"); |
| 40 |
option_string.setValueName("value"); |
| 41 |
option_int.setValueName("value"); |
| 42 |
option_color.setValueName("value"); |
| 43 |
option_remove.setValueName("key"); |
| 44 |
|
| 45 |
parser.addOption(option_window); |
| 46 |
parser.addOption(option_window_leader); |
| 47 |
parser.addOption(option_select_window); |
| 48 |
parser.addOption(option_domain); |
| 49 |
parser.addOption(option_set); |
| 50 |
parser.addOption(option_string); |
| 51 |
parser.addOption(option_int); |
| 52 |
parser.addOption(option_color); |
| 53 |
parser.addOption(option_remove); |
| 54 |
parser.addPositionalArgument("keys", "key of get settings value", "[keys...]"); |
| 55 |
parser.addHelpOption(); |
| 56 |
parser.addVersionOption(); |
| 57 |
parser.process(app); |
| 58 |
|
| 59 |
quint32 window_id = 0; |
| 60 |
|
| 61 |
if (parser.isSet(option_select_window)) { |
| 62 |
QProcess xdotool; |
| 63 |
|
| 64 |
xdotool.start("xdotool", {"selectwindow"}, QIODevice::ReadOnly); |
| 65 |
|
| 66 |
if (!xdotool.waitForFinished()) { |
| 67 |
parser.showHelp(1); |
| 68 |
} |
| 69 |
|
| 70 |
bool ok = false; |
| 71 |
window_id = xdotool.readAllStandardOutput().trimmed().toInt(&ok); |
| 72 |
|
| 73 |
if (!ok) { |
| 74 |
parser.showHelp(1); |
| 75 |
} |
| 76 |
} else { |
| 77 |
const QString &window_id_value = parser.value(option_window); |
| 78 |
bool ok = false; |
| 79 |
|
| 80 |
if (window_id_value.startsWith("0x")) { |
| 81 |
window_id = window_id_value.toInt(&ok, 16); |
| 82 |
} else { |
| 83 |
window_id = window_id_value.toInt(&ok); |
| 84 |
} |
| 85 |
|
| 86 |
if (!ok) { |
| 87 |
parser.showHelp(-1); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (parser.isSet(option_window_leader)) { |
| 92 |
QProcess xprop; |
| 93 |
xprop.start("xprop", {"-id", QString::number(window_id), "WM_CLIENT_LEADER"}, QIODevice::ReadOnly); |
| 94 |
|
| 95 |
if (!xprop.waitForFinished()) { |
| 96 |
qFatal("%s\n", xprop.errorString().toLocal8Bit().constData()); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
const QByteArrayList &list = xprop.readAllStandardOutput().split(' '); |
| 101 |
bool ok = false; |
| 102 |
|
| 103 |
if (!list.isEmpty()) { |
| 104 |
quint32 id = list.last().trimmed().toInt(&ok, 16); |
| 105 |
|
| 106 |
if (ok) { |
| 107 |
window_id = id; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if (!ok) { |
| 112 |
qFatal("%s\n", "not found WM_CLIENT_LEADER"); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
DNativeSettings settings(window_id, parser.value(option_domain).toLatin1()); |
| 118 |
|
| 119 |
if (!settings.isValid()) { |
| 120 |
qWarning() << "Settings is invalid, platform plugin is:" << qApp->platformName(); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
if (parser.isSet(option_set)) { |
| 125 |
QVariant value; |
| 126 |
|
| 127 |
if (parser.isSet(option_string)) { // string |
| 128 |
if (parser.isSet(option_int) || parser.isSet(option_color)) { |
| 129 |
parser.showHelp(-1); |
| 130 |
} |
| 131 |
|
| 132 |
value = parser.value(option_string).toLocal8Bit(); |
| 133 |
} else if (parser.isSet(option_int)) { // int |
| 134 |
if (parser.isSet(option_string) || parser.isSet(option_color)) { |
| 135 |
parser.showHelp(-1); |
| 136 |
} |
| 137 |
|
| 138 |
bool ok = false; |
| 139 |
int integer = parser.value(option_int).toInt(&ok); |
| 140 |
|
| 141 |
if (!ok) { |
| 142 |
parser.showHelp(-1); |
| 143 |
} |
| 144 |
|
| 145 |
value = QVariant(integer); |
| 146 |
} else if (parser.isSet(option_color)) { // color |
| 147 |
if (parser.isSet(option_string) || parser.isSet(option_int)) { |
| 148 |
parser.showHelp(-1); |
| 149 |
} |
| 150 |
|
| 151 |
QColor color(parser.value(option_color)); |
| 152 |
|
| 153 |
if (!color.isValid()) { |
| 154 |
parser.showHelp(-1); |
| 155 |
} |
| 156 |
|
| 157 |
value = QVariant(color); |
| 158 |
} else { |
| 159 |
parser.showHelp(-1); |
| 160 |
} |
| 161 |
|
| 162 |
// 设置一个属性的值 |
| 163 |
const QByteArray &name = parser.value(option_set).toLatin1(); |
| 164 |
|
| 165 |
if (name.isEmpty()) { |
| 166 |
parser.showHelp(-1); |
| 167 |
} |
| 168 |
|
| 169 |
settings.setSetting(name, value); |
| 170 |
qDebug() << name << settings.getSetting(name); |
| 171 |
} else if (parser.isSet(option_remove)) { |
| 172 |
const QByteArray &name = parser.value(option_remove).toLatin1(); |
| 173 |
|
| 174 |
if (name.isEmpty()) { |
| 175 |
parser.showHelp(-1); |
| 176 |
} |
| 177 |
|
| 178 |
settings.setSetting(name, QVariant()); |
| 179 |
qDebug() << name << settings.getSetting(name); |
| 180 |
} else { |
| 181 |
const QStringList &keys = parser.positionalArguments(); |
| 182 |
|
| 183 |
if (keys.isEmpty()) { |
| 184 |
// 打印所有设置项 |
| 185 |
qDebug() << settings; |
| 186 |
} else { |
| 187 |
for (const QString &key : keys) { |
| 188 |
qDebug() << key << settings.getSetting(key.toLatin1()); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |