2 This file is part of the KDE libraries
3 Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
4 Copyright (c) 1999 Preston Brown <pbrown@kde.org>
5 Portions copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #ifndef KCONFIGBACKEND_H
24 #define KCONFIGBACKEND_H
26 #include <QtCore/QObject>
27 #include <QtCore/QString>
29 #include <kdecore_export.h>
30 #include <kconfigbase.h>
31 #include <kgenericfactory.h>
32 #include <ksharedptr.h>
41 * \class KConfigBackend kconfigbackend.h <KConfigBackend>
43 * Provides the implementation for accessing configuration sources.
45 * KDELibs only provides an INI backend, but this class can be used
46 * to create plugins that allow access to other file formats and
47 * configuration systems.
49 class KDECORE_EXPORT KConfigBackend
: public QObject
, public KShared
57 * Creates a new KConfig backend.
59 * If no @p system is given, or the given @p system is unknown, this method tries
60 * to determine the correct backend to use.
62 * @param componentData the owning component
63 * @param fileName the absolute file name of the configuration file
64 * @param system the configuration system to use
65 * @return a KConfigBackend object to be used with KConfig
67 static KSharedPtr
<KConfigBackend
> create(const KComponentData
& componentData
,
68 const QString
& fileName
= QString(),
69 const QString
& system
= QString());
72 * Registers mappings from directories/files to configuration systems
74 * Allows you to tell KConfigBackend that create() should use a particular
75 * backend for a particular file or directory.
77 * @warning currently does nothing
79 * @param entryMap the KEntryMap to build the mappings from
81 static void registerMappings(const KEntryMap
& entryMap
);
83 /** Destroys the backend */
84 virtual ~KConfigBackend();
86 /** Allows the behaviour of parseConfig() to be tuned */
88 ParseGlobal
= 1, /// entries should be marked as @em global
89 ParseDefaults
= 2, /// entries should be marked as @em default
90 ParseExpansions
= 4 /// entries are allowed to be marked as @em expandable
92 /// @typedef typedef QFlags<ParseOption> ParseOptions
93 Q_DECLARE_FLAGS(ParseOptions
, ParseOption
)
95 /** Allows the behaviour of writeConfig() to be tuned */
97 WriteGlobal
= 1 /// only write entries marked as "global"
99 /// @typedef typedef QFlags<WriteOption> WriteOptions
100 Q_DECLARE_FLAGS(WriteOptions
, WriteOption
)
102 /** Return value from parseConfig() */
104 ParseOk
, /// the configuration was opened read/write
105 ParseImmutable
, /// the configuration is @em immutable
106 ParseOpenError
/// the configuration could not be opened
110 * Read persistent storage
112 * @param locale the locale to read entries for (if the backend supports localized entries)
113 * @param pWriteBackMap the KEntryMap where the entries are placed
114 * @param options @see ParseOptions
115 * @return @see ParseInfo
117 virtual ParseInfo
parseConfig(const QByteArray
& locale
,
118 KEntryMap
& pWriteBackMap
,
119 ParseOptions options
= ParseOptions()) = 0;
122 * Write the @em dirty entries to permanent storage
124 * @param locale the locale to write entries for (if the backend supports localized entries)
125 * @param entryMap the KEntryMap containing the config object's entries.
126 * @param options @see WriteOptions
127 * @param data the component that requested the write
129 * @return @c true if the write was successful, @c false if writing the configuration failed
131 virtual bool writeConfig(const QByteArray
& locale
, KEntryMap
& entryMap
,
132 WriteOptions options
, const KComponentData
&data
) = 0;
135 * If isWritable() returns false, writeConfig() will always fail.
137 * @return @c true if the configuration is writable, @c false if it is immutable
139 virtual bool isWritable() const = 0;
141 * When isWritable() returns @c false, return an error message to
142 * explain to the user why saving configuration will not work.
144 * The return value when isWritable() returns @c true is undefined.
146 * @returns a translated user-visible explanation for the configuration
147 * object not being writable
149 virtual QString
nonWritableErrorMessage() const = 0;
151 * @return the read/write status of the configuration object
153 * @see KConfigBase::AccessMode
155 virtual KConfigBase::AccessMode
accessMode() const = 0;
157 * Create the enclosing object of the configuration object
159 * For example, if the configuration object is a file, this should create
160 * the parent directory.
162 virtual void createEnclosing() = 0;
167 * @note @p path @b MUST be @em absolute.
169 * @param path the absolute file path
171 virtual void setFilePath(const QString
& path
) = 0;
176 virtual bool lock(const KComponentData
& componentData
) = 0;
178 * Release the lock on the file
180 virtual void unlock() = 0;
182 * @return @c true if the file is locked, @c false if it is not locked
184 virtual bool isLocked() const = 0;
187 * @return the date and time when the object was last modified
189 QDateTime
lastModified() const;
190 /** @return the absolute path to the object */
191 QString
filePath() const;
192 /** @return the size of the object */
197 void setLastModified(const QDateTime
& dt
);
198 void setSize(qint64 sz
);
199 void setLocalFilePath(const QString
& file
);
206 Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigBackend::ParseOptions
)
207 Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigBackend::WriteOptions
)
210 * Register a KConfig backend when it is contained in a loadable module
212 #define K_EXPORT_KCONFIGBACKEND(libname, classname) \
213 K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
214 K_EXPORT_PLUGIN(factory("kconfigbackend_" #libname))
217 #endif // KCONFIGBACKEND_H