forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsFile.cpp
More file actions
643 lines (530 loc) · 21.9 KB
/
SettingsFile.cpp
File metadata and controls
643 lines (530 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
// Copyright (c) 2021-2025 Timothy Schoen
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
#include <juce_gui_basics/juce_gui_basics.h>
#include <juce_gui_extra/juce_gui_extra.h>
#include "Utility/Config.h"
#include "Utility/Fonts.h"
#include "Utility/OSUtils.h"
#include "SettingsFile.h"
#include "LookAndFeel.h"
#include "Sidebar/CommandInput.h"
SettingsFileListener::SettingsFileListener()
{
SettingsFile::getInstance()->listeners.add(this);
}
SettingsFileListener::~SettingsFileListener()
{
SettingsFile::getInstance()->listeners.remove_one(this);
}
JUCE_IMPLEMENT_SINGLETON(SettingsFile)
SettingsFile::~SettingsFile()
{
// Save current settings before quitting
saveSettings();
clearSingletonInstance();
}
void SettingsFile::backupCorruptSettings()
{
// Backup previous corrupt settings file, so users can fix if they want to
auto const corruptSettings = getInstance()->settingsFile;
auto backupLocation = corruptSettings.getParentDirectory().getChildFile(".settings_damaged").getFullPathName();
int counter = 1;
// Increment backup settings file name if previous exists
while (File(backupLocation).existsAsFile()) {
backupLocation = corruptSettings.getParentDirectory().getChildFile(".settings_damaged_" + String(counter)).getFullPathName();
counter++;
}
backupSettingsLocation = backupLocation;
corruptSettings.moveFileTo(backupLocation);
}
String SettingsFile::getCorruptBackupSettingsLocation()
{
return backupSettingsLocation;
}
SettingsFile* SettingsFile::initialise()
{
if (isInitialised)
return getInstance();
isInitialised = true;
// #define DEBUG_CORRUPT_SETTINGS_DIALOG
#ifdef DEBUG_CORRUPT_SETTINGS_DIALOG
backupSettingsLocation = ProjectInfo::appDataDir.getChildFile(".settings_damaged").getFullPathName();
#endif
// Check if settings file exists, if not, create the default
// This is expected behaviour for first run / deleting plugdata folder
// No need to alert the user to this
if (!settingsFile.existsAsFile()) {
settingsFile.create();
} else {
std::unique_ptr<XmlElement> const xmlElement(XmlDocument::parse(settingsFile.loadFileAsString()));
// First check if settings XML is valid
if (verify(xmlElement.get())) {
// Use user .settings file
settingsTree = ValueTree::fromXml(*xmlElement);
// Overwrite previous settings_bak with current good settings (don't touch it after this)
settingsFile.copyFileTo(settingsFile.getFullPathName() + "_bak");
} else {
// Settings are invalid! Backup old file as .settings_damaged
backupCorruptSettings();
// See if there is a .settings_bak backup settings file from last run
auto const backupSettings = File(settingsFile.getFullPathName() + "_bak");
if (backupSettings.existsAsFile()) {
std::unique_ptr<XmlElement> const xmlSettingsBackup(XmlDocument::parse(backupSettings.loadFileAsString()));
if (verify(xmlSettingsBackup.get())) {
// If backup settings are good, use them
settingsTree = ValueTree::fromXml(*xmlSettingsBackup);
settingsState = BackupSettings;
} else {
// Use default plugdata settings (worst case scenario)
settingsFile.create();
settingsState = DefaultSettings;
}
} else {
// Use default plugdata settings (worst case scenario)
settingsFile.create();
settingsState = DefaultSettings;
}
}
}
// Make sure all the properties exist
for (auto& [propertyName, propertyValue] : defaultSettings) {
// If it doesn't exist, set it to the default value
if (!settingsTree.hasProperty(propertyName) || settingsTree.getProperty(propertyName).toString() == "") {
settingsTree.setProperty(propertyName, propertyValue, nullptr);
}
}
// Make sure all the child trees exist
for (auto& childName : childTrees) {
if (!settingsTree.getChildWithName(childName).isValid()) {
settingsTree.appendChild(ValueTree(childName), nullptr);
}
}
initialisePathsTree();
initialiseThemesTree();
initialiseOverlayTree();
initialiseCommandHistory();
#if JUCE_IOS
if (!ProjectInfo::isStandalone) {
Desktop::getInstance().setGlobalScaleFactor(1.0f); // scaling inside AUv3 is a bad idea
} else if (OSUtils::isIPad()) {
Desktop::getInstance().setGlobalScaleFactor(1.125f);
} else {
Desktop::getInstance().setGlobalScaleFactor(0.825f);
}
#else
Desktop::getInstance().setGlobalScaleFactor(getProperty<float>("global_scale"));
#endif
saveSettings();
settingsTree.addListener(this);
return this;
}
bool SettingsFile::verify(XmlElement const* xml)
{
// Basic settings file verification
// Verify if the xml is valid, and tags match correct name / order
// Adjust tags here if future layout changes
if (xml == nullptr || xml->getTagName() != "SettingsTree")
return false;
// These must at least be present in a valid save file!
StringArray expectedNames = {
"Paths",
"KeyMap",
"ColourThemes",
"SelectedThemes",
"RecentlyOpened",
"Libraries",
"Overlays"
};
StringArray const optionalNames {
"HeavyState",
"LastBrowserPaths",
"CommandHistory",
"EnabledMidiInputPorts",
"EnabledMidiOutputPorts"
};
// Check if all expected elements are present and in the correct order
bool unexpectedName = false;
for (auto const* child = xml->getFirstChildElement(); child != nullptr; child = child->getNextElement()) {
auto name = child->getTagName();
if (expectedNames.contains(name)) {
expectedNames.removeString(name);
} else if (!optionalNames.contains(name)) {
std::cerr << "Unexpected settings file entry: " << name << std::endl;
unexpectedName = true;
}
}
for (auto const& expectedName : expectedNames) {
std::cerr << "Expected settings file entry not found: " << expectedName << std::endl;
}
// Check if all expected elements were found
return expectedNames.isEmpty() && !unexpectedName;
}
SettingsFile::SettingsState SettingsFile::getSettingsState() const
{
return settingsState;
}
void SettingsFile::resetSettingsState()
{
settingsState = UserSettings;
}
void SettingsFile::startChangeListener()
{
settingsFileWatcher.addFolder(settingsFile.getParentDirectory());
settingsFileWatcher.addListener(this);
}
ValueTree SettingsFile::getKeyMapTree() const
{
return settingsTree.getChildWithName("KeyMap");
}
ValueTree SettingsFile::getColourThemesTree() const
{
return settingsTree.getChildWithName("ColourThemes");
}
ValueTree SettingsFile::getPathsTree() const
{
return settingsTree.getChildWithName("Paths");
}
ValueTree SettingsFile::getSelectedThemesTree() const
{
return settingsTree.getChildWithName("SelectedThemes");
}
ValueTree SettingsFile::getLibrariesTree() const
{
return settingsTree.getChildWithName("Libraries");
}
ValueTree SettingsFile::getTheme(String const& name) const
{
return getColourThemesTree().getChildWithProperty("theme", name);
}
void SettingsFile::setLastBrowserPathForId(String const& identifier, File& path)
{
if (identifier.isEmpty() || !path.exists() || path.isRoot())
return;
settingsTree.getChildWithName("LastBrowserPaths").setProperty(identifier, path.getFullPathName(), nullptr);
}
File SettingsFile::getLastBrowserPathForId(String const& identifier) const
{
if (identifier.isEmpty())
return {};
return { settingsTree.getChildWithName("LastBrowserPaths").getProperty(identifier).toString() };
}
ValueTree SettingsFile::getCurrentTheme() const
{
return getColourThemesTree().getChildWithProperty("theme", settingsTree.getProperty("theme"));
}
void SettingsFile::initialisePathsTree()
{
// Make sure all the default paths are in place
HeapArray<File> currentPaths;
currentPaths.reserve(10);
auto pathTree = getPathsTree();
// on iOS, the containerisation of apps leads to problems with custom search paths
// So we completely reset them every time
#if JUCE_IOS
pathTree.removeAllChildren(nullptr);
#endif
for (auto child : pathTree) {
currentPaths.add(File(child.getProperty("Path").toString()));
}
for (auto const& path : pd::Library::defaultPaths) {
if (!currentPaths.contains(path)) {
auto pathSubTree = ValueTree("Path");
pathSubTree.setProperty("Path", path.getFullPathName(), nullptr);
pathTree.appendChild(pathSubTree, nullptr);
}
}
// TODO: remove this later. this is to fix a mistake during v0.8.4 development
for (auto child : pathTree) {
if (child.getProperty("Path").toString().contains("Abstractions/Gem")) {
pathTree.removeChild(child, nullptr);
break;
}
}
}
void SettingsFile::saveCommandHistory()
{
auto commandHistory = settingsTree.getChildWithName("CommandHistory");
if (commandHistory.isValid()) {
commandHistory.removeAllProperties(nullptr);
} else {
commandHistory = ValueTree("CommandHistory");
SettingsFile::getInstance()->getValueTree().appendChild(commandHistory, nullptr);
}
int commandIndex = 0;
for (auto& command : CommandInput::getCommandHistory()) {
// Don't save more than 50 commands
if (commandIndex > 50)
break;
// Set each command as a unique attribute
commandHistory.setProperty("Command" + String(commandIndex), command, nullptr);
commandIndex++;
}
}
void SettingsFile::initialiseCommandHistory()
{
auto const commandHistory = settingsTree.getChildWithName("CommandHistory");
if (!commandHistory.isValid()) {
return;
}
StringArray commands;
String lastCommand;
for (int i = 0; i < commandHistory.getNumProperties(); i++) {
auto command = commandHistory.getProperty("Command" + String(i)).toString();
// Filter out duplicate commands (we also do this in CommandInput, but not during loading)
if (lastCommand != command) {
commands.add(command);
lastCommand = command;
}
}
CommandInput::setCommandHistory(commands);
}
void SettingsFile::addToRecentlyOpened(File const& path)
{
auto recentlyOpened = settingsTree.getChildWithName("RecentlyOpened");
if (!recentlyOpened.isValid()) {
recentlyOpened = ValueTree("RecentlyOpened");
SettingsFile::getInstance()->getValueTree().appendChild(recentlyOpened, nullptr);
}
if (recentlyOpened.getChildWithProperty("Path", path.getFullPathName()).isValid()) {
recentlyOpened.getChildWithProperty("Path", path.getFullPathName()).setProperty("Time", Time::getCurrentTime().toMilliseconds(), nullptr);
int const oldIdx = recentlyOpened.indexOf(recentlyOpened.getChildWithProperty("Path", path.getFullPathName()));
recentlyOpened.moveChild(oldIdx, 0, nullptr);
} else {
ValueTree subTree("Path");
subTree.setProperty("Path", path.getFullPathName(), nullptr);
subTree.setProperty("Time", Time::getCurrentTime().toMilliseconds(), nullptr);
#if JUCE_MAC || JUCE_WINDOWS
if (path.isOnRemovableDrive())
subTree.setProperty("Removable", var(1), nullptr);
#endif
recentlyOpened.addChild(subTree, 0, nullptr);
}
while (recentlyOpened.getNumChildren() > 15) {
auto minTime = Time::getCurrentTime().toMilliseconds();
int minIdx = -1;
// Find oldest entry
for (int i = 0; i < recentlyOpened.getNumChildren(); i++) {
auto child = recentlyOpened.getChild(i);
auto const pinned = child.hasProperty("Pinned") && static_cast<bool>(child.getProperty("Pinned"));
auto const time = static_cast<int64>(child.getProperty("Time"));
if (time < minTime && !pinned) {
minIdx = i;
minTime = time;
}
}
recentlyOpened.removeChild(minIdx, nullptr);
}
// If we do this inside a plugin, it will add to the DAW's recently opened list!
if (ProjectInfo::isStandalone) {
RecentlyOpenedFilesList::registerRecentFileNatively(path);
}
}
bool SettingsFile::wantsNativeDialog() const
{
if (ProjectInfo::isStandalone) {
return true;
}
if (!settingsTree.hasProperty("NativeDialog")) {
return true;
}
return static_cast<bool>(settingsTree.getProperty("NativeDialog"));
}
void SettingsFile::initialiseThemesTree()
{
// Initialise selected themes tree
auto selectedThemes = getSelectedThemesTree();
if (!selectedThemes.hasProperty("first"))
selectedThemes.setProperty("first", "light", nullptr);
if (!selectedThemes.hasProperty("second"))
selectedThemes.setProperty("second", "dark", nullptr);
if (selectedThemes.getProperty("first").toString() != getProperty<String>("theme") && selectedThemes.getProperty("second").toString() != getProperty<String>("theme")) {
setProperty("theme", selectedThemes.getProperty("first").toString());
}
PlugDataLook::selectedThemes.set(0, selectedThemes.getProperty("first").toString());
PlugDataLook::selectedThemes.set(1, selectedThemes.getProperty("second").toString());
auto const defaultColourThemesTree = ValueTree::fromXml(PlugDataLook::defaultThemesXml);
auto colourThemesTree = getColourThemesTree();
if (colourThemesTree.getNumChildren() == 0) {
colourThemesTree.copyPropertiesAndChildrenFrom(defaultColourThemesTree, nullptr);
} else {
// Check if all the default themes are still there
// Mostly for updating from v0.6.3 -> v0.7.0
for (auto themeTree : defaultColourThemesTree) {
if (!colourThemesTree.getChildWithProperty("theme", themeTree.getProperty("theme")).isValid()) {
colourThemesTree.appendChild(themeTree.createCopy(), nullptr);
}
}
// Ensure each theme is valid
for (auto themeTree : colourThemesTree) {
auto themeName = themeTree.getProperty("theme");
// If it's one of the default themes, validate it against the default theme tree
if (defaultColourThemesTree.getChildWithProperty("theme", themeName).isValid()) {
auto defaultTree = defaultColourThemesTree.getChildWithProperty("theme", themeName);
for (auto const& [colourId, colourInfo] : PlugDataColourNames) {
auto& [cId, colourName, colourCategory] = colourInfo;
// For when we add new colours in the future
if (!themeTree.hasProperty(colourName) || themeTree.getProperty(colourName).toString().isEmpty() || themeTree.getProperty(colourName).toString() == "00000000") {
themeTree.setProperty(colourName, defaultTree.getProperty(colourName).toString(), nullptr);
}
}
if (!themeTree.hasProperty("straight_connections")) {
themeTree.setProperty("straight_connections", defaultTree.getProperty("straight_connections"), nullptr);
}
if (!themeTree.hasProperty("connection_style")) {
themeTree.setProperty("connection_style", defaultTree.getProperty("connection_style"), nullptr);
}
if (!themeTree.hasProperty("square_iolets")) {
themeTree.setProperty("square_iolets", defaultTree.getProperty("square_iolets"), nullptr);
}
if (!themeTree.hasProperty("square_object_corners")) {
themeTree.setProperty("square_object_corners", defaultTree.getProperty("square_object_corners"), nullptr);
}
if (!themeTree.hasProperty("object_flag_outlined")) {
themeTree.setProperty("object_flag_outlined", defaultTree.getProperty("object_flag_outlined"), nullptr);
}
if (!themeTree.hasProperty("iolet_spacing_edge")) {
themeTree.setProperty("iolet_spacing_edge", defaultTree.getProperty("iolet_spacing_edge"), nullptr);
}
if (!themeTree.hasProperty("highlight_syntax")) {
themeTree.setProperty("highlight_syntax", defaultTree.getProperty("highlight_syntax"), nullptr);
}
}
// Otherwise, just ensure these properties exist
else {
if (!themeTree.hasProperty("straight_connections")) {
themeTree.setProperty("straight_connections", false, nullptr);
}
if (!themeTree.hasProperty("connection_style")) {
themeTree.setProperty("connection_style", String(1), nullptr);
}
if (!themeTree.hasProperty("square_iolets")) {
themeTree.setProperty("square_iolets", false, nullptr);
}
if (!themeTree.hasProperty("square_object_corners")) {
themeTree.setProperty("square_object_corners", false, nullptr);
}
if (!themeTree.hasProperty("object_flag_outlined")) {
themeTree.setProperty("object_flag_outlined", false, nullptr);
}
if (!themeTree.hasProperty("iolet_spacing_edge")) {
themeTree.setProperty("iolet_spacing_edge", false, nullptr);
}
if (!themeTree.hasProperty("highlight_syntax")) {
themeTree.setProperty("highlight_syntax", true, nullptr);
}
}
}
}
}
void SettingsFile::initialiseOverlayTree()
{
UnorderedMap<String, int> defaults = {
{ "edit", Origin | ActivationState },
{ "lock", Behind },
{ "run", None },
{ "alt", Origin | Border | ActivationState | Index | Coordinate | Order | Direction }
};
auto overlayTree = settingsTree.getChildWithName("Overlays");
if (!overlayTree.isValid()) {
overlayTree = ValueTree("Overlays");
for (auto& [name, settings] : defaults) {
overlayTree.setProperty(name, settings, nullptr);
}
settingsTree.appendChild(overlayTree, nullptr);
}
}
void SettingsFile::reloadSettings()
{
if (settingsChangedInternally) {
settingsChangedInternally = false;
return;
}
settingsChangedExternally = true;
jassert(isInitialised);
auto const newTree = ValueTree::fromXml(settingsFile.loadFileAsString());
// Children shouldn't be overwritten as that would break some valueTree links
for (auto child : settingsTree) {
child.copyPropertiesAndChildrenFrom(newTree.getChildWithName(child.getType()), nullptr);
}
settingsTree.copyPropertiesFrom(newTree, nullptr);
for (auto* listener : listeners) {
listener->settingsFileReloaded();
}
}
void SettingsFile::fileChanged(File const file, FileSystemWatcher::FileSystemEvent fileEvent)
{
if (file == settingsFile) {
reloadSettings();
}
}
void SettingsFile::valueTreePropertyChanged(ValueTree& treeWhosePropertyHasChanged, Identifier const& property)
{
for (auto* listener : listeners) {
listener->settingsChanged(property.toString(), treeWhosePropertyHasChanged.getProperty(property));
}
if (!settingsChangedExternally)
settingsChangedInternally = true;
startTimer(700);
}
void SettingsFile::valueTreeChildAdded(ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
{
if (!settingsChangedExternally)
settingsChangedInternally = true;
startTimer(700);
}
void SettingsFile::valueTreeChildRemoved(ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved, int indexFromWhichChildWasRemoved)
{
if (!settingsChangedExternally)
settingsChangedInternally = true;
startTimer(700);
}
void SettingsFile::timerCallback()
{
jassert(isInitialised);
// Don't save again if we just loaded it from file
if (settingsChangedExternally) {
settingsChangedExternally = false;
stopTimer();
return;
}
// Save settings to file whenever valuetree state changes
// Use timer to group changes together
saveSettings();
stopTimer();
}
void SettingsFile::setGlobalScale(float const newScale)
{
setProperty("global_scale", newScale);
Desktop::getInstance().setGlobalScaleFactor(newScale);
}
void SettingsFile::saveSettings()
{
jassert(isInitialised);
saveCommandHistory();
// Save settings to file
auto const xml = settingsTree.toXmlString();
settingsFile.replaceWithText(xml);
}
void SettingsFile::setProperty(String const& name, var const& value)
{
jassert(isInitialised);
settingsTree.setProperty(name, value, nullptr);
}
bool SettingsFile::hasProperty(String const& name) const
{
jassert(isInitialised);
return settingsTree.hasProperty(name);
}
Value SettingsFile::getPropertyAsValue(String const& name)
{
jassert(isInitialised);
return settingsTree.getPropertyAsValue(name, nullptr);
}
ValueTree& SettingsFile::getValueTree()
{
jassert(isInitialised);
return settingsTree;
}