OSDN Git Service

Updated the QAAC add-in for LameXP to QAAC v2.33 (2014-01-14), compiled with MSVC...
[lamexp/LameXP.git] / src / Model_Settings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Model_Settings.h"
24
25 #include "Global.h"
26
27 #include <QSettings>
28 #include <QDesktopServices>
29 #include <QApplication>
30 #include <QString>
31 #include <QFileInfo>
32 #include <QDir>
33 #include <QStringList>
34 #include <QLocale>
35 #include <QRegExp>
36 #include <QReadWriteLock>
37 #include <QReadLocker>
38 #include <QWriteLocker>
39 #include <QHash>
40 #include <QMutex>
41 #include <QSet>
42
43 ////////////////////////////////////////////////////////////
44 // SettingsCache Class
45 ////////////////////////////////////////////////////////////
46
47 class SettingsCache
48 {
49 public:
50         SettingsCache(QSettings *configFile) : m_configFile(configFile)
51         {
52                 m_cache = new QHash<QString, QVariant>();
53                 m_cacheLock = new QMutex();
54                 m_cacheDirty = new QSet<QString>();
55         }
56
57         ~SettingsCache(void)
58         {
59                 flushValues();
60
61                 LAMEXP_DELETE(m_cache);
62                 LAMEXP_DELETE(m_cacheDirty);
63                 LAMEXP_DELETE(m_cacheLock);
64                 LAMEXP_DELETE(m_configFile);
65         }
66
67         inline void storeValue(const QString &key, const QVariant &value)
68         {
69                 QMutexLocker lock(m_cacheLock);
70         
71                 if(!m_cache->contains(key))
72                 {
73                         m_cache->insert(key, value);
74                         m_cacheDirty->insert(key);
75                 }
76                 else
77                 {
78                         if(m_cache->value(key) != value)
79                         {
80                                 m_cache->insert(key, value);
81                                 m_cacheDirty->insert(key);
82                         }
83                 }
84         }
85
86         inline QVariant loadValue(const QString &key, const QVariant &defaultValue) const
87         {
88                 QMutexLocker lock(m_cacheLock);
89
90                 if(!m_cache->contains(key))
91                 {
92                         const QVariant storedValue = m_configFile->value(key, defaultValue);
93                         m_cache->insert(key, storedValue);
94                 }
95
96                 return m_cache->value(key, defaultValue);
97         }
98
99         inline void flushValues(void)
100         {
101                 QMutexLocker lock(m_cacheLock);
102
103                 if(!m_cacheDirty->isEmpty())
104                 {
105                         QSet<QString>::ConstIterator iter;
106                         for(iter = m_cacheDirty->constBegin(); iter != m_cacheDirty->constEnd(); iter++)
107                         {
108                                 if(m_cache->contains(*iter))
109                                 {
110                                         m_configFile->setValue((*iter), m_cache->value(*iter));
111                                 }
112                                 else
113                                 {
114                                         qWarning("Could not find '%s' in cache, but it has been marked as dirty!", QUTF8(*iter));
115                                 }
116                         }
117                         m_configFile->sync();
118                         m_cacheDirty->clear();
119                 }
120         }
121
122 private:
123         QSettings *m_configFile;
124         QHash<QString, QVariant> *m_cache;
125         QSet<QString> *m_cacheDirty;
126         QMutex *m_cacheLock;
127 };
128
129 ////////////////////////////////////////////////////////////
130 // Macros
131 ////////////////////////////////////////////////////////////
132
133 #define LAMEXP_MAKE_OPTION_I(OPT,DEF) \
134 int SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toInt(); } \
135 void SettingsModel::OPT(int value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
136 int SettingsModel::OPT##Default(void) { return (DEF); }
137
138 #define LAMEXP_MAKE_OPTION_S(OPT,DEF) \
139 QString SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toString().trimmed(); } \
140 void SettingsModel::OPT(const QString &value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
141 QString SettingsModel::OPT##Default(void) { return (DEF); }
142
143 #define LAMEXP_MAKE_OPTION_B(OPT,DEF) \
144 bool SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toBool(); } \
145 void SettingsModel::OPT(bool value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
146 bool SettingsModel::OPT##Default(void) { return (DEF); }
147
148 #define LAMEXP_MAKE_OPTION_U(OPT,DEF) \
149 unsigned int SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toUInt(); } \
150 void SettingsModel::OPT(unsigned int value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
151 unsigned int SettingsModel::OPT##Default(void) { return (DEF); }
152
153 #define LAMEXP_MAKE_ID(DEC,STR) static const char *g_settingsId_##DEC = STR
154
155 #define REMOVE_GROUP(OBJ,ID) do \
156 { \
157         OBJ->beginGroup(ID); \
158         OBJ->remove(""); \
159         OBJ->endGroup(); \
160 } \
161 while(0)
162
163 #define DIR_EXISTS(PATH) (QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
164
165 ////////////////////////////////////////////////////////////
166 // Constants
167 ////////////////////////////////////////////////////////////
168
169 //Setting ID's
170 LAMEXP_MAKE_ID(aacEncProfile,                "AdvancedOptions/AACEnc/ForceProfile");
171 LAMEXP_MAKE_ID(aftenAudioCodingMode,         "AdvancedOptions/Aften/AudioCodingMode");
172 LAMEXP_MAKE_ID(aftenDynamicRangeCompression, "AdvancedOptions/Aften/DynamicRangeCompression");
173 LAMEXP_MAKE_ID(aftenExponentSearchSize,      "AdvancedOptions/Aften/ExponentSearchSize");
174 LAMEXP_MAKE_ID(aftenFastBitAllocation,       "AdvancedOptions/Aften/FastBitAllocation");
175 LAMEXP_MAKE_ID(antivirNotificationsEnabled,  "Flags/EnableAntivirusNotifications");
176 LAMEXP_MAKE_ID(autoUpdateCheckBeta,          "AutoUpdate/CheckForBetaVersions");
177 LAMEXP_MAKE_ID(autoUpdateEnabled,            "AutoUpdate/Enabled");
178 LAMEXP_MAKE_ID(autoUpdateLastCheck,          "AutoUpdate/LastCheck");
179 LAMEXP_MAKE_ID(bitrateManagementEnabled,     "AdvancedOptions/BitrateManagement/Enabled");
180 LAMEXP_MAKE_ID(bitrateManagementMaxRate,     "AdvancedOptions/BitrateManagement/MaxRate");
181 LAMEXP_MAKE_ID(bitrateManagementMinRate,     "AdvancedOptions/BitrateManagement/MinRate");
182 LAMEXP_MAKE_ID(compressionAbrBitrateAacEnc,  "Compression/AbrTaretBitrate/AacEnc");
183 LAMEXP_MAKE_ID(compressionAbrBitrateAften,   "Compression/AbrTaretBitrate/Aften");
184 LAMEXP_MAKE_ID(compressionAbrBitrateDcaEnc,  "Compression/AbrTaretBitrate/DcaEnc");
185 LAMEXP_MAKE_ID(compressionAbrBitrateFLAC,    "Compression/AbrTaretBitrate/FLAC");
186 LAMEXP_MAKE_ID(compressionAbrBitrateLAME,    "Compression/AbrTaretBitrate/LAME");
187 LAMEXP_MAKE_ID(compressionAbrBitrateMacEnc,  "Compression/AbrTaretBitrate/MacEnc");
188 LAMEXP_MAKE_ID(compressionAbrBitrateOggEnc,  "Compression/AbrTaretBitrate/OggEnc");
189 LAMEXP_MAKE_ID(compressionAbrBitrateOpusEnc, "Compression/AbrTaretBitrate/OpusEnc");
190 LAMEXP_MAKE_ID(compressionAbrBitrateWave,    "Compression/AbrTaretBitrate/Wave");
191 LAMEXP_MAKE_ID(compressionCbrBitrateAacEnc,  "Compression/CbrTaretBitrate/AacEnc");
192 LAMEXP_MAKE_ID(compressionCbrBitrateAften,   "Compression/CbrTaretBitrate/Aften");
193 LAMEXP_MAKE_ID(compressionCbrBitrateDcaEnc,  "Compression/CbrTaretBitrate/DcaEnc");
194 LAMEXP_MAKE_ID(compressionCbrBitrateFLAC,    "Compression/CbrTaretBitrate/FLAC");
195 LAMEXP_MAKE_ID(compressionCbrBitrateLAME,    "Compression/CbrTaretBitrate/LAME");
196 LAMEXP_MAKE_ID(compressionCbrBitrateMacEnc,  "Compression/CbrTaretBitrate/MacEnc");
197 LAMEXP_MAKE_ID(compressionCbrBitrateOggEnc,  "Compression/CbrTaretBitrate/OggEnc");
198 LAMEXP_MAKE_ID(compressionCbrBitrateOpusEnc, "Compression/CbrTaretBitrate/OpusEnc");
199 LAMEXP_MAKE_ID(compressionCbrBitrateWave,    "Compression/CbrTaretBitrate/Wave");
200 LAMEXP_MAKE_ID(compressionEncoder,           "Compression/Encoder");
201 LAMEXP_MAKE_ID(compressionRCModeAacEnc,      "Compression/RCMode/AacEnc");
202 LAMEXP_MAKE_ID(compressionRCModeAften,       "Compression/RCMode/Aften");
203 LAMEXP_MAKE_ID(compressionRCModeDcaEnc,      "Compression/RCMode/DcaEnc");
204 LAMEXP_MAKE_ID(compressionRCModeFLAC,        "Compression/RCMode/FLAC");
205 LAMEXP_MAKE_ID(compressionRCModeLAME,        "Compression/RCMode/LAME");
206 LAMEXP_MAKE_ID(compressionRCModeMacEnc,      "Compression/RCMode/MacEnc");
207 LAMEXP_MAKE_ID(compressionRCModeOggEnc,      "Compression/RCMode/OggEnc");
208 LAMEXP_MAKE_ID(compressionRCModeOpusEnc,     "Compression/RCMode/OpusEnc");
209 LAMEXP_MAKE_ID(compressionRCModeWave,        "Compression/RCMode/Wave");
210 LAMEXP_MAKE_ID(compressionVbrQualityAacEnc,  "Compression/VbrQualityLevel/AacEnc");
211 LAMEXP_MAKE_ID(compressionVbrQualityAften,   "Compression/VbrQualityLevel/Aften");
212 LAMEXP_MAKE_ID(compressionVbrQualityDcaEnc,  "Compression/VbrQualityLevel/DcaEnc");
213 LAMEXP_MAKE_ID(compressionVbrQualityFLAC,    "Compression/VbrQualityLevel/FLAC");
214 LAMEXP_MAKE_ID(compressionVbrQualityLAME,    "Compression/VbrQualityLevel/LAME");
215 LAMEXP_MAKE_ID(compressionVbrQualityMacEnc,  "Compression/VbrQualityLevel/MacEnc");
216 LAMEXP_MAKE_ID(compressionVbrQualityOggEnc,  "Compression/VbrQualityLevel/OggEnc");
217 LAMEXP_MAKE_ID(compressionVbrQualityOpusEnc, "Compression/VbrQualityLevel/OpusEnc");
218 LAMEXP_MAKE_ID(compressionVbrQualityWave,    "Compression/VbrQualityLevel/Wave");
219 LAMEXP_MAKE_ID(createPlaylist,               "Flags/AutoCreatePlaylist");
220 LAMEXP_MAKE_ID(currentLanguage,              "Localization/Language");
221 LAMEXP_MAKE_ID(currentLanguageFile,          "Localization/UseQMFile");
222 LAMEXP_MAKE_ID(customParametersAacEnc,       "AdvancedOptions/CustomParameters/AacEnc");
223 LAMEXP_MAKE_ID(customParametersAften,        "AdvancedOptions/CustomParameters/Aften");
224 LAMEXP_MAKE_ID(customParametersDcaEnc,       "AdvancedOptions/CustomParameters/DcaEnc");
225 LAMEXP_MAKE_ID(customParametersFLAC,         "AdvancedOptions/CustomParameters/FLAC");
226 LAMEXP_MAKE_ID(customParametersLAME,         "AdvancedOptions/CustomParameters/LAME");
227 LAMEXP_MAKE_ID(customParametersMacEnc,       "AdvancedOptions/CustomParameters/MacEnc");
228 LAMEXP_MAKE_ID(customParametersOggEnc,       "AdvancedOptions/CustomParameters/OggEnc");
229 LAMEXP_MAKE_ID(customParametersOpusEnc,      "AdvancedOptions/CustomParameters/OpusEnc");
230 LAMEXP_MAKE_ID(customParametersWave,         "AdvancedOptions/CustomParameters/Wave");
231 LAMEXP_MAKE_ID(customTempPath,               "AdvancedOptions/TempDirectory/CustomPath");
232 LAMEXP_MAKE_ID(customTempPathEnabled,        "AdvancedOptions/TempDirectory/UseCustomPath");
233 LAMEXP_MAKE_ID(dropBoxWidgetEnabled,         "DropBoxWidget/Enabled");
234 LAMEXP_MAKE_ID(dropBoxWidgetPositionX,       "DropBoxWidget/Position/X");
235 LAMEXP_MAKE_ID(dropBoxWidgetPositionY,       "DropBoxWidget/Position/Y");
236 LAMEXP_MAKE_ID(favoriteOutputFolders,        "OutputDirectory/Favorites");
237 LAMEXP_MAKE_ID(forceStereoDownmix,           "AdvancedOptions/StereoDownmix/Force");
238 LAMEXP_MAKE_ID(hibernateComputer,            "AdvancedOptions/HibernateComputerOnShutdown");
239 LAMEXP_MAKE_ID(interfaceStyle,               "InterfaceStyle");
240 LAMEXP_MAKE_ID(lameAlgoQuality,              "AdvancedOptions/LAME/AlgorithmQuality");
241 LAMEXP_MAKE_ID(lameChannelMode,              "AdvancedOptions/LAME/ChannelMode");
242 LAMEXP_MAKE_ID(licenseAccepted,              "LicenseAccepted");
243 LAMEXP_MAKE_ID(maximumInstances,             "AdvancedOptions/Threading/MaximumInstances");
244 LAMEXP_MAKE_ID(metaInfoPosition,             "MetaInformation/PlaylistPosition");
245 LAMEXP_MAKE_ID(mostRecentInputPath,          "InputDirectory/MostRecentPath");
246 LAMEXP_MAKE_ID(neroAACEnable2Pass,           "AdvancedOptions/AACEnc/Enable2Pass");
247 LAMEXP_MAKE_ID(neroAacNotificationsEnabled,  "Flags/EnableNeroAacNotifications");
248 LAMEXP_MAKE_ID(normalizationFilterEnabled,   "AdvancedOptions/VolumeNormalization/Enabled");
249 LAMEXP_MAKE_ID(normalizationFilterEQMode,    "AdvancedOptions/VolumeNormalization/EqualizationMode");
250 LAMEXP_MAKE_ID(normalizationFilterMaxVolume, "AdvancedOptions/VolumeNormalization/MaxVolume");
251 LAMEXP_MAKE_ID(opusComplexity,               "AdvancedOptions/Opus/EncodingComplexity");
252 LAMEXP_MAKE_ID(opusDisableResample,          "AdvancedOptions/Opus/DisableResample");
253 LAMEXP_MAKE_ID(opusFramesize,                "AdvancedOptions/Opus/FrameSize");
254 LAMEXP_MAKE_ID(opusOptimizeFor,              "AdvancedOptions/Opus/OptimizeForSignalType");
255 LAMEXP_MAKE_ID(outputDir,                    "OutputDirectory/SelectedPath");
256 LAMEXP_MAKE_ID(outputToSourceDir,            "OutputDirectory/OutputToSourceFolder");
257 LAMEXP_MAKE_ID(overwriteMode,                "AdvancedOptions/OverwriteMode");
258 LAMEXP_MAKE_ID(prependRelativeSourcePath,    "OutputDirectory/PrependRelativeSourcePath");
259 LAMEXP_MAKE_ID(renameOutputFilesEnabled,     "AdvancedOptions/RenameOutputFiles/Enabled");
260 LAMEXP_MAKE_ID(renameOutputFilesPattern,     "AdvancedOptions/RenameOutputFiles/Pattern");
261 LAMEXP_MAKE_ID(samplingRate,                 "AdvancedOptions/Common/Resampling");
262 LAMEXP_MAKE_ID(shellIntegrationEnabled,      "Flags/EnableShellIntegration");
263 LAMEXP_MAKE_ID(slowStartup,                  "Flags/SlowStartupDetected");
264 LAMEXP_MAKE_ID(soundsEnabled,                "Flags/EnableSounds");
265 LAMEXP_MAKE_ID(toneAdjustBass,               "AdvancedOptions/ToneAdjustment/Bass");
266 LAMEXP_MAKE_ID(toneAdjustTreble,             "AdvancedOptions/ToneAdjustment/Treble");
267 LAMEXP_MAKE_ID(versionNumber,                "VersionNumber");
268 LAMEXP_MAKE_ID(writeMetaTags,                "Flags/WriteMetaTags");
269
270 //LUT
271 const int SettingsModel::samplingRates[8] = {0, 16000, 22050, 24000, 32000, 44100, 48000, -1};
272
273 static QReadWriteLock s_lock;
274
275 ////////////////////////////////////////////////////////////
276 // Constructor
277 ////////////////////////////////////////////////////////////
278
279 SettingsModel::SettingsModel(void)
280 {
281         QString configPath = "LameXP.ini";
282         
283         if(!lamexp_portable_mode())
284         {
285                 QString dataPath = initDirectory(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
286                 if(!dataPath.isEmpty())
287                 {
288                         configPath = QString("%1/config.ini").arg(QDir(dataPath).canonicalPath());
289                 }
290                 else
291                 {
292                         qWarning("SettingsModel: DataLocation could not be initialized!");
293                         dataPath = initDirectory(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
294                         if(!dataPath.isEmpty())
295                         {
296                                 configPath = QString("%1/LameXP.ini").arg(QDir(dataPath).canonicalPath());
297                         }
298                 }
299         }
300         else
301         {
302                 qDebug("LameXP is running in \"portable\" mode -> config in application dir!\n");
303                 QString appPath = QFileInfo(QApplication::applicationFilePath()).canonicalFilePath();
304                 if(appPath.isEmpty())
305                 {
306                         appPath = QFileInfo(QApplication::applicationFilePath()).absoluteFilePath();
307                 }
308                 if(QFileInfo(appPath).exists() && QFileInfo(appPath).isFile())
309                 {
310                         configPath = QString("%1/%2.ini").arg(QFileInfo(appPath).absolutePath(), QFileInfo(appPath).completeBaseName());
311                 }
312         }
313
314         //Create settings
315         QSettings *configFile = new QSettings(configPath, QSettings::IniFormat);
316         const QString groupKey = QString().sprintf("LameXP_%u%02u%05u", lamexp_version_major(), lamexp_version_minor(), lamexp_version_confg());
317         QStringList childGroups =configFile->childGroups();
318
319         //Clean-up settings
320         while(!childGroups.isEmpty())
321         {
322                 QString current = childGroups.takeFirst();
323                 QRegExp filter("^LameXP_(\\d+)(\\d\\d)(\\d\\d\\d\\d\\d)$");
324                 if(filter.indexIn(current) >= 0)
325                 {
326                         bool ok = false;
327                         unsigned int temp = filter.cap(3).toUInt(&ok) + 10;
328                         if(ok && (temp >= lamexp_version_confg()))
329                         {
330                                 continue;
331                         }
332                 }
333                 qWarning("Deleting obsolete group from config: %s", QUTF8(current));
334                 REMOVE_GROUP(configFile, current);
335         }
336
337         //Setup settings
338         configFile->beginGroup(groupKey);
339         configFile->setValue(g_settingsId_versionNumber, QApplication::applicationVersion());
340         configFile->sync();
341
342         //Create the cache
343         m_configCache = new SettingsCache(configFile);
344 }
345
346 ////////////////////////////////////////////////////////////
347 // Destructor
348 ////////////////////////////////////////////////////////////
349
350 SettingsModel::~SettingsModel(void)
351 {
352         LAMEXP_DELETE(m_configCache);
353         LAMEXP_DELETE(m_defaultLanguage);
354 }
355
356 ////////////////////////////////////////////////////////////
357 // Public Functions
358 ////////////////////////////////////////////////////////////
359
360 #define CHECK_RCMODE(NAME) do\
361 { \
362         if(this->compressionRCMode##NAME() < SettingsModel::VBRMode || this->compressionRCMode##NAME() >= SettingsModel::RCMODE_COUNT) \
363         { \
364                 this->compressionRCMode##NAME(SettingsModel::VBRMode); \
365         } \
366 } \
367 while(0)
368
369 void SettingsModel::validate(void)
370 {
371         if(this->compressionEncoder() < SettingsModel::MP3Encoder || this->compressionEncoder() >= SettingsModel::ENCODER_COUNT)
372         {
373                 this->compressionEncoder(SettingsModel::MP3Encoder);
374         }
375         
376         CHECK_RCMODE(LAME);
377         CHECK_RCMODE(OggEnc);
378         CHECK_RCMODE(AacEnc);
379         CHECK_RCMODE(Aften);
380         CHECK_RCMODE(OpusEnc);
381         
382         if(!(lamexp_check_tool("neroAacEnc.exe") && lamexp_check_tool("neroAacDec.exe") && lamexp_check_tool("neroAacTag.exe")))
383         {
384                 if(!(lamexp_check_tool("fhgaacenc.exe") && lamexp_check_tool("enc_fhgaac.dll")))
385                 {
386                         if(!(lamexp_check_tool("qaac.exe") && lamexp_check_tool("libsoxrate.dll")))
387                         {
388                                 if(this->compressionEncoder() == SettingsModel::AACEncoder)
389                                 {
390                                         qWarning("AAC encoder selected, but not available any more. Reverting to MP3!");
391                                         this->compressionEncoder(SettingsModel::MP3Encoder);
392                                 }
393                         }
394                 }
395         }
396         
397         if(this->outputDir().isEmpty() || (!DIR_EXISTS(this->outputDir())))
398         {
399                 qWarning("Output directory not set yet or does NOT exist anymore -> Resetting");
400                 this->outputDir(defaultDirectory());
401         }
402
403         if(this->mostRecentInputPath().isEmpty() || (!DIR_EXISTS(this->mostRecentInputPath())))
404         {
405                 qWarning("Most recent input directory not set yet or does NOT exist anymore -> Resetting");
406                 this->mostRecentInputPath(defaultDirectory());
407         }
408
409         if(!this->currentLanguageFile().isEmpty())
410         {
411                 const QString qmPath = QFileInfo(this->currentLanguageFile()).canonicalFilePath();
412                 if(qmPath.isEmpty() || (!(QFileInfo(qmPath).exists() && QFileInfo(qmPath).isFile() && (QFileInfo(qmPath).suffix().compare("qm", Qt::CaseInsensitive) == 0))))
413                 {
414                         qWarning("Current language file missing, reverting to built-in translator!");
415                         this->currentLanguageFile(QString());
416                 }
417         }
418
419         if(!lamexp_query_translations().contains(this->currentLanguage(), Qt::CaseInsensitive))
420         {
421                 qWarning("Current language \"%s\" is unknown, reverting to default language!", this->currentLanguage().toLatin1().constData());
422                 this->currentLanguage(defaultLanguage());
423         }
424
425         if(this->hibernateComputer())
426         {
427                 if(!lamexp_is_hibernation_supported())
428                 {
429                         this->hibernateComputer(false);
430                 }
431         }
432
433         if(this->overwriteMode() < SettingsModel::Overwrite_KeepBoth || this->overwriteMode() > SettingsModel::Overwrite_Replaces)
434         {
435                 this->overwriteMode(SettingsModel::Overwrite_KeepBoth);
436         }
437
438 }
439
440 void SettingsModel::syncNow(void)
441 {
442         m_configCache->flushValues();
443 }
444
445 ////////////////////////////////////////////////////////////
446 // Private Functions
447 ////////////////////////////////////////////////////////////
448
449 QString *SettingsModel::m_defaultLanguage = NULL;
450
451 QString SettingsModel::defaultLanguage(void) const
452 {
453         QReadLocker readLock(&s_lock);
454
455         //Default already initialized?
456         if(m_defaultLanguage)
457         {
458                 return *m_defaultLanguage;
459         }
460         
461         //Acquire write lock now
462         readLock.unlock();
463         QWriteLocker writeLock(&s_lock);
464         
465         //Default still not initialized?
466         if(m_defaultLanguage)
467         {
468                 return *m_defaultLanguage;
469         }
470
471         //Detect system langauge
472         QLocale systemLanguage= QLocale::system();
473         qDebug("[Locale]");
474         qDebug("Language: %s (%d)", QUTF8(QLocale::languageToString(systemLanguage.language())), systemLanguage.language());
475         qDebug("Country is: %s (%d)", QUTF8(QLocale::countryToString(systemLanguage.country())), systemLanguage.country());
476         qDebug("Script is: %s (%d)\n", QUTF8(QLocale::scriptToString(systemLanguage.script())), systemLanguage.script());
477
478         //Check if we can use the default translation
479         if(systemLanguage.language() == QLocale::English /*|| systemLanguage.language() == QLocale::C*/)
480         {
481                 m_defaultLanguage = new QString(LAMEXP_DEFAULT_LANGID);
482                 return LAMEXP_DEFAULT_LANGID;
483         }
484
485         //Try to find a suitable translation for the user's system language *and* country
486         QStringList languages = lamexp_query_translations();
487         while(!languages.isEmpty())
488         {
489                 QString currentLangId = languages.takeFirst();
490                 if(lamexp_translation_sysid(currentLangId) == systemLanguage.language())
491                 {
492                         if(lamexp_translation_country(currentLangId) == systemLanguage.country())
493                         {
494                                 m_defaultLanguage = new QString(currentLangId);
495                                 return currentLangId;
496                         }
497                 }
498         }
499
500         //Try to find a suitable translation for the user's system language
501         languages = lamexp_query_translations();
502         while(!languages.isEmpty())
503         {
504                 QString currentLangId = languages.takeFirst();
505                 if(lamexp_translation_sysid(currentLangId) == systemLanguage.language())
506                 {
507                         m_defaultLanguage = new QString(currentLangId);
508                         return currentLangId;
509                 }
510         }
511
512         //Fall back to the default translation
513         m_defaultLanguage = new QString(LAMEXP_DEFAULT_LANGID);
514         return LAMEXP_DEFAULT_LANGID;
515 }
516
517 QString SettingsModel::defaultDirectory(void) const
518 {
519         QString defaultLocation = initDirectory(QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
520
521         if(defaultLocation.isEmpty())
522         {
523                 defaultLocation = initDirectory(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
524
525                 if(defaultLocation.isEmpty())
526                 {
527                         defaultLocation = initDirectory(QDir::currentPath());
528                 }
529         }
530
531         return defaultLocation;
532 }
533
534 QString SettingsModel::initDirectory(const QString &path) const
535 {
536         if(path.isEmpty())
537         {
538                 return QString();
539         }
540
541         if(!QDir(path).exists())
542         {
543                 for(int i = 0; i < 32; i++)
544                 {
545                         if(QDir(path).mkpath(".")) break;
546                         lamexp_sleep(1);
547                 }
548         }
549
550         if(!QDir(path).exists())
551         {
552                 return QString();
553         }
554         
555         return QDir(path).canonicalPath();
556 }
557
558 ////////////////////////////////////////////////////////////
559 // Getter and Setter
560 ////////////////////////////////////////////////////////////
561
562 LAMEXP_MAKE_OPTION_I(aacEncProfile, 0)
563 LAMEXP_MAKE_OPTION_I(aftenAudioCodingMode, 0)
564 LAMEXP_MAKE_OPTION_I(aftenDynamicRangeCompression, 5)
565 LAMEXP_MAKE_OPTION_I(aftenExponentSearchSize, 8)
566 LAMEXP_MAKE_OPTION_B(aftenFastBitAllocation, false)
567 LAMEXP_MAKE_OPTION_B(antivirNotificationsEnabled, true)
568 LAMEXP_MAKE_OPTION_B(autoUpdateCheckBeta, false)
569 LAMEXP_MAKE_OPTION_B(autoUpdateEnabled, true)
570 LAMEXP_MAKE_OPTION_S(autoUpdateLastCheck, "Never")
571 LAMEXP_MAKE_OPTION_B(bitrateManagementEnabled, false)
572 LAMEXP_MAKE_OPTION_I(bitrateManagementMaxRate, 500)
573 LAMEXP_MAKE_OPTION_I(bitrateManagementMinRate, 32)
574 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateAacEnc, 19)
575 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateAften, 17)
576 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateDcaEnc, 13)
577 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateFLAC, 5)
578 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateLAME, 10)
579 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateMacEnc, 2)
580 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateOggEnc, 16)
581 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateOpusEnc, 11)
582 LAMEXP_MAKE_OPTION_I(compressionAbrBitrateWave, 0)
583 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateAacEnc, 19)
584 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateAften, 17)
585 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateDcaEnc, 13)
586 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateFLAC, 5)
587 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateLAME, 10)
588 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateMacEnc, 2)
589 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateOggEnc, 16)
590 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateOpusEnc, 11)
591 LAMEXP_MAKE_OPTION_I(compressionCbrBitrateWave, 0)
592 LAMEXP_MAKE_OPTION_I(compressionEncoder, 0)
593 LAMEXP_MAKE_OPTION_I(compressionRCModeAacEnc, 0)
594 LAMEXP_MAKE_OPTION_I(compressionRCModeAften, 0)
595 LAMEXP_MAKE_OPTION_I(compressionRCModeDcaEnc, 2)
596 LAMEXP_MAKE_OPTION_I(compressionRCModeFLAC, 0)
597 LAMEXP_MAKE_OPTION_I(compressionRCModeLAME, 0)
598 LAMEXP_MAKE_OPTION_I(compressionRCModeMacEnc, 0)
599 LAMEXP_MAKE_OPTION_I(compressionRCModeOggEnc, 0)
600 LAMEXP_MAKE_OPTION_I(compressionRCModeOpusEnc, 0)
601 LAMEXP_MAKE_OPTION_I(compressionRCModeWave, 2)
602 LAMEXP_MAKE_OPTION_I(compressionVbrQualityAacEnc, 10)
603 LAMEXP_MAKE_OPTION_I(compressionVbrQualityAften, 15)
604 LAMEXP_MAKE_OPTION_I(compressionVbrQualityDcaEnc, 13)
605 LAMEXP_MAKE_OPTION_I(compressionVbrQualityFLAC, 5)
606 LAMEXP_MAKE_OPTION_I(compressionVbrQualityLAME, 7)
607 LAMEXP_MAKE_OPTION_I(compressionVbrQualityMacEnc, 2)
608 LAMEXP_MAKE_OPTION_I(compressionVbrQualityOggEnc, 7)
609 LAMEXP_MAKE_OPTION_I(compressionVbrQualityOpusEnc, 11)
610 LAMEXP_MAKE_OPTION_I(compressionVbrQualityWave, 0)
611 LAMEXP_MAKE_OPTION_B(createPlaylist, true)
612 LAMEXP_MAKE_OPTION_S(currentLanguage, defaultLanguage())
613 LAMEXP_MAKE_OPTION_S(currentLanguageFile, QString())
614 LAMEXP_MAKE_OPTION_S(customParametersAacEnc, QString())
615 LAMEXP_MAKE_OPTION_S(customParametersAften, QString())
616 LAMEXP_MAKE_OPTION_S(customParametersDcaEnc, QString())
617 LAMEXP_MAKE_OPTION_S(customParametersFLAC, QString())
618 LAMEXP_MAKE_OPTION_S(customParametersLAME, QString())
619 LAMEXP_MAKE_OPTION_S(customParametersMacEnc, QString())
620 LAMEXP_MAKE_OPTION_S(customParametersOggEnc, QString())
621 LAMEXP_MAKE_OPTION_S(customParametersOpusEnc, QString())
622 LAMEXP_MAKE_OPTION_S(customParametersWave, QString())
623 LAMEXP_MAKE_OPTION_S(customTempPath, QDesktopServices::storageLocation(QDesktopServices::TempLocation))
624 LAMEXP_MAKE_OPTION_B(customTempPathEnabled, false)
625 LAMEXP_MAKE_OPTION_B(dropBoxWidgetEnabled, true)
626 LAMEXP_MAKE_OPTION_I(dropBoxWidgetPositionX, -1)
627 LAMEXP_MAKE_OPTION_I(dropBoxWidgetPositionY, -1)
628 LAMEXP_MAKE_OPTION_S(favoriteOutputFolders, QString())
629 LAMEXP_MAKE_OPTION_B(forceStereoDownmix, false)
630 LAMEXP_MAKE_OPTION_B(hibernateComputer, false)
631 LAMEXP_MAKE_OPTION_I(interfaceStyle, 0)
632 LAMEXP_MAKE_OPTION_I(lameAlgoQuality, 2)
633 LAMEXP_MAKE_OPTION_I(lameChannelMode, 0)
634 LAMEXP_MAKE_OPTION_I(licenseAccepted, 0)
635 LAMEXP_MAKE_OPTION_U(maximumInstances, 0)
636 LAMEXP_MAKE_OPTION_U(metaInfoPosition, UINT_MAX)
637 LAMEXP_MAKE_OPTION_S(mostRecentInputPath, defaultDirectory())
638 LAMEXP_MAKE_OPTION_B(neroAACEnable2Pass, true)
639 LAMEXP_MAKE_OPTION_B(neroAacNotificationsEnabled, true)
640 LAMEXP_MAKE_OPTION_B(normalizationFilterEnabled, false)
641 LAMEXP_MAKE_OPTION_I(normalizationFilterEQMode, 0)
642 LAMEXP_MAKE_OPTION_I(normalizationFilterMaxVolume, -50)
643 LAMEXP_MAKE_OPTION_I(opusComplexity, 10)
644 LAMEXP_MAKE_OPTION_B(opusDisableResample, false)
645 LAMEXP_MAKE_OPTION_I(opusFramesize, 3)
646 LAMEXP_MAKE_OPTION_I(opusOptimizeFor, 0)
647 LAMEXP_MAKE_OPTION_S(outputDir, defaultDirectory())
648 LAMEXP_MAKE_OPTION_B(outputToSourceDir, false)
649 LAMEXP_MAKE_OPTION_I(overwriteMode, Overwrite_KeepBoth)
650 LAMEXP_MAKE_OPTION_B(prependRelativeSourcePath, false)
651 LAMEXP_MAKE_OPTION_B(renameOutputFilesEnabled, false)
652 LAMEXP_MAKE_OPTION_S(renameOutputFilesPattern, "[<TrackNo>] <Artist> - <Title>")
653 LAMEXP_MAKE_OPTION_I(samplingRate, 0)
654 LAMEXP_MAKE_OPTION_B(shellIntegrationEnabled, !lamexp_portable_mode())
655 LAMEXP_MAKE_OPTION_B(slowStartup, false)
656 LAMEXP_MAKE_OPTION_B(soundsEnabled, true)
657 LAMEXP_MAKE_OPTION_I(toneAdjustBass, 0)
658 LAMEXP_MAKE_OPTION_I(toneAdjustTreble, 0)
659 LAMEXP_MAKE_OPTION_B(writeMetaTags, true)