Showing the changes to libraries/shared/src/SettingInterface.cpp (1 of 8 files). Show all
Unified · Split
| @@ -9,27 +9,33 @@ · 8 unmodified lines | |||
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 10 | // | 10 | // |
| 11 | 11 | ||
| 12 | #include "SettingInterface.h" | ||
| 13 | |||
| 12 | #include <QCoreApplication> | 14 | #include <QCoreApplication> |
| 13 | #include <QDebug> | 15 | #include <QDebug> |
| 14 | #include <QFile> | 16 | #include <QFile> |
| 15 | #include <QThread> | 17 | #include <QThread> |
| 16 | 18 | ||
| 17 | #include "PathUtils.h" | 19 | #include "PathUtils.h" |
| 18 | #include "SettingInterface.h" | ||
| 19 | #include "SettingManager.h" | 20 | #include "SettingManager.h" |
| 20 | #include "SharedLogging.h" | 21 | #include "SharedLogging.h" |
| 21 | 22 | ||
| 22 | namespace Setting { | 23 | namespace Setting { |
| 23 | static Manager* privateInstance = nullptr; | 24 | static QSharedPointer<Manager> globalManager; |
| 24 | 25 | ||
| 26 | const QString Interface::FIRST_RUN { "firstRun" }; | ||
| 27 | |||
| 25 | // cleans up the settings private instance. Should only be run once at closing down. | 28 | // cleans up the settings private instance. Should only be run once at closing down. |
| 26 | void cleanupPrivateInstance() { | 29 | void cleanupPrivateInstance() { |
| 27 | // grab the thread before we nuke the instance | 30 | // grab the thread before we nuke the instance |
| 28 | QThread* settingsManagerThread = privateInstance->thread(); | 31 | QThread* settingsManagerThread = DependencyManager::get<Manager>()->thread(); |
| 29 | 32 | ||
| 30 | // tell the private instance to clean itself up on its thread | 33 | // tell the private instance to clean itself up on its thread |
| 31 | privateInstance->deleteLater(); | 34 | DependencyManager::destroy<Manager>(); |
| 32 | privateInstance = NULL; | 35 | |
| 36 | // | ||
| 37 | globalManager->deleteLater(); | ||
| 38 | globalManager.reset(); | ||
| 33 | 39 | ||
| 34 | // quit the settings manager thread and wait on it to make sure it's gone | 40 | // quit the settings manager thread and wait on it to make sure it's gone |
| 35 | settingsManagerThread->quit(); | 41 | settingsManagerThread->quit(); |
| @@ -63,14 +69,13 @@ namespace Setting { · 27 unmodified lines | |||
| 63 | QThread* thread = new QThread(); | 69 | QThread* thread = new QThread(); |
| 64 | Q_CHECK_PTR(thread); | 70 | Q_CHECK_PTR(thread); |
| 65 | thread->setObjectName("Settings Thread"); | 71 | thread->setObjectName("Settings Thread"); |
| 66 | |||
| 67 | privateInstance = new Manager(); | ||
| 68 | Q_CHECK_PTR(privateInstance); | ||
| 69 | 72 | ||
| 70 | QObject::connect(privateInstance, SIGNAL(destroyed()), thread, SLOT(quit())); | 73 | globalManager = DependencyManager::set<Manager>(); |
| 71 | QObject::connect(thread, SIGNAL(started()), privateInstance, SLOT(startTimer())); | 74 | |
| 75 | QObject::connect(globalManager.data(), SIGNAL(destroyed()), thread, SLOT(quit())); | ||
| 76 | QObject::connect(thread, SIGNAL(started()), globalManager.data(), SLOT(startTimer())); | ||
| 72 | QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); | 77 | QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); |
| 73 | privateInstance->moveToThread(thread); | 78 | globalManager->moveToThread(thread); |
| 74 | thread->start(); | 79 | thread->start(); |
| 75 | qCDebug(shared) << "Settings thread started."; | 80 | qCDebug(shared) << "Settings thread started."; |
| 76 | 81 | ||
| @@ -79,7 +84,7 @@ namespace Setting { · 2 unmodified lines | |||
| 79 | } | 84 | } |
| 80 | 85 | ||
| 81 | void Interface::init() { | 86 | void Interface::init() { |
| 82 | if (!privateInstance) { | 87 | if (!DependencyManager::isSet<Manager>()) { |
| 83 | // WARNING: As long as we are using QSettings this should always be triggered for each Setting::Handle | 88 | // WARNING: As long as we are using QSettings this should always be triggered for each Setting::Handle |
| 84 | // in an assignment-client - the QSettings backing we use for this means persistence of these | 89 | // in an assignment-client - the QSettings backing we use for this means persistence of these |
| 85 | // settings from an AC (when there can be multiple terminating at same time on one machine) | 90 | // settings from an AC (when there can be multiple terminating at same time on one machine) |
| @@ -87,9 +92,13 @@ namespace Setting { · 1 unmodified line | |||
| 87 | qWarning() << "Setting::Interface::init() for key" << _key << "- Manager not yet created." << | 92 | qWarning() << "Setting::Interface::init() for key" << _key << "- Manager not yet created." << |
| 88 | "Settings persistence disabled."; | 93 | "Settings persistence disabled."; |
| 89 | } else { | 94 | } else { |
| 90 | // Register Handle | 95 | _manager = DependencyManager::get<Manager>(); |
| 91 | privateInstance->registerHandle(this); | 96 | auto manager = _manager.lock(); |
| 92 | _isInitialized = true; | 97 | if (manager) { |
| 98 | // Register Handle | ||
| 99 | manager->registerHandle(this); | ||
| 100 | _isInitialized = true; | ||
| 101 | } | ||
| 93 | 102 | ||
| 94 | // Load value from disk | 103 | // Load value from disk |
| 95 | load(); | 104 | load(); |
| @@ -97,11 +106,13 @@ namespace Setting { · 1 unmodified line | |||
| 97 | } | 106 | } |
| 98 | 107 | ||
| 99 | void Interface::deinit() { | 108 | void Interface::deinit() { |
| 100 | if (_isInitialized && privateInstance) { | 109 | if (_isInitialized && _manager) { |
| 101 | // Save value to disk | 110 | auto manager = _manager.lock(); |
| 102 | save(); | 111 | if (manager) { |
| 103 | 112 | // Save value to disk | |
| 104 | privateInstance->removeHandle(_key); | 113 | save(); |
| 114 | manager->removeHandle(_key); | ||
| 115 | } | ||
| 105 | } | 116 | } |
| 106 | } | 117 | } |
| 107 | 118 | ||
| @@ -113,14 +124,16 @@ namespace Setting { · 5 unmodified lines | |||
| 113 | } | 124 | } |
| 114 | 125 | ||
| 115 | void Interface::save() { | 126 | void Interface::save() { |
| 116 | if (privateInstance) { | 127 | auto manager = _manager.lock(); |
| 117 | privateInstance->saveSetting(this); | 128 | if (manager) { |
| 129 | manager->saveSetting(this); | ||
| 118 | } | 130 | } |
| 119 | } | 131 | } |
| 120 | 132 | ||
| 121 | void Interface::load() { | 133 | void Interface::load() { |
| 122 | if (privateInstance) { | 134 | auto manager = _manager.lock(); |
| 123 | privateInstance->loadSetting(this); | 135 | if (manager) { |
| 136 | manager->loadSetting(this); | ||
| 124 | } | 137 | } |
| 125 | } | 138 | } |
| 126 | } | 139 | } |