cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
context.h
1/*
2 * SPDX-FileCopyrightText: (C) 2013-2023 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#pragma once
6
7#include <Cutelyst/async.h>
8#include <Cutelyst/cutelyst_global.h>
9#include <Cutelyst/request.h>
10
11#include <QtCore/QObject>
12#include <QtCore/QStack>
13#include <QtCore/QStringList>
14#include <QtCore/QUrl>
15#include <QtCore/QVariant>
16
17namespace Cutelyst {
18
19class Action;
20class Application;
21class Component;
22class Engine;
23class Response;
24class Dispatcher;
25class Controller;
26class View;
27class Stats;
28class Plugin;
29class ContextPrivate;
30
41class CUTELYST_LIBRARY Context : public QObject
42{
43 Q_OBJECT
47 Q_PROPERTY(Action *action READ action CONSTANT)
51 Q_PROPERTY(QString actionName READ actionName CONSTANT)
56 Q_PROPERTY(QString ns READ ns CONSTANT)
61 Q_PROPERTY(QString namespace READ ns CONSTANT)
66 Q_PROPERTY(Request *req READ request CONSTANT)
71 Q_PROPERTY(Request *request READ request CONSTANT)
75 Q_PROPERTY(Controller *controller READ controller CONSTANT)
79 Q_PROPERTY(QString controllerName READ controllerName CONSTANT)
86 Q_PROPERTY(QVariantMap config READ config CONSTANT)
90 Q_PROPERTY(bool state READ state CONSTANT)
91public:
98 Context(Application *app);
99
104 virtual ~Context();
105
110 [[nodiscard]] bool error() const noexcept;
111
116 void appendError(const QString &error);
117
122 [[nodiscard]] QStringList errors() const noexcept;
123
127 [[nodiscard]] bool state() const noexcept;
128
133 void setState(bool state) noexcept;
134
138 [[nodiscard]] Engine *engine() const noexcept;
139
143 [[nodiscard]] Application *app() const noexcept;
144
148 [[nodiscard]] Response *response() const noexcept;
149
153 [[nodiscard]] Response *res() const noexcept;
154
158 [[nodiscard]] Action *action() const noexcept;
159
163 [[nodiscard]] QString actionName() const noexcept;
164
173 [[nodiscard]] QString ns() const noexcept;
174
179 [[nodiscard]] Request *request() const noexcept;
180
184 [[nodiscard]] Request *req() const noexcept;
185
189 [[nodiscard]] Dispatcher *dispatcher() const noexcept;
190
194 [[nodiscard]] QString controllerName() const noexcept;
195
199 [[nodiscard]] Controller *controller() const noexcept;
200
205 [[nodiscard]] Controller *controller(QStringView name) const;
206
210 [[nodiscard]] View *view(QStringView name = {}) const;
211
217 [[nodiscard]] View *customView() const noexcept;
218
230 bool setCustomView(QStringView name);
231
253 void stash(const QVariantHash &unite);
254
266 [[nodiscard]] QVariantHash &stash();
267
271 [[nodiscard]] QVariant stash(const QString &key) const;
272
277 [[nodiscard]] QVariant stash(const QString &key, const QVariant &defaultValue) const;
278
284 QVariant stashTake(const QString &key);
285
290 bool stashRemove(const QString &key);
291
295 void setStash(const QString &key, const QVariant &value);
296
300 void setStash(const QString &key, const ParamsMultiMap &map);
301
305 [[nodiscard]] QStack<Component *> stack() const noexcept;
306
319 [[nodiscard]] QUrl uriFor(const QString &path = {},
320 const QStringList &args = {},
321 const ParamsMultiMap &queryValues = {}) const;
322
334 [[nodiscard]] inline QUrl uriFor(const QString &path, const ParamsMultiMap &queryValues) const;
335
345 [[nodiscard]] QUrl uriFor(Action *action,
346 const QStringList &captures = {},
347 const QStringList &args = {},
348 const ParamsMultiMap &queryValues = {}) const;
349
355 [[nodiscard]] inline QUrl uriFor(Action *action, const ParamsMultiMap &queryValues) const;
356
380 [[nodiscard]] QUrl uriForAction(QStringView path,
381 const QStringList &captures = {},
382 const QStringList &args = {},
383 const ParamsMultiMap &queryValues = {}) const;
384
388 [[nodiscard]] inline QUrl uriForAction(QStringView path,
389 const ParamsMultiMap &queryValues) const;
390
395 bool detached() const noexcept;
396
404 void detach(Action *action = nullptr);
405
421 void detachAsync() noexcept;
422
429 void attachAsync();
430
449 bool forward(Component *component);
450
469 bool forward(QStringView action);
470
474 [[nodiscard]] Action *getAction(QStringView action, QStringView ns = {}) const;
475
479 [[nodiscard]] QVector<Action *> getActions(QStringView action, QStringView ns = {}) const;
480
484 [[nodiscard]] QVector<Plugin *> plugins() const;
485
489 template <typename T>
491 {
492 const auto pluginsConst = plugins();
493 for (Plugin *plugin : pluginsConst) {
494 auto p = qobject_cast<T>(plugin);
495 if (p) {
496 return p;
497 }
498 }
499 return nullptr;
500 }
501
505 bool execute(Component *code);
506
516 [[nodiscard]] QLocale locale() const noexcept;
517
535 void setLocale(const QLocale &locale);
536
546 [[nodiscard]] QVariant config(const QString &key, const QVariant &defaultValue = {}) const;
547
554 [[nodiscard]] QVariantMap config() const noexcept;
555
571 [[nodiscard]] QString translate(const char *context,
572 const char *sourceText,
573 const char *disambiguation = nullptr,
574 int n = -1) const;
612 [[nodiscard]] inline QString qtTrId(const char *id, int n = -1) const;
613
614public Q_SLOTS:
619 void finalize();
620
621protected:
625 Context(ContextPrivate *priv);
626
627 friend class Application;
628 friend class Action;
629 friend class ActionChain;
630 friend class DispatchType;
631 friend class Plugin;
632 friend class Engine;
633 friend class Controller;
634 friend class Async;
635 ContextPrivate *d_ptr;
636
637private:
638 Q_DECLARE_PRIVATE(Context)
639};
640
641inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
642{
643 return uriFor(path, QStringList(), queryValues);
644}
645
646inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
647{
648 return uriFor(action, QStringList(), QStringList(), queryValues);
649}
650
651inline QUrl Context::uriForAction(QStringView path, const ParamsMultiMap &queryValues) const
652{
653 return uriForAction(path, QStringList(), QStringList(), queryValues);
654}
655
656inline QString Context::qtTrId(const char *id, int n) const
657{
658 return translate(nullptr, id, nullptr, n);
659}
660
661} // namespace Cutelyst
662
663Q_DECLARE_METATYPE(Cutelyst::Context *)
This class represents a Cutelyst Action.
Definition action.h:36
The Cutelyst application.
Definition application.h:66
The Cutelyst Component base class.
Definition component.h:30
The Cutelyst Context.
Definition context.h:42
Cutelyst Controller base class.
Definition controller.h:56
The Cutelyst Dispatcher.
Definition dispatcher.h:29
The Cutelyst Engine.
Definition engine.h:20
Base class for Cutelyst Plugins.
Definition plugin.h:25
A request.
Definition request.h:42
A Cutelyst response.
Definition response.h:29
Abstract View component for Cutelyst.
Definition view.h:25
QMultiMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.