Cutelyst  1.11.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 QString View::name() const
41 {
42  return Component::name();
43 }
44 
45 Component::Modifiers View::modifiers() const
46 {
47  return Component::OnlyExecute;
48 }
49 
50 bool View::doExecute(Context *c)
51 {
52  Response *response = c->response();
53  if (response->hasBody()) {
54  // Ignore if we already have a body
55  return true;
56  }
57 
58  const QByteArray output = render(c);
59  if (Q_UNLIKELY(c->error())) {
60  const auto errors = c->errors();
61  for (const QString &error : errors) {
62  qCCritical(CUTELYST_VIEW) << error;
63  }
64  }
65  response->setBody(output);
66 
67  return !c->error();
68 }
69 
70 #include "moc_view.cpp"
void setName(const QString &name)
Definition: component.cpp:45
QString name() const
Definition: component.cpp:39
View(QObject *parent, const QString &name=QString())
Definition: view.cpp:31
bool error() const
Returns true if an error was set.
Definition: context.cpp:64
The Cutelyst Component base class.
Definition: component.h:38
bool hasBody() const
Definition: response.cpp:96
The Cutelyst Context.
Definition: context.h:50
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
virtual QByteArray render(Context *c) const =0
QStringList errors() const
Returns a list of errors that were defined.
Definition: context.cpp:81
virtual Modifiers modifiers() const override
Definition: view.cpp:45
Response * response() const
Definition: context.cpp:111
void setBody(QIODevice *body)
Definition: response.cpp:119
QString name() const
Definition: view.cpp:40