forked from flacon/flacon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
528 lines (432 loc) · 14 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
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
/* 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 "types.h"
#include "formats_in/informat.h"
#include "settings.h"
#include "inputaudiofile.h"
#include "formats_out/outformat.h"
#include "converter/sox.h"
#include <assert.h>
#include <QtGlobal>
#include <QDir>
#include <QDebug>
#include <QProcessEnvironment>
#include <QDir>
#include <QStandardPaths>
#include <QCoreApplication>
#include <QMetaEnum>
#include <QThread>
#ifdef Q_OS_WIN
#define PATH_ENV_SEPARATOR ';'
#define BINARY_EXT ".exe"
#elif defined(Q_OS_OS2)
#define PATH_ENV_SEPARATOR ';'
#define BINARY_EXT ".exe"
#else
#define PATH_ENV_SEPARATOR ':'
#define BINARY_EXT ""
#endif
#define PROFILES_PREFIX "Profiles"
static constexpr char const *KNOWN_FORMATS = "KnownFormats";
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)
{
setIniCodec("UTF-8");
init();
}
/************************************************
************************************************/
Settings::Settings(const QString &fileName) :
QSettings(fileName, QSettings::IniFormat)
{
setIniCodec("UTF-8");
init();
}
/************************************************
************************************************/
void Settings::init()
{
setDefaultValue(Tags_DefaultCodepage, "AUTODETECT");
// Globals **********************************
setDefaultValue(Encoder_ThreadCount, qMax(4, QThread::idealThreadCount()));
setDefaultValue(Encoder_TmpDir, "");
// Out Files ********************************
setDefaultValue(OutFiles_Profile, "FLAC");
// Internet *********************************
setDefaultValue(Inet_CDDBHost, "https://gnudb.gnudb.org/");
// Misc *************************************
setDefaultValue(Misc_LastDir, QDir::homePath());
// ConfigureDialog **********************
setDefaultValue(ConfigureDialog_Width, 645);
setDefaultValue(ConfigureDialog_Height, 425);
mPrograms << Conv::Sox::programName();
foreach (OutFormat *format, OutFormat::allFormats()) {
mPrograms << format->encoderProgramName();
mPrograms << format->gainProgramName();
}
foreach (const InputFormat *format, InputFormat::allFormats()) {
mPrograms << format->decoderProgramName();
}
mPrograms.remove("");
foreach (QString program, mPrograms) {
if (!checkProgram(program))
setValue("Programs/" + program, findProgram(program));
}
initProfiles();
}
/************************************************
************************************************/
void Settings::initProfiles()
{
// If this is the first launch, we create standard profiles for ALL formats
if (!childGroups().contains(PROFILES_PREFIX)) {
foreach (const OutFormat *format, OutFormat::allFormats()) {
QString group = QString("%1/%2/").arg(PROFILES_PREFIX, format->id());
setDefaultValue(group + "Format", format->id());
setDefaultValue(group + "Name", format->name());
}
return;
}
// 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;
def << "AAC";
def << "FLAC";
def << "MP3";
def << "OGG";
def << "OPUS";
def << "WAV";
def << "WV";
QStringList old = value(KNOWN_FORMATS, def).toStringList();
QStringList newKnownFormats;
foreach (const OutFormat *format, OutFormat::allFormats()) {
newKnownFormats << format->id();
if (old.contains(format->id())) {
continue;
}
QString group = QString("%1/%2/").arg(PROFILES_PREFIX, format->id());
setDefaultValue(group + "Format", format->id());
setDefaultValue(group + "Name", format->name());
}
setValue(KNOWN_FORMATS, newKnownFormats);
}
/************************************************
************************************************/
QString Settings::keyToString(Settings::Key key) const
{
switch (key) {
case Tags_DefaultCodepage:
return "Tags/DefaultCodepage";
// MainWindow **************************
case MainWindow_Width:
return "MainWindow/Width";
case MainWindow_Height:
return "MainWindow/Height";
// Globals *****************************
case Encoder_ThreadCount:
return "Encoder/ThreadCount";
case Encoder_TmpDir:
return "Encoder/TmpDir";
// Out Files ***************************
case OutFiles_Profile:
return "OutFiles/Profile";
case OutFiles_PatternHistory:
return "OutFiles/PatternHistory";
case OutFiles_DirectoryHistory:
return "OutFiles/DirectoryHistory";
// Internet ****************************
case Inet_CDDBHost:
return "Inet/CDDBHost";
// Misc *********************************
case Misc_LastDir:
return "Misc/LastDirectory";
// ConfigureDialog **********************
case ConfigureDialog_Width:
return "ConfigureDialog/Width";
case ConfigureDialog_Height:
return "ConfigureDialog/Height";
}
assert(false);
return "";
}
/************************************************
************************************************/
Settings::~Settings()
{
}
/************************************************
************************************************/
QVariant Settings::value(Key key, const QVariant &defaultValue) const
{
return value(keyToString(key), defaultValue);
}
/************************************************
************************************************/
QVariant Settings::value(const QString &key, const QVariant &defaultValue) const
{
return QSettings::value(key, defaultValue);
}
/************************************************
*
************************************************/
QStringList Settings::groups(const QString &parentGroup) const
{
QStringList res;
for (const QString &key : allKeys()) {
if (key.startsWith(parentGroup)) {
res << key.section("/", 1, 1);
}
}
res.removeDuplicates();
return res;
}
/************************************************
************************************************/
bool Settings::checkProgram(const QString &program) const
{
QString val = programName(program);
if (val.isEmpty())
return false;
QFileInfo fi(val);
return fi.exists() && fi.isExecutable();
}
/************************************************
************************************************/
QString Settings::programName(const QString &program) const
{
#ifdef MAC_BUNDLE
return QDir(qApp->applicationDirPath()).absoluteFilePath(program);
#else
return value("Programs/" + program).toString();
#endif
}
/************************************************
************************************************/
QString Settings::findProgram(const QString &program) const
{
QStringList paths = QProcessEnvironment::systemEnvironment().value("PATH").split(PATH_ENV_SEPARATOR);
foreach (QString path, paths) {
QFileInfo fi(path + QDir::separator() + program + BINARY_EXT);
if (fi.exists() && fi.isExecutable())
return fi.absoluteFilePath();
}
return "";
}
/************************************************
*
************************************************/
QString Settings::tmpDir() const
{
return value(Encoder_TmpDir).toString();
}
/************************************************
*
************************************************/
void Settings::setTmpDir(const QString &value)
{
setValue(Encoder_TmpDir, value);
}
/************************************************
************************************************/
QString Settings::defaultCodepage() const
{
return value(Tags_DefaultCodepage).toString();
}
/************************************************
************************************************/
void Settings::setDefaultCodepage(const QString &value)
{
setValue(Tags_DefaultCodepage, value);
}
/************************************************
************************************************/
void Settings::setValue(Settings::Key key, const QVariant &value)
{
setValue(keyToString(key), value);
emit changed();
}
/************************************************
************************************************/
void Settings::setValue(const QString &key, const QVariant &value)
{
QSettings::setValue(key, value);
emit changed();
}
/************************************************
************************************************/
void Settings::setDefaultValue(Key key, const QVariant &defaultValue)
{
setValue(key, value(key, defaultValue));
}
/************************************************
************************************************/
void Settings::setDefaultValue(const QString &key, const QVariant &defaultValue)
{
setValue(key, value(key, defaultValue));
}
/************************************************
************************************************/
const Profiles &Settings::profiles() const
{
if (mProfiles.isEmpty()) {
const_cast<Settings *>(this)->loadProfiles();
}
return mProfiles;
}
/************************************************
************************************************/
Profiles &Settings::profiles()
{
if (mProfiles.isEmpty()) {
const_cast<Settings *>(this)->loadProfiles();
}
return mProfiles;
}
/************************************************
*
************************************************/
void Settings::loadProfiles()
{
QSet<QString> loaded;
allKeys();
beginGroup(PROFILES_PREFIX);
for (QString id : childGroups()) {
Profile profile(id);
profile.load(*this, id);
if (profile.isValid()) {
mProfiles << profile;
loaded << profile.formatId();
}
}
endGroup();
}
/************************************************
*
************************************************/
void Settings::setProfiles(const Profiles &profiles)
{
allKeys();
beginGroup(PROFILES_PREFIX);
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QSet<QString> old = QSet<QString>::fromList(childGroups());
#else
// After 5.14.0, QT has stated range constructors are available and preferred.
// See: https://doc.qt.io/qt-5/qset.html#toList
QList<QString> groups = childGroups();
QSet<QString> old = QSet<QString>(groups.begin(), groups.end());
#endif
for (const Profile &profile : profiles) {
old.remove(profile.id());
profile.save(*this, profile.id());
}
for (const QString &id : old) {
remove(id);
}
endGroup();
mProfiles.clear();
}
/************************************************
*
************************************************/
const Profile &Settings::currentProfile() const
{
int n = profiles().indexOf(value(OutFiles_Profile).toString());
if (n > -1) {
return profiles()[qMax(0, n)];
}
return NullProfile();
}
/************************************************
*
************************************************/
Profile &Settings::currentProfile()
{
int n = profiles().indexOf(value(OutFiles_Profile).toString());
if (n > -1) {
return profiles()[qMax(0, n)];
}
return NullProfile();
}
/************************************************
*
************************************************/
bool Settings::selectProfile(const QString &profileId)
{
if (profiles().indexOf(profileId) < 0)
return false;
setValue(OutFiles_Profile, profileId);
return true;
}
/************************************************
*
************************************************/
uint Settings::encoderThreadsCount() const
{
return value(Encoder_ThreadCount).toUInt();
}
/************************************************
*
************************************************/
void Settings::setEncoderThreadsCount(uint value)
{
setValue(Encoder_ThreadCount, value);
}
/************************************************
*
************************************************/
QString Settings::cddbHost() const
{
return value(Inet_CDDBHost).toString();
}
/************************************************
*
************************************************/
void Settings::setCddbHost(const QString &value)
{
setValue(Inet_CDDBHost, value);
}