File indexing completed on 2024-05-05 12:11:15

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
0004     SPDX-FileCopyrightText: 2001 Waldo Bastian <bastian@kde.org>
0005     SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
0006     SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <kalle@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #ifndef KCONFIG_H
0012 #define KCONFIG_H
0013 
0014 #include "kconfigbase.h"
0015 
0016 #include <kconfigcore_export.h>
0017 
0018 #include <QByteArray>
0019 #include <QList>
0020 #include <QStandardPaths>
0021 #include <QString>
0022 #include <QVariant>
0023 
0024 class KConfigGroup;
0025 class KEntryMap;
0026 class KConfigPrivate;
0027 
0028 /**
0029  * \class KConfig kconfig.h <KConfig>
0030  *
0031  * \brief The central class of the KDE configuration data system.
0032  *
0033  * Quickstart:
0034  *
0035  * Get the default application config object via KSharedConfig::openConfig().
0036  *
0037  * Load a specific configuration file:
0038  * \code
0039  * KConfig config( "/etc/kderc", KConfig::SimpleConfig );
0040  * \endcode
0041  *
0042  * Load the configuration of a specific component:
0043  * \code
0044  * KConfig config( "pluginrc" );
0045  * \endcode
0046  *
0047  * In general it is recommended to use KSharedConfig instead of
0048  * creating multiple instances of KConfig to avoid the overhead of
0049  * separate objects or concerns about synchronizing writes to disk
0050  * even if the configuration object is updated from multiple code paths.
0051  * KSharedConfig provides a set of open methods as counterparts for the
0052  * KConfig constructors.
0053  *
0054  * \sa KSharedConfig, KConfigGroup, <a href="https://techbase.kde.org/index.php?title=Development/Tutorials/KConfig">the techbase HOWTO on KConfig</a>.
0055  */
0056 class KCONFIGCORE_EXPORT KConfig : public KConfigBase
0057 {
0058 public:
0059     /**
0060      * Determines how the system-wide and user's global settings will affect
0061      * the reading of the configuration.
0062      *
0063      * If CascadeConfig is selected, system-wide configuration sources are used
0064      * to provide defaults for the settings accessed through this object, or
0065      * possibly to override those settings in certain cases.
0066      *
0067      * If IncludeGlobals is selected, the kdeglobals configuration is used
0068      * as additional configuration sources to provide defaults. Additionally
0069      * selecting CascadeConfig will result in the system-wide kdeglobals sources
0070      * also getting included.
0071      *
0072      * Note that the main configuration source overrides the cascaded sources,
0073      * which override those provided to addConfigSources(), which override the
0074      * global sources.  The exception is that if a key or group is marked as
0075      * being immutable, it will not be overridden.
0076      *
0077      * Note that all values other than IncludeGlobals and CascadeConfig are
0078      * convenience definitions for the basic mode.
0079      * Do @em not combine them with anything.
0080      * @see OpenFlags
0081      */
0082     enum OpenFlag {
0083         IncludeGlobals = 0x01, ///< Blend kdeglobals into the config object.
0084         CascadeConfig = 0x02, ///< Cascade to system-wide config files.
0085 
0086         SimpleConfig = 0x00, ///< Just a single config file.
0087         NoCascade = IncludeGlobals, ///< Include user's globals, but omit system settings.
0088         NoGlobals = CascadeConfig, ///< Cascade to system settings, but omit user's globals.
0089         FullConfig = IncludeGlobals | CascadeConfig, ///< Fully-fledged config, including globals and cascading to system settings
0090     };
0091     /**
0092      * Stores a combination of #OpenFlag values.
0093      */
0094     Q_DECLARE_FLAGS(OpenFlags, OpenFlag)
0095 
0096     /**
0097      * Creates a KConfig object to manipulate a configuration file for the
0098      * current application.
0099      *
0100      * If an absolute path is specified for @p file, that file will be used
0101      * as the store for the configuration settings.  If a non-absolute path
0102      * is provided, the file will be looked for in the standard directory
0103      * specified by type.  If no path is provided, a default
0104      * configuration file will be used based on the name of the main
0105      * application component.
0106      *
0107      * @p mode determines whether the user or global settings will be allowed
0108      * to influence the values returned by this object.  See OpenFlags for
0109      * more details.
0110      *
0111      * @note You probably want to use KSharedConfig::openConfig instead.
0112      *
0113      * @param file         the name of the file. If an empty string is passed in
0114      *                     and SimpleConfig is passed in for the OpenFlags, then an in-memory
0115      *                     KConfig object is created which will not write out to file nor which
0116      *                     requires any file in the filesystem at all.
0117      * @param mode         how global settings should affect the configuration
0118      *                     options exposed by this KConfig object
0119      * @param type         The standard directory to look for the configuration
0120      *                     file in
0121      *
0122      * @sa KSharedConfig::openConfig(const QString&, OpenFlags, QStandardPaths::StandardLocation)
0123      */
0124     explicit KConfig(const QString &file = QString(),
0125                      OpenFlags mode = FullConfig,
0126                      QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation);
0127 
0128     /**
0129      * @internal
0130      *
0131      * Creates a KConfig object using the specified backend. If the backend can not
0132      * be found or loaded, then the standard configuration parser is used as a fallback.
0133      *
0134      * @param file the file to be parsed
0135      * @param backend the backend to load
0136      * @param type where to look for the file if an absolute path is not provided
0137      *
0138      * @since 4.1
0139      */
0140     KConfig(const QString &file, const QString &backend, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation);
0141 
0142     ~KConfig() override;
0143 
0144     /**
0145      * Returns the standard location enum passed to the constructor.
0146      * Used by KSharedConfig.
0147      * @since 5.0
0148      */
0149     QStandardPaths::StandardLocation locationType() const;
0150 
0151     /**
0152      * Returns the filename used to store the configuration.
0153      */
0154     QString name() const;
0155 
0156     /**
0157      * @return the flags this object was opened with
0158      * @since 5.3
0159      */
0160     OpenFlags openFlags() const;
0161 
0162     /// @reimp
0163     bool sync() override;
0164 
0165     /// Returns true if sync has any changes to write out.
0166     /// @since 4.12
0167     bool isDirty() const;
0168 
0169     /// @reimp
0170     void markAsClean() override;
0171 
0172     /// @{ configuration object state
0173     /// @reimp
0174     AccessMode accessMode() const override;
0175 
0176     /**
0177      * Whether the configuration can be written to.
0178      *
0179      * If @p warnUser is true and the configuration cannot be
0180      * written to (ie: this method returns @c false), a warning
0181      * message box will be shown to the user telling them to
0182      * contact their system administrator to get the problem fixed.
0183      *
0184      * The most likely cause for this method returning @c false
0185      * is that the user does not have write permission for the
0186      * configuration file.
0187      *
0188      * @param warnUser whether to show a warning message to the user
0189      *                 if the configuration cannot be written to
0190      *
0191      * @returns true if the configuration can be written to, false
0192      *          if the configuration cannot be written to
0193      */
0194     bool isConfigWritable(bool warnUser);
0195     /// @}
0196 
0197     /**
0198      * Copies all entries from this config object to a new config
0199      * object that will save itself to @p file.
0200      *
0201      * The configuration will not actually be saved to @p file
0202      * until the returned object is destroyed, or sync() is called
0203      * on it.
0204      *
0205      * Do not forget to delete the returned KConfig object if
0206      * @p config was 0.
0207      *
0208      * @param file   the new config object will save itself to
0209      * @param config if not 0, copy to the given KConfig object rather
0210      *               than creating a new one
0211      *
0212      * @return @p config if it was set, otherwise a new KConfig object
0213      */
0214     KConfig *copyTo(const QString &file, KConfig *config = nullptr) const;
0215 
0216     /**
0217      * Ensures that the configuration file contains a certain update.
0218      *
0219      * If the configuration file does not contain the update @p id
0220      * as contained in @p updateFile, kconf_update is run to update
0221      * the configuration file.
0222      *
0223      * If you install config update files with critical fixes
0224      * you may wish to use this method to verify that a critical
0225      * update has indeed been performed to catch the case where
0226      * a user restores an old config file from backup that has
0227      * not been updated yet.
0228      *
0229      * @param id the update to check
0230      * @param updateFile the file containing the update
0231      */
0232     void checkUpdate(const QString &id, const QString &updateFile);
0233 
0234     /**
0235      * Updates the state of this object to match the persistent storage.
0236      * Note that if this object has pending changes, this method will
0237      * call sync() first so as not to lose those changes.
0238      */
0239     void reparseConfiguration();
0240 
0241     /// @{ extra config files
0242     /**
0243      * Adds the list of configuration sources to the merge stack.
0244      *
0245      * Currently only files are accepted as configuration sources.
0246      *
0247      * The first entry in @p sources is treated as the most general and will
0248      * be overridden by the second entry.  The settings in the final entry
0249      * in @p sources will override all the other sources provided in the list.
0250      *
0251      * The settings in @p sources will also be overridden by the sources
0252      * provided by any previous calls to addConfigSources().
0253      *
0254      * The settings in the global configuration sources will be overridden by
0255      * the sources provided to this method (@see IncludeGlobals).
0256      * All the sources provided to any call to this method will be overridden
0257      * by any files that cascade from the source provided to the constructor
0258      * (@see CascadeConfig), which will in turn be
0259      * overridden by the source provided to the constructor.
0260      *
0261      * Note that only the most specific file, ie: the file provided to the
0262      * constructor, will be written to by this object.
0263      *
0264      * The state is automatically updated by this method, so there is no need to call
0265      * reparseConfiguration().
0266      *
0267      * @param sources A list of extra config sources.
0268      */
0269     void addConfigSources(const QStringList &sources);
0270 
0271     /**
0272      * Returns a list of the additional configuration sources used in this object
0273      */
0274     QStringList additionalConfigSources() const;
0275 
0276     /// @}
0277     /// @{ locales
0278     /**
0279      * Returns the current locale.
0280      */
0281     QString locale() const;
0282     /**
0283      * Sets the locale to @p aLocale.
0284      *
0285      * The global locale is used by default.
0286      *
0287      * @note If set to the empty string, @b no locale will be matched. This effectively disables
0288      * reading translated entries.
0289      *
0290      * @return @c true if locale was changed, @c false if the call had no
0291      *         effect (eg: @p aLocale was already the current locale for this
0292      *         object)
0293      */
0294     bool setLocale(const QString &aLocale);
0295     /// @}
0296 
0297     /// @{ defaults
0298     /**
0299      * When set, all readEntry calls return the system-wide (default) values
0300      * instead of the user's settings.
0301      *
0302      * This is off by default.
0303      *
0304      * @param b whether to read the system-wide defaults instead of the
0305      *          user's settings
0306      */
0307     void setReadDefaults(bool b);
0308     /**
0309      * @returns @c true if the system-wide defaults will be read instead of the
0310      *          user's settings
0311      */
0312     bool readDefaults() const;
0313     /// @}
0314 
0315     /// @{ immutability
0316     /// @reimp
0317     bool isImmutable() const override;
0318     /// @}
0319 
0320     /// @{ global
0321 #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(4, 0)
0322     /**
0323      * Forces all following write-operations to be performed on @c kdeglobals,
0324      * independent of the @c Global flag in writeEntry().
0325      *
0326      * @param force true to force writing to kdeglobals
0327      * @see forceGlobal
0328      * @deprecated Since 4.0
0329      */
0330     KCONFIGCORE_DEPRECATED_VERSION(4, 0, "Not recommended")
0331     void setForceGlobal(bool force);
0332 #endif
0333 
0334 #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(4, 0)
0335     /**
0336      * Returns whether all entries are being written to @c kdeglobals.
0337      *
0338      * @return @c true if all entries are being written to @c kdeglobals
0339      * @see setForceGlobal
0340      * @deprecated Since 4.0
0341      */
0342     KCONFIGCORE_DEPRECATED_VERSION(4, 0, "Not recommended")
0343     bool forceGlobal() const;
0344 #endif
0345     /// @}
0346 
0347     /// @reimp
0348     QStringList groupList() const override;
0349 
0350     /**
0351      * Returns a map (tree) of entries in a particular group.
0352      *
0353      * The entries are all returned as strings.
0354      *
0355      * @param aGroup The group to get entries from.
0356      *
0357      * @return A map of entries in the group specified, indexed by key.
0358      *         The returned map may be empty if the group is empty, or not found.
0359      * @see   QMap
0360      */
0361     QMap<QString, QString> entryMap(const QString &aGroup = QString()) const;
0362 
0363     /**
0364      * Sets the name of the application config file.
0365      * @since 5.0
0366      */
0367     static void setMainConfigName(const QString &str);
0368 
0369     /**
0370      * Get the name of application config file.
0371      * @since 5.93
0372      */
0373     static QString mainConfigName();
0374 
0375 protected:
0376     bool hasGroupImpl(const QByteArray &group) const override;
0377     KConfigGroup groupImpl(const QByteArray &b) override;
0378     const KConfigGroup groupImpl(const QByteArray &b) const override;
0379     void deleteGroupImpl(const QByteArray &group, WriteConfigFlags flags = Normal) override;
0380     bool isGroupImmutableImpl(const QByteArray &aGroup) const override;
0381 
0382     friend class KConfigGroup;
0383     friend class KConfigGroupPrivate;
0384     friend class KSharedConfig;
0385 
0386     /** Virtual hook, used to add new "virtual" functions while maintaining
0387      * binary compatibility. Unused in this class.
0388      */
0389     void virtual_hook(int id, void *data) override;
0390 
0391     KConfigPrivate *const d_ptr;
0392 
0393     KCONFIGCORE_NO_EXPORT explicit KConfig(KConfigPrivate &d);
0394 
0395 private:
0396     friend class KConfigTest;
0397 
0398     Q_DISABLE_COPY(KConfig)
0399 
0400     Q_DECLARE_PRIVATE(KConfig)
0401 };
0402 Q_DECLARE_OPERATORS_FOR_FLAGS(KConfig::OpenFlags)
0403 
0404 #endif // KCONFIG_H