KConfig

kwriteconfig.cpp
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 <[email protected]>
5 
6  Programmed by Luís Pedro Coelho <[email protected]>
7  based on kreadconfig by Bernhard Rosenkraenzer <[email protected]>
8 
9  SPDX-License-Identifier: GPL-2.0-or-later
10 */
11 
12 #include <KConfig>
13 #include <KConfigGroup>
14 #include <stdio.h>
15 #include <QCoreApplication>
16 #include <QCommandLineParser>
17 
18 int main(int argc, char **argv)
19 {
20  QCoreApplication app(argc, argv);
21 
22  QCommandLineParser parser;
23  parser.addHelpOption();
24  parser.addOption(QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file")));
25  parser.addOption(QCommandLineOption(QStringLiteral("group"), QCoreApplication::translate("main", "Group to look in. Use repeatedly for nested groups."), QStringLiteral("group"), QStringLiteral("KDE")));
26  parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key")));
27  parser.addOption(QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate("main", "Type of variable. Use \"bool\" for a boolean, otherwise it is treated as a string"), QStringLiteral("type")));
28  parser.addOption(QCommandLineOption(QStringLiteral("delete"), QCoreApplication::translate("main", "Delete the designated key if enabled")));
29  parser.addPositionalArgument(QStringLiteral("value"), QCoreApplication::translate("main", "The value to write. Mandatory, on a shell use '' for empty" ));
30 
31  parser.process(app);
32 
33  const QStringList groups=parser.values(QStringLiteral("group"));
34  QString key=parser.value(QStringLiteral("key"));
35  QString file=parser.value(QStringLiteral("file"));
36  QString type=parser.value(QStringLiteral("type")).toLower();
37  bool del=parser.isSet(QStringLiteral("delete"));
38 
39  QString value;
40  if (del) {
41  value = QStringLiteral("");
42  } else if (parser.positionalArguments().isEmpty()) {
43  parser.showHelp(1);
44  } else {
45  value = parser.positionalArguments().at(0);
46  }
47 
48  KConfig *konfig;
49  if (file.isEmpty())
50  konfig = new KConfig(QStringLiteral( "kdeglobals"), KConfig::NoGlobals );
51  else
52  konfig = new KConfig( file, KConfig::NoGlobals );
53 
54  KConfigGroup cfgGroup = konfig->group(QString());
55  for (const QString &grp : groups)
56  cfgGroup = cfgGroup.group(grp);
57  if ( konfig->accessMode() != KConfig::ReadWrite || cfgGroup.isEntryImmutable( key ) ) return 2;
58 
59  if (del) {
60  cfgGroup.deleteEntry( key );
61  } else if (type==QStringLiteral("bool")) {
62  // For symmetry with kreadconfig we accept a wider range of values as true than Qt
63  bool boolvalue=(value==QStringLiteral("true") || value==QStringLiteral("on") || value==QStringLiteral("yes") || value==QStringLiteral("1"));
64  cfgGroup.writeEntry( key, boolvalue );
65  } else if (type==QStringLiteral("path")) {
66  cfgGroup.writePathEntry( key, value );
67  } else {
68  cfgGroup.writeEntry( key, value );
69  }
70  konfig->sync();
71  delete konfig;
72  return 0;
73 }
74 
QStringList positionalArguments() const const
void writePathEntry(const QString &pKey, const QString &path, WriteConfigFlags pFlags=Normal)
Writes a file path to the configuration.
bool isEntryImmutable(const QString &key) const
Checks if it is possible to change the given entry.
const T & at(int i) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
KConfigGroup group(const QByteArray &group)
Returns an object for the named subgroup.
Definition: kconfigbase.cpp:31
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Writes a value to the configuration object.
KIOCORE_EXPORT DeleteJob * del(const QUrl &src, JobFlags flags=DefaultFlags)
void deleteEntry(const QString &pKey, WriteConfigFlags pFlags=Normal)
Deletes the entry specified by pKey in the current group.
bool sync() override
Definition: kconfig.cpp:393
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
bool isEmpty() const const
bool isEmpty() const const
QCommandLineOption addHelpOption()
bool isSet(const QString &name) const const
QString toLower() const const
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)
AccessMode accessMode() const override
Definition: kconfig.cpp:778
bool addOption(const QCommandLineOption &option)
QString value(const QString &optionName) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 11 2020 22:47:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.