summaryrefslogtreecommitdiffstats
path: root/framework/src/actions/actionhandler.h
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/actions/actionhandler.h')
-rw-r--r--framework/src/actions/actionhandler.h112
1 files changed, 0 insertions, 112 deletions
diff --git a/framework/src/actions/actionhandler.h b/framework/src/actions/actionhandler.h
deleted file mode 100644
index fbaedad1..00000000
--- a/framework/src/actions/actionhandler.h
+++ /dev/null
@@ -1,112 +0,0 @@
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
29namespace Kube {
30
31class ActionHandler : public QObject
32{
33 Q_OBJECT
34 Q_PROPERTY(QByteArray actionId READ actionId WRITE setActionId)
35
36public:
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
50private:
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 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 }
86protected:
87
88 virtual bool isActionReady(ContextType &) { return true; }
89 virtual KAsync::Job<void> execute(ContextType &) = 0;
90};
91
92class ActionHandlerHelper : public ActionHandler
93{
94public:
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;
106private:
107 const IsReadyFunction isReadyFunction;
108 const Handler handlerFunction;
109 const JobHandler jobHandlerFunction;
110};
111
112}