File indexing completed on 2024-05-12 03:54:27

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
0004     SPDX-FileCopyrightText: 1997-1999 Matthias Kalle Dalheimer <kalle@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KSHAREDCONFIG_H
0010 #define KSHAREDCONFIG_H
0011 
0012 #include <QExplicitlySharedDataPointer>
0013 #include <kconfig.h>
0014 
0015 /**
0016  * \class KSharedConfig ksharedconfig.h <KSharedConfig>
0017  *
0018  * KConfig variant using shared memory
0019  *
0020  * KSharedConfig provides a shared (reference counted) variant
0021  * of KConfig. This allows you to use/manipulate the same configuration
0022  * files from different places in your code without worrying about
0023  * accidentally overwriting changes.
0024  *
0025  * The openConfig() method is threadsafe: every thread gets a separate repository
0026  * of shared KConfig objects. This means, however, that you'll be responsible for
0027  * synchronizing the instances of KConfig for the same filename between threads,
0028  * using KConfig::reparseConfiguration() after a manual change notification, just like you have
0029  * to do between processes.
0030  */
0031 class KCONFIGCORE_EXPORT KSharedConfig : public KConfig, public QSharedData // krazy:exclude=dpointer (only for refcounting)
0032 {
0033 public:
0034     typedef QExplicitlySharedDataPointer<KSharedConfig> Ptr;
0035 
0036 public:
0037     /**
0038      * Creates a KSharedConfig object to manipulate a configuration file
0039      *
0040      * If an absolute path is specified for @p fileName, that file will be used
0041      * as the store for the configuration settings.  If a non-absolute path
0042      * is provided, the file will be looked for in the standard directory
0043      * specified by @p type.  If no path is provided, a default
0044      * configuration file will be used based on the name of the main
0045      * application component.
0046      *
0047      * @p mode determines whether the user or global settings will be allowed
0048      * to influence the values returned by this object.  See KConfig::OpenFlags for
0049      * more details.
0050      *
0051      * @param fileName     the configuration file to open. If empty, it will be determined
0052      *                     automatically (from --config on the command line, otherwise
0053      *                     from the application name + "rc")
0054      * @param mode         how global settings should affect the configuration
0055      *                     options exposed by this KConfig object
0056      * @param type         The standard directory to look for the configuration
0057      *                     file in (see QStandardPaths)
0058      *
0059      * @sa KConfig
0060      */
0061     static KSharedConfig::Ptr
0062     openConfig(const QString &fileName = QString(), OpenFlags mode = FullConfig, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation);
0063 
0064     /**
0065      * Creates a KSharedConfig object to manipulate a configuration file suitable
0066      * for storing state information. Use this for storing information that is
0067      * changing frequently and should not be saved by configuration backup
0068      * utilities.
0069      *
0070      * If an absolute path is specified for @p fileName, that file will be used
0071      * as the store for the configuration settings. If a non-absolute path
0072      * is provided, the file will be looked for in the standard data directory
0073      * (QStandardPaths::AppDataLocation). If no path is provided, a default
0074      * configuration file will be used based on the name of the main
0075      * application component.
0076      *
0077      * @param fileName the configuration file to open. If empty, it will be determined
0078      *                 automatically from the application name + "staterc"
0079      *
0080      * @since 5.67
0081      *
0082      * @sa KConfig
0083      */
0084     static KSharedConfig::Ptr openStateConfig(const QString &fileName = QString());
0085 
0086     ~KSharedConfig() override;
0087 
0088 private:
0089     Q_DISABLE_COPY(KSharedConfig)
0090     KConfigGroup groupImpl(const QString &groupName) override;
0091     const KConfigGroup groupImpl(const QString &groupName) const override;
0092 
0093     KCONFIGCORE_NO_EXPORT KSharedConfig(const QString &file, OpenFlags mode, QStandardPaths::StandardLocation resourceType);
0094 };
0095 
0096 typedef KSharedConfig::Ptr KSharedConfigPtr;
0097 
0098 #endif // multiple inclusion guard