-
Notifications
You must be signed in to change notification settings - Fork 31
/
settings.cpp
381 lines (322 loc) · 12.6 KB
/
settings.cpp
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
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* Flacon - audio File Encoder
* https://github.com/flacon/flacon
*
* Copyright: 2012-2013
* Alexander Sokoloff <sokoloff.a@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "settings.h"
#include "extprogram.h"
static constexpr auto PROFILES_GROUP = "Profiles";
static constexpr auto PROGRAMS_GROUP = "Programs";
static constexpr auto CURRENT_PROFILE_ID = "OutFiles/Profile";
static constexpr auto KNOWN_FORMATS_KEY = "KnownFormats";
static constexpr auto PROFILE_NAME_KEY = "Name";
static constexpr auto PROFILE_FORMAT_KEY = "Format";
static constexpr auto PROFILE_OUT_DIRECTORY_KEY = "OutDirectory";
static constexpr auto PROFILE_OUT_PATTERN_KEY = "OutPattern";
static constexpr auto PROFILE_BITS_PER_SAMPLE_KEY = "BitsPerSample";
static constexpr auto PROFILE_SAMPLE_RATE_KEY = "SampleRate";
static constexpr auto PROFILE_CREATE_CUE_KEY = "CreateCue";
static constexpr auto PROFILE_EMBED_CUE_KEY = "EmbedCue";
static constexpr auto PROFILE_CUE_FILE_NAME_KEY = "CueFileName";
static constexpr auto PROFILE_PREGAP_TYPE_KEY = "PregapType";
static constexpr auto PROFILE_REPLAY_GAIN_KEY = "ReplayGain";
static constexpr auto PROFILE_COVER_FILE_MODE_KEY = "CoverFile/Mode";
static constexpr auto PROFILE_COVER_FILE_SIZE_KEY = "CoverFile/Size";
static constexpr auto PROFILE_COVER_EMBED_MODE_KEY = "CoverEmbed/Mode";
static constexpr auto PROFILE_COVER_EMBED_SIZE_KEY = "CoverEmbed/Size";
static constexpr auto DEFAULTCODEPAGE_KEY = "Tags/DefaultCodepage";
static constexpr auto SPLIT_TRACK_TITLE_KEY = "Tags/SplitTrackTitle";
static constexpr auto ENCODER_THREADCOUNT_KEY = "Encoder/ThreadCount";
static constexpr auto ENCODER_TMPDIR_KEY = "Encoder/TmpDir";
QString Settings::mFileName;
Settings *Settings::mInstance = nullptr;
/************************************************
*
************************************************/
Settings *Settings::i()
{
if (!mInstance) {
if (mFileName.isEmpty())
mInstance = new Settings("flacon", "flacon");
else
mInstance = new Settings(mFileName);
}
return mInstance;
}
/************************************************
*
************************************************/
void Settings::setFileName(const QString &fileName)
{
mFileName = fileName;
delete mInstance;
mInstance = nullptr;
}
/************************************************
*
************************************************/
Settings::Settings(const QString &organization, const QString &application) :
QSettings(organization, application)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
setIniCodec("UTF-8");
#endif
}
#include <QDir>
/************************************************
*
************************************************/
Settings::Settings(const QString &fileName) :
QSettings(expandFilePath(fileName), QSettings::IniFormat)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
setIniCodec("UTF-8");
#endif
}
/************************************************
*
************************************************/
Profile Settings::readProfile(const QString &profileId)
{
QString group = QString("%1/%2").arg(PROFILES_GROUP, profileId);
beginGroup(group);
Profile profile(value(PROFILE_FORMAT_KEY).toString(), profileId);
if (!profile.outFormat()) {
endGroup();
return profile;
}
profile.setName(value(PROFILE_NAME_KEY, profile.outFormat()->name()).toString());
profile.setOutFileDir(value(PROFILE_OUT_DIRECTORY_KEY, profile.outFileDir()).toString());
profile.setOutFilePattern(value(PROFILE_OUT_PATTERN_KEY, profile.outFilePattern()).toString());
profile.setCueFileName(value(PROFILE_CUE_FILE_NAME_KEY, profile.cueFileName()).toString());
profile.setBitsPerSample(readBitsPerSample(PROFILE_BITS_PER_SAMPLE_KEY, profile.bitsPerSample()));
profile.setSampleRate(readSampleRate(PROFILE_SAMPLE_RATE_KEY, profile.sampleRate()));
profile.setGainType(strToGainType(value(PROFILE_REPLAY_GAIN_KEY).toString(), profile.gainType()));
profile.setPregapType(strToPreGapType(value(PROFILE_PREGAP_TYPE_KEY).toString(), profile.pregapType()));
profile.setCreateCue(value(PROFILE_CREATE_CUE_KEY, profile.isCreateCue()).toBool());
profile.setEmbedCue(value(PROFILE_EMBED_CUE_KEY, profile.isEmbedCue()).toBool());
CoverOptions opts = profile.copyCoverOptions();
opts.mode = strToCoverMode(value(PROFILE_COVER_FILE_MODE_KEY).toString(), opts.mode);
opts.size = value(PROFILE_COVER_FILE_SIZE_KEY, opts.size).toInt();
profile.setCopyCoverOptions(opts);
if (profile.outFormat()->options().testFlag(FormatOption::SupportEmbeddedImage)) {
CoverOptions opts = profile.embedCoverOptions();
opts.mode = strToCoverMode(value(PROFILE_COVER_EMBED_MODE_KEY).toString(), opts.mode);
opts.size = value(PROFILE_COVER_EMBED_SIZE_KEY, opts.size).toInt();
profile.setEmbedCoverOptions(opts);
}
QHash<QString, QVariant> vals = profile.encoderValues();
for (auto i = vals.begin(); i != vals.end(); ++i) {
i.value() = value(i.key(), i.value());
}
profile.setEncoderValues(vals);
endGroup();
profile.setTmpDir(value(ENCODER_TMPDIR_KEY, profile.tmpDir()).toString());
profile.setEncoderThreadsCount(readThreadsCount(ENCODER_THREADCOUNT_KEY, profile.encoderThreadsCount()));
profile.setSplitTrackTitle(value(SPLIT_TRACK_TITLE_KEY, profile.isSplitTrackTitle()).toBool());
return profile;
}
/************************************************
*
************************************************/
void Settings::writeProfile(const Profile &profile)
{
if (!profile.isValid()) {
return;
}
QString group = QString("%1/%2").arg(PROFILES_GROUP, profile.id());
beginGroup(group);
setValue(PROFILE_NAME_KEY, profile.name());
setValue(PROFILE_OUT_DIRECTORY_KEY, profile.outFileDir());
setValue(PROFILE_OUT_PATTERN_KEY, profile.outFilePattern());
setValue(PROFILE_CUE_FILE_NAME_KEY, profile.cueFileName());
setValue(PROFILE_FORMAT_KEY, profile.outFormat()->id());
setValue(PROFILE_BITS_PER_SAMPLE_KEY, profile.bitsPerSample());
setValue(PROFILE_SAMPLE_RATE_KEY, profile.sampleRate());
setValue(PROFILE_REPLAY_GAIN_KEY, gainTypeToString(profile.gainType()));
setValue(PROFILE_PREGAP_TYPE_KEY, preGapTypeToString(profile.pregapType()));
setValue(PROFILE_CREATE_CUE_KEY, profile.isCreateCue());
setValue(PROFILE_EMBED_CUE_KEY, profile.isEmbedCue());
setValue(PROFILE_COVER_FILE_MODE_KEY, coverModeToString(profile.copyCoverOptions().mode));
setValue(PROFILE_COVER_FILE_SIZE_KEY, profile.copyCoverOptions().size);
if (profile.outFormat()->options().testFlag(FormatOption::SupportEmbeddedImage)) {
setValue(PROFILE_COVER_EMBED_MODE_KEY, coverModeToString(profile.embedCoverOptions().mode));
setValue(PROFILE_COVER_EMBED_SIZE_KEY, profile.embedCoverOptions().size);
}
QHash<QString, QVariant> vals = profile.encoderValues();
for (auto i = vals.constBegin(); i != vals.constEnd(); ++i) {
setValue(i.key(), i.value());
}
endGroup();
setValue(ENCODER_TMPDIR_KEY, profile.tmpDir());
setValue(ENCODER_THREADCOUNT_KEY, profile.encoderThreadsCount());
setValue(SPLIT_TRACK_TITLE_KEY, profile.isSplitTrackTitle());
}
/************************************************
*
************************************************/
Profiles Settings::readProfiles()
{
Profiles res;
allKeys();
beginGroup(PROFILES_GROUP);
QStringList ids = childGroups();
endGroup();
for (const QString &id : ids) {
Profile profile = readProfile(id);
if (profile.isValid()) {
res << profile;
}
}
if (res.isEmpty()) {
// If this is the first launch, we create standard profiles for ALL formats
res << createStandardProfiles();
}
else {
// If a new format has been added in this version of the program,
// then we create a standard profile for the NEW FORMAT
// This functionality is introduced in version 8.4, this is a list of formats known in 8.3.
QStringList def = { "AAC", "FLAC", "MP3", "OGG", "OPUS", "WAV", "WV" };
QStringList old = value(KNOWN_FORMATS_KEY, def).toStringList();
QStringList exists;
for (const Profile &p : std::as_const(res)) {
exists << p.outFormat()->id();
}
Profiles ps = createStandardProfiles();
for (const Profile &p : ps) {
if (!exists.contains(p.outFormat()->id()) && !old.contains(p.outFormat()->id())) {
res << p;
}
}
}
return res;
}
/************************************************
*
************************************************/
BitsPerSample Settings::readBitsPerSample(const QString &key, BitsPerSample def) const
{
uint n = value(key, int(def)).toUInt();
switch (n) {
case BitsPerSample::AsSourcee:
case BitsPerSample::Bit_16:
case BitsPerSample::Bit_24:
case BitsPerSample::Bit_32:
case BitsPerSample::Bit_64:
return BitsPerSample(n);
}
return def;
}
/************************************************
*
************************************************/
SampleRate Settings::readSampleRate(const QString &key, SampleRate def) const
{
uint n = value(key, int(def)).toUInt();
switch (n) {
case SampleRate::AsSource:
case SampleRate::Hz_44100:
case SampleRate::Hz_48000:
case SampleRate::Hz_96000:
case SampleRate::Hz_192000:
case SampleRate::Hz_384000:
case SampleRate::Hz_768000:
return SampleRate(n);
}
return def;
}
/************************************************
*
************************************************/
uint Settings::readThreadsCount(const QString &key, uint def) const
{
int res = value(key, def).toInt();
if (res > 1) {
return res;
}
return def;
}
/************************************************
*
************************************************/
void Settings::writeProfiles(const Profiles &profiles)
{
allKeys();
setValue(KNOWN_FORMATS_KEY, OutFormat::allFormatsId());
remove(PROFILES_GROUP);
for (const Profile &p : profiles) {
writeProfile(p);
}
}
/************************************************
*
************************************************/
QString Settings::readCurrentProfileId() const
{
return value(CURRENT_PROFILE_ID).toString();
}
/************************************************
*
************************************************/
void Settings::writeCurrentProfileId(const QString &profileId)
{
setValue(CURRENT_PROFILE_ID, profileId);
}
#if !BUNDLED_PROGRAMS
/************************************************
*
************************************************/
void Settings::readExtPrograms() const
{
for (ExtProgram *p : ExtProgram::allPrograms()) {
auto key = QString("%1/%2").arg(PROGRAMS_GROUP, p->name());
QString path = value(key).toString();
if (path.isEmpty()) {
path = p->find();
}
p->setPath(path);
}
}
/************************************************
*
************************************************/
void Settings::writeExtPrograms()
{
remove(PROGRAMS_GROUP);
for (ExtProgram *p : ExtProgram::allPrograms()) {
auto key = QString("%1/%2").arg(PROGRAMS_GROUP, p->name());
setValue(key, p->path());
}
}
#endif
/************************************************
*
************************************************/
QString Settings::defaultCodepage() const
{
return value(DEFAULTCODEPAGE_KEY, "AUTODETECT").toString();
}
/************************************************
*
************************************************/
void Settings::setDefaultCodepage(const QString &value)
{
setValue(DEFAULTCODEPAGE_KEY, value);
}