diff options
Diffstat (limited to 'framework/src/actions/actionbroker.cpp')
-rw-r--r-- | framework/src/actions/actionbroker.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/framework/src/actions/actionbroker.cpp b/framework/src/actions/actionbroker.cpp new file mode 100644 index 00000000..f6bfdd8e --- /dev/null +++ b/framework/src/actions/actionbroker.cpp | |||
@@ -0,0 +1,101 @@ | |||
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 "actionbroker.h" | ||
21 | |||
22 | #include "context.h" | ||
23 | #include "actionhandler.h" | ||
24 | #include <sink/log.h> | ||
25 | |||
26 | #include <QDebug> | ||
27 | |||
28 | using namespace Kube; | ||
29 | |||
30 | SINK_DEBUG_AREA("actionbroker") | ||
31 | |||
32 | ActionBroker::ActionBroker(QObject *parent) | ||
33 | : QObject(parent) | ||
34 | { | ||
35 | |||
36 | } | ||
37 | |||
38 | ActionBroker &ActionBroker::instance() | ||
39 | { | ||
40 | static ActionBroker instance; | ||
41 | return instance; | ||
42 | } | ||
43 | |||
44 | bool ActionBroker::isActionReady(const QByteArray &actionId, Context *context, const QList<QPointer<ActionHandler>> &preHandler) | ||
45 | { | ||
46 | if (!context) { | ||
47 | return false; | ||
48 | } | ||
49 | for (const auto handler : preHandler) { | ||
50 | if (!handler->isActionReady(context)) { | ||
51 | return false; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | for (const auto handler : mHandler.values(actionId)) { | ||
56 | if (handler) { | ||
57 | if (handler->isActionReady(context)) { | ||
58 | return true; | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | |||
63 | return false; | ||
64 | } | ||
65 | |||
66 | ActionResult ActionBroker::executeAction(const QByteArray &actionId, Context *context, const QList<QPointer<ActionHandler>> &preHandler, const QList<QPointer<ActionHandler>> &postHandler) | ||
67 | { | ||
68 | ActionResult result; | ||
69 | if (context) { | ||
70 | SinkLog() << "Executing action " << actionId; | ||
71 | SinkLog() << *context; | ||
72 | for (const auto handler : preHandler) { | ||
73 | handler->execute(context); | ||
74 | } | ||
75 | //TODO the main handler should only execute once the pre handler is done | ||
76 | for (const auto handler : mHandler.values(actionId)) { | ||
77 | if (handler) { | ||
78 | result += handler->execute(context); | ||
79 | } | ||
80 | } | ||
81 | //TODO the post handler should only execute once the main handler is done | ||
82 | for (const auto handler : postHandler) { | ||
83 | handler->execute(context); | ||
84 | } | ||
85 | } else { | ||
86 | SinkWarning() << "Can't execute without context"; | ||
87 | result.setDone(); | ||
88 | result.setError(1); | ||
89 | } | ||
90 | return result; | ||
91 | } | ||
92 | |||
93 | void ActionBroker::registerHandler(const QByteArray &actionId, ActionHandler *handler) | ||
94 | { | ||
95 | mHandler.insert(actionId, handler); | ||
96 | } | ||
97 | |||
98 | void ActionBroker::unregisterHandler(const QByteArray &actionId, ActionHandler *handler) | ||
99 | { | ||
100 | mHandler.remove(actionId, handler); | ||
101 | } | ||