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