summaryrefslogtreecommitdiffstats
path: root/framework/src/actions/actionhandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/actions/actionhandler.cpp')
-rw-r--r--framework/src/actions/actionhandler.cpp151
1 files changed, 151 insertions, 0 deletions
diff --git a/framework/src/actions/actionhandler.cpp b/framework/src/actions/actionhandler.cpp
new file mode 100644
index 00000000..99fdf66a
--- /dev/null
+++ b/framework/src/actions/actionhandler.cpp
@@ -0,0 +1,151 @@
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
20#include "actionhandler.h"
21
22#include "context.h"
23#include "actionbroker.h"
24#include <QDebug>
25
26using namespace Kube;
27
28ActionHandler::ActionHandler(QObject *parent)
29 : QObject(parent)
30{
31
32}
33
34ActionHandler::~ActionHandler()
35{
36 ActionBroker::instance().unregisterHandler(mActionId, this);
37}
38
39bool ActionHandler::isActionReady(Context *context)
40{
41 if (context) {
42 QVariant returnedValue;
43 QMetaObject::invokeMethod(this, "isReady",
44 Q_RETURN_ARG(QVariant, returnedValue),
45 Q_ARG(QVariant, QVariant::fromValue(context)));
46 return returnedValue.toBool();
47 } else {
48 qWarning() << "The handler didn't get a context";
49 }
50 return false;
51}
52
53ActionResult ActionHandler::execute(Context *context)
54{
55 ActionResult result;
56 QVariant returnedValue;
57 qWarning() << "Executing the handler";
58 if (context) {
59 //The base implementation to call the handler in QML
60 QMetaObject::invokeMethod(this, "handler",
61 Q_RETURN_ARG(QVariant, returnedValue),
62 Q_ARG(QVariant, QVariant::fromValue(context)));
63 //TODO: support async handlers in QML
64 result.setDone();
65 } else {
66 qWarning() << "The handler didn't get a context";
67 result.setDone();
68 result.setError(1);
69 }
70 return result;
71}
72
73void ActionHandler::setActionId(const QByteArray &actionId)
74{
75 //Reassigning the id is not supported
76 Q_ASSERT(mActionId.isEmpty());
77 mActionId = actionId;
78 ActionBroker::instance().registerHandler(actionId, this);
79}
80
81QByteArray ActionHandler::actionId() const
82{
83 return mActionId;
84}
85
86void ActionHandler::setRequiredProperties(const QSet<QByteArray> &requiredProperties)
87{
88 mRequiredProperties = requiredProperties;
89}
90
91QSet<QByteArray> ActionHandler::requiredProperties() const
92{
93 return mRequiredProperties;
94}
95
96
97ActionHandlerHelper::ActionHandlerHelper(const Handler &handler)
98 : ActionHandler(nullptr),
99 handlerFunction(handler)
100{
101}
102
103ActionHandlerHelper::ActionHandlerHelper(const IsReadyFunction &isReady, const Handler &handler)
104 : ActionHandler(nullptr),
105 isReadyFunction(isReady),
106 handlerFunction(handler)
107{
108}
109
110ActionHandlerHelper::ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &isReady, const Handler &handler)
111 : ActionHandler(nullptr),
112 isReadyFunction(isReady),
113 handlerFunction(handler)
114{
115 setActionId(actionId);
116}
117
118ActionHandlerHelper::ActionHandlerHelper(const QByteArray &actionId, const IsReadyFunction &isReady, const JobHandler &handler)
119 : ActionHandler(nullptr),
120 isReadyFunction(isReady),
121 jobHandlerFunction(handler)
122{
123 setActionId(actionId);
124}
125
126bool ActionHandlerHelper::isActionReady(Context *context)
127{
128 if (isReadyFunction) {
129 return isReadyFunction(context);
130 }
131 return true;
132}
133
134ActionResult ActionHandlerHelper::execute(Context *context)
135{
136 ActionResult result;
137 if (handlerFunction) {
138 handlerFunction(context);
139 result.setDone();
140 } else {
141 jobHandlerFunction(context).then([=](const KAsync::Error &error) {
142 auto modifyableResult = result;
143 if (error) {
144 qWarning() << "Job failed: " << error.errorCode << error.errorMessage;
145 modifyableResult.setError(1);
146 }
147 modifyableResult.setDone();
148 }).exec();
149 }
150 return result;
151}