diff options
Diffstat (limited to 'framework/actions')
-rw-r--r-- | framework/actions/action.cpp | 16 | ||||
-rw-r--r-- | framework/actions/action.h | 7 | ||||
-rw-r--r-- | framework/actions/actionbroker.cpp | 27 | ||||
-rw-r--r-- | framework/actions/actionbroker.h | 4 | ||||
-rw-r--r-- | framework/actions/actionhandler.h | 2 | ||||
-rw-r--r-- | framework/actions/actionresult.h | 12 |
6 files changed, 52 insertions, 16 deletions
diff --git a/framework/actions/action.cpp b/framework/actions/action.cpp index 4bda033e..f41feb66 100644 --- a/framework/actions/action.cpp +++ b/framework/actions/action.cpp | |||
@@ -20,11 +20,13 @@ | |||
20 | 20 | ||
21 | #include <QDebug> | 21 | #include <QDebug> |
22 | #include <QEvent> | 22 | #include <QEvent> |
23 | #include <QPointer> | ||
23 | #include <QDynamicPropertyChangeEvent> | 24 | #include <QDynamicPropertyChangeEvent> |
24 | #include <QMetaObject> | 25 | #include <QMetaObject> |
25 | #include <QMetaProperty> | 26 | #include <QMetaProperty> |
26 | 27 | ||
27 | #include "actionbroker.h" | 28 | #include "actionbroker.h" |
29 | #include "actionhandler.h" | ||
28 | #include "context.h" | 30 | #include "context.h" |
29 | 31 | ||
30 | using namespace Kube; | 32 | using namespace Kube; |
@@ -87,11 +89,21 @@ bool Action::ready() const | |||
87 | 89 | ||
88 | void Action::execute() | 90 | void Action::execute() |
89 | { | 91 | { |
90 | ActionBroker::instance().executeAction(mActionId, mContext); | 92 | ActionBroker::instance().executeAction(mActionId, mContext, mPreHandler, mPostHandler); |
91 | } | 93 | } |
92 | 94 | ||
93 | ActionResult Action::executeWithResult() | 95 | ActionResult Action::executeWithResult() |
94 | { | 96 | { |
95 | return ActionBroker::instance().executeAction(mActionId, mContext); | 97 | return ActionBroker::instance().executeAction(mActionId, mContext, mPreHandler, mPostHandler); |
98 | } | ||
99 | |||
100 | void Action::addPreHandler(ActionHandler *handler) | ||
101 | { | ||
102 | mPreHandler << handler; | ||
103 | } | ||
104 | |||
105 | void Action::addPostHandler(ActionHandler *handler) | ||
106 | { | ||
107 | mPostHandler << handler; | ||
96 | } | 108 | } |
97 | 109 | ||
diff --git a/framework/actions/action.h b/framework/actions/action.h index 1abf4a5c..47e41138 100644 --- a/framework/actions/action.h +++ b/framework/actions/action.h | |||
@@ -24,6 +24,8 @@ | |||
24 | 24 | ||
25 | namespace Kube { | 25 | namespace Kube { |
26 | 26 | ||
27 | class ActionHandler; | ||
28 | |||
27 | class Action : public QObject | 29 | class Action : public QObject |
28 | { | 30 | { |
29 | Q_OBJECT | 31 | Q_OBJECT |
@@ -47,6 +49,9 @@ public: | |||
47 | Q_INVOKABLE void execute(); | 49 | Q_INVOKABLE void execute(); |
48 | ActionResult executeWithResult(); | 50 | ActionResult executeWithResult(); |
49 | 51 | ||
52 | void addPreHandler(ActionHandler *handler); | ||
53 | void addPostHandler(ActionHandler *handler); | ||
54 | |||
50 | Q_SIGNALS: | 55 | Q_SIGNALS: |
51 | void readyChanged(); | 56 | void readyChanged(); |
52 | 57 | ||
@@ -56,6 +61,8 @@ private Q_SLOTS: | |||
56 | private: | 61 | private: |
57 | Context *mContext; | 62 | Context *mContext; |
58 | QByteArray mActionId; | 63 | QByteArray mActionId; |
64 | QList<QPointer<ActionHandler>> mPreHandler; | ||
65 | QList<QPointer<ActionHandler>> mPostHandler; | ||
59 | }; | 66 | }; |
60 | 67 | ||
61 | } | 68 | } |
diff --git a/framework/actions/actionbroker.cpp b/framework/actions/actionbroker.cpp index 890a5566..24ef0b2c 100644 --- a/framework/actions/actionbroker.cpp +++ b/framework/actions/actionbroker.cpp | |||
@@ -44,36 +44,43 @@ bool ActionBroker::isActionReady(const QByteArray &actionId, Context *context) | |||
44 | return false; | 44 | return false; |
45 | } | 45 | } |
46 | 46 | ||
47 | //TODO This should return true if all handlers together promise to gather all necessary data, but not otherwise | ||
48 | for (const auto handler : mHandler.values(actionId)) { | 47 | for (const auto handler : mHandler.values(actionId)) { |
49 | if (handler && handler->isActionReady(context)) { | 48 | if (handler) { |
50 | return true; | 49 | if (handler-> isActionReady(context)) { |
50 | return true; | ||
51 | } | ||
51 | } | 52 | } |
52 | } | 53 | } |
53 | 54 | ||
54 | return false; | 55 | return false; |
55 | } | 56 | } |
56 | 57 | ||
57 | ActionResult ActionBroker::executeAction(const QByteArray &actionId, Context *context) | 58 | ActionResult ActionBroker::executeAction(const QByteArray &actionId, Context *context, const QList<QPointer<ActionHandler>> &preHandler, const QList<QPointer<ActionHandler>> &postHandler) |
58 | { | 59 | { |
60 | ActionResult result; | ||
59 | if (context) { | 61 | if (context) { |
62 | for (const auto handler : preHandler) { | ||
63 | handler->execute(context); | ||
64 | } | ||
65 | //TODO the main handler should only execute once the pre handler is done | ||
60 | for (const auto handler : mHandler.values(actionId)) { | 66 | for (const auto handler : mHandler.values(actionId)) { |
61 | if (handler) { | 67 | if (handler) { |
62 | //FIXME All handler together return one result | 68 | result += handler->execute(context); |
63 | return handler->execute(context); | ||
64 | } | 69 | } |
65 | } | 70 | } |
71 | //TODO the post handler should only execute once the main handler is done | ||
72 | for (const auto handler : postHandler) { | ||
73 | handler->execute(context); | ||
74 | } | ||
66 | } else { | 75 | } else { |
67 | qWarning() << "Can't execute without context"; | 76 | qWarning() << "Can't execute without context"; |
77 | result.setDone(); | ||
78 | result.setError(1); | ||
68 | } | 79 | } |
69 | ActionResult result; | ||
70 | result.setDone(); | ||
71 | result.setError(1); | ||
72 | return result; | 80 | return result; |
73 | } | 81 | } |
74 | 82 | ||
75 | void ActionBroker::registerHandler(const QByteArray &actionId, ActionHandler *handler) | 83 | void ActionBroker::registerHandler(const QByteArray &actionId, ActionHandler *handler) |
76 | { | 84 | { |
77 | //TODO get notified on destruction via QPointer | ||
78 | mHandler.insert(actionId, handler); | 85 | mHandler.insert(actionId, handler); |
79 | } | 86 | } |
diff --git a/framework/actions/actionbroker.h b/framework/actions/actionbroker.h index 8f3eaeb2..d2787c79 100644 --- a/framework/actions/actionbroker.h +++ b/framework/actions/actionbroker.h | |||
@@ -33,7 +33,7 @@ public: | |||
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); |
36 | ActionResult executeAction(const QByteArray &actionId, Context *context); | 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); |
39 | 39 | ||
@@ -42,7 +42,7 @@ Q_SIGNALS: | |||
42 | 42 | ||
43 | private: | 43 | private: |
44 | ActionBroker(QObject *parent = 0); | 44 | ActionBroker(QObject *parent = 0); |
45 | QMultiMap<QByteArray, ActionHandler*> mHandler; | 45 | QMultiMap<QByteArray, QPointer<ActionHandler>> mHandler; |
46 | }; | 46 | }; |
47 | 47 | ||
48 | } | 48 | } |
diff --git a/framework/actions/actionhandler.h b/framework/actions/actionhandler.h index c8c10dc7..762b8d45 100644 --- a/framework/actions/actionhandler.h +++ b/framework/actions/actionhandler.h | |||
@@ -38,9 +38,7 @@ public: | |||
38 | 38 | ||
39 | virtual bool isActionReady(Context *context); | 39 | virtual bool isActionReady(Context *context); |
40 | 40 | ||
41 | // void pre(Context *context); | ||
42 | virtual ActionResult execute(Context *context); | 41 | virtual ActionResult execute(Context *context); |
43 | // void post(Context *context); | ||
44 | 42 | ||
45 | void setActionId(const QByteArray &); | 43 | void setActionId(const QByteArray &); |
46 | QByteArray actionId() const; | 44 | QByteArray actionId() const; |
diff --git a/framework/actions/actionresult.h b/framework/actions/actionresult.h index e4d3efeb..dcf1a9ec 100644 --- a/framework/actions/actionresult.h +++ b/framework/actions/actionresult.h | |||
@@ -43,6 +43,18 @@ public: | |||
43 | } | 43 | } |
44 | virtual ~ActionResult() {} | 44 | virtual ~ActionResult() {} |
45 | 45 | ||
46 | ActionResult &operator+=(const ActionResult &rhs) | ||
47 | { | ||
48 | if (!error() && rhs.error()) { | ||
49 | setError(rhs.error()); | ||
50 | } | ||
51 | if (isDone() && rhs.isDone()) { | ||
52 | mData->mDone = false; | ||
53 | } | ||
54 | mData = rhs.mData; | ||
55 | return *this; | ||
56 | } | ||
57 | |||
46 | void setDone() { | 58 | void setDone() { |
47 | mData->mDone = true; | 59 | mData->mDone = true; |
48 | } | 60 | } |