diff options
Diffstat (limited to 'framework/src/actions/actionhandler.h')
-rw-r--r-- | framework/src/actions/actionhandler.h | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/framework/src/actions/actionhandler.h b/framework/src/actions/actionhandler.h new file mode 100644 index 00000000..fbaedad1 --- /dev/null +++ b/framework/src/actions/actionhandler.h | |||
@@ -0,0 +1,112 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #pragma once | ||
20 | |||
21 | #include <QObject> | ||
22 | #include <QMultiMap> | ||
23 | #include <functional> | ||
24 | #include <KAsync/Async> | ||
25 | |||
26 | #include "actionresult.h" | ||
27 | #include "context.h" | ||
28 | |||
29 | namespace Kube { | ||
30 | |||
31 | class ActionHandler : public QObject | ||
32 | { | ||
33 | Q_OBJECT | ||
34 | Q_PROPERTY(QByteArray actionId READ actionId WRITE setActionId) | ||
35 | |||
36 | public: | ||
37 | ActionHandler(QObject *parent = 0); | ||
38 | virtual ~ActionHandler(); | ||
39 | |||
40 | virtual bool isActionReady(Context *context); | ||
41 | |||
42 | virtual ActionResult execute(Context *context); | ||
43 | |||
44 | void setActionId(const QByteArray &); | ||
45 | QByteArray actionId() const; | ||
46 | |||
47 | void setRequiredProperties(const QSet<QByteArray> &requiredProperties); | ||
48 | QSet<QByteArray> requiredProperties() const; | ||
49 | |||
50 | private: | ||
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 then([=](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; | ||
90 | }; | ||
91 | |||
92 | class ActionHandlerHelper : public ActionHandler | ||
93 | { | ||
94 | public: | ||
95 | typedef std::function<bool(Context *)> IsReadyFunction; | ||
96 | typedef std::function<void(Context *)> Handler; | ||
97 | typedef std::function<KAsync::Job<void>(Context *)> JobHandler; | ||
98 | |||
99 | ActionHandlerHelper(const Handler &); | ||
100 | ActionHandlerHelper(const IsReadyFunction &, const Handler &); | ||
101 | ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const Handler &); | ||
102 | ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &, const JobHandler &); | ||
103 | |||
104 | bool isActionReady(Context *) Q_DECL_OVERRIDE; | ||
105 | ActionResult execute(Context *) Q_DECL_OVERRIDE; | ||
106 | private: | ||
107 | const IsReadyFunction isReadyFunction; | ||
108 | const Handler handlerFunction; | ||
109 | const JobHandler jobHandlerFunction; | ||
110 | }; | ||
111 | |||
112 | } | ||