cutelyst  4.9.0
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 using namespace Qt::Literals::StringLiterals;
18 
60  : Action(new RenderViewPrivate, parent)
61 {
62  setObjectName(QString::fromLatin1(metaObject()->className()) + u"->execute");
63 }
64 
65 bool RenderView::init(Cutelyst::Application *application, const QVariantHash &args)
66 {
67  Q_D(RenderView);
68 
69  const auto attributes = args.value(u"attributes"_s).value<ParamsMultiMap>();
70  d->view = application->view(attributes.value(u"View"_s));
71 
72  return Action::init(application, args);
73 }
74 
76 {
77  Q_D(const RenderView);
78 
79  if (!Action::doExecute(c)) {
80  return false;
81  }
82 
83  Response *res = c->res();
84  if (res->contentType().isEmpty()) {
85  res->setContentType("text/html; charset=utf-8"_ba);
86  }
87 
88  if (c->req()->isHead()) {
89  return true;
90  }
91 
92  if (res->hasBody()) {
93  return true;
94  }
95 
96  quint16 status = res->status();
97  if (status == 204 || (status >= 300 && status < 400)) {
98  return true;
99  }
100 
101  View *view = c->customView();
102  if (view) {
103  // Fist check if the user set a view
104  return c->forward(view);
105  } else if (d->view) {
106  // Then try to use the action View attribute
107  return c->forward(d->view);
108  }
109 
110  qCCritical(CUTELYST_RENDERVIEW) << "Could not find a view to render.";
111  res->setStatus(500);
112  return false;
113 }
114 
115 #include "moc_renderview.cpp"
bool doExecute(Context *c) override
Definition: action.cpp:137
Response * res() const noexcept
Definition: context.cpp:104
bool isEmpty() const const
Request req
Definition: context.h:67
Sensible default end action that forwards to a View.
Definition: renderview.h:14
void setContentType(const QByteArray &type)
Definition: response.h:238
This class represents a Cutelyst Action.
Definition: action.h:34
A Cutelyst response.
Definition: response.h:28
The Cutelyst Context.
Definition: context.h:42
bool hasBody() const noexcept
Definition: response.cpp:79
bool forward(Component *component)
Definition: context.cpp:394
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:57
The Cutelyst namespace holds all public Cutelyst API.
View * view(QStringView name={}) const
quint16 status() const noexcept
Definition: response.cpp:67
QByteArray contentType() const
Definition: response.cpp:189
bool doExecute(Cutelyst::Context *c) override
Definition: renderview.cpp:76
QString fromLatin1(QByteArrayView str)
Abstract View component for Cutelyst.
Definition: view.h:24
The Cutelyst application.
Definition: application.h:72
bool init(Application *application, const QVariantHash &args) override
Definition: renderview.cpp:66
bool isHead() const noexcept
Definition: request.cpp:337
void setStatus(quint16 status) noexcept
Definition: response.cpp:73
View * customView() const noexcept
Definition: context.cpp:164
RenderView(QObject *parent=nullptr)
Definition: renderview.cpp:60