BibleTime
btconfigcore.cpp
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2025 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#include "btconfigcore.h"
14
15#ifndef NDEBUG
16#include <QRegularExpression>
17#endif
18#include <QSettings>
19#include <QScopeGuard>
20#include "../../util/btassert.h"
21#include "../../util/btdebugonly.h"
22
23
24namespace {
25
26auto groupGuard(QSettings & settings, QString const & group) {
27 settings.beginGroup(group);
28 return qScopeGuard([&settings]() { settings.endGroup(); });
29}
30
31} // anonymous namespace
32
33BtConfigCore::BtConfigCore(std::shared_ptr<QSettings> state,
34 QString groupPrefix)
35 : m_state(std::move(state))
36 , m_groupPrefix(std::move(groupPrefix))
37{
39 static QRegularExpression const groupRegExp(
40 QStringLiteral("^([^/]+/)*$"));)
41 BT_ASSERT(groupRegExp.match(m_groupPrefix).hasMatch());
42}
43
45
46QVariant BtConfigCore::qVariantValue(QString const & key,
47 QVariant const & defaultValue) const
48{ return m_state->value(m_groupPrefix + key, defaultValue); }
49
50QStringList BtConfigCore::childKeys() const {
51 if (m_groupPrefix.isEmpty())
52 return m_state->childKeys();
53 auto const cleanup = groupGuard(*m_state, m_groupPrefix);
54 return m_state->childKeys();
55}
56
57QStringList BtConfigCore::childGroups() const {
58 if (m_groupPrefix.isEmpty())
59 return m_state->childGroups();
60 auto const cleanup = groupGuard(*m_state, m_groupPrefix);
61 return m_state->childGroups();
62}
63
64void BtConfigCore::remove(QString const & key)
65{ m_state->remove(m_groupPrefix + key); }
66
67void BtConfigCore::sync() { m_state->sync(); }
68
69void BtConfigCore::setValue_(QString const & key, QVariant value)
70{ m_state->setValue(m_groupPrefix + key, value); }
#define BT_ASSERT(...)
Definition btassert.h:17
#define BT_DEBUG_ONLY(...)
Definition btdebugonly.h:19
std::shared_ptr< QSettings > m_state
void sync()
Synchronizes the configuration to disk.
void setValue_(QString const &key, QVariant value)
QStringList childKeys() const
void remove(QString const &key)
removes a key (and its children) from the current group.
QString m_groupPrefix
Empty or absolute path with trailing slash.
T value(QString const &key, T const &defaultValue=T()) const
Returns the settings value for the given global key.
QStringList childGroups() const
BtConfigCore(BtConfigCore &&)=default
QVariant qVariantValue(QString const &key, QVariant const &defaultValue=QVariant()) const
Returns the settings value for the given global key as a QVariant.
auto groupGuard(QSettings &settings, QString const &group)