summaryrefslogtreecommitdiffstats
path: root/framework/actions
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-12-13 16:24:31 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-12-16 14:54:14 +0100
commit630f45719a527f8ee739b03bc62f886badea6df3 (patch)
treeb9d859cbfedb30ad8a9570e3b87a419bf24ba6c7 /framework/actions
parentb1a2e2de201985a00980bead5272977cda4ef637 (diff)
downloadkube-630f45719a527f8ee739b03bc62f886badea6df3.tar.gz
kube-630f45719a527f8ee739b03bc62f886badea6df3.zip
Revamp of composercontroller to use actions more.
Instead of setting all properties individually we directly assign all properties to a context that we assign to the actions. This way actions can automatically update themselves as new data becomes available, and we avoid the setter/getter boilerplate, at the cost of a less explicit interface (But that could be improved by allowing to define the required properties of a context in c++). By relying on prehandler/posthandler to execute certain actions we simplify the control flow and enable the future extension with handlers that i.e. do encryption etc.
Diffstat (limited to 'framework/actions')
-rw-r--r--framework/actions/action.cpp20
-rw-r--r--framework/actions/action.h3
-rw-r--r--framework/actions/actionbroker.cpp16
-rw-r--r--framework/actions/actionbroker.h2
-rw-r--r--framework/actions/actionhandler.cpp17
-rw-r--r--framework/actions/actionhandler.h2
-rw-r--r--framework/actions/context.cpp16
-rw-r--r--framework/actions/context.h2
8 files changed, 72 insertions, 6 deletions
diff --git a/framework/actions/action.cpp b/framework/actions/action.cpp
index f41feb66..1344d112 100644
--- a/framework/actions/action.cpp
+++ b/framework/actions/action.cpp
@@ -42,7 +42,11 @@ Action::Action(const QByteArray &actionId, Context &context, QObject *parent)
42 mContext(&context), 42 mContext(&context),
43 mActionId(actionId) 43 mActionId(actionId)
44{ 44{
45 setContext(&context);
46}
45 47
48Action::~Action()
49{
46} 50}
47 51
48void Action::setContext(Context *context) 52void Action::setContext(Context *context)
@@ -61,6 +65,16 @@ void Action::setContext(Context *context)
61 emit readyChanged(); 65 emit readyChanged();
62} 66}
63 67
68bool Action::eventFilter(QObject *obj, QEvent *e)
69{
70 if (obj == mContext) {
71 if (e->type() == QEvent::DynamicPropertyChange) {
72 contextChanged();
73 }
74 }
75 return QObject::eventFilter(obj, e);
76}
77
64void Action::contextChanged() 78void Action::contextChanged()
65{ 79{
66 emit readyChanged(); 80 emit readyChanged();
@@ -84,7 +98,7 @@ QByteArray Action::actionId() const
84 98
85bool Action::ready() const 99bool Action::ready() const
86{ 100{
87 return ActionBroker::instance().isActionReady(mActionId, mContext); 101 return ActionBroker::instance().isActionReady(mActionId, mContext, mPreHandler);
88} 102}
89 103
90void Action::execute() 104void Action::execute()
@@ -99,11 +113,15 @@ ActionResult Action::executeWithResult()
99 113
100void Action::addPreHandler(ActionHandler *handler) 114void Action::addPreHandler(ActionHandler *handler)
101{ 115{
116 //For cleanup
117 handler->setParent(this);
102 mPreHandler << handler; 118 mPreHandler << handler;
103} 119}
104 120
105void Action::addPostHandler(ActionHandler *handler) 121void Action::addPostHandler(ActionHandler *handler)
106{ 122{
123 //For cleanup
124 handler->setParent(this);
107 mPostHandler << handler; 125 mPostHandler << handler;
108} 126}
109 127
diff --git a/framework/actions/action.h b/framework/actions/action.h
index 47e41138..dda8c987 100644
--- a/framework/actions/action.h
+++ b/framework/actions/action.h
@@ -37,6 +37,7 @@ class Action : public QObject
37public: 37public:
38 Action(QObject *parent = 0); 38 Action(QObject *parent = 0);
39 Action(const QByteArray &actionId, Context &context, QObject *parent = 0); 39 Action(const QByteArray &actionId, Context &context, QObject *parent = 0);
40 ~Action();
40 41
41 void setContext(Context *); 42 void setContext(Context *);
42 Context *context() const; 43 Context *context() const;
@@ -52,6 +53,8 @@ public:
52 void addPreHandler(ActionHandler *handler); 53 void addPreHandler(ActionHandler *handler);
53 void addPostHandler(ActionHandler *handler); 54 void addPostHandler(ActionHandler *handler);
54 55
56 bool eventFilter(QObject *obj, QEvent *e) Q_DECL_OVERRIDE;
57
55Q_SIGNALS: 58Q_SIGNALS:
56 void readyChanged(); 59 void readyChanged();
57 60
diff --git a/framework/actions/actionbroker.cpp b/framework/actions/actionbroker.cpp
index 24ef0b2c..17145440 100644
--- a/framework/actions/actionbroker.cpp
+++ b/framework/actions/actionbroker.cpp
@@ -21,11 +21,14 @@
21 21
22#include "context.h" 22#include "context.h"
23#include "actionhandler.h" 23#include "actionhandler.h"
24#include <sink/log.h>
24 25
25#include <QDebug> 26#include <QDebug>
26 27
27using namespace Kube; 28using namespace Kube;
28 29
30SINK_DEBUG_AREA("actionbroker")
31
29ActionBroker::ActionBroker(QObject *parent) 32ActionBroker::ActionBroker(QObject *parent)
30 : QObject(parent) 33 : QObject(parent)
31{ 34{
@@ -38,15 +41,20 @@ ActionBroker &ActionBroker::instance()
38 return instance; 41 return instance;
39} 42}
40 43
41bool ActionBroker::isActionReady(const QByteArray &actionId, Context *context) 44bool ActionBroker::isActionReady(const QByteArray &actionId, Context *context, const QList<QPointer<ActionHandler>> &preHandler)
42{ 45{
43 if (!context) { 46 if (!context) {
44 return false; 47 return false;
45 } 48 }
49 for (const auto handler : preHandler) {
50 if (!handler->isActionReady(context)) {
51 return false;
52 }
53 }
46 54
47 for (const auto handler : mHandler.values(actionId)) { 55 for (const auto handler : mHandler.values(actionId)) {
48 if (handler) { 56 if (handler) {
49 if (handler-> isActionReady(context)) { 57 if (handler->isActionReady(context)) {
50 return true; 58 return true;
51 } 59 }
52 } 60 }
@@ -59,6 +67,8 @@ ActionResult ActionBroker::executeAction(const QByteArray &actionId, Context *co
59{ 67{
60 ActionResult result; 68 ActionResult result;
61 if (context) { 69 if (context) {
70 SinkLog() << "Executing action " << actionId;
71 SinkLog() << *context;
62 for (const auto handler : preHandler) { 72 for (const auto handler : preHandler) {
63 handler->execute(context); 73 handler->execute(context);
64 } 74 }
@@ -73,7 +83,7 @@ ActionResult ActionBroker::executeAction(const QByteArray &actionId, Context *co
73 handler->execute(context); 83 handler->execute(context);
74 } 84 }
75 } else { 85 } else {
76 qWarning() << "Can't execute without context"; 86 SinkWarning() << "Can't execute without context";
77 result.setDone(); 87 result.setDone();
78 result.setError(1); 88 result.setError(1);
79 } 89 }
diff --git a/framework/actions/actionbroker.h b/framework/actions/actionbroker.h
index d2787c79..84678c16 100644
--- a/framework/actions/actionbroker.h
+++ b/framework/actions/actionbroker.h
@@ -32,7 +32,7 @@ class ActionBroker : public QObject
32public: 32public:
33 static ActionBroker &instance(); 33 static ActionBroker &instance();
34 34
35 bool isActionReady(const QByteArray &actionId, Context *context); 35 bool isActionReady(const QByteArray &actionId, Context *context, const QList<QPointer<ActionHandler>> &preHandler);
36 ActionResult executeAction(const QByteArray &actionId, Context *context, const QList<QPointer<ActionHandler>> &preHandler, const QList<QPointer<ActionHandler>> &postHandler); 36 ActionResult executeAction(const QByteArray &actionId, Context *context, const QList<QPointer<ActionHandler>> &preHandler, const QList<QPointer<ActionHandler>> &postHandler);
37 37
38 void registerHandler(const QByteArray &actionId, ActionHandler *handler); 38 void registerHandler(const QByteArray &actionId, ActionHandler *handler);
diff --git a/framework/actions/actionhandler.cpp b/framework/actions/actionhandler.cpp
index 9d58f464..dc9edeca 100644
--- a/framework/actions/actionhandler.cpp
+++ b/framework/actions/actionhandler.cpp
@@ -77,6 +77,18 @@ QByteArray ActionHandler::actionId() const
77} 77}
78 78
79 79
80ActionHandlerHelper::ActionHandlerHelper(const Handler &handler)
81 : ActionHandler(nullptr),
82 handlerFunction(handler)
83{
84}
85
86ActionHandlerHelper::ActionHandlerHelper(const IsReadyFunction &isReady, const Handler &handler)
87 : ActionHandler(nullptr),
88 isReadyFunction(isReady),
89 handlerFunction(handler)
90{
91}
80 92
81ActionHandlerHelper::ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &isReady, const Handler &handler) 93ActionHandlerHelper::ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &isReady, const Handler &handler)
82 : ActionHandler(nullptr), 94 : ActionHandler(nullptr),
@@ -96,7 +108,10 @@ ActionHandlerHelper::ActionHandlerHelper(const QByteArray &actionId, const IsRea
96 108
97bool ActionHandlerHelper::isActionReady(Context *context) 109bool ActionHandlerHelper::isActionReady(Context *context)
98{ 110{
99 return isReadyFunction(context); 111 if (isReadyFunction) {
112 return isReadyFunction(context);
113 }
114 return true;
100} 115}
101 116
102ActionResult ActionHandlerHelper::execute(Context *context) 117ActionResult ActionHandlerHelper::execute(Context *context)
diff --git a/framework/actions/actionhandler.h b/framework/actions/actionhandler.h
index 762b8d45..09ed13c6 100644
--- a/framework/actions/actionhandler.h
+++ b/framework/actions/actionhandler.h
@@ -55,6 +55,8 @@ public:
55 typedef std::function<void(Context*)> Handler; 55 typedef std::function<void(Context*)> Handler;
56 typedef std::function<KAsync::Job<void>(Context*)> JobHandler; 56 typedef std::function<KAsync::Job<void>(Context*)> JobHandler;
57 57
58 ActionHandlerHelper(const Handler &);
59 ActionHandlerHelper(const IsReadyFunction &, const Handler &);
58 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const Handler &); 60 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const Handler &);
59 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const JobHandler &); 61 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const JobHandler &);
60 62
diff --git a/framework/actions/context.cpp b/framework/actions/context.cpp
index a7f87d16..205b1606 100644
--- a/framework/actions/context.cpp
+++ b/framework/actions/context.cpp
@@ -19,6 +19,7 @@
19#include "context.h" 19#include "context.h"
20 20
21#include <QDebug> 21#include <QDebug>
22#include <QMetaProperty>
22 23
23using namespace Kube; 24using namespace Kube;
24 25
@@ -27,3 +28,18 @@ Context::Context(QObject *parent)
27{ 28{
28 29
29} 30}
31
32QDebug operator<<(QDebug dbg, const Kube::Context &context)
33{
34 dbg << "Kube::Context {\n";
35 auto metaObject = context.QObject::metaObject();
36 for (auto i = metaObject->propertyOffset(); i < metaObject->propertyCount(); i++) {
37 auto property = metaObject->property(i);
38 dbg << property.name() << context.property(property.name()) << "\n";
39 }
40 for (const auto &p : context.dynamicPropertyNames()) {
41 dbg << p << context.property(p) << "\n";
42 }
43 dbg << "\n}";
44 return dbg;
45}
diff --git a/framework/actions/context.h b/framework/actions/context.h
index 7289f850..562e110e 100644
--- a/framework/actions/context.h
+++ b/framework/actions/context.h
@@ -31,5 +31,7 @@ public:
31 31
32} 32}
33 33
34QDebug operator<<(QDebug dbg, const Kube::Context &);
35
34Q_DECLARE_METATYPE(Kube::Context*); 36Q_DECLARE_METATYPE(Kube::Context*);
35 37