Cutelyst  2.5.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(parent)
66  , d_ptr(new RenderViewPrivate)
67 {
68  setObjectName(QString::fromLatin1(metaObject()->className()) + QLatin1String("->execute"));
69 }
70 
71 RenderView::~RenderView()
72 {
73  delete d_ptr;
74 }
75 
76 bool RenderView::init(Cutelyst::Application *application, const QVariantHash &args)
77 {
78  Q_D(RenderView);
79 
80  const auto attributes = args.value(QLatin1String("attributes")).value<ParamsMultiMap>();
81  d->view = application->view(attributes.value(QLatin1String("View")));
82 
83  return Action::init(application, args);
84 }
85 
87 {
88  Q_D(const RenderView);
89 
90  if (!Action::doExecute(c)) {
91  return false;
92  }
93 
94  Response *res = c->res();
95  if (res->contentType().isEmpty()) {
96  res->setContentType(QStringLiteral("text/html; charset=utf-8"));
97  }
98 
99  if (c->req()->method() == QLatin1String("HEAD")) {
100  return true;
101  }
102 
103  if (res->hasBody()) {
104  return true;
105  }
106 
107  quint16 status = res->status();
108  if (status == 204 || (status >= 300 && status < 400)) {
109  return true;
110  }
111 
112  View *view = c->customView();
113  if (view) {
114  // Fist check if the user set a view
115  return c->forward(view);
116  } else if (d->view) {
117  // Then try to use the action View attribute
118  return c->forward(d->view);
119  }
120 
121  qCCritical(CUTELYST_RENDERVIEW) << "Could not find a view to render.";
122  res->setStatus(500);
123  return false;
124 }
125 
126 #include "moc_renderview.cpp"
QMap< QString, QString > ParamsMultiMap
void setContentType(const QString &type)
Definition: response.h:206
virtual bool doExecute(Context *c) override
Definition: action.cpp:149
View * view(const QString &name=QString()) const
quint16 status() const
Definition: response.cpp:79
QMap< QString, QString > attributes() const
Definition: action.cpp:81
Response * res() const
Definition: context.cpp:116
void setStatus(quint16 status)
Definition: response.cpp:85
Sensible default end action.
Definition: renderview.h:28
bool hasBody() const
Definition: response.cpp:91
This class represents a Cutelyst Action.
Definition: action.h:47
The Cutelyst Context.
Definition: context.h:50
bool forward(Component *component)
Definition: context.cpp:326
virtual bool init(Application *application, const QVariantHash &args)
Definition: component.cpp:63
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
virtual bool doExecute(Cutelyst::Context *c) override
Definition: renderview.cpp:86
QString contentType() const
Definition: response.cpp:187
Cutelyst View abstract view component
Definition: view.h:33
View * customView() const
Definition: context.cpp:176
The Cutelyst Application.
Definition: application.h:55
virtual bool init(Application *application, const QVariantHash &args) override
Definition: renderview.cpp:76
RenderView(QObject *parent=nullptr)
Definition: renderview.cpp:65
QString className() const
Definition: action.cpp:99