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