diff options
Diffstat (limited to 'common/resourcefacade.cpp')
-rw-r--r-- | common/resourcefacade.cpp | 83 |
1 files changed, 42 insertions, 41 deletions
diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp index 583d6ec..bf4239d 100644 --- a/common/resourcefacade.cpp +++ b/common/resourcefacade.cpp | |||
@@ -85,34 +85,35 @@ LocalStorageQueryRunner<DomainType>::LocalStorageQueryRunner(const Query &query, | |||
85 | updateStatus(*entity); | 85 | updateStatus(*entity); |
86 | mResultProvider->add(entity); | 86 | mResultProvider->add(entity); |
87 | } | 87 | } |
88 | if (query.liveQuery) { | ||
89 | { | ||
90 | auto ret = QObject::connect(&configNotifier, &ConfigNotifier::added, guard, [this](const ApplicationDomain::ApplicationDomainType::Ptr &entry) { | ||
91 | auto entity = entry.staticCast<DomainType>(); | ||
92 | updateStatus(*entity); | ||
93 | mResultProvider->add(entity); | ||
94 | }); | ||
95 | Q_ASSERT(ret); | ||
96 | } | ||
97 | { | ||
98 | auto ret = QObject::connect(&configNotifier, &ConfigNotifier::modified, guard, [this](const ApplicationDomain::ApplicationDomainType::Ptr &entry) { | ||
99 | auto entity = entry.staticCast<DomainType>(); | ||
100 | updateStatus(*entity); | ||
101 | mResultProvider->modify(entity); | ||
102 | }); | ||
103 | Q_ASSERT(ret); | ||
104 | } | ||
105 | { | ||
106 | auto ret = QObject::connect(&configNotifier, &ConfigNotifier::removed, guard, [this](const ApplicationDomain::ApplicationDomainType::Ptr &entry) { | ||
107 | mResultProvider->remove(entry.staticCast<DomainType>()); | ||
108 | }); | ||
109 | Q_ASSERT(ret); | ||
110 | } | ||
111 | } | ||
112 | // TODO initialResultSetComplete should be implicit | 88 | // TODO initialResultSetComplete should be implicit |
113 | mResultProvider->initialResultSetComplete(typename DomainType::Ptr()); | 89 | mResultProvider->initialResultSetComplete(typename DomainType::Ptr()); |
114 | mResultProvider->complete(); | 90 | mResultProvider->complete(); |
115 | }); | 91 | }); |
92 | if (query.liveQuery) { | ||
93 | { | ||
94 | auto ret = QObject::connect(&configNotifier, &ConfigNotifier::added, guard, [this](const ApplicationDomain::ApplicationDomainType::Ptr &entry) { | ||
95 | auto entity = entry.staticCast<DomainType>(); | ||
96 | SinkTrace() << "A new resource has been added: " << entity->identifier(); | ||
97 | updateStatus(*entity); | ||
98 | mResultProvider->add(entity); | ||
99 | }); | ||
100 | Q_ASSERT(ret); | ||
101 | } | ||
102 | { | ||
103 | auto ret = QObject::connect(&configNotifier, &ConfigNotifier::modified, guard, [this](const ApplicationDomain::ApplicationDomainType::Ptr &entry) { | ||
104 | auto entity = entry.staticCast<DomainType>(); | ||
105 | updateStatus(*entity); | ||
106 | mResultProvider->modify(entity); | ||
107 | }); | ||
108 | Q_ASSERT(ret); | ||
109 | } | ||
110 | { | ||
111 | auto ret = QObject::connect(&configNotifier, &ConfigNotifier::removed, guard, [this](const ApplicationDomain::ApplicationDomainType::Ptr &entry) { | ||
112 | mResultProvider->remove(entry.staticCast<DomainType>()); | ||
113 | }); | ||
114 | Q_ASSERT(ret); | ||
115 | } | ||
116 | } | ||
116 | mResultProvider->onDone([=]() { delete guard; delete this; }); | 117 | mResultProvider->onDone([=]() { delete guard; delete this; }); |
117 | } | 118 | } |
118 | 119 | ||
@@ -153,7 +154,7 @@ typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr LocalStorageQueryRun | |||
153 | 154 | ||
154 | 155 | ||
155 | template <typename DomainType> | 156 | template <typename DomainType> |
156 | LocalStorageFacade<DomainType>::LocalStorageFacade(const QByteArray &identifier) : StoreFacade<DomainType>(), mIdentifier(identifier), mConfigStore(identifier) | 157 | LocalStorageFacade<DomainType>::LocalStorageFacade(const QByteArray &identifier) : StoreFacade<DomainType>(), mIdentifier(identifier) |
157 | { | 158 | { |
158 | } | 159 | } |
159 | 160 | ||
@@ -163,19 +164,15 @@ LocalStorageFacade<DomainType>::~LocalStorageFacade() | |||
163 | } | 164 | } |
164 | 165 | ||
165 | template <typename DomainType> | 166 | template <typename DomainType> |
166 | typename DomainType::Ptr LocalStorageFacade<DomainType>::readFromConfig(const QByteArray &id, const QByteArray &type) | ||
167 | { | ||
168 | return ::readFromConfig<DomainType>(mConfigStore, id, type); | ||
169 | } | ||
170 | |||
171 | template <typename DomainType> | ||
172 | KAsync::Job<void> LocalStorageFacade<DomainType>::create(const DomainType &domainObject) | 167 | KAsync::Job<void> LocalStorageFacade<DomainType>::create(const DomainType &domainObject) |
173 | { | 168 | { |
174 | return KAsync::start<void>([domainObject, this]() { | 169 | auto configStoreIdentifier = mIdentifier; |
170 | return KAsync::start<void>([domainObject, configStoreIdentifier]() { | ||
175 | const QByteArray type = domainObject.getProperty("type").toByteArray(); | 171 | const QByteArray type = domainObject.getProperty("type").toByteArray(); |
176 | const QByteArray providedIdentifier = domainObject.identifier().isEmpty() ? domainObject.getProperty("identifier").toByteArray() : domainObject.identifier(); | 172 | const QByteArray providedIdentifier = domainObject.identifier().isEmpty() ? domainObject.getProperty("identifier").toByteArray() : domainObject.identifier(); |
177 | const QByteArray identifier = providedIdentifier.isEmpty() ? ResourceConfig::newIdentifier(type) : providedIdentifier; | 173 | const QByteArray identifier = providedIdentifier.isEmpty() ? ResourceConfig::newIdentifier(type) : providedIdentifier; |
178 | mConfigStore.add(identifier, type); | 174 | auto configStore = ConfigStore(configStoreIdentifier); |
175 | configStore.add(identifier, type); | ||
179 | auto changedProperties = domainObject.changedProperties(); | 176 | auto changedProperties = domainObject.changedProperties(); |
180 | changedProperties.removeOne("identifier"); | 177 | changedProperties.removeOne("identifier"); |
181 | changedProperties.removeOne("type"); | 178 | changedProperties.removeOne("type"); |
@@ -185,16 +182,17 @@ KAsync::Job<void> LocalStorageFacade<DomainType>::create(const DomainType &domai | |||
185 | for (const auto &property : changedProperties) { | 182 | for (const auto &property : changedProperties) { |
186 | configurationValues.insert(property, domainObject.getProperty(property)); | 183 | configurationValues.insert(property, domainObject.getProperty(property)); |
187 | } | 184 | } |
188 | mConfigStore.modify(identifier, configurationValues); | 185 | configStore.modify(identifier, configurationValues); |
189 | } | 186 | } |
190 | sConfigNotifier.add(readFromConfig(identifier, type)); | 187 | sConfigNotifier.add(::readFromConfig<DomainType>(configStore, identifier, type)); |
191 | }); | 188 | }); |
192 | } | 189 | } |
193 | 190 | ||
194 | template <typename DomainType> | 191 | template <typename DomainType> |
195 | KAsync::Job<void> LocalStorageFacade<DomainType>::modify(const DomainType &domainObject) | 192 | KAsync::Job<void> LocalStorageFacade<DomainType>::modify(const DomainType &domainObject) |
196 | { | 193 | { |
197 | return KAsync::start<void>([domainObject, this]() { | 194 | auto configStoreIdentifier = mIdentifier; |
195 | return KAsync::start<void>([domainObject, configStoreIdentifier]() { | ||
198 | const QByteArray identifier = domainObject.identifier(); | 196 | const QByteArray identifier = domainObject.identifier(); |
199 | if (identifier.isEmpty()) { | 197 | if (identifier.isEmpty()) { |
200 | SinkWarning() << "We need an \"identifier\" property to identify the entity to configure."; | 198 | SinkWarning() << "We need an \"identifier\" property to identify the entity to configure."; |
@@ -203,31 +201,34 @@ KAsync::Job<void> LocalStorageFacade<DomainType>::modify(const DomainType &domai | |||
203 | auto changedProperties = domainObject.changedProperties(); | 201 | auto changedProperties = domainObject.changedProperties(); |
204 | changedProperties.removeOne("identifier"); | 202 | changedProperties.removeOne("identifier"); |
205 | changedProperties.removeOne("type"); | 203 | changedProperties.removeOne("type"); |
204 | auto configStore = ConfigStore(configStoreIdentifier); | ||
206 | if (!changedProperties.isEmpty()) { | 205 | if (!changedProperties.isEmpty()) { |
207 | // We have some configuration values | 206 | // We have some configuration values |
208 | QMap<QByteArray, QVariant> configurationValues; | 207 | QMap<QByteArray, QVariant> configurationValues; |
209 | for (const auto &property : changedProperties) { | 208 | for (const auto &property : changedProperties) { |
210 | configurationValues.insert(property, domainObject.getProperty(property)); | 209 | configurationValues.insert(property, domainObject.getProperty(property)); |
211 | } | 210 | } |
212 | mConfigStore.modify(identifier, configurationValues); | 211 | configStore.modify(identifier, configurationValues); |
213 | } | 212 | } |
214 | 213 | ||
215 | const auto type = mConfigStore.getEntries().value(identifier); | 214 | const auto type = configStore.getEntries().value(identifier); |
216 | sConfigNotifier.modify(readFromConfig(identifier, type)); | 215 | sConfigNotifier.modify(::readFromConfig<DomainType>(configStore, identifier, type)); |
217 | }); | 216 | }); |
218 | } | 217 | } |
219 | 218 | ||
220 | template <typename DomainType> | 219 | template <typename DomainType> |
221 | KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domainObject) | 220 | KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domainObject) |
222 | { | 221 | { |
223 | return KAsync::start<void>([domainObject, this]() { | 222 | auto configStoreIdentifier = mIdentifier; |
223 | return KAsync::start<void>([domainObject, configStoreIdentifier]() { | ||
224 | const QByteArray identifier = domainObject.identifier(); | 224 | const QByteArray identifier = domainObject.identifier(); |
225 | if (identifier.isEmpty()) { | 225 | if (identifier.isEmpty()) { |
226 | SinkWarning() << "We need an \"identifier\" property to identify the entity to configure"; | 226 | SinkWarning() << "We need an \"identifier\" property to identify the entity to configure"; |
227 | return; | 227 | return; |
228 | } | 228 | } |
229 | SinkTrace() << "Removing: " << identifier; | 229 | SinkTrace() << "Removing: " << identifier; |
230 | mConfigStore.remove(identifier); | 230 | auto configStore = ConfigStore(configStoreIdentifier); |
231 | configStore.remove(identifier); | ||
231 | sConfigNotifier.remove(QSharedPointer<DomainType>::create(domainObject)); | 232 | sConfigNotifier.remove(QSharedPointer<DomainType>::create(domainObject)); |
232 | }); | 233 | }); |
233 | } | 234 | } |