| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2006, 2007 Thomas Braxton <kde.braxton@gmail.com> |
| 4 | SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org> |
| 5 | SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <kalle@kde.org> |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #ifndef KCONFIGINI_P_H |
| 11 | #define KCONFIGINI_P_H |
| 12 | |
| 13 | #include <QCoreApplication> |
| 14 | #include <QFile> |
| 15 | #include <QLockFile> |
| 16 | #include <QMutex> |
| 17 | #include <QSharedData> |
| 18 | |
| 19 | #include <kconfigbase.h> |
| 20 | #include <kconfigcore_export.h> |
| 21 | |
| 22 | #include <memory> |
| 23 | |
| 24 | class QIODevice; |
| 25 | class KEntryMap; |
| 26 | |
| 27 | class KConfigIniBackend |
| 28 | { |
| 29 | Q_GADGET |
| 30 | Q_DECLARE_TR_FUNCTIONS(KConfigIniBackend) |
| 31 | Q_DISABLE_COPY(KConfigIniBackend) |
| 32 | |
| 33 | private: |
| 34 | std::unique_ptr<QLockFile> lockFile; |
| 35 | QMutex m_mutex; |
| 36 | |
| 37 | public: |
| 38 | KConfigIniBackend(); |
| 39 | |
| 40 | /* Allows the behaviour of parseConfig() to be tuned */ |
| 41 | enum ParseOption { |
| 42 | ParseGlobal = 1, // entries should be marked as global |
| 43 | ParseDefaults = 2, // entries should be marked as default |
| 44 | ParseExpansions = 4, // entries are allowed to be marked as expandable |
| 45 | }; |
| 46 | Q_FLAG(ParseOption) |
| 47 | Q_DECLARE_FLAGS(ParseOptions, ParseOption) |
| 48 | |
| 49 | /* Allows the behaviour of writeConfig() to be tuned */ |
| 50 | enum WriteOption { |
| 51 | WriteGlobal = 1 /// only write entries marked as "global" |
| 52 | }; |
| 53 | Q_FLAG(WriteOption) |
| 54 | Q_DECLARE_FLAGS(WriteOptions, WriteOption) |
| 55 | |
| 56 | /* Return value from parseConfig() */ |
| 57 | enum ParseInfo { |
| 58 | ParseOk, // the configuration was opened read/write |
| 59 | ParseImmutable, // the configuration is immutable |
| 60 | ParseOpenError, // the configuration could not be opened |
| 61 | }; |
| 62 | |
| 63 | ParseInfo parseConfig(const QByteArray &locale, KEntryMap &entryMap, ParseOptions options); |
| 64 | ParseInfo parseConfig(const QByteArray &locale, KEntryMap &entryMap, ParseOptions options, bool merging); |
| 65 | bool writeConfig(const QByteArray &locale, KEntryMap &entryMap, WriteOptions options); |
| 66 | |
| 67 | /** Group that will always be the first in the ini file, to serve as a magic file signature */ |
| 68 | void setPrimaryGroup(const QString &group); |
| 69 | |
| 70 | bool isWritable() const; |
| 71 | QString nonWritableErrorMessage() const; |
| 72 | KConfigBase::AccessMode accessMode() const; |
| 73 | void createEnclosing(); |
| 74 | void setFilePath(const QString &path); |
| 75 | bool lock(); |
| 76 | void unlock(); |
| 77 | bool isLocked() const; |
| 78 | |
| 79 | /* the absolute path to the object */ |
| 80 | QString filePath() const; |
| 81 | |
| 82 | private: |
| 83 | enum StringType { |
| 84 | GroupString = 0, |
| 85 | KeyString = 1, |
| 86 | ValueString = 2, |
| 87 | }; |
| 88 | // Warning: this modifies data in-place. Other QByteArrayView objects referencing the same buffer |
| 89 | // fragment will get their data modified too. |
| 90 | static void printableToString(QByteArrayView &aString, const QFile &file, int line); |
| 91 | static QByteArray stringToPrintable(const QByteArray &aString, StringType type); |
| 92 | static char charFromHex(const char *str, const QFile &file, int line); |
| 93 | static QString warningProlog(const QFile &file, int line); |
| 94 | |
| 95 | void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map); |
| 96 | void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map, bool defaultGroup, bool primaryGroup, bool &firstEntry); |
| 97 | |
| 98 | void setLocalFilePath(const QString &file); |
| 99 | |
| 100 | QString mLocalFilePath; |
| 101 | QString mPrimaryGroup; |
| 102 | }; |
| 103 | |
| 104 | Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigIniBackend::ParseOptions) |
| 105 | Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigIniBackend::WriteOptions) |
| 106 | |
| 107 | #endif // KCONFIGINI_P_H |
| 108 | |