Cutelyst  3.1.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 {
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  qDebug() << "render view" << attributes;
76  d->view = application->view(attributes.value(QLatin1String("View")));
77 
78  return Action::init(application, args);
79 }
80 
82 {
83  Q_D(const RenderView);
84 
85  if (!Action::doExecute(c)) {
86  return false;
87  }
88 
89  Response *res = c->res();
90  if (res->contentType().isEmpty()) {
91  res->setContentType(QStringLiteral("text/html; charset=utf-8"));
92  }
93 
94  if (c->req()->method() == QLatin1String("HEAD")) {
95  return true;
96  }
97 
98  if (res->hasBody()) {
99  return true;
100  }
101 
102  quint16 status = res->status();
103  if (status == 204 || (status >= 300 && status < 400)) {
104  return true;
105  }
106 
107  View *view = c->customView();
108  if (view) {
109  // Fist check if the user set a view
110  return c->forward(view);
111  } else if (d->view) {
112  // Then try to use the action View attribute
113  return c->forward(d->view);
114  }
115 
116  qCCritical(CUTELYST_RENDERVIEW) << "Could not find a view to render.";
117  res->setStatus(500);
118  return false;
119 }
120 
121 #include "moc_renderview.cpp"
This class represents a Cutelyst Action.
Definition: action.h:48
virtual bool doExecute(Context *c) override
Definition: action.cpp:147
ParamsMultiMap attributes() const
Definition: action.cpp:79
QString className() const
Definition: action.cpp:97
The Cutelyst Application.
Definition: application.h:56
View * view(const QString &name=QString()) const
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:68
The Cutelyst Context.
Definition: context.h:52
Response * res() const
Definition: context.cpp:116
bool forward(Component *component)
Definition: context.cpp:390
View * customView() const
Definition: context.cpp:176
Sensible default end action.
Definition: renderview.h:29
virtual bool doExecute(Cutelyst::Context *c) override
Definition: renderview.cpp:81
RenderView(QObject *parent=nullptr)
Definition: renderview.cpp:65
virtual bool init(Application *application, const QVariantHash &args) override
Definition: renderview.cpp:70
QString contentType() const
Definition: response.cpp:204
quint16 status() const
Definition: response.cpp:79
void setStatus(quint16 status)
Definition: response.cpp:85
bool hasBody() const
Definition: response.cpp:91
void setContentType(const QString &type)
Definition: response.h:218
Cutelyst View abstract view component
Definition: view.h:35
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
const T value(const Key &key, const T &defaultValue) const const
virtual const QMetaObject * metaObject() const const
void setObjectName(const QString &name)
QString fromLatin1(const char *str, int size)
bool isEmpty() const const