Some code refactoring.
This commit is contained in:
parent
94d6d16ce3
commit
ceec5fa5e2
@ -34,7 +34,7 @@
|
|||||||
#define VER_LAMEXP_MINOR_LO 8
|
#define VER_LAMEXP_MINOR_LO 8
|
||||||
#define VER_LAMEXP_TYPE Beta
|
#define VER_LAMEXP_TYPE Beta
|
||||||
#define VER_LAMEXP_PATCH 1
|
#define VER_LAMEXP_PATCH 1
|
||||||
#define VER_LAMEXP_BUILD 1319
|
#define VER_LAMEXP_BUILD 1320
|
||||||
#define VER_LAMEXP_CONFG 1288
|
#define VER_LAMEXP_CONFG 1288
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -39,32 +39,125 @@
|
|||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
//SettingsCache Class
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class SettingsCache
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SettingsCache(QSettings *configFile) : m_configFile(configFile)
|
||||||
|
{
|
||||||
|
m_cache = new QHash<QString, QVariant>();
|
||||||
|
m_cacheLock = new QMutex();
|
||||||
|
m_cacheDirty = new QSet<QString>();
|
||||||
|
}
|
||||||
|
|
||||||
|
~SettingsCache(void)
|
||||||
|
{
|
||||||
|
flushValues();
|
||||||
|
|
||||||
|
LAMEXP_DELETE(m_cache);
|
||||||
|
LAMEXP_DELETE(m_cacheDirty);
|
||||||
|
LAMEXP_DELETE(m_cacheLock);
|
||||||
|
LAMEXP_DELETE(m_configFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void storeValue(const QString &key, const QVariant &value)
|
||||||
|
{
|
||||||
|
QMutexLocker lock(m_cacheLock);
|
||||||
|
|
||||||
|
if(!m_cache->contains(key))
|
||||||
|
{
|
||||||
|
m_cache->insert(key, value);
|
||||||
|
m_cacheDirty->insert(key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(m_cache->value(key) != value)
|
||||||
|
{
|
||||||
|
m_cache->insert(key, value);
|
||||||
|
m_cacheDirty->insert(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QVariant loadValue(const QString &key, const QVariant &defaultValue) const
|
||||||
|
{
|
||||||
|
QMutexLocker lock(m_cacheLock);
|
||||||
|
|
||||||
|
if(!m_cache->contains(key))
|
||||||
|
{
|
||||||
|
const QVariant storedValue = m_configFile->value(key, defaultValue);
|
||||||
|
m_cache->insert(key, storedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_cache->value(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void flushValues(void)
|
||||||
|
{
|
||||||
|
QMutexLocker lock(m_cacheLock);
|
||||||
|
|
||||||
|
if(!m_cacheDirty->isEmpty())
|
||||||
|
{
|
||||||
|
QSet<QString>::ConstIterator iter;
|
||||||
|
for(iter = m_cacheDirty->constBegin(); iter != m_cacheDirty->constEnd(); iter++)
|
||||||
|
{
|
||||||
|
if(m_cache->contains(*iter))
|
||||||
|
{
|
||||||
|
m_configFile->setValue((*iter), m_cache->value(*iter));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qWarning("Could not find '%s' in cache, but it has been marked as dirty!", (*iter).toUtf8().constData());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_configFile->sync();
|
||||||
|
m_cacheDirty->clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QSettings *m_configFile;
|
||||||
|
QHash<QString, QVariant> *m_cache;
|
||||||
|
QSet<QString> *m_cacheDirty;
|
||||||
|
QMutex *m_cacheLock;
|
||||||
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
//Macros
|
//Macros
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#define LAMEXP_MAKE_OPTION_I(OPT,DEF) \
|
#define LAMEXP_MAKE_OPTION_I(OPT,DEF) \
|
||||||
int SettingsModel::OPT(void) const { return loadValue(g_settingsId_##OPT, (DEF)).toInt(); } \
|
int SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toInt(); } \
|
||||||
void SettingsModel::OPT(int value) { storeValue(g_settingsId_##OPT, value); } \
|
void SettingsModel::OPT(int value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
|
||||||
int SettingsModel::OPT##Default(void) { return (DEF); }
|
int SettingsModel::OPT##Default(void) { return (DEF); }
|
||||||
|
|
||||||
#define LAMEXP_MAKE_OPTION_S(OPT,DEF) \
|
#define LAMEXP_MAKE_OPTION_S(OPT,DEF) \
|
||||||
QString SettingsModel::OPT(void) const { return loadValue(g_settingsId_##OPT, (DEF)).toString().trimmed(); } \
|
QString SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toString().trimmed(); } \
|
||||||
void SettingsModel::OPT(const QString &value) { storeValue(g_settingsId_##OPT, value); } \
|
void SettingsModel::OPT(const QString &value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
|
||||||
QString SettingsModel::OPT##Default(void) { return (DEF); }
|
QString SettingsModel::OPT##Default(void) { return (DEF); }
|
||||||
|
|
||||||
#define LAMEXP_MAKE_OPTION_B(OPT,DEF) \
|
#define LAMEXP_MAKE_OPTION_B(OPT,DEF) \
|
||||||
bool SettingsModel::OPT(void) const { return loadValue(g_settingsId_##OPT, (DEF)).toBool(); } \
|
bool SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toBool(); } \
|
||||||
void SettingsModel::OPT(bool value) { storeValue(g_settingsId_##OPT, value); } \
|
void SettingsModel::OPT(bool value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
|
||||||
bool SettingsModel::OPT##Default(void) { return (DEF); }
|
bool SettingsModel::OPT##Default(void) { return (DEF); }
|
||||||
|
|
||||||
#define LAMEXP_MAKE_OPTION_U(OPT,DEF) \
|
#define LAMEXP_MAKE_OPTION_U(OPT,DEF) \
|
||||||
unsigned int SettingsModel::OPT(void) const { return loadValue(g_settingsId_##OPT, (DEF)).toUInt(); } \
|
unsigned int SettingsModel::OPT(void) const { return m_configCache->loadValue(g_settingsId_##OPT, (DEF)).toUInt(); } \
|
||||||
void SettingsModel::OPT(unsigned int value) { storeValue(g_settingsId_##OPT, value); } \
|
void SettingsModel::OPT(unsigned int value) { m_configCache->storeValue(g_settingsId_##OPT, value); } \
|
||||||
unsigned int SettingsModel::OPT##Default(void) { return (DEF); }
|
unsigned int SettingsModel::OPT##Default(void) { return (DEF); }
|
||||||
|
|
||||||
#define LAMEXP_MAKE_ID(DEC,STR) static const char *g_settingsId_##DEC = STR
|
#define LAMEXP_MAKE_ID(DEC,STR) static const char *g_settingsId_##DEC = STR
|
||||||
#define REMOVE_GROUP(OBJ,ID) OBJ->beginGroup(ID); OBJ->remove(""); OBJ->endGroup();
|
|
||||||
|
#define REMOVE_GROUP(OBJ,ID) do \
|
||||||
|
{ \
|
||||||
|
OBJ->beginGroup(ID); \
|
||||||
|
OBJ->remove(""); \
|
||||||
|
OBJ->endGroup(); \
|
||||||
|
} \
|
||||||
|
while(0)
|
||||||
|
|
||||||
#define DIR_EXISTS(PATH) (QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
|
#define DIR_EXISTS(PATH) (QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
|
||||||
|
|
||||||
@ -194,15 +287,10 @@ SettingsModel::SettingsModel(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create the cache
|
|
||||||
m_cache = new QHash<QString, QVariant>();
|
|
||||||
m_cacheLock = new QMutex();
|
|
||||||
m_cacheDirty = new QSet<QString>();
|
|
||||||
|
|
||||||
//Create settings
|
//Create settings
|
||||||
m_configFile = new QSettings(configPath, QSettings::IniFormat);
|
QSettings *configFile = new QSettings(configPath, QSettings::IniFormat);
|
||||||
const QString groupKey = QString().sprintf("LameXP_%u%02u%05u", lamexp_version_major(), lamexp_version_minor(), lamexp_version_confg());
|
const QString groupKey = QString().sprintf("LameXP_%u%02u%05u", lamexp_version_major(), lamexp_version_minor(), lamexp_version_confg());
|
||||||
QStringList childGroups = m_configFile->childGroups();
|
QStringList childGroups =configFile->childGroups();
|
||||||
|
|
||||||
//Clean-up settings
|
//Clean-up settings
|
||||||
while(!childGroups.isEmpty())
|
while(!childGroups.isEmpty())
|
||||||
@ -219,13 +307,16 @@ SettingsModel::SettingsModel(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
qWarning("Deleting obsolete group from config: %s", current.toUtf8().constData());
|
qWarning("Deleting obsolete group from config: %s", current.toUtf8().constData());
|
||||||
REMOVE_GROUP(m_configFile, current);
|
REMOVE_GROUP(configFile, current);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Setup settings
|
//Setup settings
|
||||||
m_configFile->beginGroup(groupKey);
|
configFile->beginGroup(groupKey);
|
||||||
m_configFile->setValue(g_settingsId_versionNumber, QApplication::applicationVersion());
|
configFile->setValue(g_settingsId_versionNumber, QApplication::applicationVersion());
|
||||||
m_configFile->sync();
|
configFile->sync();
|
||||||
|
|
||||||
|
//Create the cache
|
||||||
|
m_configCache = new SettingsCache(configFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
@ -234,12 +325,7 @@ SettingsModel::SettingsModel(void)
|
|||||||
|
|
||||||
SettingsModel::~SettingsModel(void)
|
SettingsModel::~SettingsModel(void)
|
||||||
{
|
{
|
||||||
flushValues();
|
LAMEXP_DELETE(m_configCache);
|
||||||
|
|
||||||
LAMEXP_DELETE(m_cache);
|
|
||||||
LAMEXP_DELETE(m_cacheDirty);
|
|
||||||
LAMEXP_DELETE(m_cacheLock);
|
|
||||||
LAMEXP_DELETE(m_configFile);
|
|
||||||
LAMEXP_DELETE(m_defaultLanguage);
|
LAMEXP_DELETE(m_defaultLanguage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,7 +415,7 @@ void SettingsModel::validate(void)
|
|||||||
|
|
||||||
void SettingsModel::syncNow(void)
|
void SettingsModel::syncNow(void)
|
||||||
{
|
{
|
||||||
flushValues();
|
m_configCache->flushValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
@ -341,7 +427,8 @@ QString *SettingsModel::m_defaultLanguage = NULL;
|
|||||||
QString SettingsModel::defaultLanguage(void) const
|
QString SettingsModel::defaultLanguage(void) const
|
||||||
{
|
{
|
||||||
QReadLocker readLock(&s_lock);
|
QReadLocker readLock(&s_lock);
|
||||||
|
|
||||||
|
//Default already initialized?
|
||||||
if(m_defaultLanguage)
|
if(m_defaultLanguage)
|
||||||
{
|
{
|
||||||
return *m_defaultLanguage;
|
return *m_defaultLanguage;
|
||||||
@ -351,6 +438,12 @@ QString SettingsModel::defaultLanguage(void) const
|
|||||||
readLock.unlock();
|
readLock.unlock();
|
||||||
QWriteLocker writeLock(&s_lock);
|
QWriteLocker writeLock(&s_lock);
|
||||||
|
|
||||||
|
//Default still not initialized?
|
||||||
|
if(m_defaultLanguage)
|
||||||
|
{
|
||||||
|
return *m_defaultLanguage;
|
||||||
|
}
|
||||||
|
|
||||||
//Detect system langauge
|
//Detect system langauge
|
||||||
QLocale systemLanguage= QLocale::system();
|
QLocale systemLanguage= QLocale::system();
|
||||||
qDebug("[Locale]");
|
qDebug("[Locale]");
|
||||||
@ -438,65 +531,6 @@ QString SettingsModel::initDirectory(const QString &path) const
|
|||||||
return QDir(path).canonicalPath();
|
return QDir(path).canonicalPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
|
||||||
// Cache support
|
|
||||||
////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void SettingsModel::storeValue(const QString &key, const QVariant &value)
|
|
||||||
{
|
|
||||||
QMutexLocker lock(m_cacheLock);
|
|
||||||
|
|
||||||
if(!m_cache->contains(key))
|
|
||||||
{
|
|
||||||
m_cache->insert(key, value);
|
|
||||||
m_cacheDirty->insert(key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(m_cache->value(key) != value)
|
|
||||||
{
|
|
||||||
m_cache->insert(key, value);
|
|
||||||
m_cacheDirty->insert(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant SettingsModel::loadValue(const QString &key, const QVariant &defaultValue) const
|
|
||||||
{
|
|
||||||
QMutexLocker lock(m_cacheLock);
|
|
||||||
|
|
||||||
if(!m_cache->contains(key))
|
|
||||||
{
|
|
||||||
const QVariant storedValue = m_configFile->value(key, defaultValue);
|
|
||||||
m_cache->insert(key, storedValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_cache->value(key, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SettingsModel::flushValues(void)
|
|
||||||
{
|
|
||||||
QMutexLocker lock(m_cacheLock);
|
|
||||||
|
|
||||||
if(!m_cacheDirty->isEmpty())
|
|
||||||
{
|
|
||||||
QSet<QString>::ConstIterator iter;
|
|
||||||
for(iter = m_cacheDirty->constBegin(); iter != m_cacheDirty->constEnd(); iter++)
|
|
||||||
{
|
|
||||||
if(m_cache->contains(*iter))
|
|
||||||
{
|
|
||||||
m_configFile->setValue((*iter), m_cache->value(*iter));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qWarning("Could not find '%s' in cache, but it has been marked as dirty!", (*iter).toUtf8().constData());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_configFile->sync();
|
|
||||||
m_cacheDirty->clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Getter and Setter
|
// Getter and Setter
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
|
@ -21,13 +21,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class QSettings;
|
|
||||||
class QString;
|
class QString;
|
||||||
class QVariant;
|
class SettingsCache;
|
||||||
class QMutex;
|
|
||||||
|
|
||||||
template<class K, class V> class QHash;
|
|
||||||
template<class T> class QSet;
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@ -173,20 +168,13 @@ public:
|
|||||||
void syncNow(void);
|
void syncNow(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QSettings *m_configFile;
|
SettingsCache *m_configCache;
|
||||||
QHash<QString, QVariant> *m_cache;
|
|
||||||
QSet<QString> *m_cacheDirty;
|
|
||||||
QMutex *m_cacheLock;
|
|
||||||
|
|
||||||
static QString *m_defaultLanguage;
|
|
||||||
|
|
||||||
inline void storeValue(const QString &key, const QVariant &value);
|
|
||||||
inline QVariant loadValue(const QString &key, const QVariant &defaultValue) const;
|
|
||||||
inline void flushValues(void);
|
|
||||||
|
|
||||||
QString initDirectory(const QString &path) const;
|
QString initDirectory(const QString &path) const;
|
||||||
QString defaultLanguage(void) const;
|
QString defaultLanguage(void) const;
|
||||||
QString defaultDirectory(void) const;
|
QString defaultDirectory(void) const;
|
||||||
|
|
||||||
|
static QString *m_defaultLanguage;
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
x
Reference in New Issue
Block a user