KConfig

kconfig.h
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
4 SPDX-FileCopyrightText: 2001 Waldo Bastian <bastian@kde.org>
5 SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
6 SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <kalle@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#ifndef KCONFIG_H
12#define KCONFIG_H
13
14#include "kconfigbase.h"
15
16#include <kconfigcore_export.h>
17
18#include <QByteArray>
19#include <QList>
20#include <QStandardPaths>
21#include <QString>
22#include <QVariant>
23
24class KConfigGroup;
25class KConfigPrivate;
26
27/**
28 * \class KConfig kconfig.h <KConfig>
29 *
30 * \brief The central class of the KDE configuration data system.
31 *
32 * Quickstart:
33 *
34 * Get the default application config object via KSharedConfig::openConfig().
35 *
36 * Load a specific configuration file:
37 * \code
38 * KConfig config("/etc/kderc", KConfig::SimpleConfig);
39 * \endcode
40 *
41 * Load the configuration of a specific component:
42 * \code
43 * KConfig config("pluginrc");
44 * \endcode
45 *
46 * In general it is recommended to use KSharedConfig instead of
47 * creating multiple instances of KConfig to avoid the overhead of
48 * separate objects or concerns about synchronizing writes to disk
49 * even if the configuration object is updated from multiple code paths.
50 * KSharedConfig provides a set of open methods as counterparts for the
51 * KConfig constructors.
52 *
53 * \sa KSharedConfig, KConfigGroup, <a href="https://techbase.kde.org/index.php?title=Development/Tutorials/KConfig">the techbase HOWTO on KConfig</a>.
54 */
55class KCONFIGCORE_EXPORT KConfig : public KConfigBase
56{
57public:
58 /**
59 * Determines how the system-wide and user's global settings will affect
60 * the reading of the configuration.
61 *
62 * If CascadeConfig is selected, system-wide configuration sources are used
63 * to provide defaults for the settings accessed through this object, or
64 * possibly to override those settings in certain cases.
65 *
66 * If IncludeGlobals is selected, the kdeglobals configuration is used
67 * as additional configuration sources to provide defaults. Additionally
68 * selecting CascadeConfig will result in the system-wide kdeglobals sources
69 * also getting included.
70 *
71 * Note that the main configuration source overrides the cascaded sources,
72 * which override those provided to addConfigSources(), which override the
73 * global sources. The exception is that if a key or group is marked as
74 * being immutable, it will not be overridden.
75 *
76 * Note that all values other than IncludeGlobals and CascadeConfig are
77 * convenience definitions for the basic mode.
78 * Do @em not combine them with anything.
79 * @see OpenFlags
80 */
81 enum OpenFlag {
82 IncludeGlobals = 0x01, ///< Blend kdeglobals into the config object.
83 CascadeConfig = 0x02, ///< Cascade to system-wide config files.
84
85 SimpleConfig = 0x00, ///< Just a single config file.
86 NoCascade = IncludeGlobals, ///< Include user's globals, but omit system settings.
87 NoGlobals = CascadeConfig, ///< Cascade to system settings, but omit user's globals.
88 FullConfig = IncludeGlobals | CascadeConfig, ///< Fully-fledged config, including globals and cascading to system settings
89 };
90 /**
91 * Stores a combination of #OpenFlag values.
92 */
93 Q_DECLARE_FLAGS(OpenFlags, OpenFlag)
94
95 /**
96 * Creates a KConfig object to manipulate a configuration file for the
97 * current application.
98 *
99 * If an absolute path is specified for @p file, that file will be used
100 * as the store for the configuration settings. If a non-absolute path
101 * is provided, the file will be looked for in the standard directory
102 * specified by type. If no path is provided, a default
103 * configuration file will be used based on the name of the main
104 * application component.
105 *
106 * @p mode determines whether the user or global settings will be allowed
107 * to influence the values returned by this object. See OpenFlags for
108 * more details.
109 *
110 * @note You probably want to use KSharedConfig::openConfig instead.
111 *
112 * @param file the name of the file. If an empty string is passed in
113 * and SimpleConfig is passed in for the OpenFlags, then an in-memory
114 * KConfig object is created which will not write out to file nor which
115 * requires any file in the filesystem at all.
116 * @param mode how global settings should affect the configuration
117 * options exposed by this KConfig object
118 * @param type The standard directory to look for the configuration
119 * file in
120 *
121 * @sa KSharedConfig::openConfig(const QString&, OpenFlags, QStandardPaths::StandardLocation)
122 */
123 explicit KConfig(const QString &file = QString(),
124 OpenFlags mode = FullConfig,
126
127#if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(6, 3)
128 /**
129 * @internal
130 *
131 * Creates a KConfig object using the specified backend. If the backend can not
132 * be found or loaded, then the standard configuration parser is used as a fallback.
133 *
134 * @param file the file to be parsed
135 * @param backend the backend to load
136 * @param type where to look for the file if an absolute path is not provided
137 *
138 * @since 4.1
139 *
140 * @deprecated since 6.3, use other constructor
141 */
142 KCONFIGCORE_DEPRECATED_VERSION(6, 3, "Use other constructor")
143 KConfig(const QString &file, const QString &backend, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation);
144#endif
145
146 ~KConfig() override;
147
148 /**
149 * Returns the standard location enum passed to the constructor.
150 * Used by KSharedConfig.
151 * @since 5.0
152 */
153 QStandardPaths::StandardLocation locationType() const;
154
155 /**
156 * Returns the filename used to store the configuration.
157 */
158 QString name() const;
159
160 /**
161 * @return the flags this object was opened with
162 * @since 5.3
163 */
164 OpenFlags openFlags() const;
165
166 /// @reimp
167 bool sync() override;
168
169 /// Returns true if sync has any changes to write out.
170 /// @since 4.12
171 bool isDirty() const;
172
173 /// @reimp
174 void markAsClean() override;
175
176 /// @{ configuration object state
177 /// @reimp
178 AccessMode accessMode() const override;
179
180 /**
181 * Whether the configuration can be written to.
182 *
183 * If @p warnUser is true and the configuration cannot be
184 * written to (ie: this method returns @c false), a warning
185 * message box will be shown to the user telling them to
186 * contact their system administrator to get the problem fixed.
187 *
188 * The most likely cause for this method returning @c false
189 * is that the user does not have write permission for the
190 * configuration file.
191 *
192 * @param warnUser whether to show a warning message to the user
193 * if the configuration cannot be written to
194 *
195 * @returns true if the configuration can be written to, false
196 * if the configuration cannot be written to
197 */
198 bool isConfigWritable(bool warnUser);
199 /// @}
200
201 /**
202 * Copies all entries from this config object to a new config
203 * object that will save itself to @p file.
204 *
205 * The configuration will not actually be saved to @p file
206 * until the returned object is destroyed, or sync() is called
207 * on it.
208 *
209 * Do not forget to delete the returned KConfig object if
210 * @p config was 0.
211 *
212 * @param file the new config object will save itself to
213 * @param config if not 0, copy to the given KConfig object rather
214 * than creating a new one
215 *
216 * @return @p config if it was set, otherwise a new KConfig object
217 */
218 KConfig *copyTo(const QString &file, KConfig *config = nullptr) const;
219
220 /**
221 * Ensures that the configuration file contains a certain update.
222 *
223 * If the configuration file does not contain the update @p id
224 * as contained in @p updateFile, kconf_update is run to update
225 * the configuration file.
226 *
227 * If you install config update files with critical fixes
228 * you may wish to use this method to verify that a critical
229 * update has indeed been performed to catch the case where
230 * a user restores an old config file from backup that has
231 * not been updated yet.
232 *
233 * @param id the update to check
234 * @param updateFile the file containing the update
235 */
236 void checkUpdate(const QString &id, const QString &updateFile);
237
238 /**
239 * Updates the state of this object to match the persistent storage.
240 * Note that if this object has pending changes, this method will
241 * call sync() first so as not to lose those changes.
242 */
243 void reparseConfiguration();
244
245 /// @{ extra config files
246 /**
247 * Adds the list of configuration sources to the merge stack.
248 *
249 * Currently only files are accepted as configuration sources.
250 *
251 * The first entry in @p sources is treated as the most general and will
252 * be overridden by the second entry. The settings in the final entry
253 * in @p sources will override all the other sources provided in the list.
254 *
255 * The settings in @p sources will also be overridden by the sources
256 * provided by any previous calls to addConfigSources().
257 *
258 * The settings in the global configuration sources will be overridden by
259 * the sources provided to this method (@see IncludeGlobals).
260 * All the sources provided to any call to this method will be overridden
261 * by any files that cascade from the source provided to the constructor
262 * (@see CascadeConfig), which will in turn be
263 * overridden by the source provided to the constructor.
264 *
265 * Note that only the most specific file, ie: the file provided to the
266 * constructor, will be written to by this object.
267 *
268 * The state is automatically updated by this method, so there is no need to call
269 * reparseConfiguration().
270 *
271 * @param sources A list of extra config sources.
272 */
273 void addConfigSources(const QStringList &sources);
274
275 /**
276 * Returns a list of the additional configuration sources used in this object
277 */
278 QStringList additionalConfigSources() const;
279
280 /// @}
281 /// @{ locales
282 /**
283 * Returns the current locale.
284 */
285 QString locale() const;
286 /**
287 * Sets the locale to @p aLocale.
288 *
289 * The global locale is used by default.
290 *
291 * @note If set to the empty string, @b no locale will be matched. This effectively disables
292 * reading translated entries.
293 *
294 * @return @c true if locale was changed, @c false if the call had no
295 * effect (eg: @p aLocale was already the current locale for this
296 * object)
297 */
298 bool setLocale(const QString &aLocale);
299 /// @}
300
301 /// @{ defaults
302 /**
303 * When set, all readEntry calls return the system-wide (default) values
304 * instead of the user's settings.
305 *
306 * This is off by default.
307 *
308 * @param b whether to read the system-wide defaults instead of the
309 * user's settings
310 */
311 void setReadDefaults(bool b);
312 /**
313 * @returns @c true if the system-wide defaults will be read instead of the
314 * user's settings
315 */
316 bool readDefaults() const;
317 /// @}
318
319 /// @{ immutability
320 /// @reimp
321 bool isImmutable() const override;
322 /// @}
323
324 /// @reimp
325 QStringList groupList() const override;
326
327 /**
328 * Returns a map (tree) of entries in a particular group.
329 *
330 * The entries are all returned as strings.
331 *
332 * @param aGroup The group to get entries from.
333 *
334 * @return A map of entries in the group specified, indexed by key.
335 * The returned map may be empty if the group is empty, or not found.
336 * @see QMap
337 */
338 QMap<QString, QString> entryMap(const QString &aGroup = QString()) const;
339
340 /**
341 * Sets the name of the application config file.
342 * @since 5.0
343 */
344 static void setMainConfigName(const QString &str);
345
346 /**
347 * Get the name of application config file.
348 * @since 5.93
349 */
350 static QString mainConfigName();
351
352protected:
353 bool hasGroupImpl(const QString &groupName) const override;
354 KConfigGroup groupImpl(const QString &groupName) override;
355 const KConfigGroup groupImpl(const QString &groupName) const override;
356 void deleteGroupImpl(const QString &groupName, WriteConfigFlags flags = Normal) override;
357 bool isGroupImmutableImpl(const QString &groupName) const override;
358
359 friend class KConfigGroup;
360 friend class KConfigGroupPrivate;
361 friend class KSharedConfig;
362
363 /** Virtual hook, used to add new "virtual" functions while maintaining
364 * binary compatibility. Unused in this class.
365 */
366 void virtual_hook(int id, void *data) override;
367
368 KConfigPrivate *const d_ptr;
369
370 KCONFIGCORE_NO_EXPORT explicit KConfig(KConfigPrivate &d);
371
372private:
373 friend class KConfigTest;
374
375 Q_DISABLE_COPY(KConfig)
376
377 Q_DECLARE_PRIVATE(KConfig)
378};
379Q_DECLARE_OPERATORS_FOR_FLAGS(KConfig::OpenFlags)
380
381#endif // KCONFIG_H
Interface to interact with configuration.
Definition kconfigbase.h:31
virtual QStringList groupList() const =0
Returns a list of groups that are known about.
virtual bool isGroupImmutableImpl(const QString &groupName) const =0
virtual bool hasGroupImpl(const QString &groupName) const =0
virtual void markAsClean()=0
Reset the dirty flags of all entries in the entry map, so the values will not be written to disk on a...
virtual void virtual_hook(int id, void *data)
Virtual hook, used to add new "virtual" functions while maintaining binary compatibility.
virtual bool isImmutable() const =0
Checks whether this configuration object can be modified.
AccessMode
Possible return values for accessMode().
virtual KConfigGroup groupImpl(const QString &groupName)=0
virtual AccessMode accessMode() const =0
Returns the access mode of the app-config object.
virtual void deleteGroupImpl(const QString &groupName, WriteConfigFlags flags=Normal)=0
virtual bool sync()=0
Syncs the configuration object that this group belongs to.
A class for one specific group in a KConfig object.
The central class of the KDE configuration data system.
Definition kconfig.h:56
OpenFlag
Determines how the system-wide and user's global settings will affect the reading of the configuratio...
Definition kconfig.h:81
KConfig variant using shared memory.
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.