summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-03-15 18:41:42 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-03-15 18:41:42 +0100
commitd40710eb005cde8c6962d6f5689bc63a4745b1c1 (patch)
tree349d956f114072764a992afe67aaa7c54038fc63
parent52047ca6d72957dd36c13e1cd93453a7c48e4b17 (diff)
downloadsink-d40710eb005cde8c6962d6f5689bc63a4745b1c1.tar.gz
sink-d40710eb005cde8c6962d6f5689bc63a4745b1c1.zip
Preparation to get useful notifications to Kube
-rw-r--r--common/notification.h1
-rw-r--r--common/notifier.cpp54
-rw-r--r--common/notifier.h5
-rw-r--r--examples/mailtransportresource/mailtransportresource.cpp11
4 files changed, 57 insertions, 14 deletions
diff --git a/common/notification.h b/common/notification.h
index 8224f2a..b67a84a 100644
--- a/common/notification.h
+++ b/common/notification.h
@@ -34,6 +34,7 @@ public:
34 enum NoticationType { 34 enum NoticationType {
35 Shutdown, 35 Shutdown,
36 Status, 36 Status,
37 Info,
37 Warning, 38 Warning,
38 Progress, 39 Progress,
39 Inspection, 40 Inspection,
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
29using namespace Sink; 31using 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 &notification) {
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
42Notifier::Notifier(const QSharedPointer<ResourceAccess> &resourceAccess) : d(new Sink::Notifier::Private) 56Notifier::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 &notification) { 58 d->listenForNotifications(resourceAccess);
45 for (const auto &handler : d->handler) {
46 handler(notification);
47 }
48 });
49 d->resourceAccess << resourceAccess;
50} 59}
51 60
52Notifier::Notifier(const QByteArray &instanceIdentifier, const QByteArray &resourceType) : d(new Sink::Notifier::Private) 61Notifier::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 &notification) { 65 d->listenForNotifications(resourceAccess);
57 for (const auto &handler : d->handler) {
58 handler(notification);
59 }
60 });
61 d->resourceAccess << resourceAccess;
62} 66}
63 67
64Notifier::Notifier(const QByteArray &instanceIdentifier) : Notifier(instanceIdentifier, ResourceConfig::getResourceType(instanceIdentifier)) 68Notifier::Notifier(const QByteArray &instanceIdentifier) : Notifier(instanceIdentifier, ResourceConfig::getResourceType(instanceIdentifier))
65{ 69{
66} 70}
67 71
72Notifier::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
68void Notifier::registerHandler(std::function<void(const Notification &)> handler) 98void Notifier::registerHandler(std::function<void(const Notification &)> handler)
69{ 99{
70 d->handler << handler; 100 d->handler << handler;
diff --git a/common/notifier.h b/common/notifier.h
index 290458a..b5d3dfa 100644
--- a/common/notifier.h
+++ b/common/notifier.h
@@ -23,14 +23,14 @@
23#include "sink_export.h" 23#include "sink_export.h"
24#include <QByteArray> 24#include <QByteArray>
25#include <QSharedPointer> 25#include <QSharedPointer>
26 26#include <functional>
27#include <KAsync/Async>
28 27
29class QAbstractItemModel; 28class QAbstractItemModel;
30 29
31namespace Sink { 30namespace Sink {
32class ResourceAccess; 31class ResourceAccess;
33class Notification; 32class Notification;
33class Query;
34 34
35class SINK_EXPORT Notifier 35class SINK_EXPORT Notifier
36{ 36{
@@ -38,6 +38,7 @@ public:
38 Notifier(const QSharedPointer<ResourceAccess> &resourceAccess); 38 Notifier(const QSharedPointer<ResourceAccess> &resourceAccess);
39 Notifier(const QByteArray &resourceInstanceIdentifier); 39 Notifier(const QByteArray &resourceInstanceIdentifier);
40 Notifier(const QByteArray &resourceInstanceIdentifier, const QByteArray &resourceType); 40 Notifier(const QByteArray &resourceInstanceIdentifier, const QByteArray &resourceType);
41 Notifier(const Sink::Query &resourceQuery);
41 void registerHandler(std::function<void(const Notification &)>); 42 void registerHandler(std::function<void(const Notification &)>);
42 43
43private: 44private:
diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp
index fa7eba4..cd7f4a7 100644
--- a/examples/mailtransportresource/mailtransportresource.cpp
+++ b/examples/mailtransportresource/mailtransportresource.cpp
@@ -81,7 +81,18 @@ public:
81 } 81 }
82 if (!MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8(), options)) { 82 if (!MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8(), options)) {
83 SinkWarning() << "Failed to send message: " << mail; 83 SinkWarning() << "Failed to send message: " << mail;
84 //Emit failure notification
85 Sink::Notification n;
86 n.type = Notification::Warning;
87 n.message = "Failed to send message.";
88 emit notify(n);
84 return KAsync::error("Failed to send the message."); 89 return KAsync::error("Failed to send the message.");
90 } else {
91 //Emit success notification
92 Sink::Notification n;
93 n.type = Notification::Info;
94 n.message = "Message successfully sent.";
95 emit notify(n);
85 } 96 }
86 } 97 }
87 syncStore().writeValue(mail.identifier(), "sent"); 98 syncStore().writeValue(mail.identifier(), "sent");