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 <luis_pedro@netcabo.pt>
5
6 Programmed by Luís Pedro Coelho <luis_pedro@netcabo.pt>
7 based on kreadconfig by Bernhard Rosenkraenzer <bero@redhat.com>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11
12#include <KConfig>
13#include <KConfigGroup>
14#include <QCommandLineParser>
15#include <QCoreApplication>
16#include <stdio.h>
17
18int main(int argc, char **argv)
19{
20 QCoreApplication app(argc, argv);
21
22 QCommandLineParser parser;
23 parser.addHelpOption();
24 parser.addOption(
25 QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file")));
26 parser.addOption(
27 QCommandLineOption(QStringLiteral("group"),
28 QCoreApplication::translate("main", "Group to look in. Use \"<default>\" for the root group, or use repeatedly for nested groups."),
29 QStringLiteral("group"),
30 QStringLiteral("KDE")));
31 parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key")));
32 parser.addOption(
33 QCommandLineOption(QStringLiteral("type"),
34 QCoreApplication::translate("main", "Type of variable. Use \"bool\" for a boolean, otherwise it is treated as a string"),
35 QStringLiteral("type")));
36 parser.addOption(QCommandLineOption(QStringLiteral("delete"), QCoreApplication::translate("main", "Delete the designated key if enabled")));
37 parser.addPositionalArgument(QStringLiteral("value"), QCoreApplication::translate("main", "The value to write. Mandatory, on a shell use '' for empty"));
38
39 parser.process(app);
40
41 const QStringList groups = parser.values(QStringLiteral("group"));
42 QString key = parser.value(QStringLiteral("key"));
43 QString file = parser.value(QStringLiteral("file"));
44 QString type = parser.value(QStringLiteral("type")).toLower();
45 bool del = parser.isSet(QStringLiteral("delete"));
46
47 QString value;
48 if (del) {
49 value = QString{};
50 } else if (parser.positionalArguments().isEmpty()) {
51 parser.showHelp(1);
52 } else {
53 value = parser.positionalArguments().at(0);
54 }
55
56 KConfig *konfig;
57 if (file.isEmpty()) {
58 konfig = new KConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
59 } else {
60 konfig = new KConfig(file, KConfig::NoGlobals);
61 }
62
63 KConfigGroup cfgGroup = konfig->group(QString());
64 for (const QString &grp : groups) {
65 if (grp.isEmpty()) {
66 fprintf(stderr,
67 "%s: %s\n",
69 qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"<default>\" for the root group")));
70 return 2;
71 }
72 cfgGroup = cfgGroup.group(grp);
73 }
74
75 if (konfig->accessMode() != KConfig::ReadWrite || cfgGroup.isEntryImmutable(key)) {
76 return 2;
77 }
78
79 if (del) {
80 cfgGroup.deleteEntry(key);
81 } else if (type == QLatin1String{"bool"}) {
82 // For symmetry with kreadconfig we accept a wider range of values as true than Qt
83 /* clang-format off */
84 bool boolvalue = value == QLatin1String{"true"}
85 || value == QLatin1String{"on"}
86 || value == QLatin1String{"yes"}
87 || value == QLatin1String{"1"}; /* clang-format on */
88 cfgGroup.writeEntry(key, boolvalue);
89 } else if (type == QLatin1String{"path"}) {
90 cfgGroup.writePathEntry(key, value);
91 } else {
92 cfgGroup.writeEntry(key, value);
93 }
94 konfig->sync();
95 delete konfig;
96 return 0;
97}
KConfigGroup group(const QString &group)
Returns an object for the named subgroup.
A class for one specific group in a KConfig object.
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.
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Writes a value to the configuration object.
void deleteEntry(const QString &pKey, WriteConfigFlags pFlags=Normal)
Deletes the entry specified by pKey in the current group.
The central class of the KDE configuration data system.
Definition kconfig.h:56
AccessMode accessMode() const override
Definition kconfig.cpp:807
bool sync() override
Definition kconfig.cpp:407
@ NoGlobals
Cascade to system settings, but omit user's globals.
Definition kconfig.h:87
Type type(const QSqlDatabase &db)
KGuiItem del()
QCommandLineOption addHelpOption()
bool addOption(const QCommandLineOption &option)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
bool isSet(const QCommandLineOption &option) const const
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)
const_reference at(qsizetype i) const const
bool isEmpty() const const
bool isEmpty() const const
QString toLower() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.