Cutelyst  2.13.0
renderview.cpp
1 /*
2  * Copyright (C) 2014-2018 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 "renderview_p.h"
20 
21 #include "response.h"
22 #include "view.h"
23 #include "application.h"
24 #include "context.h"
25 #include "componentfactory.h"
26 
27 #include <QtCore/QLoggingCategory>
28 
29 Q_LOGGING_CATEGORY(CUTELYST_RENDERVIEW, "cutelyst.renderview", QtWarningMsg)
30 
31 using namespace Cutelyst;
32 
65 RenderView::RenderView(QObject *parent) : Action(new RenderViewPrivate, parent)
66 {
67  setObjectName(QString::fromLatin1(metaObject()->className()) + QLatin1String("->execute"));
68 }
69 
70 bool RenderView::init(Cutelyst::Application *application, const QVariantHash &args)
71 {
72  Q_D(RenderView);
73 
74  const auto attributes = args.value(QLatin1String("attributes")).value<ParamsMultiMap>();
75  d->view = application->view(attributes.value(QLatin1String("View")));
76 
77  return Action::init(application, args);
78 }
79 
81 {
82  Q_D(const RenderView);
83 
84  if (!Action::doExecute(c)) {
85  return false;
86  }
87 
88  Response *res = c->res();
89  if (res->contentType().isEmpty()) {
90  res->setContentType(QStringLiteral("text/html; charset=utf-8"));
91  }
92 
93  if (c->req()->method() == QLatin1String("HEAD")) {
94  return true;
95  }
96 
97  if (res->hasBody()) {
98  return true;
99  }
100 
101  quint16 status = res->status();
102  if (status == 204 || (status >= 300 && status < 400)) {
103  return true;
104  }
105 
106  View *view = c->customView();
107  if (view) {
108  // Fist check if the user set a view
109  return c->forward(view);
110  } else if (d->view) {
111  // Then try to use the action View attribute
112  return c->forward(d->view);
113  }
114 
115  qCCritical(CUTELYST_RENDERVIEW) << "Could not find a view to render.";
116  res->setStatus(500);
117  return false;
118 }
119 
120 #include "moc_renderview.cpp"
Cutelyst::RenderView::RenderView
RenderView(QObject *parent=nullptr)
Definition: renderview.cpp:65
Cutelyst::ParamsMultiMap
QMap< QString, QString > ParamsMultiMap
Definition: paramsmultimap.h:36
Cutelyst::Response::setStatus
void setStatus(quint16 status)
Definition: response.cpp:85
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Application::view
View * view(const QString &name=QString()) const
Definition: application.cpp:185
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::Component::init
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:68
Cutelyst::Response::setContentType
void setContentType(const QString &type)
Definition: response.h:218
Cutelyst::RenderView::init
virtual bool init(Application *application, const QVariantHash &args) override
Definition: renderview.cpp:70
Cutelyst::Response::hasBody
bool hasBody() const
Definition: response.cpp:91
Cutelyst::Context::customView
View * customView() const
Definition: context.cpp:176
Cutelyst::View
Cutelyst View abstract view component
Definition: view.h:34
Cutelyst::Action::attributes
QMap< QString, QString > attributes() const
Definition: action.cpp:79
Cutelyst::RenderView
Sensible default end action.
Definition: renderview.h:28
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Action::doExecute
virtual bool doExecute(Context *c) override
Definition: action.cpp:147
Cutelyst::Action
This class represents a Cutelyst Action.
Definition: action.h:47
Cutelyst::Context::res
Response * res() const
Definition: context.cpp:116
Cutelyst::Action::className
QString className() const
Definition: action.cpp:97
Cutelyst::Response::status
quint16 status() const
Definition: response.cpp:79
Cutelyst::RenderView::doExecute
virtual bool doExecute(Cutelyst::Context *c) override
Definition: renderview.cpp:80
Cutelyst::Response::contentType
QString contentType() const
Definition: response.cpp:204
Cutelyst::Context::forward
bool forward(Component *component)
Definition: context.cpp:385
Cutelyst::Response
Definition: response.h:34