| 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 | |
| 18 | int 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.addOption(commandLineOption: QCommandLineOption(QStringLiteral("notify" ), QCoreApplication::translate(context: "notify" , key: "Notify applications of the change" ))); |
| 38 | parser.addPositionalArgument(QStringLiteral("value" ), description: QCoreApplication::translate(context: "main" , key: "The value to write. Mandatory, on a shell use '' for empty" )); |
| 39 | |
| 40 | parser.process(app); |
| 41 | |
| 42 | const QStringList groups = parser.values(QStringLiteral("group" )); |
| 43 | QString key = parser.value(QStringLiteral("key" )); |
| 44 | QString file = parser.value(QStringLiteral("file" )); |
| 45 | QString type = parser.value(QStringLiteral("type" )).toLower(); |
| 46 | bool del = parser.isSet(QStringLiteral("delete" )); |
| 47 | bool notify = parser.isSet(QStringLiteral("notify" )); |
| 48 | |
| 49 | QString value; |
| 50 | if (del) { |
| 51 | value = QString{}; |
| 52 | } else if (parser.positionalArguments().isEmpty()) { |
| 53 | parser.showHelp(exitCode: 1); |
| 54 | } else { |
| 55 | value = parser.positionalArguments().at(i: 0); |
| 56 | } |
| 57 | |
| 58 | KConfig *konfig; |
| 59 | if (file.isEmpty()) { |
| 60 | konfig = new KConfig(QStringLiteral("kdeglobals" ), KConfig::NoGlobals); |
| 61 | } else { |
| 62 | konfig = new KConfig(file, KConfig::NoGlobals); |
| 63 | } |
| 64 | |
| 65 | KConfigGroup cfgGroup = konfig->group(group: QString()); |
| 66 | for (const QString &grp : groups) { |
| 67 | if (grp.isEmpty()) { |
| 68 | fprintf(stderr, |
| 69 | format: "%s: %s\n" , |
| 70 | qPrintable(QCoreApplication::applicationName()), |
| 71 | qPrintable(QCoreApplication::translate("main" , "Group name cannot be empty, use \"<default>\" for the root group" ))); |
| 72 | return 2; |
| 73 | } |
| 74 | cfgGroup = cfgGroup.group(group: grp); |
| 75 | } |
| 76 | |
| 77 | if (konfig->accessMode() != KConfig::ReadWrite || cfgGroup.isEntryImmutable(key)) { |
| 78 | return 2; |
| 79 | } |
| 80 | |
| 81 | KConfig::WriteConfigFlags flags = notify ? KConfig::Notify : KConfig::Normal; |
| 82 | |
| 83 | if (del) { |
| 84 | cfgGroup.deleteEntry(pKey: key, pFlags: flags); |
| 85 | } else if (type == QLatin1String{"bool" }) { |
| 86 | // For symmetry with kreadconfig we accept a wider range of values as true than Qt |
| 87 | /* clang-format off */ |
| 88 | bool boolvalue = value == QLatin1String{"true" } |
| 89 | || value == QLatin1String{"on" } |
| 90 | || value == QLatin1String{"yes" } |
| 91 | || value == QLatin1String{"1" }; /* clang-format on */ |
| 92 | cfgGroup.writeEntry(key, value: boolvalue, pFlags: flags); |
| 93 | } else if (type == QLatin1String{"path" }) { |
| 94 | cfgGroup.writePathEntry(pKey: key, path: value, pFlags: flags); |
| 95 | } else { |
| 96 | cfgGroup.writeEntry(key, value, pFlags: flags); |
| 97 | } |
| 98 | konfig->sync(); |
| 99 | delete konfig; |
| 100 | return 0; |
| 101 | } |
| 102 | |