swift
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"
14 #include "misc/swiftmiscexport.h"
15 #include "misc/valuecache.h"
16 
17 namespace swift::misc
18 {
23  {
24  Q_OBJECT
25 
26  public:
28  static CSettingsCache *instance();
29 
31  static const QString &persistentStore();
32 
34  CStatusMessage saveToStore(const QString &keyPrefix = {});
35 
37  CStatusMessage saveToStore(const QStringList &keys);
38 
41  void enableLocalSave();
42 
44  CStatusMessage loadFromStore();
45 
47  static QString filenameForKey(const QString &key);
48 
50  static const QString &relativeFilePath();
51 
53  QStringList enumerateStore() const;
54 
55  private:
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 
103  QString getFilename() const { return CSettingsCache::filenameForKey(this->getKey()); }
104  };
105 
110  template <typename Trait>
112  {
113  public:
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;
121  CStatusMessage save() = 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
bool isInitialized() const
Can be false if key contains OwnerName% and owner's objectName was empty.
Definition: valuecache.h:465
CStatusMessage set(const Trait::type &value, qint64 timestamp=0)
Write a new value. Must be called from the thread in which the owner lives.
Definition: valuecache.h:411
const QString & getKey() const
Get the key string of this value.
Definition: valuecache.h:445
void setNotifySlot(F slot)
Set a callback to be called when the value is changed by another source.
Definition: valuecache.h:384
Trait::type get() const
Get a copy of the current value.
Definition: valuecache.h:408
Non-owning reference to a CPropertyIndex with a subset of its features.
Class template for accessing a specific value in the CSettingsCache.
Definition: settingscache.h:68
auto getDefault() const
Gets the default value.
Definition: settingscache.h:97
CStatusMessage setDefault()
Reset the setting to its default value.
Definition: settingscache.h:94
CSetting(T *owner)
Constructor.
Definition: settingscache.h:73
CSetting(T *owner, F slot)
Constructor.
Definition: settingscache.h:88
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.
Definition: settingscache.h:23
static QString filenameForKey(const QString &key)
Return the filename where the value with the given key 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:66
Free functions in swift::misc.
Base class for traits to be used as template argument to swift::misc::CSetting.
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 T & defaultValue()
Return the value to use in case the supplied value does not satisfy the validator....
static const QString & humanReadable()
Optional human readable name.
TSettingTrait & operator=(const TSettingTrait &)=delete
Deleted copy assignment operator.
T type
Data type of the value.
static const char * key()
Key string of the value. Reimplemented in derived class.
#define SWIFT_MISC_EXPORT
Export a class or function from the library.