KConfig

kreadconfig.cpp
1/* Read KConfig() entries - for use in shell scripts.
2
3 SPDX-FileCopyrightText: 2001 Red Hat, Inc.
4 SPDX-FileContributor: Programmed by Bernhard Rosenkraenzer <bero@redhat.com>
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7*/
8
9/*
10 * If --type is specified as bool, the return value is 0 if the value
11 * is set, 1 if it isn't set. There is no output.
12 *
13 * If --type is specified as num, the return value matches the value
14 * of the key. There is no output.
15 *
16 * If --type is not set, the value of the key is simply printed to stdout.
17 *
18 * Usage examples:
19 * if kreadconfig6 --group KDE --key macStyle --type bool; then
20 * echo "We're using Mac-Style menus."
21 * else
22 * echo "We're using normal menus."
23 * fi
24 *
25 * TRASH=`kreadconfig6 --group Paths --key Trash`
26 * if test -n "$TRASH"; then
27 * mv someFile "$TRASH"
28 * else
29 * rm someFile
30 * fi
31 */
32
33#include <KConfig>
34#include <KConfigGroup>
35#include <KSharedConfig>
36#include <QCommandLineParser>
37#include <stdio.h>
38
39int main(int argc, char **argv)
40{
41 QCoreApplication app(argc, argv);
42
43 QCommandLineParser parser;
44 parser.addHelpOption();
45 parser.addOption(
46 QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file")));
47 parser.addOption(
48 QCommandLineOption(QStringLiteral("group"),
49 QCoreApplication::translate("main", "Group to look in. Use \"<default>\" for the root group, or use repeatedly for nested groups."),
50 QStringLiteral("group"),
51 QStringLiteral("KDE")));
52 parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key")));
53 parser.addOption(QCommandLineOption(QStringLiteral("default"), QCoreApplication::translate("main", "Default value"), QStringLiteral("value")));
54 parser.addOption(QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate("main", "Type of variable"), QStringLiteral("type")));
55
56 parser.process(app);
57
58 const QStringList groups = parser.values(QStringLiteral("group"));
59 QString key = parser.value(QStringLiteral("key"));
60 QString file = parser.value(QStringLiteral("file"));
61 QString dflt = parser.value(QStringLiteral("default"));
62 QString type = parser.value(QStringLiteral("type")).toLower();
63
64 if (key.isNull() || !parser.positionalArguments().isEmpty()) {
65 parser.showHelp(1);
66 }
67
69
70 KConfig *konfig;
71 bool configMustDeleted = false;
72 if (file.isEmpty()) {
74 } else {
75 konfig = new KConfig(file, KConfig::NoGlobals);
76 configMustDeleted = true;
77 }
78 KConfigGroup cfgGroup = konfig->group(QString());
79 for (const QString &grp : groups) {
80 if (grp.isEmpty()) {
81 fprintf(stderr,
82 "%s: %s\n",
84 qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"<default>\" for the root group")));
85 if (configMustDeleted) {
86 delete konfig;
87 }
88 return 2;
89 }
90 cfgGroup = cfgGroup.group(grp);
91 }
92
93 if (type == QLatin1String{"bool"}) {
94 dflt = dflt.toLower();
95 bool def = (dflt == QLatin1String{"true"} || dflt == QLatin1String{"on"} || dflt == QLatin1String{"yes"} || dflt == QLatin1String{"1"});
96 bool retValue = !cfgGroup.readEntry(key, def);
97 if (configMustDeleted) {
98 delete konfig;
99 }
100 return retValue;
101 } else if (type == QLatin1String{"num"} || type == QLatin1String{"int"}) {
102 int retValue = cfgGroup.readEntry(key, dflt.toInt());
103 if (configMustDeleted) {
104 delete konfig;
105 }
106 return retValue;
107 } else if (type == QLatin1String{"path"}) {
108 fprintf(stdout, "%s\n", cfgGroup.readPathEntry(key, dflt).toLocal8Bit().data());
109 if (configMustDeleted) {
110 delete konfig;
111 }
112 return 0;
113 } else {
114 /* Assume it's a string... */
115 fprintf(stdout, "%s\n", cfgGroup.readEntry(key, dflt).toLocal8Bit().data());
116 if (configMustDeleted) {
117 delete konfig;
118 }
119 return 0;
120 }
121}
KConfigGroup group(const QString &group)
Returns an object for the named subgroup.
A class for one specific group in a KConfig object.
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
QString readPathEntry(const QString &pKey, const QString &aDefault) const
Reads a path.
The central class of the KDE configuration data system.
Definition kconfig.h:56
@ NoGlobals
Cascade to system settings, but omit user's globals.
Definition kconfig.h:87
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
Creates a KSharedConfig object to manipulate a configuration file.
Type type(const QSqlDatabase &db)
char * data()
QCommandLineOption addHelpOption()
bool addOption(const QCommandLineOption &option)
QStringList positionalArguments() const const
void process(const QCoreApplication &app)
void showHelp(int exitCode)
QString value(const QCommandLineOption &option) const const
QStringList values(const QCommandLineOption &option) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
bool isEmpty() const const
bool isEmpty() const const
bool isNull() const const
int toInt(bool *ok, int base) const const
QByteArray toLocal8Bit() const const
QString toLower() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.