From b1a2e2de201985a00980bead5272977cda4ef637 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 13 Dec 2016 14:52:27 +0100 Subject: Preactionhandler --- framework/actions/action.cpp | 16 ++++++++++++++-- framework/actions/action.h | 7 +++++++ framework/actions/actionbroker.cpp | 27 +++++++++++++++++---------- framework/actions/actionbroker.h | 4 ++-- framework/actions/actionhandler.h | 2 -- framework/actions/actionresult.h | 12 ++++++++++++ 6 files changed, 52 insertions(+), 16 deletions(-) (limited to 'framework') 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 @@ #include #include +#include #include #include #include #include "actionbroker.h" +#include "actionhandler.h" #include "context.h" using namespace Kube; @@ -87,11 +89,21 @@ bool Action::ready() const void Action::execute() { - ActionBroker::instance().executeAction(mActionId, mContext); + ActionBroker::instance().executeAction(mActionId, mContext, mPreHandler, mPostHandler); } ActionResult Action::executeWithResult() { - return ActionBroker::instance().executeAction(mActionId, mContext); + return ActionBroker::instance().executeAction(mActionId, mContext, mPreHandler, mPostHandler); +} + +void Action::addPreHandler(ActionHandler *handler) +{ + mPreHandler << handler; +} + +void Action::addPostHandler(ActionHandler *handler) +{ + mPostHandler << handler; } 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 @@ namespace Kube { +class ActionHandler; + class Action : public QObject { Q_OBJECT @@ -47,6 +49,9 @@ public: Q_INVOKABLE void execute(); ActionResult executeWithResult(); + void addPreHandler(ActionHandler *handler); + void addPostHandler(ActionHandler *handler); + Q_SIGNALS: void readyChanged(); @@ -56,6 +61,8 @@ private Q_SLOTS: private: Context *mContext; QByteArray mActionId; + QList> mPreHandler; + QList> mPostHandler; }; } 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) return false; } - //TODO This should return true if all handlers together promise to gather all necessary data, but not otherwise for (const auto handler : mHandler.values(actionId)) { - if (handler && handler->isActionReady(context)) { - return true; + if (handler) { + if (handler-> isActionReady(context)) { + return true; + } } } return false; } -ActionResult ActionBroker::executeAction(const QByteArray &actionId, Context *context) +ActionResult ActionBroker::executeAction(const QByteArray &actionId, Context *context, const QList> &preHandler, const QList> &postHandler) { + ActionResult result; if (context) { + for (const auto handler : preHandler) { + handler->execute(context); + } + //TODO the main handler should only execute once the pre handler is done for (const auto handler : mHandler.values(actionId)) { if (handler) { - //FIXME All handler together return one result - return handler->execute(context); + result += handler->execute(context); } } + //TODO the post handler should only execute once the main handler is done + for (const auto handler : postHandler) { + handler->execute(context); + } } else { qWarning() << "Can't execute without context"; + result.setDone(); + result.setError(1); } - ActionResult result; - result.setDone(); - result.setError(1); return result; } void ActionBroker::registerHandler(const QByteArray &actionId, ActionHandler *handler) { - //TODO get notified on destruction via QPointer mHandler.insert(actionId, handler); } 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: static ActionBroker &instance(); bool isActionReady(const QByteArray &actionId, Context *context); - ActionResult executeAction(const QByteArray &actionId, Context *context); + ActionResult executeAction(const QByteArray &actionId, Context *context, const QList> &preHandler, const QList> &postHandler); void registerHandler(const QByteArray &actionId, ActionHandler *handler); @@ -42,7 +42,7 @@ Q_SIGNALS: private: ActionBroker(QObject *parent = 0); - QMultiMap mHandler; + QMultiMap> mHandler; }; } 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: virtual bool isActionReady(Context *context); - // void pre(Context *context); virtual ActionResult execute(Context *context); - // void post(Context *context); void setActionId(const QByteArray &); 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: } virtual ~ActionResult() {} + ActionResult &operator+=(const ActionResult &rhs) + { + if (!error() && rhs.error()) { + setError(rhs.error()); + } + if (isDone() && rhs.isDone()) { + mData->mDone = false; + } + mData = rhs.mData; + return *this; + } + void setDone() { mData->mDone = true; } -- cgit v1.2.3