From 599f925d582438a5676698f4d03913bed22248e9 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Wed, 2 Mar 2016 16:45:59 +0100 Subject: [PATCH 1/5] 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; From 85acf0d64367d3f68aaaa43bcf625c3c169c0a5d Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Fri, 4 Mar 2016 10:31:21 +0100 Subject: [PATCH 2/5] Settings: save values to queue as they are requested UBSettings::value() now saves the requested value to the settings "queue" (hash table) for faster subsequent access --- src/core/UBSettings.cpp | 25 ++++++++++++++++++------- src/core/UBSettings.h | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index da9b46e7..f567a0f5 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -426,8 +426,14 @@ void UBSettings::init() /** * @brief Returns the value for the *key* setting, or *defaultValue* if the key doesn't exist + * + * The value is also added to the local settings queue, to prevent future disk I/O when accessing + * that same setting. + * + * If the value doesn't exist in the application settings (i.e it was not present in the config file), + * it is also added there. */ -QVariant UBSettings::value ( const QString & key, const QVariant & defaultValue) const +QVariant UBSettings::value ( const QString & key, const QVariant & defaultValue) { // 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. @@ -435,10 +441,17 @@ QVariant UBSettings::value ( const QString & key, const QVariant & defaultValue) if (mSettingsQueue.contains(key)) return mSettingsQueue.value(key); + // If the setting doesn't exist in the App settings, add it there if (!sAppSettings->contains(key) && !(defaultValue == QVariant())) sAppSettings->setValue(key, defaultValue); - return mUserSettings->value(key, sAppSettings->value(key, defaultValue)); + QVariant val = mUserSettings->value(key, sAppSettings->value(key, defaultValue)); + + // If we got here, then the settings queue doesn't contain the value; add it + mSettingsQueue[key] = val; + + + return val; } @@ -453,8 +466,6 @@ void UBSettings::setValue (const QString & key, const QVariant & value) */ void UBSettings::save() { - // TODO: move this to a thread if it is too slow - QHash::const_iterator it = mSettingsQueue.constBegin(); while (it != mSettingsQueue.constEnd()) { @@ -480,9 +491,9 @@ void UBSettings::load() keyList.removeDuplicates(); foreach(const QString& key, keyList) { - QVariant val = mUserSettings->value(key); - if (val != QVariant()) - setValue(key, val); + value(key); + // value() actually handles saving the value to the queue, so + // we don't need to do it here } } diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index a1c4fecf..6f3a8c1f 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -399,7 +399,7 @@ class UBSettings : public QObject void setPenPressureSensitive(bool sensitive); void setMarkerPressureSensitive(bool sensitive); - QVariant value ( const QString & key, const QVariant & defaultValue = QVariant() ) const; + QVariant value ( const QString & key, const QVariant & defaultValue = QVariant() ); void setValue (const QString & key,const QVariant & value); void colorChanged() { emit colorContextChanged(); } From 46ce553d75c6e0a79e79be085dccc55068559e08 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Fri, 4 Mar 2016 14:31:25 +0100 Subject: [PATCH 3/5] Check settings at launch to avoid crashing Some settings were changed between v1.02 and 1.10 (current), and some of these changes cause OpenBoard to crash at launch. This commit adds a function to check for these specific new settings, and wipe the old values if they are found. This avoids problems when the user upgrades from 1.02 without deleting their configuration file. (This is an alternative to having a post-install script, which would be ineffective in a multi-user configuration) --- src/core/UBSetting.h | 5 +++ src/core/UBSettings.cpp | 86 ++++++++++++++++++++++++++++++++++++++--- src/core/UBSettings.h | 3 ++ 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/src/core/UBSetting.h b/src/core/UBSetting.h index 1294a82c..01ac9f35 100644 --- a/src/core/UBSetting.h +++ b/src/core/UBSetting.h @@ -62,6 +62,11 @@ class UBSetting : public QObject return mPath; } + virtual QVariant defaultValue() const + { + return mDefaultValue; + } + public slots: void setBool(bool pValue); diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index f567a0f5..23e2fe27 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -226,13 +226,13 @@ void UBSettings::init() appPreferredLanguage = new UBSetting(this,"App","PreferredLanguage", ""); rightLibPaletteBoardModeWidth = new UBSetting(this, "Board", "RightLibPaletteBoardModeWidth", 270); - rightLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteBoardModeIsCollapsed",false); + rightLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteBoardModeIsCollapsed",true); rightLibPaletteDesktopModeWidth = new UBSetting(this, "Board", "RightLibPaletteDesktopModeWidth", 270); - rightLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteDesktopModeIsCollapsed",false); + rightLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteDesktopModeIsCollapsed",true); leftLibPaletteBoardModeWidth = new UBSetting(this, "Board", "LeftLibPaletteBoardModeWidth",270); - leftLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteBoardModeIsCollapsed",false); + leftLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteBoardModeIsCollapsed",true); leftLibPaletteDesktopModeWidth = new UBSetting(this, "Board", "LeftLibPaletteDesktopModeWidth",270); - leftLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteDesktopModeIsCollapsed",false); + leftLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteDesktopModeIsCollapsed",true); appIsInSoftwareUpdateProcess = new UBSetting(this, "App", "IsInSoftwareUpdateProcess", false); appLastSessionDocumentUUID = new UBSetting(this, "App", "LastSessionDocumentUUID", ""); @@ -268,11 +268,11 @@ void UBSettings::init() pageDpi = new UBSetting(this, "Board", "pageDpi", 0); QStringList penLightBackgroundColors; - penLightBackgroundColors << "#000000" << "#FF0000" <<"#004080" << "#008000" << "#FFDD00" << "#C87400" << "#800040" << "#008080" << "#5F2D0A"; + penLightBackgroundColors << "#000000" << "#FF0000" <<"#004080" << "#008000" << "#FFDD00" << "#C87400" << "#800040" << "#008080" << "#5F2D0A" << "#FFFFFF"; boardPenLightBackgroundColors = new UBColorListSetting(this, "Board", "PenLightBackgroundColors", penLightBackgroundColors, 1.0); QStringList penDarkBackgroundColors; - penDarkBackgroundColors << "#FFFFFF" << "#FF3400" <<"#66C0FF" << "#81FF5C" << "#FFFF00" << "#B68360" << "#FF497E" << "#8D69FF"; + penDarkBackgroundColors << "#FFFFFF" << "#FF3400" <<"#66C0FF" << "#81FF5C" << "#FFFF00" << "#B68360" << "#FF497E" << "#8D69FF" << "#000000"; boardPenDarkBackgroundColors = new UBColorListSetting(this, "Board", "PenDarkBackgroundColors", penDarkBackgroundColors, 1.0); boardMarkerAlpha = new UBSetting(this, "Board", "MarkerAlpha", 0.5); @@ -421,6 +421,7 @@ void UBSettings::init() useSystemOnScreenKeyboard = new UBSetting(this, "App", "UseSystemOnScreenKeyboard", true); cleanNonPersistentSettings(); + checkNewSettings(); } @@ -1339,3 +1340,76 @@ void UBSettings::cleanNonPersistentSettings() youTubeUserEMail->set(QVariant("")); } } + +/** + * @brief Permanently remove a setting, from local memory and config files + * @param setting The setting to remove + */ +void UBSettings::removeSetting(const QString &setting) +{ + if (sAppSettings->contains(setting)) + sAppSettings->remove(setting); + + if (mUserSettings->contains(setting)) + mUserSettings->remove(setting); + + if (mSettingsQueue.contains(setting)) + mSettingsQueue.remove(setting); +} + +void UBSettings::checkNewSettings() +{ + /* + * Some settings were modified in new versions and OpenBoard can crash + * if an old settings file is used. This function checks these settings and + * if necessary, resets them to the values initialized in UBSettings::init(). + * + * Thus this method can be removed when it is no longer deemed useful. + */ + + // OB 1.10 introduced an extra pen color; for simplicity, if the old settings + // are still present (i.e only 4 selected pen colors), we just reset all color settings. + // Having too few colors actually causes OpenBoard to crash, hence these measures. + + QList colorSettings; + colorSettings << boardPenDarkBackgroundSelectedColors + << boardPenLightBackgroundSelectedColors + << boardMarkerDarkBackgroundSelectedColors + << boardMarkerLightBackgroundSelectedColors; + + foreach (UBColorListSetting* setting, colorSettings) { + if (setting->colors().size() < 5) + setting->reset(); // Make sure that there are 5 selected colors + } + + colorSettings.clear(); + + // Next we check whether all the new colors were added; i.e if some default + // colors are not also in the config file, we add them to it. + + // This is not nearly as critical as the above issue, but the users (or admins) seemingly + // can't be trusted to delete or modify their own config file when upgrading, so they might + // wonder why they don't have the new colors in their app. + + colorSettings << boardPenDarkBackgroundColors + << boardPenLightBackgroundColors + << boardMarkerDarkBackgroundColors + << boardMarkerLightBackgroundColors; + + foreach (UBColorListSetting* setting, colorSettings) { + QStringList defaultColors = qvariant_cast(setting->defaultValue()); + QStringList currentColors(qvariant_cast(setting->get())); // copy + + foreach (QString c, defaultColors) { + if (!currentColors.contains(c)) + currentColors.append(QString(c)); + } + + setting->set(currentColors); + } + + + // A typo was corrected in version 1.10 + removeSetting("Board/useSystemOnScreenKeybard"); + +} diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index 6f3a8c1f..a8e6745d 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -425,6 +425,9 @@ class UBSettings : public QObject static bool checkDirectory(QString& dirPath); static QString replaceWildcard(QString& path); + void removeSetting(const QString& setting); + void checkNewSettings(); + }; From 59207cbbf27c6ae034beddfb778e294df60a2a50 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Mon, 7 Mar 2016 14:23:44 +0100 Subject: [PATCH 4/5] Moved document metadata saving to UBPersistenceManager's worker thread (Reading is still done synchronously, for now at least) --- src/board/UBBoardController.cpp | 2 +- src/core/UBPersistenceManager.cpp | 58 ++++++++++++++++++++------- src/core/UBPersistenceManager.h | 3 +- src/core/UBPersistenceWorker.cpp | 16 +++++++- src/core/UBPersistenceWorker.h | 5 ++- src/document/UBDocumentController.cpp | 16 ++++---- src/document/UBDocumentProxy.cpp | 10 +++++ src/document/UBDocumentProxy.h | 2 + 8 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index fb19c166..6e10056d 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1552,7 +1552,7 @@ void UBBoardController::moveSceneToIndex(int source, int target) UBDocumentContainer::movePageToIndex(source, target); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(selectedDocument()); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(selectedDocument()); mMovingSceneIndex = source; setActiveDocumentScene(target); mMovingSceneIndex = -1; diff --git a/src/core/UBPersistenceManager.cpp b/src/core/UBPersistenceManager.cpp index 7114bfae..3f6606bf 100644 --- a/src/core/UBPersistenceManager.cpp +++ b/src/core/UBPersistenceManager.cpp @@ -79,14 +79,33 @@ UBPersistenceManager::UBPersistenceManager(QObject *pParent) mThread = new QThread; mWorker = new UBPersistenceWorker(); mWorker->moveToThread(mThread); - connect(mWorker, SIGNAL(error(QString)), this, SLOT(errorString(QString))); - connect(mThread, SIGNAL(started()), mWorker, SLOT(process())); - connect(mWorker, SIGNAL(finished()), mThread, SLOT(quit())); - connect(mWorker, SIGNAL(finished()), this, SLOT(onWorkerFinished())); - connect(mWorker, SIGNAL(finished()), mWorker, SLOT(deleteLater())); - connect(mThread, SIGNAL(finished()), mThread, SLOT(deleteLater())); - connect(mWorker,SIGNAL(sceneLoaded(QByteArray,UBDocumentProxy*,int)),this,SLOT(onSceneLoaded(QByteArray,UBDocumentProxy*,int))); - connect(mWorker,SIGNAL(scenePersisted(UBGraphicsScene*)),this,SLOT(onScenePersisted(UBGraphicsScene*))); + connect(mWorker, SIGNAL(error(QString)), + this, SLOT(errorString(QString))); + + connect(mThread, SIGNAL(started()), + mWorker, SLOT(process())); + + connect(mWorker, SIGNAL(finished()), + mThread, SLOT(quit())); + + connect(mWorker, SIGNAL(finished()), + this, SLOT(onWorkerFinished())); + + connect(mWorker, SIGNAL(finished()), + mWorker, SLOT(deleteLater())); + + connect(mThread, SIGNAL(finished()), + mThread, SLOT(deleteLater())); + + connect(mWorker, SIGNAL(sceneLoaded(QByteArray,UBDocumentProxy*,int)), + this, SLOT(onSceneLoaded(QByteArray,UBDocumentProxy*,int))); + + connect(mWorker, SIGNAL(scenePersisted(UBGraphicsScene*)), + this, SLOT(onScenePersisted(UBGraphicsScene*))); + + connect(mWorker, SIGNAL(metadataPersisted(UBDocumentProxy*)), + this, SLOT(onMetadataPersisted(UBDocumentProxy*))); + mThread->start(); } @@ -114,6 +133,11 @@ void UBPersistenceManager::onScenePersisted(UBGraphicsScene* scene) scene = NULL; } +void UBPersistenceManager::onMetadataPersisted(UBDocumentProxy* proxy) +{ + delete proxy; +} + void UBPersistenceManager::onWorkerFinished() { mIsWorkerFinished = true; @@ -752,14 +776,14 @@ void UBPersistenceManager::persistDocumentScene(UBDocumentProxy* pDocumentProxy, mSceneCache.insert(pDocumentProxy, pSceneIndex, pScene); if (pDocumentProxy->isModified()) - UBMetadataDcSubsetAdaptor::persist(pDocumentProxy); + persistDocumentMetadata(pDocumentProxy, forceImmediateSaving); if (pScene->isModified()) { UBThumbnailAdaptor::persistScene(pDocumentProxy, pScene, pSceneIndex); if(forceImmediateSaving) UBSvgSubsetAdaptor::persistScene(pDocumentProxy,pScene,pSceneIndex); - else{ + else { UBGraphicsScene* copiedScene = pScene->sceneDeepCopy(); mWorker->saveScene(pDocumentProxy, copiedScene, pSceneIndex); pScene->setModified(false); @@ -769,13 +793,17 @@ void UBPersistenceManager::persistDocumentScene(UBDocumentProxy* pDocumentProxy, } -UBDocumentProxy* UBPersistenceManager::persistDocumentMetadata(UBDocumentProxy* pDocumentProxy) +void UBPersistenceManager::persistDocumentMetadata(UBDocumentProxy* pDocumentProxy, bool forceImmediateSaving) { - UBMetadataDcSubsetAdaptor::persist(pDocumentProxy); - - emit documentMetadataChanged(pDocumentProxy); + if (forceImmediateSaving) { + UBMetadataDcSubsetAdaptor::persist(pDocumentProxy); + emit documentMetadataChanged(pDocumentProxy); + } - return pDocumentProxy; + else { + UBDocumentProxy* copy = pDocumentProxy->deepCopy(); + mWorker->saveMetadata(copy); + } } diff --git a/src/core/UBPersistenceManager.h b/src/core/UBPersistenceManager.h index 6a4de5bc..e2380493 100644 --- a/src/core/UBPersistenceManager.h +++ b/src/core/UBPersistenceManager.h @@ -64,7 +64,7 @@ class UBPersistenceManager : public QObject virtual UBDocumentProxy* createDocument(const QString& pGroupName = "", const QString& pName = "", bool withEmptyPage = true); virtual UBDocumentProxy* createDocumentFromDir(const QString& pDocumentDirectory, const QString& pGroupName = "", const QString& pName = ""); - virtual UBDocumentProxy* persistDocumentMetadata(UBDocumentProxy* pDocumentProxy); + virtual void persistDocumentMetadata(UBDocumentProxy* pDocumentProxy, bool forceImmediateSaving = false); virtual UBDocumentProxy* duplicateDocument(UBDocumentProxy* pDocumentProxy); @@ -161,6 +161,7 @@ class UBPersistenceManager : public QObject void onSceneLoaded(QByteArray,UBDocumentProxy*,int); void onWorkerFinished(); void onScenePersisted(UBGraphicsScene* scene); + void onMetadataPersisted(UBDocumentProxy* proxy); }; diff --git a/src/core/UBPersistenceWorker.cpp b/src/core/UBPersistenceWorker.cpp index 6127cb18..e19c09c9 100644 --- a/src/core/UBPersistenceWorker.cpp +++ b/src/core/UBPersistenceWorker.cpp @@ -25,6 +25,7 @@ #include "UBPersistenceWorker.h" #include "adaptors/UBSvgSubsetAdaptor.h" #include "adaptors/UBThumbnailAdaptor.h" +#include "adaptors/UBMetadataDcSubsetAdaptor.h" UBPersistenceWorker::UBPersistenceWorker(QObject *parent) : QObject(parent) @@ -48,6 +49,13 @@ void UBPersistenceWorker::readScene(UBDocumentProxy* proxy, const int pageIndex) mSemaphore.release(); } +void UBPersistenceWorker::saveMetadata(UBDocumentProxy *proxy) +{ + PersistenceInformation entry = {WriteMetadata, proxy, NULL, 0}; + saves.append(entry); + mSemaphore.release(); +} + void UBPersistenceWorker::applicationWillClose() { qDebug() << "applicaiton Will close signal received"; @@ -65,9 +73,15 @@ void UBPersistenceWorker::process() UBSvgSubsetAdaptor::persistScene(info.proxy, info.scene, info.sceneIndex); emit scenePersisted(info.scene); } - else{ + else if (info.action == ReadScene){ emit sceneLoaded(UBSvgSubsetAdaptor::loadSceneAsText(info.proxy,info.sceneIndex), info.proxy, info.sceneIndex); } + else if (info.action == WriteMetadata) { + if (info.proxy->isModified()) { + UBMetadataDcSubsetAdaptor::persist(info.proxy); + emit metadataPersisted(info.proxy); + } + } mSemaphore.acquire(); }while(!mReceivedApplicationClosing); qDebug() << "process will stop"; diff --git a/src/core/UBPersistenceWorker.h b/src/core/UBPersistenceWorker.h index eb3c273b..b4f77a0a 100644 --- a/src/core/UBPersistenceWorker.h +++ b/src/core/UBPersistenceWorker.h @@ -32,7 +32,8 @@ typedef enum{ WriteScene = 0, - ReadScene + ReadScene, + WriteMetadata }ActionType; typedef struct{ @@ -50,12 +51,14 @@ public: void saveScene(UBDocumentProxy* proxy, UBGraphicsScene* scene, const int pageIndex); void readScene(UBDocumentProxy* proxy, const int pageIndex); + void saveMetadata(UBDocumentProxy* proxy); signals: void finished(); void error(QString string); void sceneLoaded(QByteArray text,UBDocumentProxy* proxy, const int pageIndex); void scenePersisted(UBGraphicsScene* scene); + void metadataPersisted(UBDocumentProxy* proxy); public slots: void process(); diff --git a/src/document/UBDocumentController.cpp b/src/document/UBDocumentController.cpp index bf0cb0e8..8711eb6f 100644 --- a/src/document/UBDocumentController.cpp +++ b/src/document/UBDocumentController.cpp @@ -517,7 +517,7 @@ void UBDocumentController::duplicateSelectedItem() duplicatePages(selectedSceneIndexes); emit documentThumbnailsUpdated(this); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(selectedDocument()); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(selectedDocument()); mDocumentUI->thumbnailWidget->selectItemAt(selectedSceneIndexes.last() + selectedSceneIndexes.size()); } } @@ -534,7 +534,7 @@ void UBDocumentController::duplicateSelectedItem() UBDocumentProxy* duplicatedDoc = UBPersistenceManager::persistenceManager()->duplicateDocument(source); duplicatedDoc->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(duplicatedDoc); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(duplicatedDoc); selectDocument(duplicatedDoc, false); @@ -1104,7 +1104,7 @@ void UBDocumentController::addFolderOfImages() else { document->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(document); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(document); reloadThumbnails(); } } @@ -1150,7 +1150,7 @@ bool UBDocumentController::addFileToDocument(UBDocumentProxy* document) if (success) { document->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(document); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(document); } else { @@ -1169,7 +1169,7 @@ void UBDocumentController::moveSceneToIndex(UBDocumentProxy* proxy, int source, if (UBDocumentContainer::movePageToIndex(source, target)) { proxy->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(proxy); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(proxy); mDocumentUI->thumbnailWidget->hightlightItem(target); } @@ -1497,7 +1497,7 @@ void UBDocumentController::addToDocument() mDocumentUI->thumbnailWidget->selectItemAt(newActiveSceneIndex, false); selectDocument(mBoardController->selectedDocument()); mBoardController->selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(mBoardController->selectedDocument()); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(mBoardController->selectedDocument()); UBApplication::applicationController->showBoard(); } @@ -1678,7 +1678,7 @@ void UBDocumentController::addImages() else { document->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(document); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(document); reloadThumbnails(); } } @@ -1791,7 +1791,7 @@ void UBDocumentController::deletePages(QList itemsToDelete) UBDocumentContainer::deletePages(sceneIndexes); proxy->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBMetadataDcSubsetAdaptor::persist(proxy); + UBPersistenceManager::persistenceManager()->persistDocumentMetadata(proxy); int minIndex = proxy->pageCount() - 1; foreach (int i, sceneIndexes) diff --git a/src/document/UBDocumentProxy.cpp b/src/document/UBDocumentProxy.cpp index d5d54d6d..d8c04b49 100644 --- a/src/document/UBDocumentProxy.cpp +++ b/src/document/UBDocumentProxy.cpp @@ -69,6 +69,16 @@ UBDocumentProxy::~UBDocumentProxy() // NOOP } +UBDocumentProxy* UBDocumentProxy::deepCopy() const +{ + UBDocumentProxy* copy = new UBDocumentProxy(); + + copy->mPersistencePath = QString(mPersistencePath); + copy->mMetaDatas = QHash(mMetaDatas); + copy->mIsModified = mIsModified; + copy->mPageCount = mPageCount; +} + int UBDocumentProxy::pageCount() { diff --git a/src/document/UBDocumentProxy.h b/src/document/UBDocumentProxy.h index a8f07ec4..5f0db518 100644 --- a/src/document/UBDocumentProxy.h +++ b/src/document/UBDocumentProxy.h @@ -49,6 +49,8 @@ class UBDocumentProxy : public QObject virtual ~UBDocumentProxy(); + UBDocumentProxy * deepCopy() const; + QString persistencePath() const; void setPersistencePath(const QString& pPersistencePath); From 11b9bf74fbf4fd8b60f6cc0a79c9bedcb2df0779 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Tue, 8 Mar 2016 12:35:57 +0100 Subject: [PATCH 5/5] Save metadata.rdf less often The documents' metadata.rdf file is now persisted only when a scene in the document is also persisted; as well as when the document is modified (trashed / path changed) in the the Documents pane. Code was cleaned-up a bit too (added a forgotten return value, etc) --- src/adaptors/UBMetadataDcSubsetAdaptor.cpp | 2 +- src/core/UBPersistenceManager.cpp | 21 ++++----------------- src/document/UBDocumentController.cpp | 17 +---------------- src/document/UBDocumentProxy.cpp | 11 ++++++++--- src/document/UBDocumentProxy.h | 4 ++-- 5 files changed, 16 insertions(+), 39 deletions(-) diff --git a/src/adaptors/UBMetadataDcSubsetAdaptor.cpp b/src/adaptors/UBMetadataDcSubsetAdaptor.cpp index d882ea6a..f8ba2f52 100644 --- a/src/adaptors/UBMetadataDcSubsetAdaptor.cpp +++ b/src/adaptors/UBMetadataDcSubsetAdaptor.cpp @@ -86,7 +86,7 @@ void UBMetadataDcSubsetAdaptor::persist(UBDocumentProxy* proxy) return; } QString fileName = proxy->persistencePath() + "/" + metadataFilename; - qWarning() << "Persisting document path is" << fileName; + qWarning() << "Persisting document; path is" << fileName; QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { diff --git a/src/core/UBPersistenceManager.cpp b/src/core/UBPersistenceManager.cpp index 3f6606bf..ebd31f25 100644 --- a/src/core/UBPersistenceManager.cpp +++ b/src/core/UBPersistenceManager.cpp @@ -211,13 +211,6 @@ QList > UBPersistenceManager::allDocumentProxies() { UBDocumentProxy* proxy = new UBDocumentProxy(fullPath); // deleted in UBPersistenceManager::destructor - QMap metadatas = UBMetadataDcSubsetAdaptor::load(fullPath); - - foreach(QString key, metadatas.keys()) - { - proxy->setMetaData(key, metadatas.value(key)); - } - proxy->setPageCount(sceneCount(proxy)); proxies << QPointer(proxy); @@ -376,13 +369,6 @@ UBDocumentProxy* UBPersistenceManager::createDocumentFromDir(const QString& pDoc doc->setMetaData(UBSettings::documentName, pName); } - QMap metadatas = UBMetadataDcSubsetAdaptor::load(pDocumentDirectory); - - foreach(QString key, metadatas.keys()) - { - doc->setMetaData(key, metadatas.value(key)); - } - doc->setUuid(QUuid::createUuid()); doc->setPageCount(sceneCount(doc)); @@ -775,11 +761,12 @@ void UBPersistenceManager::persistDocumentScene(UBDocumentProxy* pDocumentProxy, mSceneCache.insert(pDocumentProxy, pSceneIndex, pScene); - if (pDocumentProxy->isModified()) - persistDocumentMetadata(pDocumentProxy, forceImmediateSaving); - if (pScene->isModified()) { + //qDebug() << "Persisting scene"; + if (pDocumentProxy->isModified()) + persistDocumentMetadata(pDocumentProxy, forceImmediateSaving); + UBThumbnailAdaptor::persistScene(pDocumentProxy, pScene, pSceneIndex); if(forceImmediateSaving) UBSvgSubsetAdaptor::persistScene(pDocumentProxy,pScene,pSceneIndex); diff --git a/src/document/UBDocumentController.cpp b/src/document/UBDocumentController.cpp index 8711eb6f..206837e8 100644 --- a/src/document/UBDocumentController.cpp +++ b/src/document/UBDocumentController.cpp @@ -517,7 +517,6 @@ void UBDocumentController::duplicateSelectedItem() duplicatePages(selectedSceneIndexes); emit documentThumbnailsUpdated(this); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(selectedDocument()); mDocumentUI->thumbnailWidget->selectItemAt(selectedSceneIndexes.last() + selectedSceneIndexes.size()); } } @@ -534,7 +533,6 @@ void UBDocumentController::duplicateSelectedItem() UBDocumentProxy* duplicatedDoc = UBPersistenceManager::persistenceManager()->duplicateDocument(source); duplicatedDoc->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(duplicatedDoc); selectDocument(duplicatedDoc, false); @@ -995,13 +993,7 @@ void UBDocumentController::itemChanged(QTreeWidgetItem * item, int column) , this, SLOT(updateDocumentInTree(UBDocumentProxy*))); if (proxyItem) - { - if (proxyItem->proxy()->metaData(UBSettings::documentName).toString() != item->text(column)) - { - proxyItem->proxy()->setMetaData(UBSettings::documentName, item->text(column)); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(proxyItem->proxy()); - } - } + proxyItem->proxy()->setMetaData(UBSettings::documentName, item->text(column)); else { // it is a group @@ -1017,7 +1009,6 @@ void UBDocumentController::itemChanged(QTreeWidgetItem * item, int column) if (0 != (item->flags() & Qt::ItemIsEditable)) { childItem->proxy()->setMetaData(UBSettings::documentGroupName, item->text(column)); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(childItem->proxy()); } } } @@ -1104,7 +1095,6 @@ void UBDocumentController::addFolderOfImages() else { document->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(document); reloadThumbnails(); } } @@ -1150,7 +1140,6 @@ bool UBDocumentController::addFileToDocument(UBDocumentProxy* document) if (success) { document->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(document); } else { @@ -1169,7 +1158,6 @@ void UBDocumentController::moveSceneToIndex(UBDocumentProxy* proxy, int source, if (UBDocumentContainer::movePageToIndex(source, target)) { proxy->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(proxy); mDocumentUI->thumbnailWidget->hightlightItem(target); } @@ -1497,7 +1485,6 @@ void UBDocumentController::addToDocument() mDocumentUI->thumbnailWidget->selectItemAt(newActiveSceneIndex, false); selectDocument(mBoardController->selectedDocument()); mBoardController->selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(mBoardController->selectedDocument()); UBApplication::applicationController->showBoard(); } @@ -1678,7 +1665,6 @@ void UBDocumentController::addImages() else { document->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(document); reloadThumbnails(); } } @@ -1791,7 +1777,6 @@ void UBDocumentController::deletePages(QList itemsToDelete) UBDocumentContainer::deletePages(sceneIndexes); proxy->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); - UBPersistenceManager::persistenceManager()->persistDocumentMetadata(proxy); int minIndex = proxy->pageCount() - 1; foreach (int i, sceneIndexes) diff --git a/src/document/UBDocumentProxy.cpp b/src/document/UBDocumentProxy.cpp index d8c04b49..a87ae4c0 100644 --- a/src/document/UBDocumentProxy.cpp +++ b/src/document/UBDocumentProxy.cpp @@ -33,9 +33,10 @@ #include "core/UBPersistenceManager.h" #include "core/UBSettings.h" #include "core/UBDocumentManager.h" - #include "core/memcheck.h" +#include "adaptors/UBMetadataDcSubsetAdaptor.h" + UBDocumentProxy::UBDocumentProxy() : mPageCount(0) { @@ -48,6 +49,8 @@ UBDocumentProxy::UBDocumentProxy(const QString& pPersistancePath) { init(); setPersistencePath(pPersistancePath); + + mMetaDatas = UBMetadataDcSubsetAdaptor::load(pPersistancePath); } @@ -74,9 +77,11 @@ UBDocumentProxy* UBDocumentProxy::deepCopy() const UBDocumentProxy* copy = new UBDocumentProxy(); copy->mPersistencePath = QString(mPersistencePath); - copy->mMetaDatas = QHash(mMetaDatas); + copy->mMetaDatas = QMap(mMetaDatas); copy->mIsModified = mIsModified; copy->mPageCount = mPageCount; + + return copy; } @@ -164,7 +169,7 @@ QVariant UBDocumentProxy::metaData(const QString& pKey) const } } -QHash UBDocumentProxy::metaDatas() const +QMap UBDocumentProxy::metaDatas() const { return mMetaDatas; } diff --git a/src/document/UBDocumentProxy.h b/src/document/UBDocumentProxy.h index 5f0db518..8ccfbe4a 100644 --- a/src/document/UBDocumentProxy.h +++ b/src/document/UBDocumentProxy.h @@ -57,7 +57,7 @@ class UBDocumentProxy : public QObject void setMetaData(const QString& pKey , const QVariant& pValue); QVariant metaData(const QString& pKey) const; - QHash metaDatas() const; + QMap metaDatas() const; QString name() const; QString groupName() const; @@ -88,7 +88,7 @@ class UBDocumentProxy : public QObject QString mPersistencePath; - QHash mMetaDatas; + QMap mMetaDatas; bool mIsModified;