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-2021 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 
24 namespace {
25 
26 auto groupGuard(QSettings & settings, QString const & group) {
27  settings.beginGroup(group);
28  return qScopeGuard([&settings]() { settings.endGroup(); });
29 }
30 
31 } // anonymous namespace
32 
33 BtConfigCore::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 
44 BtConfigCore::~BtConfigCore() = default;
45 
46 QVariant BtConfigCore::qVariantValue(QString const & key,
47  QVariant const & defaultValue) const
48 { return m_state->value(m_groupPrefix + key, defaultValue); }
49 
50 QStringList 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 
57 QStringList 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 
64 void BtConfigCore::remove(QString const & key)
65 { m_state->remove(m_groupPrefix + key); }
66 
67 void BtConfigCore::sync() { m_state->sync(); }
68 
69 void 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
Definition: btconfigcore.h:118
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.
Definition: btconfigcore.h:119
T value(QString const &key, T const &defaultValue=T()) const
Returns the settings value for the given global key.
Definition: btconfigcore.h:50
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)