-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCPresetManager.cpp
More file actions
executable file
·246 lines (192 loc) · 7.13 KB
/
CPresetManager.cpp
File metadata and controls
executable file
·246 lines (192 loc) · 7.13 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
/*************************************************************************************
cpl - cross-platform library - v. 0.1.0.
Copyright (C) 2016 Janus Lynggaard Thorborg (www.jthorborg.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
See \licenses\ for additional details on licenses associated with this program.
**************************************************************************************
file:CPresetManager.cpp
Implementation of CPresetManager.h
*************************************************************************************/
#include "CPresetManager.h"
#include "Misc.h"
#include <vector>
#include <memory>
#include "PlatformMisc.h"
namespace cpl
{
auto presetDirectory = []() { return cpl::Misc::DirectoryPath() + "/presets/"; };
CPresetManager & CPresetManager::instance()
{
static CPresetManager ins;
return ins;
}
std::string CPresetManager::getPresetDirectory() const noexcept
{
return ::cpl::presetDirectory();
}
CPresetManager::DialogState CPresetManager::savePresetAs(const CCheckedSerializer& archive, FileSavedCallback callback)
{
// should we really save empty files?
if (archive.isEmpty())
return nullptr;
std::string extension = !archive.getName().empty() ? archive.getName() + "." + programInfo.programAbbr : programInfo.programAbbr;
auto fileChooser = std::make_unique<juce::FileChooser>(programInfo.name + ": Save preset to a file...",
juce::File(presetDirectory()),
#ifdef CPL_UNIXC
// native dialogs hangs programs on the distros I've tried
"*." + extension, false);
#else
"*." + extension);
#endif
fileChooser->launchAsync(
juce::FileBrowserComponent::saveMode,
[this, archive, callback, extension](const juce::FileChooser& chooser)
{
auto result = chooser.getResult();
if (result.existsAsFile() || result.getParentDirectory().exists())
{
juce::String dotExt = "." + extension;
bool hasExt = false;
juce::String tempString = result.getFullPathName();
juce::String finalString = tempString;
// OS X handles multiple endings by duplicating them.. litterally.
// how nice.
while (tempString.endsWith(dotExt))
{
hasExt = true;
finalString = tempString;
tempString = tempString.dropLastCharacters(dotExt.length());
}
std::string path =
hasExt ?
finalString.toStdString() :
result.withFileExtension(extension.c_str()).getFullPathName().toStdString();
if (savePreset(path, archive))
{
if (callback)
callback(result);
}
}
}
);
return std::move(fileChooser);
}
CPresetManager::DialogState CPresetManager::loadPresetAs(CCheckedSerializer builder, FileLoadedCallback whenDone)
{
std::string extension = !builder.getName().empty() ? builder.getName() + "." + programInfo.programAbbr : programInfo.programAbbr;
auto fileChooser = std::make_unique<juce::FileChooser>(programInfo.name + ": Load preset from a file...",
juce::File(presetDirectory()),
#ifdef CPL_MAC
"*." + extension, false); // native file selector on macos can only filter for one extension
#elif defined(CPL_UNIXC)
// native dialogs hangs programs on the distros I've tried
"*." + extension, false);
#else
"*." + extension);
#endif
fileChooser->launchAsync(
juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles,
[this, whenDone, extension, builder = std::move(builder)] (const juce::FileChooser& chooser) mutable
{
auto result = chooser.getResult();
if (result.existsAsFile())
{
// the file chooser can only choose file names with the correct extension,
// no need to check.
std::string path = result.getFullPathName().toStdString();
if (!result.getFileName().contains(extension.c_str()))
{
// Warning about extension mismatch could be handled here
}
if (loadPreset(path, builder))
{
if (whenDone)
whenDone(result, builder);
}
}
}
);
return std::move(fileChooser);
}
// these functions saves/loads directly
bool CPresetManager::savePreset(cpl::string_ref path, const ISerializerSystem & archive)
{
CExclusiveFile file;
if (!file.open(path.c_str(), file.writeMode))
return false;
// clear existing file..
file.remove();
if (!file.open(path.c_str()))
return false;
auto content = archive.compile(true);
return file.write(content.getBlock(), (std::int64_t)content.getSize());
}
bool CPresetManager::loadPreset(cpl::string_ref path, ISerializerSystem & builder)
{
try
{
CExclusiveFile file;
if (!file.open(path.c_str(), file.readMode))
return false;
std::vector<std::uint8_t> data;
std::size_t size = (std::size_t)file.getFileSize();
data.resize(size);
if (!file.read(data.data(), size))
return false;
builder.clear();
return builder.build(WeakContentWrapper(data.data(), size));
}
catch (const std::exception & e)
{
Misc::MsgBox("Exception loading preset at " + path.string() + ":\n" + e.what(), programInfo.name, Misc::MsgIcon::iStop);
}
return false;
}
const std::vector<juce::File> & CPresetManager::getPresets()
{
currentPresets.clear();
juce::DirectoryIterator iter(File(presetDirectory()), false, "*." + programInfo.programAbbr);
while (iter.next())
{
currentPresets.push_back(iter.getFile());
}
return currentPresets;
}
bool CPresetManager::saveDefaultPreset(const ISerializerSystem & archive)
{
return savePreset(presetDirectory() + "default." + programInfo.programAbbr, archive);
}
CPresetManager::DialogState CPresetManager::loadDefaultPreset(FileLoadedCallback whenDone)
{
auto path = presetDirectory() + "default." + programInfo.programAbbr;
CCheckedSerializer builder("default");
if (loadPreset(path, builder))
{
whenDone({ path }, builder);
return {};
}
auto answer = cpl::Misc::MsgBox(
"Error loading default preset at:\n" + path + "\n" + GetLastOSErrorMessage() +
"\nLoad a different preset?",
programInfo.name + ": Error loading preset...",
Misc::MsgIcon::iQuestion | Misc::MsgStyle::sYesNoCancel);
if (answer == Misc::MsgButton::bYes)
{
// Since loadPresetAs is now async, we can't easily use it here
// This would need a different approach or just return false
return loadPresetAs(builder, whenDone);
}
return {};
}
CPresetManager::CPresetManager() {}
CPresetManager::~CPresetManager() {}
};