cutelyst 4.0.0
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{
44 Q_OBJECT
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(QStringView name = {}) const;
75
79 QVariant config(const QString &key, const QVariant &defaultValue = {}) const;
80
84 Dispatcher *dispatcher() const noexcept;
85
91 QVector<DispatchType *> dispatchers() const noexcept;
92
96 QVector<Plugin *> plugins() const noexcept;
97
101 template <typename T>
103 {
104 const auto pluginsConst = plugins();
105 for (Plugin *plugin : pluginsConst) {
106 auto p = qobject_cast<T>(plugin);
107 if (p) {
108 return p;
109 }
110 }
111 return nullptr;
112 }
113
118 QVariantMap config() const noexcept;
119
123 QString pathTo(const QString &path) const;
124
128 QString pathTo(const QStringList &path) const;
129
133 bool inited() const noexcept;
134
138 Engine *engine() const noexcept;
139
144 Component *createComponentPlugin(const QString &name, QObject *parent = nullptr);
145
149 static const char *cutelystVersion() noexcept;
150
179 void addTranslator(const QLocale &locale, QTranslator *translator);
180
190 void addTranslator(const QString &locale, QTranslator *translator);
191
199 void addTranslators(const QLocale &locale, const QVector<QTranslator *> &translators);
200
212 QString translate(const QLocale &locale,
213 const char *context,
214 const char *sourceText,
215 const char *disambiguation = nullptr,
216 int n = -1) const;
217
246 void loadTranslations(const QString &filename,
247 const QString &directory = {},
248 const QString &prefix = {},
249 const QString &suffix = {});
250
280 QVector<QLocale> loadTranslationsFromDir(const QString &filename,
281 const QString &directory = QString(),
282 const QString &prefix = QStringLiteral("."),
283 const QString &suffix = QStringLiteral(".qm"));
284
310 QVector<QLocale> loadTranslationsFromDirs(const QString &directory, const QString &filename);
311
312protected:
326 virtual bool init();
327
345 virtual bool postFork();
346
352 Headers &defaultHeaders() noexcept;
353
357 void addXCutelystVersionHeader();
358
366 bool registerPlugin(Plugin *plugin);
367
378 bool registerController(Controller *controller);
379
387 bool registerView(View *view);
388
393 bool registerDispatcher(DispatchType *dispatcher);
394
395Q_SIGNALS:
405 void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod);
406
411 void beforeDispatch(Cutelyst::Context *c);
412
417 void afterDispatch(Cutelyst::Context *c);
418
423 void preForked(Cutelyst::Application *app);
424
428 void postForked(Cutelyst::Application *app);
429
435 void shuttingDown(Cutelyst::Application *app);
436
437protected:
444 void setConfig(const QString &key, const QVariant &value);
445
446 friend class Engine;
447 friend class Context;
448
452 bool setup(Engine *engine);
453
457 void handleRequest(Cutelyst::EngineRequest *request);
458
462 bool enginePostFork();
463
464 ApplicationPrivate *d_ptr;
465};
466
467} // namespace Cutelyst
468
469#define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
470
471Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
472
473#endif // CUTELYST_APPLICATION_H
The Cutelyst Application.
Definition: application.h:43
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:102
The Cutelyst Component base class.
Definition: component.h:26
The Cutelyst Context.
Definition: context.h:38
The Cutelyst Dispatcher.
Definition: dispatcher.h:27
The Cutelyst Engine.
Definition: engine.h:20
Cutelyst View abstract view component
Definition: view.h:21
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8