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#include "kconfiginibackendreader_p.h"
25
26class QIODevice;
27class KEntryMap;
28
29class KConfigIniBackend
30{
31 Q_GADGET
32 Q_DECLARE_TR_FUNCTIONS(KConfigIniBackend)
33 Q_DISABLE_COPY(KConfigIniBackend)
34
35private:
36 std::unique_ptr<AbstractLockFile> lockFile;
37 QMutex m_mutex;
38
39public:
40 explicit KConfigIniBackend(std::unique_ptr<KConfigIniBackendAbstractDevice> deviceInterface);
41
42 /* Allows the behaviour of parseConfig() to be tuned */
43 enum ParseOption {
44 ParseGlobal = 1, // entries should be marked as global
45 ParseDefaults = 2, // entries should be marked as default
46 ParseExpansions = 4, // entries are allowed to be marked as expandable
47 };
48 Q_FLAG(ParseOption)
49 Q_DECLARE_FLAGS(ParseOptions, ParseOption)
50
51 /* Allows the behaviour of writeConfig() to be tuned */
52 enum WriteOption {
53 WriteGlobal = 1 /// only write entries marked as "global"
54 };
55 Q_FLAG(WriteOption)
56 Q_DECLARE_FLAGS(WriteOptions, WriteOption)
57
58 /* Return value from parseConfig() */
59 enum ParseInfo {
60 ParseOk, // the configuration was opened read/write
61 ParseImmutable, // the configuration is immutable
62 ParseOpenError, // the configuration could not be opened
63 };
64
65 ParseInfo parseConfig(const QByteArray &locale, KEntryMap &entryMap, ParseOptions options);
66 ParseInfo parseConfig(const QByteArray &locale, KEntryMap &entryMap, ParseOptions options, bool merging);
67 bool writeConfig(const QByteArray &locale, KEntryMap &entryMap, WriteOptions options);
68
69 /** Group that will always be the first in the ini file, to serve as a magic file signature */
70 void setPrimaryGroup(const QString &group);
71
72 bool isWritable() const;
73 QString nonWritableErrorMessage() const;
74 KConfigBase::AccessMode accessMode() const;
75 void createEnclosing();
76 bool lock();
77 void unlock();
78 bool isLocked() const;
79 void setDeviceInterface(std::unique_ptr<KConfigIniBackendAbstractDevice> deviceInterface)
80 {
81 mDeviceInterface = std::move(deviceInterface);
82 }
83 [[nodiscard]] bool hasOpenableDeviceInterface() const;
84 [[nodiscard]] QString backingDevicePath() const;
85
86private:
87 enum StringType {
88 GroupString = 0,
89 KeyString = 1,
90 ValueString = 2,
91 };
92 // Warning: this modifies data in-place. Other QByteArrayView objects referencing the same buffer
93 // fragment will get their data modified too.
94 static bool printableToString(QByteArrayView &aString, const KConfigIniBackendAbstractDevice *device, int line);
95 static QByteArray stringToPrintable(const QByteArray &aString, StringType type);
96 [[nodiscard]] static char charFromHex(const char *str, const KConfigIniBackendAbstractDevice *device, int line);
97
98 void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map);
99 void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map, bool defaultGroup, bool primaryGroup, bool &firstEntry);
100
101 std::unique_ptr<KConfigIniBackendAbstractDevice> mDeviceInterface;
102 QString mPrimaryGroup;
103};
104
105Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigIniBackend::ParseOptions)
106Q_DECLARE_OPERATORS_FOR_FLAGS(KConfigIniBackend::WriteOptions)
107
108#endif // KCONFIGINI_P_H
109

source code of kconfig/src/core/kconfigini_p.h