File indexing completed on 2024-05-12 03:54:31
0001 /* Write KConfig() entries - for use in shell scripts. 0002 0003 SPDX-FileCopyrightText: 2001 Red Hat , Inc. 0004 SPDX-FileCopyrightText: 2001 Luís Pedro Coelho <luis_pedro@netcabo.pt> 0005 0006 Programmed by Luís Pedro Coelho <luis_pedro@netcabo.pt> 0007 based on kreadconfig by Bernhard Rosenkraenzer <bero@redhat.com> 0008 0009 SPDX-License-Identifier: GPL-2.0-or-later 0010 */ 0011 0012 #include <KConfig> 0013 #include <KConfigGroup> 0014 #include <QCommandLineParser> 0015 #include <QCoreApplication> 0016 #include <stdio.h> 0017 0018 int main(int argc, char **argv) 0019 { 0020 QCoreApplication app(argc, argv); 0021 0022 QCommandLineParser parser; 0023 parser.addHelpOption(); 0024 parser.addOption( 0025 QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file"))); 0026 parser.addOption( 0027 QCommandLineOption(QStringLiteral("group"), 0028 QCoreApplication::translate("main", "Group to look in. Use \"<default>\" for the root group, or use repeatedly for nested groups."), 0029 QStringLiteral("group"), 0030 QStringLiteral("KDE"))); 0031 parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key"))); 0032 parser.addOption( 0033 QCommandLineOption(QStringLiteral("type"), 0034 QCoreApplication::translate("main", "Type of variable. Use \"bool\" for a boolean, otherwise it is treated as a string"), 0035 QStringLiteral("type"))); 0036 parser.addOption(QCommandLineOption(QStringLiteral("delete"), QCoreApplication::translate("main", "Delete the designated key if enabled"))); 0037 parser.addPositionalArgument(QStringLiteral("value"), QCoreApplication::translate("main", "The value to write. Mandatory, on a shell use '' for empty")); 0038 0039 parser.process(app); 0040 0041 const QStringList groups = parser.values(QStringLiteral("group")); 0042 QString key = parser.value(QStringLiteral("key")); 0043 QString file = parser.value(QStringLiteral("file")); 0044 QString type = parser.value(QStringLiteral("type")).toLower(); 0045 bool del = parser.isSet(QStringLiteral("delete")); 0046 0047 QString value; 0048 if (del) { 0049 value = QString{}; 0050 } else if (parser.positionalArguments().isEmpty()) { 0051 parser.showHelp(1); 0052 } else { 0053 value = parser.positionalArguments().at(0); 0054 } 0055 0056 KConfig *konfig; 0057 if (file.isEmpty()) { 0058 konfig = new KConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals); 0059 } else { 0060 konfig = new KConfig(file, KConfig::NoGlobals); 0061 } 0062 0063 KConfigGroup cfgGroup = konfig->group(QString()); 0064 for (const QString &grp : groups) { 0065 if (grp.isEmpty()) { 0066 fprintf(stderr, 0067 "%s: %s\n", 0068 qPrintable(QCoreApplication::applicationName()), 0069 qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"<default>\" for the root group"))); 0070 return 2; 0071 } 0072 cfgGroup = cfgGroup.group(grp); 0073 } 0074 0075 if (konfig->accessMode() != KConfig::ReadWrite || cfgGroup.isEntryImmutable(key)) { 0076 return 2; 0077 } 0078 0079 if (del) { 0080 cfgGroup.deleteEntry(key); 0081 } else if (type == QLatin1String{"bool"}) { 0082 // For symmetry with kreadconfig we accept a wider range of values as true than Qt 0083 /* clang-format off */ 0084 bool boolvalue = value == QLatin1String{"true"} 0085 || value == QLatin1String{"on"} 0086 || value == QLatin1String{"yes"} 0087 || value == QLatin1String{"1"}; /* clang-format on */ 0088 cfgGroup.writeEntry(key, boolvalue); 0089 } else if (type == QLatin1String{"path"}) { 0090 cfgGroup.writePathEntry(key, value); 0091 } else { 0092 cfgGroup.writeEntry(key, value); 0093 } 0094 konfig->sync(); 0095 delete konfig; 0096 return 0; 0097 }