Cutelyst  2.3.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(parent)
26  , d_ptr(new ActionChainPrivate)
27 {
28  Q_D(ActionChain);
29  d->chain = chain;
30 
31  Action *final = d->chain.takeLast();
32  d->final = final;
33 
34  QVariantHash args;
35  args.insert(QStringLiteral("namespace"), final->ns());
36  setupAction(args, 0);
37 
38  setName(final->name());
39  setReverse(final->reverse());
40  setAttributes(final->attributes());
41  setController(final->controller());
42 
43  for (Action *action : chain) {
44  if (action->numberOfCaptures() > 0) {
45  d->captures += action->numberOfCaptures();
46  }
47  }
48 }
49 
50 ActionChain::~ActionChain()
51 {
52  delete d_ptr;
53 }
54 
56 {
57  Q_D(const ActionChain);
58  return d->chain;
59 }
60 
62 {
63  Q_D(const ActionChain);
64  return d->captures;
65 }
66 
68 {
69  Q_D(ActionChain);
70 
71  Request *request = c->request();
72  const QStringList captures = request->captures();
73  const QStringList currentArgs = request->args();
74  const ActionList chain = d->chain;
75  Action *final = d->final;
76 
77  int captured = 0;
78  for (Action *action : chain) {
79  QStringList args;
80  while (args.size() < action->numberOfCaptures() && captured < captures.size()) {
81  args.append(captures.at(captured++));
82  }
83 
84  request->setArguments(args);
85  if (!action->dispatch(c)) {
86  return false;
87  }
88  }
89  request->setArguments(currentArgs);
90 
91  return final->dispatch(c);
92 }
93 
94 #include "moc_actionchain.cpp"
void setName(const QString &name)
Definition: component.cpp:45
QVector< Action * > ActionList
Definition: action.h:162
QStringList captures() const
Definition: request.cpp:171
void setReverse(const QString &reverse)
Definition: component.cpp:57
Holds a chain of Cutelyst Actions.
Definition: actionchain.h:36
void setAttributes(const QMap< QString, QString > &attributes)
Definition: action.cpp:93
virtual qint8 numberOfCaptures() const override
Definition: actionchain.cpp:61
This class represents a Cutelyst Action.
Definition: action.h:47
The Cutelyst Context.
Definition: context.h:50
virtual bool doExecute(Context *c) override
Definition: actionchain.cpp:67
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void setArguments(const QStringList &arguments)
Definition: request.cpp:165
void setController(Controller *controller)
Definition: action.cpp:53
void setupAction(const QVariantHash &args, Application *app)
Definition: action.cpp:59
ActionChain(const ActionList &chain, QObject *parent=nullptr)
Definition: actionchain.cpp:25
ActionList chain() const
Definition: actionchain.cpp:55