diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-01-01 13:37:19 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-01-02 00:31:41 +0100 |
commit | ba7128b30850594c7efb258d1794e377eede364a (patch) | |
tree | f805d511f6d12b0799c05b414054db8b8635bfc9 /framework/actions/actionhandler.h | |
parent | 431c257dd29e2e3d8878db200f0de4d452bffe92 (diff) | |
download | kube-ba7128b30850594c7efb258d1794e377eede364a.tar.gz kube-ba7128b30850594c7efb258d1794e377eede364a.zip |
Instead of using the action system we use controllers only.
It's simpler, and the action system was just too complex to use in a
typesafe way.
Diffstat (limited to 'framework/actions/actionhandler.h')
-rw-r--r-- | framework/actions/actionhandler.h | 55 |
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 | ||
28 | namespace Kube { | 29 | namespace Kube { |
29 | class Context; | ||
30 | 30 | ||
31 | class ActionHandler : public QObject | 31 | class ActionHandler : public QObject |
32 | { | 32 | { |
@@ -35,6 +35,7 @@ class ActionHandler : public QObject | |||
35 | 35 | ||
36 | public: | 36 | public: |
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 | |||
46 | private: | 50 | private: |
47 | QByteArray mActionId; | 51 | QByteArray mActionId; |
52 | QSet<QByteArray> mRequiredProperties; | ||
53 | }; | ||
54 | |||
55 | template <typename ContextType> | ||
56 | class ActionHandlerBase : public ActionHandler | ||
57 | { | ||
58 | public: | ||
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 | } | ||
86 | protected: | ||
87 | |||
88 | virtual bool isActionReady(ContextType &) { return true; } | ||
89 | virtual KAsync::Job<void> execute(ContextType &) = 0; | ||
48 | }; | 90 | }; |
49 | 91 | ||
50 | class ActionHandlerHelper : public ActionHandler | 92 | class ActionHandlerHelper : public ActionHandler |
51 | { | 93 | { |
52 | Q_OBJECT | ||
53 | public: | 94 | public: |
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; |
65 | private: | 106 | private: |
66 | const IsReadyFunction isReadyFunction; | 107 | const IsReadyFunction isReadyFunction; |
67 | const Handler handlerFunction; | 108 | const Handler handlerFunction; |