summaryrefslogtreecommitdiffstats
path: root/framework/actions/actionhandler.h
diff options
context:
space:
mode:
Diffstat (limited to 'framework/actions/actionhandler.h')
-rw-r--r--framework/actions/actionhandler.h55
1 files changed, 48 insertions, 7 deletions
diff --git a/framework/actions/actionhandler.h b/framework/actions/actionhandler.h
index 09ed13c6..5ccf0ac7 100644
--- a/framework/actions/actionhandler.h
+++ b/framework/actions/actionhandler.h
@@ -24,9 +24,9 @@
24#include <Async/Async> 24#include <Async/Async>
25 25
26#include "actionresult.h" 26#include "actionresult.h"
27#include "context.h"
27 28
28namespace Kube { 29namespace Kube {
29class Context;
30 30
31class ActionHandler : public QObject 31class ActionHandler : public QObject
32{ 32{
@@ -35,6 +35,7 @@ class ActionHandler : public QObject
35 35
36public: 36public:
37 ActionHandler(QObject *parent = 0); 37 ActionHandler(QObject *parent = 0);
38 virtual ~ActionHandler();
38 39
39 virtual bool isActionReady(Context *context); 40 virtual bool isActionReady(Context *context);
40 41
@@ -43,25 +44,65 @@ public:
43 void setActionId(const QByteArray &); 44 void setActionId(const QByteArray &);
44 QByteArray actionId() const; 45 QByteArray actionId() const;
45 46
47 void setRequiredProperties(const QSet<QByteArray> &requiredProperties);
48 QSet<QByteArray> requiredProperties() const;
49
46private: 50private:
47 QByteArray mActionId; 51 QByteArray mActionId;
52 QSet<QByteArray> mRequiredProperties;
53};
54
55template <typename ContextType>
56class ActionHandlerBase : public ActionHandler
57{
58public:
59 ActionHandlerBase(const QByteArray &actionId)
60 : ActionHandler{}
61 {
62 setActionId(actionId);
63 }
64
65 bool isActionReady(Context *c) Q_DECL_OVERRIDE
66 {
67 auto wrapper = ContextType{*c};
68 return isActionReady(wrapper);
69 }
70
71 ActionResult execute(Context *c) Q_DECL_OVERRIDE
72 {
73 ActionResult result;
74 auto wrapper = ContextType{*c};
75 execute(wrapper)
76 .template syncThen<void>([=](const KAsync::Error &error) {
77 auto modifyableResult = result;
78 if (error) {
79 qWarning() << "Job failed: " << error.errorCode << error.errorMessage;
80 modifyableResult.setError(1);
81 }
82 modifyableResult.setDone();
83 }).exec();
84 return result;
85 }
86protected:
87
88 virtual bool isActionReady(ContextType &) { return true; }
89 virtual KAsync::Job<void> execute(ContextType &) = 0;
48}; 90};
49 91
50class ActionHandlerHelper : public ActionHandler 92class ActionHandlerHelper : public ActionHandler
51{ 93{
52 Q_OBJECT
53public: 94public:
54 typedef std::function<bool(Context*)> IsReadyFunction; 95 typedef std::function<bool(Context *)> IsReadyFunction;
55 typedef std::function<void(Context*)> Handler; 96 typedef std::function<void(Context *)> Handler;
56 typedef std::function<KAsync::Job<void>(Context*)> JobHandler; 97 typedef std::function<KAsync::Job<void>(Context *)> JobHandler;
57 98
58 ActionHandlerHelper(const Handler &); 99 ActionHandlerHelper(const Handler &);
59 ActionHandlerHelper(const IsReadyFunction &, const Handler &); 100 ActionHandlerHelper(const IsReadyFunction &, const Handler &);
60 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const Handler &); 101 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const Handler &);
61 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const JobHandler &); 102 ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const JobHandler &);
62 103
63 bool isActionReady(Context *context) Q_DECL_OVERRIDE; 104 bool isActionReady(Context *) Q_DECL_OVERRIDE;
64 ActionResult execute(Context *context) Q_DECL_OVERRIDE; 105 ActionResult execute(Context *) Q_DECL_OVERRIDE;
65private: 106private:
66 const IsReadyFunction isReadyFunction; 107 const IsReadyFunction isReadyFunction;
67 const Handler handlerFunction; 108 const Handler handlerFunction;