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.addOption(QCommandLineOption(QStringLiteral("notify"), QCoreApplication::translate("notify", "Notify applications of the change")));
38 parser.addPositionalArgument(QStringLiteral("value"), QCoreApplication::translate("main", "The value to write. Mandatory, on a shell use '' for empty"));
39
40 parser.process(app);
41
42 const QStringList groups = parser.values(QStringLiteral("group"));
43 QString key = parser.value(QStringLiteral("key"));
44 QString file = parser.value(QStringLiteral("file"));
45 QString type = parser.value(QStringLiteral("type")).toLower();
46 bool del = parser.isSet(QStringLiteral("delete"));
47 bool notify = parser.isSet(QStringLiteral("notify"));
48
49 QString value;
50 if (del) {
51 value = QString{};
52 } else if (parser.positionalArguments().isEmpty()) {
53 parser.showHelp(1);
54 } else {
55 value = parser.positionalArguments().at(0);
56 }
57
58 KConfig *konfig;
59 if (file.isEmpty()) {
60 konfig = new KConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
61 } else {
62 konfig = new KConfig(file, KConfig::NoGlobals);
63 }
64
65 KConfigGroup cfgGroup = konfig->group(QString());
66 for (const QString &grp : groups) {
67 if (grp.isEmpty()) {
68 fprintf(stderr,
69 "%s: %s\n",
71 qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"<default>\" for the root group")));
72 return 2;
73 }
74 cfgGroup = cfgGroup.group(grp);
75 }
76
77 if (konfig->accessMode() != KConfig::ReadWrite || cfgGroup.isEntryImmutable(key)) {
78 return 2;
79 }
80
82
83 if (del) {
84 cfgGroup.deleteEntry(key, flags);
85 } else if (type == QLatin1String{"bool"}) {
86 // For symmetry with kreadconfig we accept a wider range of values as true than Qt
87 /* clang-format off */
88 bool boolvalue = value == QLatin1String{"true"}
89 || value == QLatin1String{"on"}
90 || value == QLatin1String{"yes"}
91 || value == QLatin1String{"1"}; /* clang-format on */
92 cfgGroup.writeEntry(key, boolvalue, flags);
93 } else if (type == QLatin1String{"path"}) {
94 cfgGroup.writePathEntry(key, value, flags);
95 } else {
96 cfgGroup.writeEntry(key, value, flags);
97 }
98 konfig->sync();
99 delete konfig;
100 return 0;
101}
@ Notify
Notify remote KConfigWatchers of changes (requires DBus support) Implied persistent.
Definition kconfigbase.h:51
@ Normal
Save the entry to the application specific config file without a locale tag.
Definition kconfigbase.h:57
QFlags< WriteConfigFlag > WriteConfigFlags
Stores a combination of WriteConfigFlag values.
Definition kconfigbase.h:67
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:830
bool sync() override
Definition kconfig.cpp:409
@ 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-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:49:40 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.