Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "settings.h"
24 :
25 : #include <QCoreApplication>
26 : #include <QDateTime>
27 : #include <QSettings>
28 : #include <QStandardPaths>
29 : #include <QDir>
30 :
31 : #ifdef MUSE_MODULE_MULTIWINDOWS
32 : #include "multiwindows/resourcelockguard.h"
33 : #endif
34 :
35 : #include "muse_framework_config.h"
36 :
37 : #include "log.h"
38 :
39 : using namespace muse;
40 : using namespace muse::async;
41 :
42 : static const std::string SETTINGS_RESOURCE_NAME("SETTINGS");
43 :
44 41414 : Settings* Settings::instance()
45 : {
46 41414 : static Settings s;
47 41414 : return &s;
48 : }
49 :
50 48 : Settings::Settings()
51 : {
52 : #ifdef WIN_PORTABLE
53 : QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, dataPath().toQString());
54 : QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, dataPath().toQString());
55 : #endif
56 :
57 : #ifndef Q_OS_MAC
58 48 : QSettings::setDefaultFormat(QSettings::IniFormat);
59 : #endif
60 48 : }
61 :
62 48 : Settings::~Settings()
63 : {
64 48 : delete m_settings;
65 48 : }
66 :
67 209 : QSettings* Settings::qsettings() const
68 : {
69 209 : if (!m_settings) {
70 48 : m_settings = new QSettings();
71 : }
72 209 : return m_settings;
73 : }
74 :
75 0 : io::path_t Settings::filePath() const
76 : {
77 0 : return qsettings()->fileName();
78 : }
79 :
80 0 : const Settings::Items& Settings::items() const
81 : {
82 0 : return m_isTransactionStarted ? m_localSettings : m_items;
83 : }
84 :
85 : /**
86 : * @brief Settings::reload method needed only for compatibility with the old MU preferences
87 : */
88 0 : void Settings::reload()
89 : {
90 0 : Items items = readItems();
91 :
92 0 : for (auto it = items.cbegin(); it != items.cend(); ++it) {
93 0 : setSharedValue(it->first, it->second.value);
94 : }
95 0 : }
96 :
97 0 : void Settings::setDataPath(const io::path_t& path)
98 : {
99 : //! NOTE Otherwise the settings were already read from (and maybe written to) the previous path
100 0 : IF_ASSERT_FAILED(!m_settings) {
101 0 : delete m_settings;
102 0 : m_settings = nullptr;
103 : }
104 :
105 0 : m_dataPath = path;
106 :
107 0 : QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, m_dataPath.toQString());
108 0 : QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, m_dataPath.toQString());
109 0 : QSettings::setDefaultFormat(QSettings::IniFormat);
110 0 : }
111 :
112 48 : void Settings::load()
113 : {
114 48 : m_items = readItems();
115 48 : }
116 :
117 0 : void Settings::reset(bool keepDefaultSettings, bool notifyAboutChanges, bool notifyOtherInstances)
118 : {
119 0 : qsettings()->clear();
120 :
121 0 : m_isTransactionStarted = false;
122 :
123 0 : std::vector<Settings::Key> locallyAddedKeys;
124 0 : for (auto it = m_localSettings.begin(); it != m_localSettings.end(); ++it) {
125 0 : auto item = m_items.find(it->first);
126 0 : if (item == m_items.end()) {
127 0 : locallyAddedKeys.push_back(it->first);
128 : } else {
129 : // UI currently has the values from m_localSettings but we've turned off the transaction.
130 0 : item->second.value = it->second.value;
131 : }
132 : }
133 :
134 0 : m_localSettings.clear();
135 :
136 0 : if (!keepDefaultSettings) {
137 0 : QDir(dataPath().toQString()).removeRecursively();
138 0 : QDir().mkpath(dataPath().toQString());
139 : }
140 :
141 0 : if (!notifyAboutChanges) {
142 0 : return;
143 : }
144 :
145 0 : for (auto it = m_items.begin(); it != m_items.end(); ++it) {
146 0 : if (it->second.value == it->second.defaultValue) {
147 0 : continue;
148 : }
149 :
150 0 : it->second.value = it->second.defaultValue;
151 :
152 0 : Channel<Val>& channel = findChannel(it->first);
153 0 : channel.send(it->second.value);
154 : }
155 :
156 0 : for (auto it = locallyAddedKeys.cbegin(); it != locallyAddedKeys.cend(); ++it) {
157 0 : Channel<Val>& channel = findChannel(*it);
158 0 : channel.send(Val());
159 : }
160 :
161 : UNUSED(notifyOtherInstances);
162 : #ifdef MUSE_MODULE_MULTIWINDOWS
163 0 : if (notifyOtherInstances && multiwindowsProvider()) {
164 0 : multiwindowsProvider()->settingsReset();
165 : }
166 : #endif
167 0 : }
168 :
169 0 : static Val compat_QVariantToVal(const QVariant& var)
170 : {
171 0 : if (!var.isValid()) {
172 0 : return Val();
173 : }
174 :
175 0 : switch (var.typeId()) {
176 0 : case QMetaType::QByteArray: return Val(var.toByteArray().toStdString());
177 0 : case QMetaType::QDateTime: return Val(var.toDateTime().toString(Qt::ISODate));
178 0 : case QMetaType::QStringList: {
179 0 : QStringList sl = var.toStringList();
180 0 : ValList vl;
181 0 : for (const QString& s : sl) {
182 0 : vl.push_back(Val(s));
183 : }
184 0 : return Val(vl);
185 0 : }
186 0 : default:
187 0 : break;
188 : }
189 :
190 0 : return Val::fromQVariant(var);
191 : }
192 :
193 48 : Settings::Items Settings::readItems() const
194 : {
195 48 : Items result;
196 : #ifdef MUSE_MODULE_MULTIWINDOWS
197 48 : muse::mi::ReadResourceLockGuard resource_lock(multiwindowsProvider.get(), SETTINGS_RESOURCE_NAME);
198 : #endif
199 48 : for (const QString& key : qsettings()->allKeys()) {
200 0 : Item item;
201 0 : item.key = Key(std::string(), key.toStdString());
202 0 : item.value = compat_QVariantToVal(qsettings()->value(key));
203 :
204 0 : result[item.key] = item;
205 48 : }
206 :
207 48 : return result;
208 48 : }
209 :
210 38215 : const Val& Settings::value(const Key& key) const
211 : {
212 38215 : return findItem(key).value;
213 : }
214 :
215 0 : const Val& Settings::defaultValue(const Key& key) const
216 : {
217 0 : return findItem(key).defaultValue;
218 : }
219 :
220 2035 : void Settings::setSharedValue(const Key& key, const Val& value)
221 : {
222 2035 : setLocalValue(key, value);
223 : #ifdef MUSE_MODULE_MULTIWINDOWS
224 2035 : if (multiwindowsProvider()) {
225 0 : multiwindowsProvider()->settingsSetValue(key.key, value);
226 : }
227 : #endif
228 2035 : }
229 :
230 2035 : void Settings::setLocalValue(const Key& key, const Val& value)
231 : {
232 2035 : Item& item = findItem(key);
233 :
234 2035 : if (!item.isNull() && item.value == value) {
235 1874 : return;
236 : }
237 :
238 161 : if (!m_isTransactionStarted) {
239 161 : writeValue(key, value);
240 : }
241 :
242 161 : if (item.isNull()) {
243 0 : insertNewItem(key, value);
244 : } else {
245 161 : item.value = value;
246 : }
247 :
248 161 : auto it = m_channels.find(key);
249 161 : if (it != m_channels.end()) {
250 3 : async::Channel<Val> channel = it->second;
251 3 : channel.send(value);
252 3 : }
253 : }
254 :
255 161 : void Settings::writeValue(const Key& key, const Val& value)
256 : {
257 : #ifdef MUSE_MODULE_MULTIWINDOWS
258 161 : muse::mi::WriteResourceLockGuard resource_lock(multiwindowsProvider.get(), SETTINGS_RESOURCE_NAME);
259 : #endif
260 : // TODO: implement writing/reading first part of key (module name)
261 161 : qsettings()->setValue(QString::fromStdString(key.key), value.toQVariant());
262 161 : }
263 :
264 0 : io::path_t Settings::dataPath() const
265 : {
266 0 : if (!m_dataPath.empty()) {
267 0 : return m_dataPath;
268 : }
269 :
270 : #ifdef WIN_PORTABLE
271 : return io::path_t(QDir::cleanPath(QString("%1/../../../Data/settings").arg(QCoreApplication::applicationDirPath())));
272 : #else
273 0 : return io::path_t(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
274 : #endif
275 : }
276 :
277 402 : void Settings::setDefaultValue(const Key& key, const Val& value)
278 : {
279 402 : Item& item = findItem(key);
280 :
281 402 : if (item.isNull()) {
282 1206 : m_items[key] = Item{ key, value, value, "", false, Val(), Val() };
283 : } else {
284 0 : item.defaultValue = value;
285 0 : item.value.setType(value.type());
286 : }
287 402 : }
288 :
289 218 : void Settings::setDescription(const Key& key, const std::string& value)
290 : {
291 218 : Item& item = findItem(key);
292 218 : if (item.isNull()) {
293 0 : return;
294 : }
295 :
296 218 : item.description = value;
297 : }
298 :
299 218 : void Settings::setCanBeManuallyEdited(const Settings::Key& key, bool canBeManuallyEdited, const Val& minValue, const Val& maxValue)
300 : {
301 218 : Item& item = findItem(key);
302 :
303 218 : if (item.isNull()) {
304 0 : m_items[key] = Item{ key, Val(), Val(), "", canBeManuallyEdited, minValue, maxValue };
305 : } else {
306 218 : item.canBeManuallyEdited = canBeManuallyEdited;
307 218 : item.minValue = minValue;
308 218 : item.maxValue = maxValue;
309 : }
310 218 : }
311 :
312 0 : void Settings::insertNewItem(const Settings::Key& key, const Val& value)
313 : {
314 0 : Item item = Item{ key, value, value, "", false, Val(), Val() };
315 0 : if (m_isTransactionStarted) {
316 0 : m_localSettings[key] = item;
317 : } else {
318 0 : m_items[key] = item;
319 : }
320 0 : }
321 :
322 0 : void Settings::beginTransaction(bool notifyToOtherInstances)
323 : {
324 0 : if (m_isTransactionStarted) {
325 0 : LOGW() << "Transaction is already started";
326 0 : return;
327 : }
328 :
329 0 : m_localSettings = m_items;
330 0 : m_isTransactionStarted = true;
331 :
332 : UNUSED(notifyToOtherInstances)
333 : #ifdef MUSE_MODULE_MULTIWINDOWS
334 0 : if (notifyToOtherInstances && multiwindowsProvider()) {
335 0 : multiwindowsProvider()->settingsBeginTransaction();
336 : }
337 : #endif
338 : }
339 :
340 0 : void Settings::commitTransaction(bool notifyToOtherInstances)
341 : {
342 0 : m_isTransactionStarted = false;
343 :
344 0 : for (auto it = m_localSettings.begin(); it != m_localSettings.end(); ++it) {
345 0 : Item& item = findItem(it->first);
346 0 : if (item.value == it->second.value) {
347 0 : continue;
348 : }
349 :
350 0 : if (item.isNull()) {
351 0 : insertNewItem(it->first, it->second.value);
352 : } else {
353 0 : item.value = it->second.value;
354 : }
355 :
356 0 : writeValue(it->first, it->second.value);
357 : }
358 :
359 0 : m_localSettings.clear();
360 :
361 : UNUSED(notifyToOtherInstances)
362 : #ifdef MUSE_MODULE_MULTIWINDOWS
363 0 : if (notifyToOtherInstances && multiwindowsProvider()) {
364 0 : multiwindowsProvider()->settingsCommitTransaction();
365 : }
366 : #endif
367 0 : }
368 :
369 0 : void Settings::rollbackTransaction(bool notifyToOtherInstances)
370 : {
371 0 : m_isTransactionStarted = false;
372 :
373 0 : for (auto it = m_localSettings.begin(); it != m_localSettings.end(); ++it) {
374 0 : Item item = findItem(it->first);
375 0 : if (item.value == it->second.value) {
376 0 : continue;
377 : }
378 :
379 0 : Channel<Val>& channel = findChannel(it->first);
380 0 : channel.send(item.value);
381 0 : }
382 :
383 0 : m_localSettings.clear();
384 :
385 : UNUSED(notifyToOtherInstances)
386 : #ifdef MUSE_MODULE_MULTIWINDOWS
387 0 : if (notifyToOtherInstances && multiwindowsProvider()) {
388 0 : multiwindowsProvider()->settingsRollbackTransaction();
389 : }
390 : #endif
391 0 : }
392 :
393 41088 : Settings::Item& Settings::findItem(const Key& key) const
394 : {
395 41088 : Items& items = m_isTransactionStarted ? m_localSettings : m_items;
396 :
397 41088 : auto it = items.find(key);
398 :
399 41088 : if (it == items.end()) {
400 402 : static Item null;
401 402 : return null;
402 : }
403 :
404 40686 : return it->second;
405 : }
406 :
407 0 : async::Channel<Val>& Settings::findChannel(const Settings::Key& key) const
408 : {
409 0 : auto it = m_channels.find(key);
410 :
411 0 : if (it == m_channels.end()) {
412 0 : static async::Channel<Val> null;
413 0 : return null;
414 : }
415 :
416 0 : return it->second;
417 0 : }
418 :
419 278 : async::Channel<Val> Settings::valueChanged(const Key& key) const
420 : {
421 278 : return m_channels[key];
422 : }
423 :
424 2485 : Settings::Key::Key(std::string moduleName, std::string key)
425 2485 : : moduleName(std::move(moduleName)), key(std::move(key))
426 : {
427 2485 : }
428 :
429 0 : bool Settings::Key::operator==(const Key& k) const
430 : {
431 0 : return key == k.key;
432 : }
433 :
434 289551 : bool Settings::Key::operator<(const Key& k) const
435 : {
436 289551 : return key < k.key;
437 : }
438 :
439 3034 : bool Settings::Key::isNull() const
440 : {
441 3034 : return key.empty();
442 : }
|