swift
Loading...
Searching...
No Matches
settingscache.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_SETTINGSCACHE_H
7#define SWIFT_MISC_SETTINGSCACHE_H
8
9#include <QObject>
10#include <QString>
11#include <QStringList>
12
13#include "misc/statusmessage.h"
15#include "misc/valuecache.h"
16
17namespace swift::misc
18{
22 class SWIFT_MISC_EXPORT CSettingsCache : public swift::misc::CValueCache
23 {
25
26 public:
28 static CSettingsCache *instance();
29
31 static const QString &persistentStore();
32
34 CStatusMessage saveToStore(const QString &keyPrefix = {});
35
38
41 void enableLocalSave();
42
45
47 static QString filenameForKey(const QString &key);
48
50 static const QString &relativeFilePath();
51
54
55 private:
56 CSettingsCache();
57 void saveToStoreByPacket(const CValueCachePacket &values);
58 static const QString &lockFileName();
59 CStatusMessage lockFile(QLockFile &);
60 };
61
66 template <typename Trait>
67 class CSetting : public swift::misc::CCached<typename Trait::type>
68 {
69 public:
72 template <typename T>
73 CSetting(T *owner)
74 : CSetting::CCached(CSettingsCache::instance(), Trait::key(), Trait::humanReadable(), Trait::isValid,
75 Trait::defaultValue(), owner)
76 {
77 if (!this->isInitialized())
78 {
79 this->onOwnerNameChanged([this, owner] { private_ns::reconstruct(this, owner); });
80 }
81 }
82
87 template <typename T, typename F>
88 CSetting(T *owner, F slot) : CSetting(owner)
89 {
90 this->setNotifySlot(slot);
91 }
92
94 CStatusMessage setDefault() { return this->set(Trait::defaultValue()); }
95
97 auto getDefault() const { return Trait::defaultValue(); }
98
100 bool isDefault() const { return this->getDefault() == this->get(); }
101
104 };
105
110 template <typename Trait>
112 {
113 public:
115 using CSetting<Trait>::CSetting;
116
119 CStatusMessage set(const typename Trait::type &value, qint64 timestamp = 0) = delete;
120 CStatusMessage setAndSave(const typename Trait::type &value, qint64 timestamp = 0) = delete;
122 CStatusMessage setProperty(CPropertyIndexRef index, const CVariant &value, qint64 timestamp = 0) = delete;
124 qint64 timestamp = 0) = delete;
127 };
128
132 template <typename T>
134 {
136 using type = T;
137
139 static const char *key()
140 {
141 qFatal("Not implemented");
142 return "";
143 }
144
146 static const QString &humanReadable()
147 {
148 static const QString name;
149 return name;
150 }
151
154 static bool isValid(const T &value, QString &reason)
155 {
156 Q_UNUSED(value);
157 Q_UNUSED(reason);
158 return true;
159 }
160
163 static const T &defaultValue()
164 {
165 static const T def {};
166 return def;
167 }
168
170 TSettingTrait() = delete;
171
173 TSettingTrait(const TSettingTrait &) = delete;
174
177 };
178} // namespace swift::misc
179
180#endif // SWIFT_MISC_SETTINGSCACHE_H
Provides access to one of the values stored in a CValueCache.
Definition valuecache.h:353
const QString & getKey() const
Definition valuecache.h:448
CStatusMessage set(const Trait::type &value, qint64 timestamp=0)
Definition valuecache.h:414
CCached(CValueCache *cache, const QString &key, const QString &name, U *owner)
Definition valuecache.h:361
Non-owning reference to a CPropertyIndex with a subset of its features.
Class template for accessing a specific value in the CSettingsCache.
auto getDefault() const
Gets the default value.
CStatusMessage setDefault()
Reset the setting to its default value.
CSetting(T *owner)
Constructor.
CSetting(T *owner, F slot)
Constructor.
bool isDefault() const
Is default value?
QString getFilename() const
Return the file that is used for persistence for this value.
Class template for accessing a specific value in the CSettingsCache.
CStatusMessage setDefault()=delete
Deleted mutators.
CStatusMessage save()=delete
Deleted mutators.
CStatusMessage setAndSaveProperty(CPropertyIndexRef index, const CVariant &value, qint64 timestamp=0)=delete
Deleted mutators.
CStatusMessage setProperty(CPropertyIndexRef index, const CVariant &value, qint64 timestamp=0)=delete
Deleted mutators.
CStatusMessage setAndSave(const typename Trait::type &value, qint64 timestamp=0)=delete
Deleted mutators.
CStatusMessage set(const typename Trait::type &value, qint64 timestamp=0)=delete
Deleted mutators.
Singleton derived class of CValueCache, for settings.
static QString filenameForKey(const QString &key)
Return the filename where the value with the given key may be stored.
static const QString & persistentStore()
The directory where settings are stored.
CStatusMessage loadFromStore()
Load settings from disk.
void enableLocalSave()
Connects signal CValueCache::valuesSaveRequested to a private slot that saves the values....
static const QString & relativeFilePath()
Relative file path in application data directory.
static CSettingsCache * instance()
Return the singleton instance.
CStatusMessage saveToStore(const QString &keyPrefix={})
Save settings to disk.
QStringList enumerateStore() const
Return all files where settings may be stored.
Streamable status message, e.g.
Manages a map of { QString, CVariant } pairs, which can be distributed among multiple processes.
Definition valuecache.h:154
Value class used for signalling changed values in the cache.
Definition valuecache.h:67
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
Free functions in swift::misc.
Q_OBJECTQ_OBJECT
TSettingTrait(const TSettingTrait &)=delete
Deleted copy constructor.
static bool isValid(const T &value, QString &reason)
Validator function. Return true if the argument is valid, false otherwise. Default implementation jus...
TSettingTrait()=delete
Deleted default constructor.
static const char * key()
Key string of the value. Reimplemented in derived class.
static const QString & humanReadable()
Optional human readable name.
static const T & defaultValue()
Return the value to use in case the supplied value does not satisfy the validator....
T type
Data type of the value.
TSettingTrait & operator=(const TSettingTrait &)=delete
Deleted copy assignment operator.
#define SWIFT_MISC_EXPORT
Export a class or function from the library.