|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + |
| 3 | +#include <QSettings> |
| 4 | +#include <QCoreApplication> |
| 5 | +#include <QStandardPaths> |
| 6 | +#include <QDebug> |
| 7 | + |
| 8 | +#include "settings.h" |
| 9 | +#include "ifilepaths.h" |
| 10 | + |
| 11 | +using namespace scratchcpp; |
| 12 | + |
| 13 | +Settings::Settings(QObject *parent) : |
| 14 | + QObject(parent), |
| 15 | + m_mainSettingsInstance(paths()->configLocation(), QSettings::IniFormat), |
| 16 | + m_tmpSettingsInstance(paths()->configLocation() + ".tmp", QSettings::IniFormat) |
| 17 | +{ |
| 18 | + m_settingsInstance = &m_mainSettingsInstance; |
| 19 | +} |
| 20 | + |
| 21 | +void Settings::addKey(const QString &moduleName, const QString &keyName, const QVariant &defaultValue) |
| 22 | +{ |
| 23 | + m_defaults.insert({ moduleName, keyName }, defaultValue); |
| 24 | +} |
| 25 | + |
| 26 | +void Settings::setValue(const QString &moduleName, const QString &keyName, const QVariant &value) |
| 27 | +{ |
| 28 | + set(moduleName + "/" + keyName, value); |
| 29 | +} |
| 30 | + |
| 31 | +QVariant Settings::getValue(const QString &moduleName, const QString &keyName) |
| 32 | +{ |
| 33 | + QPair<QString, QString> key(moduleName, keyName); |
| 34 | + QVariant defaultValue; |
| 35 | + auto it = m_defaults.find(key); |
| 36 | + |
| 37 | + if (it != m_defaults.cend()) |
| 38 | + defaultValue = it.value(); |
| 39 | + |
| 40 | + return get(moduleName + "/" + keyName, defaultValue); |
| 41 | +} |
| 42 | + |
| 43 | +bool Settings::containsKey(const QString &moduleName, const QString &keyName) |
| 44 | +{ |
| 45 | + QPair<QString, QString> key(moduleName, keyName); |
| 46 | + return contains(moduleName + "/" + keyName); |
| 47 | +} |
| 48 | + |
| 49 | +/* |
| 50 | + * Switches to temporary settings. You can decide to saveChanges() or discardChanges() later. |
| 51 | + * This is useful for settings dialogs with a discard button. |
| 52 | + */ |
| 53 | +void Settings::freeze(void) |
| 54 | +{ |
| 55 | + Q_ASSERT(!m_frozen); |
| 56 | + m_settingsInstance = &m_tmpSettingsInstance; |
| 57 | + copySettings(&m_mainSettingsInstance, m_settingsInstance); |
| 58 | + m_frozen = true; |
| 59 | + emit stateChanged(); |
| 60 | +} |
| 61 | + |
| 62 | +// Saves changes to real settings and switches back to them. |
| 63 | +void Settings::saveChanges(void) |
| 64 | +{ |
| 65 | + Q_ASSERT(m_frozen); |
| 66 | + copySettings(m_settingsInstance, &m_mainSettingsInstance); |
| 67 | + m_mainSettingsInstance.sync(); |
| 68 | + m_settingsInstance = &m_mainSettingsInstance; |
| 69 | + m_frozen = false; |
| 70 | + emit stateChanged(); |
| 71 | + emit saved(); |
| 72 | +} |
| 73 | + |
| 74 | +// Discards changes and switches back to real settings. |
| 75 | +void Settings::discardChanges(void) |
| 76 | +{ |
| 77 | + Q_ASSERT(m_frozen); |
| 78 | + m_settingsInstance = &m_mainSettingsInstance; |
| 79 | + m_frozen = false; |
| 80 | + emit stateChanged(); |
| 81 | + emit discarded(); |
| 82 | +} |
| 83 | + |
| 84 | +bool Settings::isFrozen(void) |
| 85 | +{ |
| 86 | + return m_frozen; |
| 87 | +} |
| 88 | + |
| 89 | +QVariant Settings::get(const QString &key, const QVariant &defaultValue) |
| 90 | +{ |
| 91 | + Q_ASSERT(m_settingsInstance != nullptr); |
| 92 | +#ifdef Q_OS_WASM |
| 93 | + if (m_settingsInstance->isWritable()) { |
| 94 | + if (!m_tempSettingsCopied) |
| 95 | + copyTempSettings(); |
| 96 | + return m_settingsInstance->value(key, defaultValue); |
| 97 | + } else { |
| 98 | + // Use temporary settings until sandbox is initialized |
| 99 | + QSettings settings(paths()->configLocation(), QSettings::IniFormat); |
| 100 | + return settings.value(key, defaultValue); |
| 101 | + } |
| 102 | +#else |
| 103 | + return m_settingsInstance->value(key, defaultValue); |
| 104 | +#endif // Q_OS_WASM |
| 105 | +} |
| 106 | + |
| 107 | +bool Settings::contains(const QString &key) |
| 108 | +{ |
| 109 | + Q_ASSERT(m_settingsInstance != nullptr); |
| 110 | +#ifdef Q_OS_WASM |
| 111 | + if (m_settingsInstance->isWritable()) { |
| 112 | + if (!m_tempSettingsCopied) |
| 113 | + copyTempSettings(); |
| 114 | + return m_settingsInstance->contains(key); |
| 115 | + } else { |
| 116 | + // Use temporary settings until sandbox is initialized |
| 117 | + QSettings settings(paths()->configLocation(), QSettings::IniFormat); |
| 118 | + return settings.contains(key); |
| 119 | + } |
| 120 | +#else |
| 121 | + return m_settingsInstance->contains(key); |
| 122 | +#endif // Q_OS_WASM |
| 123 | +} |
| 124 | + |
| 125 | +void Settings::set(const QString &key, const QVariant &value) |
| 126 | +{ |
| 127 | + Q_ASSERT(m_settingsInstance != nullptr); |
| 128 | +#ifdef Q_OS_WASM |
| 129 | + if (m_settingsInstance->isWritable()) { |
| 130 | + if (!m_tempSettingsCopied) |
| 131 | + copyTempSettings(); |
| 132 | + m_settingsInstance->setValue(key, value); |
| 133 | + m_settingsInstance->sync(); |
| 134 | + } else { |
| 135 | + // Use temporary settings until sandbox is initialized |
| 136 | + QSettings settings(paths()->configLocation(), QSettings::IniFormat); |
| 137 | + settings.setValue(key, value); |
| 138 | + } |
| 139 | +#else |
| 140 | + m_settingsInstance->setValue(key, value); |
| 141 | +#endif // Q_OS_WASM |
| 142 | +} |
| 143 | + |
| 144 | +void Settings::copySettings(QSettings *source, QSettings *target) |
| 145 | +{ |
| 146 | +#ifndef Q_OS_WASM |
| 147 | + target->clear(); |
| 148 | +#endif |
| 149 | + QStringList keys = source->allKeys(); |
| 150 | + for (int i = 0; i < keys.count(); i++) |
| 151 | + target->setValue(keys[i], source->value(keys[i])); |
| 152 | +} |
| 153 | + |
| 154 | +#ifdef Q_OS_WASM |
| 155 | +void Settings::copyTempSettings(void) |
| 156 | +{ |
| 157 | + QSettings settings(paths()->configLocation(), QSettings::IniFormat); |
| 158 | + copySettings(&settings, m_settingsInstance); |
| 159 | + m_settingsInstance->sync(); |
| 160 | + m_tempSettingsCopied = true; |
| 161 | +} |
| 162 | +#endif // Q_OS_WASM |
0 commit comments