From 599f925d582438a5676698f4d03913bed22248e9 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Wed, 2 Mar 2016 16:45:59 +0100 Subject: [PATCH] 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. --- src/board/UBBoardController.cpp | 1 + src/core/UBSettings.cpp | 59 ++++++++++++++++++++++++++++++--- src/core/UBSettings.h | 4 +++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 87288625..fb19c166 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1777,6 +1777,7 @@ void UBBoardController::autosaveTimeout() } saveData(sf_showProgress); + UBSettings::settings()->save(); } void UBBoardController::appMainModeChanged(UBApplicationController::MainMode md) diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index 3e439dcd..da9b46e7 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -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::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(); } diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index adf3d996..a1c4fecf 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -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 mSettingsQueue; + static const int sDefaultFontPixelSize; static const char *sDefaultFontFamily;