cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
application.h
1/*
2 * SPDX-FileCopyrightText: (C) 2013-2022 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef CUTELYST_APPLICATION_H
6#define CUTELYST_APPLICATION_H
7
8#include <Cutelyst/cutelyst_global.h>
9
10#include <QtCore/QLocale>
11#include <QtCore/QObject>
12#include <QtCore/QVariant>
13#include <QtCore/QVector>
14
15class QTranslator;
16
17namespace Cutelyst {
18
19#define CUTELYST_APPLICATION(x) \
20 Q_PLUGIN_METADATA(x) \
21 Q_INTERFACES(Cutelyst::Application)
22
23class Context;
24class Controller;
25class Component;
26class View;
27class Dispatcher;
28class DispatchType;
29class Request;
30class Response;
31class Engine;
32class EngineRequest;
33class Plugin;
34class Headers;
35class ApplicationPrivate;
36
42class CUTELYST_LIBRARY Application : public QObject
43{
45 Q_DECLARE_PRIVATE(Application)
46public:
61 explicit Application(QObject *parent = nullptr);
62 virtual ~Application();
63
69 QVector<Controller *> controllers() const noexcept;
70
74 View *view(const QString &name) const;
75
79 View *view(QStringView name = {}) const;
80
84 QVariant config(const QString &key, const QVariant &defaultValue = {}) const;
85
89 Dispatcher *dispatcher() const noexcept;
90
96 QVector<DispatchType *> dispatchers() const noexcept;
97
101 QVector<Plugin *> plugins() const noexcept;
102
106 template <typename T>
108 {
109 const auto pluginsConst = plugins();
110 for (Plugin *plugin : pluginsConst) {
111 auto p = qobject_cast<T>(plugin);
112 if (p) {
113 return p;
114 }
115 }
116 return nullptr;
117 }
118
123 QVariantMap config() const noexcept;
124
128 QString pathTo(const QString &path) const;
129
133 QString pathTo(const QStringList &path) const;
134
138 bool inited() const noexcept;
139
143 Engine *engine() const noexcept;
144
149 Component *createComponentPlugin(const QString &name, QObject *parent = nullptr);
150
154 static const char *cutelystVersion() noexcept;
155
184 void addTranslator(const QLocale &locale, QTranslator *translator);
185
195 void addTranslator(const QString &locale, QTranslator *translator);
196
204 void addTranslators(const QLocale &locale, const QVector<QTranslator *> &translators);
205
217 QString translate(const QLocale &locale,
218 const char *context,
219 const char *sourceText,
220 const char *disambiguation = nullptr,
221 int n = -1) const;
222
251 void loadTranslations(const QString &filename,
252 const QString &directory = QString(),
253 const QString &prefix = QString(),
254 const QString &suffix = QString());
255
285 QVector<QLocale> loadTranslationsFromDir(const QString &filename,
286 const QString &directory = QString(),
287 const QString &prefix = QStringLiteral("."),
288 const QString &suffix = QStringLiteral(".qm"));
289
315 QVector<QLocale> loadTranslationsFromDirs(const QString &directory, const QString &filename);
316
317protected:
331 virtual bool init();
332
350 virtual bool postFork();
351
357 Headers &defaultHeaders() noexcept;
358
362 void addXCutelystVersionHeader();
363
371 bool registerPlugin(Plugin *plugin);
372
383 bool registerController(Controller *controller);
384
392 bool registerView(View *view);
393
398 bool registerDispatcher(DispatchType *dispatcher);
399
400Q_SIGNALS:
410 void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod);
411
416 void beforeDispatch(Cutelyst::Context *c);
417
422 void afterDispatch(Cutelyst::Context *c);
423
429
434
441
442protected:
449 void setConfig(const QString &key, const QVariant &value);
450
451 friend class Engine;
452 friend class Context;
453
457 bool setup(Engine *engine);
458
462 void handleRequest(Cutelyst::EngineRequest *request);
463
467 bool enginePostFork();
468
469 ApplicationPrivate *d_ptr;
470};
471
472} // namespace Cutelyst
473
474#define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
475
476Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
477
478#endif // CUTELYST_APPLICATION_H
The Cutelyst Application.
Definition application.h:43
Engine * engine() const noexcept
void shuttingDown(Cutelyst::Application *app)
QVector< Controller * > controllers() const noexcept
void afterDispatch(Cutelyst::Context *c)
bool setup(Engine *engine)
Called by the Engine to setup the internal data.
void handleRequest(Cutelyst::EngineRequest *request)
Called by the Engine to handle a new Request object.
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Application(QObject *parent=nullptr)
void setConfig(const QString &key, const QVariant &value)
Dispatcher * dispatcher() const noexcept
void beforeDispatch(Cutelyst::Context *c)
void preForked(Cutelyst::Application *app)
QVector< Plugin * > plugins() const noexcept
bool enginePostFork()
Called by the Engine once post fork happened.
QVector< DispatchType * > dispatchers() const noexcept
T plugin()
Returns the registered plugin that casts to the template type T.
QVariant config(const QString &key, const QVariant &defaultValue={}) const
View * view(const QString &name) const
void postForked(Cutelyst::Application *app)
The Cutelyst Component base class.
Definition component.h:26
The Cutelyst Context.
Definition context.h:39
Cutelyst Controller base class
Definition controller.h:88
The Cutelyst Dispatcher.
Definition dispatcher.h:28
The Cutelyst Engine.
Definition engine.h:21
Cutelyst View abstract view component
Definition view.h:22
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
QObject * parent() const const
T qobject_cast(QObject *object)