File indexing completed on 2024-05-12 15:34:13

0001 /*
0002     SPDX-FileCopyrightText: 2007 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KCONFIGLOADER_H
0008 #define KCONFIGLOADER_H
0009 
0010 #include <QIODevice>
0011 
0012 #include <kconfiggroup.h>
0013 #include <kconfigskeleton.h>
0014 #include <ksharedconfig.h>
0015 
0016 #include <kconfiggui_export.h>
0017 
0018 class ConfigLoaderPrivate;
0019 
0020 /**
0021  * @class KConfigLoader kconfigloader.h <KConfigLoader>
0022  *
0023  * @short A KConfigSkeleton that populates itself based on KConfigXT XML
0024  *
0025  * This class allows one to ship an XML file and reconstitute it into a
0026  * KConfigSkeleton object at runtime. Common usage might look like this:
0027  *
0028  * \code
0029  * QFile file(xmlFilePath);
0030  * KConfigLoader appletConfig(configFilePath, &file);
0031  * \endcode
0032  *
0033  * Alternatively, any QIODevice may be used in place of QFile in the
0034  * example above.
0035  *
0036  * KConfigLoader is useful if it is not possible to use compiled code
0037  * and by that the kconfig compiler cannot be used. Common examples are
0038  * scripted plugins which want to provide a configuration interface.
0039  * With the help of KConfigLoader a dynamically loaded ui file can be
0040  * populated with the stored values and also stored back to the config
0041  * file.
0042  *
0043  * An example for populating a QDialog with a dynamically populated UI
0044  * with the help of a KConfigDialogManager:
0045  * \code
0046  * QDialog *dialog = new QDialog();
0047  * QFile xmlFile("path/to/kconfigxt.xml");
0048  * KConfigGroup cg = KSharedConfig::openConfig()->group(QString());
0049  * KConfigLoader *configLoader = new KConfigLoader(cg, &xmlFile, this);
0050  *
0051  * // load the ui file
0052  * QUiLoader *loader = new QUiLoader(this);
0053  * QFile uiFile("path/to/userinterface.ui");
0054  * uiFile.open(QFile::ReadOnly);
0055  * QWidget *customConfigForm = loader->load(&uiFile, dialog);
0056  * uiFile.close();
0057  *
0058  * KConfigDialogManager *manager = new KConfigDialogManager(customConfigForm, configLoader);
0059  * if (dialog->exec() == QDialog::Accepted) {
0060  *     manager->updateSettings();
0061  * }
0062  * \endcode
0063  *
0064  * Currently the following data types are supported:
0065  *
0066  * @li bools
0067  * @li colors
0068  * @li datetimes
0069  * @li enumerations
0070  * @li fonts
0071  * @li ints
0072  * @li passwords
0073  * @li paths
0074  * @li strings
0075  * @li stringlists
0076  * @li uints
0077  * @li urls
0078  * @li doubles
0079  * @li int lists
0080  * @li longlongs
0081  * @li path lists
0082  * @li points
0083  * @li rects
0084  * @li sizes
0085  * @li ulonglongs
0086  * @li url lists
0087  **/
0088 class KCONFIGGUI_EXPORT KConfigLoader : public KConfigSkeleton
0089 {
0090 public:
0091     /**
0092      * Creates a KConfigSkeleton populated using the definition found in
0093      * the XML data passed in.
0094      *
0095      * @param configFile path to the configuration file to use
0096      * @param xml the xml data; must be valid KConfigXT data
0097      * @param parent optional QObject parent
0098      **/
0099     KConfigLoader(const QString &configFile, QIODevice *xml, QObject *parent = nullptr);
0100 
0101     /**
0102      * Creates a KConfigSkeleton populated using the definition found in
0103      * the XML data passed in.
0104      *
0105      * @param config the configuration object to use
0106      * @param xml the xml data; must be valid KConfigXT data
0107      * @param parent optional QObject parent
0108      **/
0109     KConfigLoader(KSharedConfigPtr config, QIODevice *xml, QObject *parent = nullptr);
0110 
0111     /**
0112      * Creates a KConfigSkeleton populated using the definition found in
0113      * the XML data passed in.
0114      *
0115      * @param config the group to use as the root for configuration items
0116      * @param xml the xml data; must be valid KConfigXT data
0117      * @param parent optional QObject parent
0118      **/
0119     KConfigLoader(const KConfigGroup &config, QIODevice *xml, QObject *parent = nullptr);
0120 
0121     ~KConfigLoader() override;
0122 
0123     /**
0124      * Finds the item for the given group and key.
0125      *
0126      * @param group the group in the config file to look in
0127      * @param key the configuration key to find
0128      * @return the associated KConfigSkeletonItem, or @c nullptr if none
0129      */
0130     KConfigSkeletonItem *findItem(const QString &group, const QString &key) const;
0131 
0132     /**
0133      * Finds an item by its name
0134      */
0135     KConfigSkeletonItem *findItemByName(const QString &name) const;
0136 
0137     /**
0138      * Returns the property (variantized value) of the named item
0139      */
0140     QVariant property(const QString &name) const;
0141 
0142     /**
0143      * Check to see if a group exists
0144      *
0145      * @param group the name of the group to check for
0146      * @return true if the group exists, or false if it does not
0147      */
0148     bool hasGroup(const QString &group) const;
0149 
0150     /**
0151      * @return the list of groups defined by the XML
0152      */
0153     QStringList groupList() const;
0154 
0155 protected:
0156 #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0)
0157     /**
0158      * Hack used to force writing when no default exists in config file.
0159      */
0160     bool usrWriteConfig() override;
0161 #else
0162     bool usrSave() override;
0163 #endif
0164 
0165 private:
0166     ConfigLoaderPrivate *const d;
0167 };
0168 
0169 #endif // multiple inclusion guard