OSDN Git Service

Added support for the Visual Leak Detector. This effects "Debug" builds only.
[lamexp/LameXP.git] / src / Model_Settings.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Model_Settings.h"
23
24 #include "Global.h"
25
26 #include <QSettings>
27 #include <QDesktopServices>
28 #include <QApplication>
29 #include <QString>
30 #include <QFileInfo>
31 #include <QDir>
32 #include <QStringList>
33 #include <QLocale>
34 #include <QRegExp>
35
36
37 ////////////////////////////////////////////////////////////
38 //Macros
39 ////////////////////////////////////////////////////////////
40
41 #define LAMEXP_MAKE_OPTION_I(OPT,DEF) \
42 int SettingsModel::OPT(void) { return m_settings->value(g_settingsId_##OPT, DEF).toInt(); } \
43 void SettingsModel::OPT(int value) { m_settings->setValue(g_settingsId_##OPT, value); } \
44 int SettingsModel::OPT##Default(void) { return DEF; }
45
46 #define LAMEXP_MAKE_OPTION_S(OPT,DEF) \
47 QString SettingsModel::OPT(void) { return m_settings->value(g_settingsId_##OPT, DEF).toString().trimmed(); } \
48 void SettingsModel::OPT(const QString &value) { m_settings->setValue(g_settingsId_##OPT, value); } \
49 QString SettingsModel::OPT##Default(void) { return DEF; }
50
51 #define LAMEXP_MAKE_OPTION_B(OPT,DEF) \
52 bool SettingsModel::OPT(void) { return m_settings->value(g_settingsId_##OPT, DEF).toBool(); } \
53 void SettingsModel::OPT(bool value) { m_settings->setValue(g_settingsId_##OPT, value); } \
54 bool SettingsModel::OPT##Default(void) { return DEF; }
55
56 #define LAMEXP_MAKE_OPTION_U(OPT,DEF) \
57 unsigned int SettingsModel::OPT(void) { return m_settings->value(g_settingsId_##OPT, DEF).toUInt(); } \
58 void SettingsModel::OPT(unsigned int value) { m_settings->setValue(g_settingsId_##OPT, value); } \
59 unsigned int SettingsModel::OPT##Default(void) { return DEF; }
60
61 #define LAMEXP_MAKE_ID(DEC,STR) static const char *g_settingsId_##DEC = STR
62 #define REMOVE_GROUP(OBJ,ID) OBJ->beginGroup(ID); OBJ->remove(""); OBJ->endGroup();
63
64 ////////////////////////////////////////////////////////////
65 //Constants
66 ////////////////////////////////////////////////////////////
67
68 //Setting ID's
69 LAMEXP_MAKE_ID(versionNumber, "VersionNumber");
70 LAMEXP_MAKE_ID(licenseAccepted, "LicenseAccepted");
71 LAMEXP_MAKE_ID(interfaceStyle, "InterfaceStyle");
72 LAMEXP_MAKE_ID(compressionEncoder, "Compression/Encoder");
73 LAMEXP_MAKE_ID(compressionRCMode, "Compression/RCMode");
74 LAMEXP_MAKE_ID(compressionBitrate, "Compression/Bitrate");
75 LAMEXP_MAKE_ID(outputDir, "OutputDirectory/SelectedPath");
76 LAMEXP_MAKE_ID(outputToSourceDir, "OutputDirectory/OutputToSourceFolder");
77 LAMEXP_MAKE_ID(prependRelativeSourcePath, "OutputDirectory/PrependRelativeSourcePath");
78 LAMEXP_MAKE_ID(favoriteOutputFolders, "OutputDirectory/Favorites");
79 LAMEXP_MAKE_ID(mostRecentInputPath, "InputDirectory/MostRecentPath");
80 LAMEXP_MAKE_ID(writeMetaTags, "Flags/WriteMetaTags");
81 LAMEXP_MAKE_ID(createPlaylist, "Flags/AutoCreatePlaylist");
82 LAMEXP_MAKE_ID(autoUpdateLastCheck, "AutoUpdate/LastCheck");
83 LAMEXP_MAKE_ID(autoUpdateEnabled, "AutoUpdate/Enabled");
84 LAMEXP_MAKE_ID(autoUpdateCheckBeta, "AutoUpdate/CheckForBetaVersions");
85 LAMEXP_MAKE_ID(soundsEnabled, "Flags/EnableSounds");
86 LAMEXP_MAKE_ID(neroAacNotificationsEnabled, "Flags/EnableNeroAacNotifications");
87 LAMEXP_MAKE_ID(antivirNotificationsEnabled, "Flags/EnableAntivirusNotifications");
88 LAMEXP_MAKE_ID(dropBoxWidgetEnabled, "Flags/EnableDropBoxWidget");
89 LAMEXP_MAKE_ID(shellIntegrationEnabled, "Flags/EnableShellIntegration");
90 LAMEXP_MAKE_ID(currentLanguage, "Localization/Language");
91 LAMEXP_MAKE_ID(lameAlgoQuality, "AdvancedOptions/LAME/AlgorithmQuality");
92 LAMEXP_MAKE_ID(lameChannelMode, "AdvancedOptions/LAME/ChannelMode");
93 LAMEXP_MAKE_ID(forceStereoDownmix, "AdvancedOptions/StereoDownmix/Force");
94 LAMEXP_MAKE_ID(bitrateManagementEnabled, "AdvancedOptions/BitrateManagement/Enabled");
95 LAMEXP_MAKE_ID(bitrateManagementMinRate, "AdvancedOptions/BitrateManagement/MinRate");
96 LAMEXP_MAKE_ID(bitrateManagementMaxRate, "AdvancedOptions/BitrateManagement/MaxRate");
97 LAMEXP_MAKE_ID(aftenAudioCodingMode, "AdvancedOptions/Aften/AudioCodingMode");
98 LAMEXP_MAKE_ID(aftenDynamicRangeCompression, "AdvancedOptions/Aften/DynamicRangeCompression");
99 LAMEXP_MAKE_ID(aftenFastBitAllocation, "AdvancedOptions/Aften/FastBitAllocation");
100 LAMEXP_MAKE_ID(aftenExponentSearchSize, "AdvancedOptions/Aften/ExponentSearchSize");
101 LAMEXP_MAKE_ID(samplingRate, "AdvancedOptions/Common/Resampling");
102 LAMEXP_MAKE_ID(neroAACEnable2Pass, "AdvancedOptions/AACEnc/Enable2Pass");
103 LAMEXP_MAKE_ID(aacEncProfile, "AdvancedOptions/AACEnc/ForceProfile");
104 LAMEXP_MAKE_ID(normalizationFilterEnabled, "AdvancedOptions/VolumeNormalization/Enabled");
105 LAMEXP_MAKE_ID(normalizationFilterMaxVolume, "AdvancedOptions/VolumeNormalization/MaxVolume");
106 LAMEXP_MAKE_ID(normalizationFilterEqualizationMode, "AdvancedOptions/VolumeNormalization/EqualizationMode");
107 LAMEXP_MAKE_ID(toneAdjustBass, "AdvancedOptions/ToneAdjustment/Bass");
108 LAMEXP_MAKE_ID(toneAdjustTreble, "AdvancedOptions/ToneAdjustment/Treble");
109 LAMEXP_MAKE_ID(customParametersLAME, "AdvancedOptions/CustomParameters/LAME");
110 LAMEXP_MAKE_ID(customParametersOggEnc, "AdvancedOptions/CustomParameters/OggEnc");
111 LAMEXP_MAKE_ID(customParametersAacEnc, "AdvancedOptions/CustomParameters/AacEnc");
112 LAMEXP_MAKE_ID(customParametersAften, "AdvancedOptions/CustomParameters/Aften");
113 LAMEXP_MAKE_ID(customParametersFLAC, "AdvancedOptions/CustomParameters/FLAC");
114 LAMEXP_MAKE_ID(renameOutputFilesEnabled, "AdvancedOptions/RenameOutputFiles/Enabled");
115 LAMEXP_MAKE_ID(renameOutputFilesPattern, "AdvancedOptions/RenameOutputFiles/Pattern");
116 LAMEXP_MAKE_ID(hibernateComputer, "AdvancedOptions/HibernateComputerOnShutdown");
117 LAMEXP_MAKE_ID(metaInfoPosition, "MetaInformation/PlaylistPosition");
118 LAMEXP_MAKE_ID(maximumInstances, "AdvancedOptions/Threading/MaximumInstances");
119 LAMEXP_MAKE_ID(customTempPath, "AdvancedOptions/TempDirectory/CustomPath");
120 LAMEXP_MAKE_ID(customTempPathEnabled, "AdvancedOptions/TempDirectory/UseCustomPath");
121 LAMEXP_MAKE_ID(slowStartup, "Flags/SlowStartupDetected");
122
123 //LUT
124 const int SettingsModel::mp3Bitrates[15] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1};
125 const int SettingsModel::ac3Bitrates[20] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640, -1};
126 const int SettingsModel::samplingRates[8] = {0, 16000, 22050, 24000, 32000, 44100, 48000, -1};
127
128 ////////////////////////////////////////////////////////////
129 // Constructor
130 ////////////////////////////////////////////////////////////
131
132 SettingsModel::SettingsModel(void)
133 :
134         m_defaultLanguage(NULL)
135 {
136         QString configPath = "LameXP.ini";
137         
138         if(!lamexp_portable_mode())
139         {
140                 QString dataPath = initDirectory(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
141                 if(!dataPath.isEmpty())
142                 {
143                         configPath = QString("%1/config.ini").arg(QDir(dataPath).canonicalPath());
144                 }
145                 else
146                 {
147                         qWarning("SettingsModel: DataLocation could not be initialized!");
148                         dataPath = initDirectory(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
149                         if(!dataPath.isEmpty())
150                         {
151                                 configPath = QString("%1/LameXP.ini").arg(QDir(dataPath).canonicalPath());
152                         }
153                 }
154         }
155         else
156         {
157                 qDebug("LameXP is running in \"portable\" mode -> config in application dir!\n");
158                 QString appPath = QFileInfo(QApplication::applicationFilePath()).canonicalFilePath();
159                 if(appPath.isEmpty())
160                 {
161                         appPath = QFileInfo(QApplication::applicationFilePath()).absoluteFilePath();
162                 }
163                 if(QFileInfo(appPath).exists() && QFileInfo(appPath).isFile())
164                 {
165                         configPath = QString("%1/%2.ini").arg(QFileInfo(appPath).absolutePath(), QFileInfo(appPath).completeBaseName());
166                 }
167         }
168
169         m_settings = new QSettings(configPath, QSettings::IniFormat);
170         const QString groupKey = QString().sprintf("LameXP_%u%02u%05u", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build());
171         QStringList childGroups = m_settings->childGroups();
172
173         while(!childGroups.isEmpty())
174         {
175                 QString current = childGroups.takeFirst();
176                 QRegExp filter("^LameXP_(\\d+)(\\d\\d)(\\d\\d\\d\\d\\d)$");
177                 if(filter.indexIn(current) >= 0)
178                 {
179                         bool ok = false;
180                         unsigned int temp = filter.cap(3).toUInt(&ok) + 10;
181                         if(ok && (temp >= lamexp_version_build()))
182                         {
183                                 continue;
184                         }
185                 }
186                 qWarning("Deleting obsolete group from config: %s", current.toUtf8().constData());
187                 REMOVE_GROUP(m_settings, current);
188         }
189
190         m_settings->beginGroup(groupKey);
191         m_settings->setValue(g_settingsId_versionNumber, QApplication::applicationVersion());
192         m_settings->sync();
193 }
194
195 ////////////////////////////////////////////////////////////
196 // Destructor
197 ////////////////////////////////////////////////////////////
198
199 SettingsModel::~SettingsModel(void)
200 {
201         LAMEXP_DELETE(m_settings);
202         LAMEXP_DELETE(m_defaultLanguage);
203 }
204
205 ////////////////////////////////////////////////////////////
206 // Public Functions
207 ////////////////////////////////////////////////////////////
208
209 void SettingsModel::validate(void)
210 {
211         if(this->compressionEncoder() < SettingsModel::MP3Encoder || this->compressionEncoder() > SettingsModel::PCMEncoder)
212         {
213                 this->compressionEncoder(SettingsModel::MP3Encoder);
214         }
215         
216         if(this->compressionRCMode() < SettingsModel::VBRMode || this->compressionRCMode() > SettingsModel::CBRMode)
217         {
218                 this->compressionEncoder(SettingsModel::VBRMode);
219         }
220         
221         if(!(lamexp_check_tool("neroAacEnc.exe") && lamexp_check_tool("neroAacDec.exe") && lamexp_check_tool("neroAacTag.exe")))
222         {
223                 if(!(lamexp_check_tool("fhgaacenc.exe") && lamexp_check_tool("enc_fhgaac.dll")))
224                 {
225                         if(!(lamexp_check_tool("qaac.exe") && lamexp_check_tool("libsoxrate.dll")))
226                         {
227                                 if(this->compressionEncoder() == SettingsModel::AACEncoder)
228                                 {
229                                         qWarning("AAC encoder selected, but not available any more. Reverting to MP3!");
230                                         this->compressionEncoder(SettingsModel::MP3Encoder);
231                                 }
232                         }
233                 }
234         }
235         
236         if(this->outputDir().isEmpty() || !QFileInfo(this->outputDir()).isDir())
237         {
238                 QString musicLocation = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
239                 this->outputDir(musicLocation.isEmpty() ? QDesktopServices::storageLocation(QDesktopServices::HomeLocation) : musicLocation);
240         }
241
242         if(this->mostRecentInputPath().isEmpty() || !QFileInfo(this->mostRecentInputPath()).isDir())
243         {
244                 QString musicLocation = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
245                 this->outputDir(musicLocation.isEmpty() ? QDesktopServices::storageLocation(QDesktopServices::HomeLocation) : musicLocation);
246         }
247
248         if(!lamexp_query_translations().contains(this->currentLanguage(), Qt::CaseInsensitive))
249         {
250                 qWarning("Current language \"%s\" is unknown, reverting to default language!", this->currentLanguage().toLatin1().constData());
251                 this->currentLanguage(defaultLanguage());
252         }
253
254         if(this->hibernateComputer())
255         {
256                 if(!lamexp_is_hibernation_supported())
257                 {
258                         this->hibernateComputer(false);
259                 }
260         }
261 }
262
263 ////////////////////////////////////////////////////////////
264 // Private Functions
265 ////////////////////////////////////////////////////////////
266
267 QString SettingsModel::defaultLanguage(void)
268 {
269         if(m_defaultLanguage)
270         {
271                 return *m_defaultLanguage;
272         }
273         
274         //Detect system langauge
275         QLocale systemLanguage= QLocale::system();
276         qDebug("[Locale]");
277         qDebug("Language: %s (%d)", QLocale::languageToString(systemLanguage.language()).toUtf8().constData(), systemLanguage.language());
278         qDebug("Country is: %s (%d)", QLocale::countryToString(systemLanguage.country()).toUtf8().constData(), systemLanguage.country());
279         qDebug("Script is: %s (%d)\n", QLocale::scriptToString(systemLanguage.script()).toUtf8().constData(), systemLanguage.script());
280
281         //Check if we can use the default translation
282         if(systemLanguage.language() == QLocale::English /*|| systemLanguage.language() == QLocale::C*/)
283         {
284                 m_defaultLanguage = new QString(LAMEXP_DEFAULT_LANGID);
285                 return LAMEXP_DEFAULT_LANGID;
286         }
287
288         //Try to find a suitable translation for the user's system language *and* country
289         QStringList languages = lamexp_query_translations();
290         while(!languages.isEmpty())
291         {
292                 QString currentLangId = languages.takeFirst();
293                 if(lamexp_translation_sysid(currentLangId) == systemLanguage.language())
294                 {
295                         if(lamexp_translation_country(currentLangId) == systemLanguage.country())
296                         {
297                                 m_defaultLanguage = new QString(currentLangId);
298                                 return currentLangId;
299                         }
300                 }
301         }
302
303         //Try to find a suitable translation for the user's system language
304         languages = lamexp_query_translations();
305         while(!languages.isEmpty())
306         {
307                 QString currentLangId = languages.takeFirst();
308                 if(lamexp_translation_sysid(currentLangId) == systemLanguage.language())
309                 {
310                         m_defaultLanguage = new QString(currentLangId);
311                         return currentLangId;
312                 }
313         }
314
315         //Fall back to the default translation
316         m_defaultLanguage = new QString(LAMEXP_DEFAULT_LANGID);
317         return LAMEXP_DEFAULT_LANGID;
318 }
319
320 QString SettingsModel::initDirectory(const QString &path)
321 {
322         if(path.isEmpty())
323         {
324                 return QString();
325         }
326
327         if(!QDir(path).exists())
328         {
329                 for(int i = 0; i < 32; i++)
330                 {
331                         if(QDir(path).mkpath(".")) break;
332                         Sleep(1);
333                 }
334         }
335
336         if(!QDir(path).exists())
337         {
338                 return QString();
339         }
340         
341         return QDir(path).canonicalPath();
342 }
343
344 ////////////////////////////////////////////////////////////
345 // Getter and Setter
346 ////////////////////////////////////////////////////////////
347
348 LAMEXP_MAKE_OPTION_I(licenseAccepted, 0)
349 LAMEXP_MAKE_OPTION_I(interfaceStyle, 0)
350 LAMEXP_MAKE_OPTION_I(compressionEncoder, 0)
351 LAMEXP_MAKE_OPTION_I(compressionRCMode, 0)
352 LAMEXP_MAKE_OPTION_I(compressionBitrate, 7)
353 LAMEXP_MAKE_OPTION_S(outputDir, QDesktopServices::storageLocation(QDesktopServices::MusicLocation))
354 LAMEXP_MAKE_OPTION_B(outputToSourceDir, false)
355 LAMEXP_MAKE_OPTION_B(prependRelativeSourcePath, false)
356 LAMEXP_MAKE_OPTION_S(favoriteOutputFolders, QString());
357 LAMEXP_MAKE_OPTION_B(writeMetaTags, true)
358 LAMEXP_MAKE_OPTION_B(createPlaylist, true)
359 LAMEXP_MAKE_OPTION_S(autoUpdateLastCheck, "Never")
360 LAMEXP_MAKE_OPTION_B(autoUpdateEnabled, true)
361 LAMEXP_MAKE_OPTION_B(autoUpdateCheckBeta, false)
362 LAMEXP_MAKE_OPTION_B(soundsEnabled, true)
363 LAMEXP_MAKE_OPTION_B(neroAacNotificationsEnabled, true)
364 LAMEXP_MAKE_OPTION_B(antivirNotificationsEnabled, true)
365 LAMEXP_MAKE_OPTION_B(dropBoxWidgetEnabled, true)
366 LAMEXP_MAKE_OPTION_B(shellIntegrationEnabled, !lamexp_portable_mode())
367 LAMEXP_MAKE_OPTION_S(currentLanguage, defaultLanguage())
368 LAMEXP_MAKE_OPTION_I(lameAlgoQuality, 3)
369 LAMEXP_MAKE_OPTION_I(lameChannelMode, 0)
370 LAMEXP_MAKE_OPTION_B(forceStereoDownmix, false)
371 LAMEXP_MAKE_OPTION_B(bitrateManagementEnabled, false)
372 LAMEXP_MAKE_OPTION_I(bitrateManagementMinRate, 32)
373 LAMEXP_MAKE_OPTION_I(bitrateManagementMaxRate, 500)
374 LAMEXP_MAKE_OPTION_I(samplingRate, 0)
375 LAMEXP_MAKE_OPTION_B(neroAACEnable2Pass, true)
376 LAMEXP_MAKE_OPTION_I(aacEncProfile, 0)
377 LAMEXP_MAKE_OPTION_I(aftenAudioCodingMode, 0);
378 LAMEXP_MAKE_OPTION_I(aftenDynamicRangeCompression, 5);
379 LAMEXP_MAKE_OPTION_B(aftenFastBitAllocation, false);
380 LAMEXP_MAKE_OPTION_I(aftenExponentSearchSize, 8);
381 LAMEXP_MAKE_OPTION_B(normalizationFilterEnabled, false)
382 LAMEXP_MAKE_OPTION_I(normalizationFilterMaxVolume, -50)
383 LAMEXP_MAKE_OPTION_I(normalizationFilterEqualizationMode, 0);
384 LAMEXP_MAKE_OPTION_I(toneAdjustBass, 0)
385 LAMEXP_MAKE_OPTION_I(toneAdjustTreble, 0)
386 LAMEXP_MAKE_OPTION_S(customParametersLAME, QString());
387 LAMEXP_MAKE_OPTION_S(customParametersOggEnc, QString());
388 LAMEXP_MAKE_OPTION_S(customParametersAacEnc, QString());
389 LAMEXP_MAKE_OPTION_S(customParametersAften, QString());
390 LAMEXP_MAKE_OPTION_S(customParametersFLAC, QString());
391 LAMEXP_MAKE_OPTION_B(renameOutputFilesEnabled, false);
392 LAMEXP_MAKE_OPTION_S(renameOutputFilesPattern, "[<TrackNo>] <Artist> - <Title>");
393 LAMEXP_MAKE_OPTION_U(metaInfoPosition, UINT_MAX);
394 LAMEXP_MAKE_OPTION_U(maximumInstances, 0);
395 LAMEXP_MAKE_OPTION_S(customTempPath, QDesktopServices::storageLocation(QDesktopServices::TempLocation));
396 LAMEXP_MAKE_OPTION_B(customTempPathEnabled, false);
397 LAMEXP_MAKE_OPTION_B(slowStartup, false);
398 LAMEXP_MAKE_OPTION_S(mostRecentInputPath, QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
399 LAMEXP_MAKE_OPTION_B(hibernateComputer, false);