forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.cpp
227 lines (194 loc) · 6.63 KB
/
action.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (C) 2013-2017 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "action_p.h"
#include "controller.h"
#include "context.h"
#include "common.h"
using namespace Cutelyst;
Action::Action(QObject *parent) : Component(new ActionPrivate, parent)
{
}
Action::Action(ActionPrivate *ptr, QObject *parent) : Component(ptr, parent)
{
}
Action::~Action()
{
}
Component::Modifiers Action::modifiers() const
{
return Component::OnlyExecute;
}
void Action::setMethod(const QMetaMethod &method)
{
Q_D(Action);
d->method = method;
if (method.returnType() == QMetaType::Bool) {
d->evaluateBool = true;
}
if (method.parameterCount() == 2 && method.parameterType(1) == QMetaType::QStringList) {
d->listSignature = true;
}
}
void Action::setController(Controller *controller)
{
Q_D(Action);
d->controller = controller;
}
void Action::setupAction(const QVariantHash &args, Application *app)
{
Q_D(Action);
init(app, args);
d->ns = args.value(QLatin1String("namespace")).toString();
const auto attributes = args.value(QLatin1String("attributes")).value<QMap<QString, QString> >();
d->attributes = attributes;
const QString argsAttr = attributes.value(QLatin1String("Args"));
if (!argsAttr.isEmpty()) {
d->numberOfArgs = argsAttr.toInt();
}
const QString capturesAttr = attributes.value(QLatin1String("CaptureArgs"));
if (!capturesAttr.isEmpty()) {
d->numberOfCaptures = capturesAttr.toInt();
}
}
QMap<QString, QString> Action::attributes() const
{
Q_D(const Action);
return d->attributes;
}
QString Action::attribute(const QString &name, const QString &defaultValue) const
{
Q_D(const Action);
return d->attributes.value(name, defaultValue);
}
void Action::setAttributes(const QMap<QString, QString> &attributes)
{
Q_D(Action);
d->attributes = attributes;
}
QString Action::className() const
{
Q_D(const Action);
return QString::fromLatin1(d->controller->metaObject()->className());
}
Controller *Action::controller() const
{
Q_D(const Action);
return d->controller;
}
bool Action::match(int numberOfArgs) const
{
Q_D(const Action);
// If the number of args is -1 (not defined)
// it will slurp all args so we don't care
// about how many args was passed, otherwise
// count them
return d->numberOfArgs == -1 || d->numberOfArgs == numberOfArgs;
}
bool Action::matchCaptures(int numberOfCaptures) const
{
Q_D(const Action);
// If the number of capture args is -1 (not defined)
// it will slurp all args so we don't care
// about how many args was passed, otherwise
// count them
return d->numberOfCaptures == -1 || d->numberOfCaptures == numberOfCaptures;
}
QString Action::ns() const
{
Q_D(const Action);
return d->ns;
}
qint8 Action::numberOfArgs() const
{
Q_D(const Action);
return d->numberOfArgs;
}
qint8 Action::numberOfCaptures() const
{
Q_D(const Action);
return d->numberOfCaptures;
}
bool Action::doExecute(Context *c)
{
Q_D(const Action);
if (c->detached()) {
return false;
}
bool ret;
if (d->evaluateBool) {
bool methodRet;
if (d->listSignature) {
ret = d->method.invoke(d->controller,
Qt::DirectConnection,
Q_RETURN_ARG(bool, methodRet),
Q_ARG(Cutelyst::Context*, c),
Q_ARG(QStringList, c->request()->args()));
} else {
QStringList args = c->request()->args();
// Fill the missing arguments
args.append(d->emptyArgs);
ret = d->method.invoke(d->controller,
Qt::DirectConnection,
Q_RETURN_ARG(bool, methodRet),
Q_ARG(Cutelyst::Context*, c),
Q_ARG(QString, args.at(0)),
Q_ARG(QString, args.at(1)),
Q_ARG(QString, args.at(2)),
Q_ARG(QString, args.at(3)),
Q_ARG(QString, args.at(4)),
Q_ARG(QString, args.at(5)),
Q_ARG(QString, args.at(6)),
Q_ARG(QString, args.at(7)),
Q_ARG(QString, args.at(8)));
}
if (ret) {
c->setState(methodRet);
return methodRet;
}
// The method failed to be called which means we should detach
c->detach();
c->setState(false);
return false;
} else {
if (d->listSignature) {
ret = d->method.invoke(d->controller,
Qt::DirectConnection,
Q_ARG(Cutelyst::Context*, c),
Q_ARG(QStringList, c->request()->args()));
} else {
QStringList args = c->request()->args();
// Fill the missing arguments
args.append(d->emptyArgs);
ret = d->method.invoke(d->controller,
Qt::DirectConnection,
Q_ARG(Cutelyst::Context*, c),
Q_ARG(QString, args.at(0)),
Q_ARG(QString, args.at(1)),
Q_ARG(QString, args.at(2)),
Q_ARG(QString, args.at(3)),
Q_ARG(QString, args.at(4)),
Q_ARG(QString, args.at(5)),
Q_ARG(QString, args.at(6)),
Q_ARG(QString, args.at(7)),
Q_ARG(QString, args.at(8)));
}
c->setState(ret);
return ret;
}
}
#include "moc_action.cpp"