File indexing completed on 2024-05-12 15:34:17
0001 /* Read KConfig() entries - for use in shell scripts. 0002 0003 SPDX-FileCopyrightText: 2001 Red Hat, Inc. 0004 SPDX-FileContributor: Programmed by Bernhard Rosenkraenzer <bero@redhat.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 /* 0010 * If --type is specified as bool, the return value is 0 if the value 0011 * is set, 1 if it isn't set. There is no output. 0012 * 0013 * If --type is specified as num, the return value matches the value 0014 * of the key. There is no output. 0015 * 0016 * If --type is not set, the value of the key is simply printed to stdout. 0017 * 0018 * Usage examples: 0019 * if kreadconfig5 --group KDE --key macStyle --type bool; then 0020 * echo "We're using Mac-Style menus." 0021 * else 0022 * echo "We're using normal menus." 0023 * fi 0024 * 0025 * TRASH=`kreadconfig5 --group Paths --key Trash` 0026 * if test -n "$TRASH"; then 0027 * mv someFile "$TRASH" 0028 * else 0029 * rm someFile 0030 * fi 0031 */ 0032 0033 #include <KConfig> 0034 #include <KConfigGroup> 0035 #include <KSharedConfig> 0036 #include <QCommandLineParser> 0037 #include <stdio.h> 0038 0039 int main(int argc, char **argv) 0040 { 0041 QCoreApplication app(argc, argv); 0042 0043 QCommandLineParser parser; 0044 parser.addHelpOption(); 0045 parser.addOption( 0046 QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file"))); 0047 parser.addOption( 0048 QCommandLineOption(QStringLiteral("group"), 0049 QCoreApplication::translate("main", "Group to look in. Use \"<default>\" for the root group, or use repeatedly for nested groups."), 0050 QStringLiteral("group"), 0051 QStringLiteral("KDE"))); 0052 parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key"))); 0053 parser.addOption(QCommandLineOption(QStringLiteral("default"), QCoreApplication::translate("main", "Default value"), QStringLiteral("value"))); 0054 parser.addOption(QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate("main", "Type of variable"), QStringLiteral("type"))); 0055 0056 parser.process(app); 0057 0058 const QStringList groups = parser.values(QStringLiteral("group")); 0059 QString key = parser.value(QStringLiteral("key")); 0060 QString file = parser.value(QStringLiteral("file")); 0061 QString dflt = parser.value(QStringLiteral("default")); 0062 QString type = parser.value(QStringLiteral("type")).toLower(); 0063 0064 if (key.isNull() || !parser.positionalArguments().isEmpty()) { 0065 parser.showHelp(1); 0066 } 0067 0068 KSharedConfig::openConfig(); 0069 0070 KConfig *konfig; 0071 bool configMustDeleted = false; 0072 if (file.isEmpty()) { 0073 konfig = KSharedConfig::openConfig().data(); 0074 } else { 0075 konfig = new KConfig(file, KConfig::NoGlobals); 0076 configMustDeleted = true; 0077 } 0078 KConfigGroup cfgGroup = konfig->group(QString()); 0079 for (const QString &grp : groups) { 0080 if (grp.isEmpty()) { 0081 fprintf(stderr, 0082 "%s: %s\n", 0083 qPrintable(QCoreApplication::applicationName()), 0084 qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"<default>\" for the root group"))); 0085 if (configMustDeleted) { 0086 delete konfig; 0087 } 0088 return 2; 0089 } 0090 cfgGroup = cfgGroup.group(grp); 0091 } 0092 0093 if (type == QLatin1String{"bool"}) { 0094 dflt = dflt.toLower(); 0095 bool def = (dflt == QLatin1String{"true"} || dflt == QLatin1String{"on"} || dflt == QLatin1String{"yes"} || dflt == QLatin1String{"1"}); 0096 bool retValue = !cfgGroup.readEntry(key, def); 0097 if (configMustDeleted) { 0098 delete konfig; 0099 } 0100 return retValue; 0101 } else if (type == QLatin1String{"num"} || type == QLatin1String{"int"}) { 0102 int retValue = cfgGroup.readEntry(key, dflt.toInt()); 0103 if (configMustDeleted) { 0104 delete konfig; 0105 } 0106 return retValue; 0107 } else if (type == QLatin1String{"path"}) { 0108 fprintf(stdout, "%s\n", cfgGroup.readPathEntry(key, dflt).toLocal8Bit().data()); 0109 if (configMustDeleted) { 0110 delete konfig; 0111 } 0112 return 0; 0113 } else { 0114 /* Assume it's a string... */ 0115 fprintf(stdout, "%s\n", cfgGroup.readEntry(key, dflt).toLocal8Bit().data()); 0116 if (configMustDeleted) { 0117 delete konfig; 0118 } 0119 return 0; 0120 } 0121 }