New settings queue for UBSettings

This should cut down on disk access. Instead of loading and saving
settings directly through QSettings instances (which occasionally
read and write to their associated file; but there is no way to control
how often this happens), they are now added to a QHash for in-app
access.

Save() and load() functions were also added to enable manually saving
the settings, and loading all settings from file, respectively.
preferencesAboutTextFull
Craig Watson 9 years ago
parent 8e1b5c4ee0
commit 599f925d58
  1. 1
      src/board/UBBoardController.cpp
  2. 59
      src/core/UBSettings.cpp
  3. 4
      src/core/UBSettings.h

@ -1777,6 +1777,7 @@ void UBBoardController::autosaveTimeout()
}
saveData(sf_showProgress);
UBSettings::settings()->save();
}
void UBBoardController::appMainModeChanged(UBApplicationController::MainMode md)

@ -118,8 +118,10 @@ QString UBSettings::appPingMessage = "__uniboard_ping";
UBSettings* UBSettings::settings()
{
if (!sSingleton)
if (!sSingleton) {
sSingleton = new UBSettings(qApp);
sSingleton->load();
}
return sSingleton;
}
@ -146,9 +148,10 @@ QSettings* UBSettings::getAppSettings()
UBSettings::sAppSettings = new QSettings(appSettings, QSettings::IniFormat, 0);
UBSettings::sAppSettings->setIniCodec("utf-8");
qDebug() << "sAppSettings location: " << appSettings;
}
qDebug() << "sAppSettings" << sAppSettings;
return UBSettings::sAppSettings;
}
@ -421,12 +424,19 @@ void UBSettings::init()
}
/**
* @brief Returns the value for the *key* setting, or *defaultValue* if the key doesn't exist
*/
QVariant UBSettings::value ( const QString & key, const QVariant & defaultValue) const
{
// Check first the settings queue, then the app settings, then the user settings.
// If the key exists in neither of these, then defaultValue is returned.
if (mSettingsQueue.contains(key))
return mSettingsQueue.value(key);
if (!sAppSettings->contains(key) && !(defaultValue == QVariant()))
{
sAppSettings->setValue(key, defaultValue);
}
return mUserSettings->value(key, sAppSettings->value(key, defaultValue));
}
@ -434,9 +444,47 @@ QVariant UBSettings::value ( const QString & key, const QVariant & defaultValue)
void UBSettings::setValue (const QString & key, const QVariant & value)
{
mUserSettings->setValue(key, value);
// Save the setting to the queue only; a call to save() is necessary to persist the settings
mSettingsQueue[key] = value;
}
/**
* @brief Save all the queued settings to disk
*/
void UBSettings::save()
{
// TODO: move this to a thread if it is too slow
QHash<QString, QVariant>::const_iterator it = mSettingsQueue.constBegin();
while (it != mSettingsQueue.constEnd()) {
mUserSettings->setValue(it.key(), it.value());
++it;
}
// Force save to file
mUserSettings->sync();
qDebug() << "User settings saved";
}
/**
* @brief Force load all settings, to cut down on subsequent file access
*/
void UBSettings::load()
{
qDebug() << "Loading all settings";
QStringList keyList = mUserSettings->allKeys() + sAppSettings->allKeys();
keyList.removeDuplicates();
foreach(const QString& key, keyList) {
QVariant val = mUserSettings->value(key);
if (val != QVariant())
setValue(key, val);
}
}
int UBSettings::penWidthIndex()
{
@ -1264,6 +1312,7 @@ QString UBSettings::replaceWildcard(QString& path)
void UBSettings::closing()
{
save();
cleanNonPersistentSettings();
}

@ -57,6 +57,8 @@ class UBSettings : public QObject
void InitKeyboardPaletteKeyBtnSizes();
void ValidateKeyboardPaletteKeyBtnSize();
void closing();
void save();
void load();
int penWidthIndex();
@ -410,6 +412,8 @@ class UBSettings : public QObject
QSettings* mAppSettings;
QSettings* mUserSettings;
QHash<QString, QVariant> mSettingsQueue;
static const int sDefaultFontPixelSize;
static const char *sDefaultFontFamily;

Loading…
Cancel
Save