Cutelyst  2.5.0
view.cpp
1 /*
2  * Copyright (C) 2013-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 
19 #include "view.h"
20 
21 #include "common.h"
22 
23 #include <Cutelyst/Context>
24 #include <Cutelyst/Response>
25 
26 #include <QtCore/QVariant>
27 #include <QtCore/QLoggingCategory>
28 
29 using namespace Cutelyst;
30 
31 View::View(QObject *parent, const QString &name) : Component(parent)
32 {
33  setName(name);
34 }
35 
36 View::~View()
37 {
38 }
39 
40 Component::Modifiers View::modifiers() const
41 {
42  return Component::OnlyExecute;
43 }
44 
45 bool View::doExecute(Context *c)
46 {
47  Response *response = c->response();
48  if (response->hasBody()) {
49  // Ignore if we already have a body
50  return true;
51  }
52 
53  const QByteArray output = render(c);
54  if (Q_UNLIKELY(c->error())) {
55  const auto errors = c->errors();
56  for (const QString &error : errors) {
57  qCCritical(CUTELYST_VIEW) << error;
58  }
59  }
60  response->setBody(output);
61 
62  return !c->error();
63 }
64 
65 #include "moc_view.cpp"
void setName(const QString &name)
Definition: component.cpp:45
The Cutelyst Component base class.
Definition: component.h:38
bool hasBody() const
Definition: response.cpp:91
The Cutelyst Context.
Definition: context.h:50
QStringList errors() const
Returns a list of errors that were defined.
Definition: context.cpp:80
Response * response() const
Definition: context.cpp:110
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
virtual QByteArray render(Context *c) const =0
virtual Modifiers modifiers() const override
Definition: view.cpp:40
View(QObject *parent, const QString &name)
Definition: view.cpp:31
void setBody(QIODevice *body)
Definition: response.cpp:114
bool error() const
Returns true if an error was set.
Definition: context.cpp:63