File indexing completed on 2024-04-28 03:53: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: 1999 Preston Brown <pbrown@kde.org> 0005 SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <kalle@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #ifndef KCONFIGBACKEND_H 0011 #define KCONFIGBACKEND_H 0012 0013 #include <QExplicitlySharedDataPointer> 0014 #include <QObject> 0015 #include <QString> 0016 0017 #include <kconfigbase.h> 0018 #include <kconfigcore_export.h> 0019 class KConfigBackendPrivate; 0020 0021 class KEntryMap; 0022 class QFile; 0023 class QByteArray; 0024 0025 /** 0026 * \class KConfigBackend kconfigbackend_p.h <KConfigBackend> 0027 * 0028 * Provides the implementation for accessing configuration sources. 0029 * 0030 * KConfig only provides an INI backend, but this class can be used 0031 * to create plugins that allow access to other file formats and 0032 * configuration systems. 0033 * 0034 * \internal 0035 */ 0036 class KConfigBackend : public QObject, public QSharedData 0037 { 0038 Q_OBJECT 0039 0040 public: 0041 /** 0042 * Creates a new KConfig backend. 0043 * 0044 * If no @p system is given, or the given @p system is unknown, this method tries 0045 * to determine the correct backend to use. 0046 * 0047 * @param fileName the absolute file name of the configuration file 0048 * @param system the configuration system to use 0049 * @return a KConfigBackend object to be used with KConfig 0050 */ 0051 static QExplicitlySharedDataPointer<KConfigBackend> create(const QString &fileName = QString(), const QString &system = QString()); 0052 0053 /** 0054 * Registers mappings from directories/files to configuration systems 0055 * 0056 * Allows you to tell KConfigBackend that create() should use a particular 0057 * backend for a particular file or directory. 0058 * 0059 * @warning currently does nothing 0060 * 0061 * @param entryMap the KEntryMap to build the mappings from 0062 */ 0063 static void registerMappings(const KEntryMap &entryMap); 0064 0065 /** Destroys the backend */ 0066 ~KConfigBackend() override; 0067 0068 /** Allows the behaviour of parseConfig() to be tuned */ 0069 enum ParseOption { 0070 ParseGlobal = 1, /// entries should be marked as @em global 0071 ParseDefaults = 2, /// entries should be marked as @em default 0072 ParseExpansions = 4, /// entries are allowed to be marked as @em expandable 0073 }; 0074 Q_FLAG(ParseOption) 0075 /// @typedef typedef QFlags<ParseOption> ParseOptions 0076 Q_DECLARE_FLAGS(ParseOptions, ParseOption) 0077 0078 /** Allows the behaviour of writeConfig() to be tuned */ 0079 enum WriteOption { 0080 WriteGlobal = 1 /// only write entries marked as "global" 0081 }; 0082 Q_FLAG(WriteOption) 0083 /// @typedef typedef QFlags<WriteOption> WriteOptions 0084 Q_DECLARE_FLAGS(WriteOptions, WriteOption) 0085 0086 /** Return value from parseConfig() */ 0087 enum ParseInfo { 0088 ParseOk, /// the configuration was opened read/write 0089 ParseImmutable, /// the configuration is @em immutable 0090 ParseOpenError, /// the configuration could not be opened 0091 }; 0092 0093 /** 0094 * Read persistent storage 0095 * 0096 * @param locale the locale to read entries for (if the backend supports localized entries) 0097 * @param pWriteBackMap the KEntryMap where the entries are placed 0098 * @param options See ParseOptions 0099 * @return See ParseInfo 0100 */ 0101 virtual ParseInfo parseConfig(const QByteArray &locale, KEntryMap &pWriteBackMap, ParseOptions options = ParseOptions()) = 0; 0102 0103 /** 0104 * Write the @em dirty entries to permanent storage 0105 * 0106 * @param locale the locale to write entries for (if the backend supports localized entries) 0107 * @param entryMap the KEntryMap containing the config object's entries. 0108 * @param options See WriteOptions 0109 * 0110 * @return @c true if the write was successful, @c false if writing the configuration failed 0111 */ 0112 virtual bool writeConfig(const QByteArray &locale, KEntryMap &entryMap, WriteOptions options) = 0; 0113 0114 /** 0115 * If isWritable() returns false, writeConfig() will always fail. 0116 * 0117 * @return @c true if the configuration is writable, @c false if it is immutable 0118 */ 0119 virtual bool isWritable() const = 0; 0120 /** 0121 * When isWritable() returns @c false, return an error message to 0122 * explain to the user why saving configuration will not work. 0123 * 0124 * The return value when isWritable() returns @c true is undefined. 0125 * 0126 * @returns a translated user-visible explanation for the configuration 0127 * object not being writable 0128 */ 0129 virtual QString nonWritableErrorMessage() const = 0; 0130 /** 0131 * @return the read/write status of the configuration object 0132 * 0133 * @see KConfigBase::AccessMode 0134 */ 0135 virtual KConfigBase::AccessMode accessMode() const = 0; 0136 /** 0137 * Create the enclosing object of the configuration object 0138 * 0139 * For example, if the configuration object is a file, this should create 0140 * the parent directory. 0141 */ 0142 virtual void createEnclosing() = 0; 0143 0144 /** 0145 * Set the file path. 0146 * 0147 * @note @p path @b MUST be @em absolute. 0148 * 0149 * @param path the absolute file path 0150 */ 0151 virtual void setFilePath(const QString &path) = 0; 0152 0153 /** 0154 * Lock the file 0155 */ 0156 virtual bool lock() = 0; 0157 /** 0158 * Release the lock on the file 0159 */ 0160 virtual void unlock() = 0; 0161 /** 0162 * @return @c true if the file is locked, @c false if it is not locked 0163 */ 0164 virtual bool isLocked() const = 0; 0165 0166 /** @return the absolute path to the object */ 0167 QString filePath() const; 0168 0169 protected: 0170 KConfigBackend(); 0171 void setLocalFilePath(const QString &file); 0172 0173 private: 0174 KConfigBackendPrivate *const d; 0175 }; 0176 0177 Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigBackend::ParseOptions) 0178 Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigBackend::WriteOptions) 0179 0180 #if 0 // TODO re-enable if the plugin loading code is re-enabled 0181 /** 0182 * Register a KConfig backend when it is contained in a loadable module 0183 */ 0184 #define K_EXPORT_KCONFIGBACKEND(libname, classname) K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) 0185 #endif 0186 0187 #endif // KCONFIGBACKEND_H