Refactored selection of the "data" directory into a global function and killed some Copy&Paste code.

This commit is contained in:
LoRd_MuldeR 2012-02-10 18:40:28 +01:00
parent 5b01c67785
commit 9f2a0c4f10
4 changed files with 37 additions and 7 deletions

View File

@ -36,6 +36,7 @@
#include <QSysInfo> #include <QSysInfo>
#include <QStringList> #include <QStringList>
#include <QSystemSemaphore> #include <QSystemSemaphore>
#include <QDesktopServices>
#include <QMutex> #include <QMutex>
#include <QTextCodec> #include <QTextCodec>
#include <QLibrary> #include <QLibrary>
@ -391,6 +392,34 @@ bool x264_portable(void)
return portable; return portable;
} }
/*
* Get data path (i.e. path to store config files)
*/
const QString &x264_data_path(void)
{
static QString *pathCache = NULL;
if(!pathCache)
{
pathCache = new QString();
if(!x264_portable())
{
*pathCache = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
}
if(pathCache->isEmpty() || x264_portable())
{
*pathCache = QApplication::applicationDirPath();
}
if(!QDir(*pathCache).mkpath("."))
{
qWarning("Data directory could not be created:\n%s\n", pathCache->toUtf8().constData());
*pathCache = QDir::currentPath();
}
}
return *pathCache;
}
/* /*
* Get build date date * Get build date date
*/ */

View File

@ -100,6 +100,7 @@ unsigned int x264_version_minor(void);
unsigned int x264_version_patch(void); unsigned int x264_version_patch(void);
const QDate &x264_version_date(void); const QDate &x264_version_date(void);
bool x264_portable(void); bool x264_portable(void);
const QString &x264_data_path(void);
bool x264_is_prerelease(void); bool x264_is_prerelease(void);
const char *x264_version_time(void); const char *x264_version_time(void);
const char *x264_version_compiler(void); const char *x264_version_compiler(void);

View File

@ -83,7 +83,7 @@ bool OptionsModel::equals(OptionsModel *model)
bool OptionsModel::saveTemplate(OptionsModel *model, const QString &name) bool OptionsModel::saveTemplate(OptionsModel *model, const QString &name)
{ {
const QString templateName = name.simplified(); const QString templateName = name.simplified();
const QString appDir = x264_portable() ? QApplication::applicationDirPath() : QDesktopServices::storageLocation(QDesktopServices::DataLocation); const QString appDir = x264_data_path();
if(templateName.contains('\\') || templateName.contains('/')) if(templateName.contains('\\') || templateName.contains('/'))
{ {
@ -109,7 +109,7 @@ bool OptionsModel::saveTemplate(OptionsModel *model, const QString &name)
bool OptionsModel::loadTemplate(OptionsModel *model, const QString &name) bool OptionsModel::loadTemplate(OptionsModel *model, const QString &name)
{ {
const QString appDir = x264_portable() ? QApplication::applicationDirPath() : QDesktopServices::storageLocation(QDesktopServices::DataLocation); const QString appDir = x264_data_path();
if(name.contains('\\') || name.contains('/')) if(name.contains('\\') || name.contains('/'))
{ {
@ -146,7 +146,7 @@ bool OptionsModel::loadTemplate(OptionsModel *model, const QString &name)
QMap<QString, OptionsModel*> OptionsModel::loadAllTemplates(void) QMap<QString, OptionsModel*> OptionsModel::loadAllTemplates(void)
{ {
QMap<QString, OptionsModel*> list; QMap<QString, OptionsModel*> list;
const QString appDir = x264_portable() ? QApplication::applicationDirPath() : QDesktopServices::storageLocation(QDesktopServices::DataLocation); const QString appDir = x264_data_path();
QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat); QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
QStringList allTemplates = settings.childGroups(); QStringList allTemplates = settings.childGroups();
@ -170,7 +170,7 @@ QMap<QString, OptionsModel*> OptionsModel::loadAllTemplates(void)
bool OptionsModel::templateExists(const QString &name) bool OptionsModel::templateExists(const QString &name)
{ {
const QString appDir = x264_portable() ? QApplication::applicationDirPath() : QDesktopServices::storageLocation(QDesktopServices::DataLocation); const QString appDir = x264_data_path();
QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat); QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
QStringList allGroups = settings.childGroups(); QStringList allGroups = settings.childGroups();
return allGroups.contains(name, Qt::CaseInsensitive); return allGroups.contains(name, Qt::CaseInsensitive);
@ -178,7 +178,7 @@ bool OptionsModel::templateExists(const QString &name)
bool OptionsModel::deleteTemplate(const QString &name) bool OptionsModel::deleteTemplate(const QString &name)
{ {
const QString appDir = x264_portable() ? QApplication::applicationDirPath() : QDesktopServices::storageLocation(QDesktopServices::DataLocation); const QString appDir = x264_data_path();
QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat); QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
if(settings.childGroups().contains(name, Qt::CaseInsensitive)) if(settings.childGroups().contains(name, Qt::CaseInsensitive))

View File

@ -130,7 +130,7 @@ void PreferencesDialog::initPreferences(Preferences *preferences)
void PreferencesDialog::loadPreferences(Preferences *preferences) void PreferencesDialog::loadPreferences(Preferences *preferences)
{ {
const QString appDir = x264_portable() ? QApplication::applicationDirPath() : QDesktopServices::storageLocation(QDesktopServices::DataLocation); const QString appDir = x264_data_path();
QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat); QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
Preferences defaults; Preferences defaults;
@ -145,7 +145,7 @@ void PreferencesDialog::loadPreferences(Preferences *preferences)
void PreferencesDialog::savePreferences(Preferences *preferences) void PreferencesDialog::savePreferences(Preferences *preferences)
{ {
const QString appDir = x264_portable() ? QApplication::applicationDirPath() : QDesktopServices::storageLocation(QDesktopServices::DataLocation); const QString appDir = x264_data_path();
QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat); QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
settings.beginGroup("preferences"); settings.beginGroup("preferences");