Cutelyst  2.13.0
actionchain.cpp
1 /*
2  * Copyright (C) 2015-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "actionchain_p.h"
19 #include "request_p.h"
20 
21 #include "context.h"
22 
23 using namespace Cutelyst;
24 
25 ActionChain::ActionChain(const ActionList &chain, QObject *parent) : Action(new ActionChainPrivate, parent)
26 {
27  Q_D(ActionChain);
28  d->chain = chain;
29 
30  const Action *final = d->chain.last();
31 
32  QVariantHash args;
33  args.insert(QStringLiteral("namespace"), final->ns());
34  setupAction(args, nullptr);
35 
36  setName(QLatin1Char('_') + final->name());
37  setReverse(final->reverse());
38  setAttributes(final->attributes());
39  setController(final->controller());
40 
41  for (Action *action : chain) {
42  // FINAL should not have captures?
43  if (/*action != final && */action->numberOfCaptures() > 0) {
44  d->captures += action->numberOfCaptures();
45  }
46  }
47 }
48 
50 {
51  Q_D(const ActionChain);
52  return d->chain;
53 }
54 
56 {
57  Q_D(const ActionChain);
58  return d->captures;
59 }
60 
62 {
63  Q_D(const ActionChain);
64 
65  Request *request = c->request();
66  const QStringList captures = request->captures();
67  const QStringList currentArgs = request->args();
68  const ActionList chain = d->chain;
69  Action *final = d->chain.last();
70 
71  int captured = 0;
72  for (Action *action : chain) {
73  if (action != final) {
74  QStringList args;
75  while (args.size() < action->numberOfCaptures() && captured < captures.size()) {
76  args.append(captures.at(captured++));
77  }
78 
79  request->setArguments(args);
80  if (!action->dispatch(c)) {
81  return false;
82  }
83  }
84  }
85  request->setArguments(currentArgs);
86 
87  return final->dispatch(c);
88 }
89 
90 #include "moc_actionchain.cpp"
Cutelyst::Action::setupAction
void setupAction(const QVariantHash &args, Application *app)
Definition: action.cpp:57
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::Request
Definition: request.h:42
Cutelyst::ActionChain::numberOfCaptures
virtual qint8 numberOfCaptures() const override
Definition: actionchain.cpp:55
Cutelyst::ActionChain
Holds a chain of Cutelyst Actions.
Definition: actionchain.h:36
Cutelyst::ActionChain::doExecute
virtual bool doExecute(Context *c) override
Definition: actionchain.cpp:61
Cutelyst::Component::setReverse
void setReverse(const QString &reverse)
Definition: component.cpp:62
Cutelyst::Request::setArguments
void setArguments(const QStringList &arguments)
Definition: request.cpp:168
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::ActionChain::chain
ActionList chain() const
Definition: actionchain.cpp:49
Cutelyst::Action
This class represents a Cutelyst Action.
Definition: action.h:47
Cutelyst::Component::setName
void setName(const QString &name)
Definition: component.cpp:50
Cutelyst::Action::setController
void setController(Controller *controller)
Definition: action.cpp:51
Cutelyst::ActionChain::ActionChain
ActionChain(const ActionList &chain, QObject *parent=nullptr)
Definition: actionchain.cpp:25
Cutelyst::Request::captures
QStringList captures() const
Definition: request.cpp:174
Cutelyst::Action::setAttributes
void setAttributes(const QMap< QString, QString > &attributes)
Definition: action.cpp:91
Cutelyst::ActionList
QVector< Action * > ActionList
Definition: action.h:166