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 <[email protected]>
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 kreadconfig5 --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=`kreadconfig5 --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 
39 int main(int argc, char **argv)
40 {
41  QCoreApplication app(argc, argv);
42 
43  QCommandLineParser parser;
44  parser.addOption(QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file")));
45  parser.addOption(QCommandLineOption(QStringLiteral("group"), QCoreApplication::translate("main", "Group to look in. Use repeatedly for nested groups."), QStringLiteral("group"), QStringLiteral("KDE")));
46  parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key")));
47  parser.addOption(QCommandLineOption(QStringLiteral("default"), QCoreApplication::translate("main", "Default value"), QStringLiteral("value")));
48  parser.addOption(QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate("main", "Type of variable"), QStringLiteral("type")));
49 
50  parser.process(app);
51 
52  const QStringList groups=parser.values(QStringLiteral("group"));
53  QString key=parser.value(QStringLiteral("key"));
54  QString file=parser.value(QStringLiteral("file"));
55  QString dflt=parser.value(QStringLiteral("default"));
56  QString type=parser.value(QStringLiteral("type")).toLower();
57 
58  if (key.isNull() || !parser.positionalArguments().isEmpty()) {
59  parser.showHelp(1);
60  }
61 
63 
64  KConfig *konfig;
65  bool configMustDeleted = false;
66  if (file.isEmpty())
67  konfig = KSharedConfig::openConfig().data();
68  else
69  {
70  konfig = new KConfig( file, KConfig::NoGlobals );
71  configMustDeleted=true;
72  }
73  KConfigGroup cfgGroup = konfig->group(QString());
74  for (const QString &grp : groups)
75  cfgGroup = cfgGroup.group(grp);
76  if(type==QStringLiteral("bool")) {
77  dflt=dflt.toLower();
78  bool def=(dflt==QStringLiteral("true") || dflt==QStringLiteral("on") || dflt==QStringLiteral("yes") || dflt==QStringLiteral("1"));
79  bool retValue = !cfgGroup.readEntry(key, def);
80  if ( configMustDeleted )
81  delete konfig;
82  return retValue;
83  } else if((type==QStringLiteral("num")) || (type==QStringLiteral("int"))) {
84  int retValue = cfgGroup.readEntry(key, dflt.toInt());
85  if ( configMustDeleted )
86  delete konfig;
87  return retValue;
88  } else if (type==QStringLiteral("path")){
89  fprintf(stdout, "%s\n", cfgGroup.readPathEntry(key, dflt).toLocal8Bit().data());
90  if ( configMustDeleted )
91  delete konfig;
92  return 0;
93  } else {
94  /* Assume it's a string... */
95  fprintf(stdout, "%s\n", cfgGroup.readEntry(key, dflt).toLocal8Bit().data());
96  if ( configMustDeleted )
97  delete konfig;
98  return 0;
99  }
100 }
101 
QString readPathEntry(const QString &pKey, const QString &aDefault) const
Reads a path.
QStringList positionalArguments() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
KConfigGroup group(const QByteArray &group)
Returns an object for the named subgroup.
Definition: kconfigbase.cpp:31
bool isNull() const const
Cascade to system settings, but omit user&#39;s globals.
Definition: kconfig.h:88
Type type(const QSqlDatabase &db)
QStringList values(const QString &optionName) const const
int toInt(bool *ok, int base) const const
bool isEmpty() const const
bool isEmpty() const const
QString toLower() const const
QByteArray toLocal8Bit() const const
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.
void process(const QStringList &arguments)
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:38
The central class of the KDE configuration data system.
Definition: kconfig.h:56
void showHelp(int exitCode)
bool addOption(const QCommandLineOption &option)
char * data()
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
Definition: kconfiggroup.h:236
QString value(const QString &optionName) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 22:48:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.