Overte C++ Documentation
SettingManager.h
1 //
2 // SettingManager.h
3 //
4 //
5 // Created by Clement on 2/2/15.
6 // Copyright 2015 High Fidelity, Inc.
7 // Copyright 2022 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_SettingManager_h
14 #define hifi_SettingManager_h
15 
16 #include <QtCore/QPointer>
17 #include <QtCore/QSettings>
18 #include <QtCore/QTimer>
19 #include <QtCore/QUuid>
20 
21 #include <QSettings>
22 #include <QThread>
23 #include <QLoggingCategory>
24 
25 #include "DependencyManager.h"
26 #include "shared/ReadWriteLockable.h"
27 
28 Q_DECLARE_LOGGING_CATEGORY(settings_manager)
29 Q_DECLARE_LOGGING_CATEGORY(settings_writer)
30 
31 // This is for the testing system.
32 class SettingsTests;
33 class SettingsTestsWorker;
34 
35 
36 
37 namespace Setting {
38  class Interface;
39 
52  class WriteWorker : public QObject {
53  Q_OBJECT
54 
55  public slots:
56 
61  void start();
62 
69  void setValue(const QString key, const QVariant value);
70 
76  void removeKey(const QString key);
77 
82  void clearAllSettings();
83 
88  void sync();
89 
94  void threadFinished();
95 
100  void terminate();
101 
102  private:
103 
104  void init() {
105  if (!_qSettings) {
106  _qSettings = new QSettings();
107  }
108  }
109 
110  QSettings* _qSettings = nullptr;
111  };
112 
126  class Manager : public QObject, public ReadWriteLockable, public Dependency {
127  Q_OBJECT
128 
129  public:
130  Manager(QObject *parent = nullptr);
131 
132  void customDeleter() override;
133 
139  QString fileName() const;
140 
146  void remove(const QString &key);
147 
153  QStringList allKeys() const;
154 
162  bool contains(const QString &key) const;
163 
170  void setValue(const QString &key, const QVariant &value);
171 
179  QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
180 
187  void clearAllSettings();
188  protected:
195  const int THREAD_TERMINATION_TIMEOUT = 2000;
196 
197  ~Manager();
198  void registerHandle(Interface* handle);
199  void removeHandle(const QString& key);
200 
201  void loadSetting(Interface* handle);
202  void saveSetting(Interface* handle);
203 
204 
210  void forceSave();
211 
217  void terminateThread();
218 
219  signals:
226  void valueChanged(const QString key, QVariant value);
227 
233  void keyRemoved(const QString key);
234 
240 
246 
254 
255  private:
256  QHash<QString, Interface*> _handles;
257 
258  friend class Interface;
259  friend class ::SettingsTests;
260  friend class ::SettingsTestsWorker;
261 
262  friend void cleanupSettingsSaveThread();
263  friend void setupSettingsSaveThread();
264 
265 
266  QHash<QString, QVariant> _settings;
267  QString _fileName;
268  QThread _workerThread;
269  };
270 }
271 
272 #endif // hifi_SettingManager_h
Settings manager.
Definition: SettingManager.h:126
void valueChanged(const QString key, QVariant value)
The value of a setting was changed.
void setValue(const QString &key, const QVariant &value)
Set a setting to a value.
Definition: SettingManager.cpp:206
void remove(const QString &key)
Remove a configuration key.
Definition: SettingManager.cpp:186
void forceSave()
Force saving the config to disk.
Definition: SettingManager.cpp:166
void clearAllSettings()
Clear all the settings.
Definition: SettingManager.cpp:220
void syncRequested()
A request to synchronize the settings to permanent storage was made.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value of a setting.
Definition: SettingManager.cpp:214
bool contains(const QString &key) const
Returns whether a key is part of the configuration.
Definition: SettingManager.cpp:200
const int THREAD_TERMINATION_TIMEOUT
How long to wait for writer thread termination.
Definition: SettingManager.h:195
void terminateThread()
Write config to disk and terminate the writer thread.
Definition: SettingManager.cpp:170
QString fileName() const
Returns the filename where the config file will be written.
Definition: SettingManager.cpp:180
void keyRemoved(const QString key)
A setting was removed.
QStringList allKeys() const
Lists all keys in the configuration.
Definition: SettingManager.cpp:194
void terminationRequested()
The termination of the settings system was requested.
void clearAllSettingsRequested()
A request to clear all the settings was made.
Settings write worker.
Definition: SettingManager.h:52
void threadFinished()
Called when the thread is terminating.
Definition: SettingManager.cpp:60
void clearAllSettings()
Remove all values from the configuration.
Definition: SettingManager.cpp:48
void start()
Initialize anything that needs initializing, called on thread start.
Definition: SettingManager.cpp:27
void setValue(const QString key, const QVariant value)
Sets a configuration value.
Definition: SettingManager.cpp:33
void removeKey(const QString key)
Remove a value from the configuration.
Definition: SettingManager.cpp:43
void sync()
Force writing the config to disk.
Definition: SettingManager.cpp:54
void terminate()
Thread is being asked to finish work and quit.
Definition: SettingManager.cpp:66