BibleTime
btconfigcore.h
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 #pragma once
14 
15 #include <memory>
16 #include <QString>
17 #include <QStringList>
18 #include <QVariant>
19 #include <utility>
20 
21 
22 class QSettings;
23 
24 /**
25  \note Session keys are QStrings because we even want to handle cases where the
26  configuration file is manually changed. When creating new sessions, they
27  are still generated from unsigned integers.
28 */
29 class BtConfigCore {
30 
31  friend class BtConfig;
32 
33 public: // methods:
34 
35  BtConfigCore(BtConfigCore &&) = default;
36  BtConfigCore(BtConfigCore const &) = default;
38  BtConfigCore & operator=(BtConfigCore const &) = default;
39 
41 
42  /**
43  \brief Returns the settings value for the given global key.
44 
45  \param[in] key Key to get the value for.
46  \param[in] defaultValue The value to return if no saved value is found.
47  \returns the value of type specified by the template parameter.
48  */
49  template <typename T>
50  T value(QString const & key, T const & defaultValue = T()) const {
51  return qVariantValue(
52  key,
53  QVariant::fromValue(defaultValue)).template value<T>();
54  }
55 
56  /**
57  \brief Returns the settings value for the given global key as a QVariant.
58 
59  \param[in] key Key to get the value for.
60  \param[in] defaultValue The value to return if no saved value is found.
61  \returns the value.
62  */
63  QVariant qVariantValue(QString const & key,
64  QVariant const & defaultValue = QVariant()) const;
65 
66  /**
67  \brief Sets a value for a key.
68 
69  \param[in] key Ket to set.
70  \param[in] value Value to set.
71  */
72  template <typename T>
73  void setValue(QString const & key, T const & value)
74  { return setValue_(key, QVariant::fromValue(value)); }
75 
76  /** \returns a list of first-level keys in the current group. */
77  QStringList childKeys() const;
78 
79  /** \returns a list of first-level groups in the current group. */
80  QStringList childGroups() const;
81 
82  /**
83  \brief removes a key (and its children) from the current group.
84  \param[in] key the key to remove
85  */
86  void remove(QString const & key);
87 
88  /**
89  \param[in] prefix the prefix to append
90  \returns a BtConfigCore object for a subgroup.
91  */
92  template <typename Prefix>
93  BtConfigCore group(Prefix && prefix) const &
94  { return {m_state, m_groupPrefix + prefix + '/'}; }
95 
96  /**
97  \param[in] prefix the prefix to append
98  \returns a BtConfigCore object for a subgroup.
99  */
100  template <typename Prefix>
101  BtConfigCore group(Prefix && prefix) && {
102  m_groupPrefix.append(prefix).append('/');
103  return std::move(*this);
104  }
105 
106  /** \brief Synchronizes the configuration to disk. */
107  void sync();
108 
109 private: // methods:
110 
111  BtConfigCore(std::shared_ptr<QSettings> state,
112  QString groupPrefix = QString());
113 
114  void setValue_(QString const & key, QVariant value);
115 
116 private: // fields:
117 
118  std::shared_ptr<QSettings> m_state;
119  QString m_groupPrefix; ///< Empty or absolute path with trailing slash
120 
121 }; /* class BtConfigCore */
std::shared_ptr< QSettings > m_state
Definition: btconfigcore.h:118
BtConfigCore group(Prefix &&prefix) &&
Definition: btconfigcore.h:101
BtConfigCore & operator=(BtConfigCore &&)=default
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.
BtConfigCore group(Prefix &&prefix) const &
Definition: btconfigcore.h:93
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
BtConfigCore & operator=(BtConfigCore const &)=default
void setValue(QString const &key, T const &value)
Sets a value for a key.
Definition: btconfigcore.h:73
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.
BtConfigCore(BtConfigCore const &)=default
#define T(f)