1/* Write KConfig() entries - for use in shell scripts.
2
3 SPDX-FileCopyrightText: 2001 Red Hat , Inc.
4 SPDX-FileCopyrightText: 2001 Luís Pedro Coelho <luis_pedro@netcabo.pt>
5
6 Programmed by Luís Pedro Coelho <luis_pedro@netcabo.pt>
7 based on kreadconfig by Bernhard Rosenkraenzer <bero@redhat.com>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11
12#include <KConfig>
13#include <KConfigGroup>
14#include <QCommandLineParser>
15#include <QCoreApplication>
16#include <stdio.h>
17
18int main(int argc, char **argv)
19{
20 QCoreApplication app(argc, argv);
21
22 QCommandLineParser parser;
23 parser.addHelpOption();
24 parser.addOption(
25 commandLineOption: QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate(context: "main", key: "Use <file> instead of global config"), QStringLiteral("file")));
26 parser.addOption(
27 commandLineOption: QCommandLineOption(QStringLiteral("group"),
28 QCoreApplication::translate(context: "main", key: "Group to look in. Use \"<default>\" for the root group, or use repeatedly for nested groups."),
29 QStringLiteral("group"),
30 QStringLiteral("KDE")));
31 parser.addOption(commandLineOption: QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate(context: "main", key: "Key to look for"), QStringLiteral("key")));
32 parser.addOption(
33 commandLineOption: QCommandLineOption(QStringLiteral("type"),
34 QCoreApplication::translate(context: "main", key: "Type of variable. Use \"bool\" for a boolean, otherwise it is treated as a string"),
35 QStringLiteral("type")));
36 parser.addOption(commandLineOption: QCommandLineOption(QStringLiteral("delete"), QCoreApplication::translate(context: "main", key: "Delete the designated key if enabled")));
37 parser.addPositionalArgument(QStringLiteral("value"), description: QCoreApplication::translate(context: "main", key: "The value to write. Mandatory, on a shell use '' for empty"));
38
39 parser.process(app);
40
41 const QStringList groups = parser.values(QStringLiteral("group"));
42 QString key = parser.value(QStringLiteral("key"));
43 QString file = parser.value(QStringLiteral("file"));
44 QString type = parser.value(QStringLiteral("type")).toLower();
45 bool del = parser.isSet(QStringLiteral("delete"));
46
47 QString value;
48 if (del) {
49 value = QString{};
50 } else if (parser.positionalArguments().isEmpty()) {
51 parser.showHelp(exitCode: 1);
52 } else {
53 value = parser.positionalArguments().at(i: 0);
54 }
55
56 KConfig *konfig;
57 if (file.isEmpty()) {
58 konfig = new KConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
59 } else {
60 konfig = new KConfig(file, KConfig::NoGlobals);
61 }
62
63 KConfigGroup cfgGroup = konfig->group(group: QString());
64 for (const QString &grp : groups) {
65 if (grp.isEmpty()) {
66 fprintf(stderr,
67 format: "%s: %s\n",
68 qPrintable(QCoreApplication::applicationName()),
69 qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"<default>\" for the root group")));
70 return 2;
71 }
72 cfgGroup = cfgGroup.group(group: grp);
73 }
74
75 if (konfig->accessMode() != KConfig::ReadWrite || cfgGroup.isEntryImmutable(key)) {
76 return 2;
77 }
78
79 if (del) {
80 cfgGroup.deleteEntry(pKey: key);
81 } else if (type == QLatin1String{"bool"}) {
82 // For symmetry with kreadconfig we accept a wider range of values as true than Qt
83 /* clang-format off */
84 bool boolvalue = value == QLatin1String{"true"}
85 || value == QLatin1String{"on"}
86 || value == QLatin1String{"yes"}
87 || value == QLatin1String{"1"}; /* clang-format on */
88 cfgGroup.writeEntry(key, value: boolvalue);
89 } else if (type == QLatin1String{"path"}) {
90 cfgGroup.writePathEntry(pKey: key, path: value);
91 } else {
92 cfgGroup.writeEntry(key, value);
93 }
94 konfig->sync();
95 delete konfig;
96 return 0;
97}
98

source code of kconfig/src/kreadconfig/kwriteconfig.cpp