swift
Loading...
Searching...
No Matches
settingscache.cpp
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#include <QStandardPaths>
7
8#include "misc/logmessage.h"
9
10namespace swift::misc
11{
12 CSettingsCache::CSettingsCache() : CValueCache(0) {}
13
14 CSettingsCache *CSettingsCache::instance()
15 {
16 static std::unique_ptr<CSettingsCache> cache(new CSettingsCache);
17 static auto dummy = (connect(qApp, &QObject::destroyed, cache.get(), [] { cache.reset(); }), nullptr);
18 Q_UNUSED(dummy) // declared as static to get thread-safe initialization
19 return cache.get();
20 }
21
23 {
24 static const QString dir = CFileUtils::appendFilePaths(getCacheRootDirectory(), relativeFilePath());
25 return dir;
26 }
27
28 const QString &CSettingsCache::lockFileName()
29 {
30 static const QString file = CFileUtils::appendFilePaths(persistentStore(), ".lock");
31 return file;
32 }
33
34 CStatusMessage CSettingsCache::lockFile(QLockFile &lock)
35 {
36 Q_ASSERT(!lock.isLocked());
37 if (!QDir::root().mkpath(persistentStore()))
38 {
39 return CStatusMessage(this).error(u"Failed to create %1") << persistentStore();
40 }
41 if (!lock.lock())
42 {
43 return CStatusMessage(this).error(u"Failed to lock %1: %2")
44 << lockFileName() << CFileUtils::lockFileError(lock);
45 }
46 return {};
47 }
48
50 {
51 QLockFile lock(lockFileName());
52 const CStatusMessage lockStatus = lockFile(lock);
53 if (lockStatus.isFailure())
54 {
55 return lockStatus;
56 }
57
58 return saveToFiles(persistentStore(), keyPrefix);
59 }
60
62 {
63 QLockFile lock(lockFileName());
64 const CStatusMessage lockStatus = lockFile(lock);
65 if (lockStatus.isFailure())
66 {
67 return lockStatus;
68 }
69
70 return saveToFiles(persistentStore(), keys);
71 }
72
78
79 void CSettingsCache::saveToStoreByPacket(const CValueCachePacket &values)
80 {
81 QLockFile lock(lockFileName());
82 const CStatusMessage lockStatus = lockFile(lock);
83 if (lockStatus.isFailure())
84 {
85 CLogMessage::preformatted(lockStatus);
86 return;
87 }
88
89 CStatusMessage status = saveToFiles(persistentStore(), values.toVariantMap());
90 CLogMessage::preformatted(status);
91 }
92
94 {
95 QLockFile lock(lockFileName());
96 const CStatusMessage lockStatus = lockFile(lock);
97 if (lockStatus.isFailure())
98 {
99 return lockStatus;
100 }
101
103 }
104
109
111 {
112 static const QString p("/settings/core");
113 return p;
114 }
115
117} // namespace swift::misc
static QString appendFilePaths(const QString &path1, const QString &path2)
Append file paths.
static void preformatted(const CStatusMessage &statusMessage)
Sends a verbatim, preformatted message to the log.
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.
bool isFailure() const
Operation considered unsuccessful.
Manages a map of { QString, CVariant } pairs, which can be distributed among multiple processes.
Definition valuecache.h:154
QStringList enumerateFiles(const QString &directory) const
List the Json files which are (or would be) used to save the current values. The files may or may not...
QString filenameForKey(const QString &key) const
Return the (relative) filename that may is (or would be) used to save the value with the given key....
void valuesSaveRequested(const swift::misc::CValueCachePacket &values)
Emitted when this cache has ratified a change which included a request to save (i....
CStatusMessage loadFromFiles(const QString &directory)
Load all values from Json files in a given directory. Values already in the cache will remain in the ...
CStatusMessage saveToFiles(const QString &directory, const QString &keyPrefix={})
Save values to Json files in a given directory. If prefix is provided then only those values whose ke...
Value class used for signalling changed values in the cache.
Definition valuecache.h:67
CVariantMap toVariantMap() const
Discard timestamps and return as variant map.
Free functions in swift::misc.
QDir root()
bool isLocked() const const
bool lock()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)