diff options
Diffstat (limited to 'common/notifier.cpp')
-rw-r--r-- | common/notifier.cpp | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/common/notifier.cpp b/common/notifier.cpp index 53db5be..f52e28b 100644 --- a/common/notifier.cpp +++ b/common/notifier.cpp | |||
@@ -24,6 +24,8 @@ | |||
24 | 24 | ||
25 | #include "resourceaccess.h" | 25 | #include "resourceaccess.h" |
26 | #include "resourceconfig.h" | 26 | #include "resourceconfig.h" |
27 | #include "query.h" | ||
28 | #include "facadefactory.h" | ||
27 | #include "log.h" | 29 | #include "log.h" |
28 | 30 | ||
29 | using namespace Sink; | 31 | using namespace Sink; |
@@ -34,37 +36,64 @@ public: | |||
34 | Private() : context(new QObject) | 36 | Private() : context(new QObject) |
35 | { | 37 | { |
36 | } | 38 | } |
39 | |||
40 | void listenForNotifications(const QSharedPointer<ResourceAccess> &access) | ||
41 | { | ||
42 | QObject::connect(access.data(), &ResourceAccess::notification, &context, [this](const Notification ¬ification) { | ||
43 | for (const auto &handler : handler) { | ||
44 | handler(notification); | ||
45 | } | ||
46 | }); | ||
47 | resourceAccess << access; | ||
48 | } | ||
49 | |||
37 | QList<QSharedPointer<ResourceAccess>> resourceAccess; | 50 | QList<QSharedPointer<ResourceAccess>> resourceAccess; |
38 | QList<std::function<void(const Notification &)>> handler; | 51 | QList<std::function<void(const Notification &)>> handler; |
39 | QSharedPointer<QObject> context; | 52 | QObject context; |
40 | }; | 53 | }; |
41 | 54 | ||
42 | Notifier::Notifier(const QSharedPointer<ResourceAccess> &resourceAccess) : d(new Sink::Notifier::Private) | 55 | Notifier::Notifier(const QSharedPointer<ResourceAccess> &resourceAccess) : d(new Sink::Notifier::Private) |
43 | { | 56 | { |
44 | QObject::connect(resourceAccess.data(), &ResourceAccess::notification, d->context.data(), [this](const Notification ¬ification) { | 57 | d->listenForNotifications(resourceAccess); |
45 | for (const auto &handler : d->handler) { | ||
46 | handler(notification); | ||
47 | } | ||
48 | }); | ||
49 | d->resourceAccess << resourceAccess; | ||
50 | } | 58 | } |
51 | 59 | ||
52 | Notifier::Notifier(const QByteArray &instanceIdentifier, const QByteArray &resourceType) : d(new Sink::Notifier::Private) | 60 | Notifier::Notifier(const QByteArray &instanceIdentifier, const QByteArray &resourceType) : d(new Sink::Notifier::Private) |
53 | { | 61 | { |
54 | auto resourceAccess = Sink::ResourceAccess::Ptr::create(instanceIdentifier, resourceType); | 62 | auto resourceAccess = Sink::ResourceAccessFactory::instance().getAccess(instanceIdentifier, resourceType); |
55 | resourceAccess->open(); | 63 | resourceAccess->open(); |
56 | QObject::connect(resourceAccess.data(), &ResourceAccess::notification, d->context.data(), [this](const Notification ¬ification) { | 64 | d->listenForNotifications(resourceAccess); |
57 | for (const auto &handler : d->handler) { | ||
58 | handler(notification); | ||
59 | } | ||
60 | }); | ||
61 | d->resourceAccess << resourceAccess; | ||
62 | } | 65 | } |
63 | 66 | ||
64 | Notifier::Notifier(const QByteArray &instanceIdentifier) : Notifier(instanceIdentifier, ResourceConfig::getResourceType(instanceIdentifier)) | 67 | Notifier::Notifier(const QByteArray &instanceIdentifier) : Notifier(instanceIdentifier, ResourceConfig::getResourceType(instanceIdentifier)) |
65 | { | 68 | { |
66 | } | 69 | } |
67 | 70 | ||
71 | Notifier::Notifier(const Sink::Query &resourceQuery) : d(new Sink::Notifier::Private) | ||
72 | { | ||
73 | Sink::Log::Context resourceCtx{"notifier"}; | ||
74 | auto facade = FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>(); | ||
75 | Q_ASSERT(facade); | ||
76 | |||
77 | auto result = facade->load(resourceQuery, resourceCtx); | ||
78 | auto emitter = result.second; | ||
79 | emitter->onAdded([=](const ApplicationDomain::SinkResource::Ptr &resource) { | ||
80 | auto resourceAccess = Sink::ResourceAccessFactory::instance().getAccess(resource->identifier(), ResourceConfig::getResourceType(resource->identifier())); | ||
81 | resourceAccess->open(); | ||
82 | d->listenForNotifications(resourceAccess); | ||
83 | }); | ||
84 | emitter->onModified([](const ApplicationDomain::SinkResource::Ptr &) { | ||
85 | }); | ||
86 | emitter->onRemoved([](const ApplicationDomain::SinkResource::Ptr &) { | ||
87 | }); | ||
88 | emitter->onInitialResultSetComplete([](const ApplicationDomain::SinkResource::Ptr &, bool) { | ||
89 | }); | ||
90 | emitter->onComplete([resourceCtx]() { | ||
91 | SinkTraceCtx(resourceCtx) << "Resource query complete"; | ||
92 | }); | ||
93 | emitter->fetch({}); | ||
94 | result.first.exec(); | ||
95 | } | ||
96 | |||
68 | void Notifier::registerHandler(std::function<void(const Notification &)> handler) | 97 | void Notifier::registerHandler(std::function<void(const Notification &)> handler) |
69 | { | 98 | { |
70 | d->handler << handler; | 99 | d->handler << handler; |