diff options
Diffstat (limited to 'framework/src/actions/actionhandler.cpp')
-rw-r--r-- | framework/src/actions/actionhandler.cpp | 151 |
1 files changed, 0 insertions, 151 deletions
diff --git a/framework/src/actions/actionhandler.cpp b/framework/src/actions/actionhandler.cpp deleted file mode 100644 index 99fdf66a..00000000 --- a/framework/src/actions/actionhandler.cpp +++ /dev/null | |||
@@ -1,151 +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 | |||
20 | #include "actionhandler.h" | ||
21 | |||
22 | #include "context.h" | ||
23 | #include "actionbroker.h" | ||
24 | #include <QDebug> | ||
25 | |||
26 | using namespace Kube; | ||
27 | |||
28 | ActionHandler::ActionHandler(QObject *parent) | ||
29 | : QObject(parent) | ||
30 | { | ||
31 | |||
32 | } | ||
33 | |||
34 | ActionHandler::~ActionHandler() | ||
35 | { | ||
36 | ActionBroker::instance().unregisterHandler(mActionId, this); | ||
37 | } | ||
38 | |||
39 | bool 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 | |||
53 | ActionResult 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 | |||
73 | void 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 | |||
81 | QByteArray ActionHandler::actionId() const | ||
82 | { | ||
83 | return mActionId; | ||
84 | } | ||
85 | |||
86 | void ActionHandler::setRequiredProperties(const QSet<QByteArray> &requiredProperties) | ||
87 | { | ||
88 | mRequiredProperties = requiredProperties; | ||
89 | } | ||
90 | |||
91 | QSet<QByteArray> ActionHandler::requiredProperties() const | ||
92 | { | ||
93 | return mRequiredProperties; | ||
94 | } | ||
95 | |||
96 | |||
97 | ActionHandlerHelper::ActionHandlerHelper(const Handler &handler) | ||
98 | : ActionHandler(nullptr), | ||
99 | handlerFunction(handler) | ||
100 | { | ||
101 | } | ||
102 | |||
103 | ActionHandlerHelper::ActionHandlerHelper(const IsReadyFunction &isReady, const Handler &handler) | ||
104 | : ActionHandler(nullptr), | ||
105 | isReadyFunction(isReady), | ||
106 | handlerFunction(handler) | ||
107 | { | ||
108 | } | ||
109 | |||
110 | ActionHandlerHelper::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 | |||
118 | ActionHandlerHelper::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 | |||
126 | bool ActionHandlerHelper::isActionReady(Context *context) | ||
127 | { | ||
128 | if (isReadyFunction) { | ||
129 | return isReadyFunction(context); | ||
130 | } | ||
131 | return true; | ||
132 | } | ||
133 | |||
134 | ActionResult 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 | } | ||