cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
renderview.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2014-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "application.h"
6 #include "componentfactory.h"
7 #include "context.h"
8 #include "renderview_p.h"
9 #include "response.h"
10 #include "view.h"
11 
12 #include <QtCore/QLoggingCategory>
13 
14 Q_LOGGING_CATEGORY(CUTELYST_RENDERVIEW, "cutelyst.renderview", QtWarningMsg)
15 
16 using namespace Cutelyst;
17 
59  : Action(new RenderViewPrivate, parent)
60 {
62 }
63 
64 bool RenderView::init(Cutelyst::Application *application, const QVariantHash &args)
65 {
66  Q_D(RenderView);
67 
68  const auto attributes = args.value(u"attributes"_qs).value<ParamsMultiMap>();
69  d->view = application->view(attributes.value(u"View"_qs));
70 
71  return Action::init(application, args);
72 }
73 
75 {
76  Q_D(const RenderView);
77 
78  if (!Action::doExecute(c)) {
79  return false;
80  }
81 
82  Response *res = c->res();
83  if (res->contentType().isEmpty()) {
84  res->setContentType("text/html; charset=utf-8"_qba);
85  }
86 
87  if (c->req()->isHead()) {
88  return true;
89  }
90 
91  if (res->hasBody()) {
92  return true;
93  }
94 
95  quint16 status = res->status();
96  if (status == 204 || (status >= 300 && status < 400)) {
97  return true;
98  }
99 
100  View *view = c->customView();
101  if (view) {
102  // Fist check if the user set a view
103  return c->forward(view);
104  } else if (d->view) {
105  // Then try to use the action View attribute
106  return c->forward(d->view);
107  }
108 
109  qCCritical(CUTELYST_RENDERVIEW) << "Could not find a view to render.";
110  res->setStatus(500);
111  return false;
112 }
113 
114 #include "moc_renderview.cpp"
This class represents a Cutelyst Action.
Definition: action.h:35
bool doExecute(Context *c) override
Definition: action.cpp:136
QString className() const noexcept
Definition: action.cpp:86
ParamsMultiMap attributes() const noexcept
Definition: action.cpp:68
The Cutelyst application.
Definition: application.h:66
View * view(QStringView name={}) const
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:57
The Cutelyst Context.
Definition: context.h:42
bool forward(Component *component)
Definition: context.cpp:393
Response * res() const noexcept
Definition: context.cpp:103
Request * req
Definition: context.h:66
View * customView() const noexcept
Definition: context.cpp:163
Sensible default end action that forwards to a View.
Definition: renderview.h:15
bool doExecute(Cutelyst::Context *c) override
Definition: renderview.cpp:74
RenderView(QObject *parent=nullptr)
Definition: renderview.cpp:58
bool init(Application *application, const QVariantHash &args) override
Definition: renderview.cpp:64
bool isHead() const noexcept
Definition: request.cpp:336
A Cutelyst response.
Definition: response.h:29
void setContentType(const QByteArray &type)
Definition: response.h:238
bool hasBody() const noexcept
Definition: response.cpp:78
void setStatus(quint16 status) noexcept
Definition: response.cpp:72
quint16 status() const noexcept
Definition: response.cpp:66
QByteArray contentType() const
Definition: response.cpp:188
Abstract View component for Cutelyst.
Definition: view.h:25
The Cutelyst namespace holds all public Cutelyst API.
bool isEmpty() const const
T value(const Key &key, const T &defaultValue) const const
virtual const QMetaObject * metaObject() const const
void setObjectName(QAnyStringView name)
QString fromLatin1(QByteArrayView str)