diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/resourcefacade.cpp | 48 | ||||
-rw-r--r-- | common/resourcefacade.h | 1 |
2 files changed, 37 insertions, 12 deletions
diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp index 829375c..0687bbc 100644 --- a/common/resourcefacade.cpp +++ b/common/resourcefacade.cpp | |||
@@ -24,6 +24,7 @@ | |||
24 | #include "store.h" | 24 | #include "store.h" |
25 | #include "resourceaccess.h" | 25 | #include "resourceaccess.h" |
26 | #include "resource.h" | 26 | #include "resource.h" |
27 | #include "facadefactory.h" | ||
27 | 28 | ||
28 | using namespace Sink; | 29 | using namespace Sink; |
29 | 30 | ||
@@ -358,27 +359,50 @@ QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename ApplicationDomain | |||
358 | auto ctx = parentCtx.subContext("accounts"); | 359 | auto ctx = parentCtx.subContext("accounts"); |
359 | auto runner = new LocalStorageQueryRunner<ApplicationDomain::SinkAccount>(query, mIdentifier, mTypeName, sConfigNotifier, ctx); | 360 | auto runner = new LocalStorageQueryRunner<ApplicationDomain::SinkAccount>(query, mIdentifier, mTypeName, sConfigNotifier, ctx); |
360 | auto monitoredResources = QSharedPointer<QSet<QByteArray>>::create(); | 361 | auto monitoredResources = QSharedPointer<QSet<QByteArray>>::create(); |
361 | runner->setStatusUpdater([runner, monitoredResources, ctx](ApplicationDomain::SinkAccount &account) { | 362 | auto monitorResource = [monitoredResources, runner, ctx] (const QByteArray &accountIdentifier, const ApplicationDomain::SinkResource &resource, const ResourceAccess::Ptr &resourceAccess) { |
362 | Query query; | 363 | if (!monitoredResources->contains(resource.identifier())) { |
364 | auto ret = QObject::connect(resourceAccess.data(), &ResourceAccess::notification, runner->guard(), [resource, runner, resourceAccess, accountIdentifier, ctx](const Notification ¬ification) { | ||
365 | SinkTraceCtx(ctx) << "Received notification in facade: " << notification.type; | ||
366 | if (notification.type == Notification::Status) { | ||
367 | runner->statusChanged(accountIdentifier); | ||
368 | } | ||
369 | }); | ||
370 | Q_ASSERT(ret); | ||
371 | monitoredResources->insert(resource.identifier()); | ||
372 | } | ||
373 | }; | ||
374 | runner->setStatusUpdater([this, runner, monitoredResources, ctx, monitorResource](ApplicationDomain::SinkAccount &account) { | ||
375 | Query query{Query::LiveQuery}; | ||
363 | query.filter<ApplicationDomain::SinkResource::Account>(account.identifier()); | 376 | query.filter<ApplicationDomain::SinkResource::Account>(account.identifier()); |
364 | query.request<ApplicationDomain::SinkResource::Account>() | 377 | query.request<ApplicationDomain::SinkResource::Account>() |
365 | .request<ApplicationDomain::SinkResource::Capabilities>(); | 378 | .request<ApplicationDomain::SinkResource::Capabilities>(); |
366 | const auto resources = Store::read<ApplicationDomain::SinkResource>(query); | 379 | const auto resources = Store::read<ApplicationDomain::SinkResource>(query); |
367 | SinkTraceCtx(ctx) << "Found resource belonging to the account " << account.identifier() << " : " << resources; | 380 | SinkTraceCtx(ctx) << "Found resource belonging to the account " << account.identifier() << " : " << resources; |
368 | auto accountIdentifier = account.identifier(); | 381 | auto accountIdentifier = account.identifier(); |
382 | |||
383 | //Monitor for new resources so they can be monitored as well | ||
384 | if (!runner->mResourceEmitter.contains(accountIdentifier)) { | ||
385 | auto facade = Sink::FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>(); | ||
386 | Q_ASSERT(facade); | ||
387 | |||
388 | auto emitter = facade->load(query, ctx).second; | ||
389 | emitter->onAdded([=](const ApplicationDomain::SinkResource::Ptr &resource) { | ||
390 | auto resourceAccess = Sink::ResourceAccessFactory::instance().getAccess(resource->identifier(), ResourceConfig::getResourceType(resource->identifier())); | ||
391 | monitorResource(accountIdentifier, *resource, resourceAccess); | ||
392 | }); | ||
393 | emitter->onModified([](const ApplicationDomain::SinkResource::Ptr &) {}); | ||
394 | emitter->onRemoved([](const ApplicationDomain::SinkResource::Ptr &) {}); | ||
395 | emitter->onInitialResultSetComplete([](const ApplicationDomain::SinkResource::Ptr &, bool) {}); | ||
396 | emitter->onComplete([]() {}); | ||
397 | emitter->fetch({}); | ||
398 | runner->mResourceEmitter[accountIdentifier] = emitter; | ||
399 | } | ||
400 | |||
369 | QList<int> states; | 401 | QList<int> states; |
402 | //Gather all resources and ensure they are monitored | ||
370 | for (const auto &resource : resources) { | 403 | for (const auto &resource : resources) { |
371 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource.identifier(), ResourceConfig::getResourceType(resource.identifier())); | 404 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource.identifier(), ResourceConfig::getResourceType(resource.identifier())); |
372 | if (!monitoredResources->contains(resource.identifier())) { | 405 | monitorResource(accountIdentifier, resource, resourceAccess); |
373 | auto ret = QObject::connect(resourceAccess.data(), &ResourceAccess::notification, runner->guard(), [resource, runner, resourceAccess, accountIdentifier, ctx](const Notification ¬ification) { | ||
374 | SinkTraceCtx(ctx) << "Received notification in facade: " << notification.type; | ||
375 | if (notification.type == Notification::Status) { | ||
376 | runner->statusChanged(accountIdentifier); | ||
377 | } | ||
378 | }); | ||
379 | Q_ASSERT(ret); | ||
380 | monitoredResources->insert(resource.identifier()); | ||
381 | } | ||
382 | states << resourceAccess->getResourceStatus(); | 406 | states << resourceAccess->getResourceStatus(); |
383 | } | 407 | } |
384 | const auto status = [&] { | 408 | const auto status = [&] { |
diff --git a/common/resourcefacade.h b/common/resourcefacade.h index 76fadce..36049c4 100644 --- a/common/resourcefacade.h +++ b/common/resourcefacade.h | |||
@@ -65,6 +65,7 @@ public: | |||
65 | void setStatusUpdater(const std::function<void(DomainType &)> &); | 65 | void setStatusUpdater(const std::function<void(DomainType &)> &); |
66 | void statusChanged(const QByteArray &identifier); | 66 | void statusChanged(const QByteArray &identifier); |
67 | QObject *guard() const; | 67 | QObject *guard() const; |
68 | QMap<QByteArray, QSharedPointer<Sink::ResultEmitter<QSharedPointer<Sink::ApplicationDomain::SinkResource> > > > mResourceEmitter; | ||
68 | 69 | ||
69 | private: | 70 | private: |
70 | void updateStatus(DomainType &entity); | 71 | void updateStatus(DomainType &entity); |