![]() |
cutelyst 4.8.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
|
The Cutelyst application. More...
#include <Cutelyst/Application>

Signals | |
| void | afterDispatch (Cutelyst::Context *c) |
| void | beforeDispatch (Cutelyst::Context *c) |
| void | beforePrepareAction (Cutelyst::Context *c, bool *skipMethod) |
| void | postForked (Cutelyst::Application *app) |
| void | preForked (Cutelyst::Application *app) |
| void | shuttingDown (Cutelyst::Application *app) |
Public Member Functions | |
| Application (QObject *parent=nullptr) | |
| virtual | ~Application () |
| void | addTranslator (const QLocale &locale, QTranslator *translator) |
| void | addTranslator (const QString &locale, QTranslator *translator) |
| void | addTranslators (const QLocale &locale, const QVector< QTranslator * > &translators) |
| QVariantMap | config () const noexcept |
| QVariant | config (const QString &key, const QVariant &defaultValue={}) const |
| QVector< Controller * > | controllers () const noexcept |
| Component * | createComponentPlugin (const QString &name, QObject *parent=nullptr) |
| QLocale | defaultLocale () const noexcept |
| Dispatcher * | dispatcher () const noexcept |
| QVector< DispatchType * > | dispatchers () const noexcept |
| Engine * | engine () const noexcept |
| bool | inited () const noexcept |
| void | loadTranslations (const QString &filename, const QString &directory={}, const QString &prefix={}, const QString &suffix={}) |
| QVector< QLocale > | loadTranslationsFromDir (const QString &filename, const QString &directory=QString(), const QString &prefix=QStringLiteral("."), const QString &suffix=QStringLiteral(".qm")) |
| QVector< QLocale > | loadTranslationsFromDirs (const QString &directory, const QString &filename) |
| QString | pathTo (const QString &path) const |
| QString | pathTo (const QStringList &path) const |
| template<typename T> | |
| T | plugin () |
| QVector< Plugin * > | plugins () const noexcept |
| void | setDefaultLocale (const QLocale &locale) |
| QString | translate (const QLocale &locale, const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const |
| View * | view (QStringView name={}) const |
| Public Member Functions inherited from QObject | |
| QObject (QObject *parent) | |
| QBindable< QString > | bindableObjectName () |
| bool | blockSignals (bool block) |
| const QObjectList & | children () const const |
| QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const |
| void | deleteLater () |
| void | destroyed (QObject *obj) |
| bool | disconnect (const char *signal, const QObject *receiver, const char *method) const const |
| bool | disconnect (const QObject *receiver, const char *method) const const |
| void | dumpObjectInfo () const const |
| void | dumpObjectTree () const const |
| QList< QByteArray > | dynamicPropertyNames () const const |
| virtual bool | event (QEvent *e) |
| virtual bool | eventFilter (QObject *watched, QEvent *event) |
| T | findChild (QAnyStringView name, Qt::FindChildOptions options) const const |
| T | findChild (Qt::FindChildOptions options) const const |
| QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
| QList< T > | findChildren (QAnyStringView name, Qt::FindChildOptions options) const const |
| QList< T > | findChildren (Qt::FindChildOptions options) const const |
| bool | inherits (const char *className) const const |
| void | installEventFilter (QObject *filterObj) |
| bool | isQmlExposed () const const |
| bool | isQuickItemType () const const |
| bool | isWidgetType () const const |
| bool | isWindowType () const const |
| void | killTimer (int id) |
| void | killTimer (Qt::TimerId id) |
| virtual const QMetaObject * | metaObject () const const |
| bool | moveToThread (QThread *targetThread) |
| QString | objectName () const const |
| void | objectNameChanged (const QString &objectName) |
| QObject * | parent () const const |
| QVariant | property (const char *name) const const |
| Q_CLASSINFO (Name, Value) | |
| Q_EMIT Q_EMIT | |
| Q_ENUM (...) | |
| Q_ENUM_NS (...) | |
| Q_ENUMS (...) | |
| Q_FLAG (...) | |
| Q_FLAG_NS (...) | |
| Q_FLAGS (...) | |
| Q_GADGET Q_GADGET | |
| Q_GADGET_EXPORT (EXPORT_MACRO) | |
| Q_INTERFACES (...) | |
| Q_INVOKABLE Q_INVOKABLE | |
| Q_MOC_INCLUDE Q_MOC_INCLUDE | |
| Q_NAMESPACE Q_NAMESPACE | |
| Q_NAMESPACE_EXPORT (EXPORT_MACRO) | |
| Q_OBJECT Q_OBJECT | |
| Q_PROPERTY (...) | |
| Q_REVISION Q_REVISION | |
| Q_SET_OBJECT_NAME (Object) | |
| Q_SIGNAL Q_SIGNAL | |
| Q_SIGNALS Q_SIGNALS | |
| Q_SLOT Q_SLOT | |
| Q_SLOTS Q_SLOTS | |
| T | qobject_cast (const QObject *object) |
| T | qobject_cast (QObject *object) |
| QT_NO_CONTEXTLESS_CONNECT QT_NO_CONTEXTLESS_CONNECT | |
| QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT | |
| void | removeEventFilter (QObject *obj) |
| void | setObjectName (const QString &name) |
| void | setObjectName (QAnyStringView name) |
| void | setParent (QObject *parent) |
| bool | setProperty (const char *name, const QVariant &value) |
| bool | setProperty (const char *name, QVariant &&value) |
| bool | signalsBlocked () const const |
| int | startTimer (int interval, Qt::TimerType timerType) |
| int | startTimer (std::chrono::nanoseconds interval, Qt::TimerType timerType) |
| QThread * | thread () const const |
Static Public Member Functions | |
| static const char * | cutelystVersion () noexcept |
| Static Public Member Functions inherited from QObject | |
| QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
| QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
| QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) |
| QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) |
| QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, Functor functor) |
| bool | disconnect (const QMetaObject::Connection &connection) |
| bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
| bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
| bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
| QString | tr (const char *sourceText, const char *disambiguation, int n) |
Protected Member Functions | |
| void | addXCutelystVersionHeader () |
| Headers & | defaultHeaders () noexcept |
| bool | enginePostFork () |
| void | handleRequest (Cutelyst::EngineRequest *request) |
| virtual bool | init () |
| virtual bool | postFork () |
| bool | registerController (Controller *controller) |
| bool | registerDispatcher (DispatchType *dispatcher) |
| bool | registerPlugin (Plugin *plugin) |
| bool | registerView (View *view) |
| void | setConfig (const QString &key, const QVariant &value) |
| bool | setup (Engine *engine) |
| Protected Member Functions inherited from QObject | |
| virtual void | childEvent (QChildEvent *event) |
| virtual void | connectNotify (const QMetaMethod &signal) |
| virtual void | customEvent (QEvent *event) |
| virtual void | disconnectNotify (const QMetaMethod &signal) |
| bool | isSignalConnected (const QMetaMethod &signal) const const |
| int | receivers (const char *signal) const const |
| QObject * | sender () const const |
| int | senderSignalIndex () const const |
| virtual void | timerEvent (QTimerEvent *event) |
Friends | |
| class | Context |
| class | Engine |
Additional Inherited Members | |
| Public Types inherited from QObject | |
| enum | TimerId |
| Public Attributes inherited from QObject | |
| Invalid | |
| typedef | QObjectList |
| Properties inherited from QObject | |
| objectName | |
This is the main class of a Cutelyst appplication.
There are some options you can set in your application configuration file in the Cutelyst section. If you want to get keys via the config() method of Application, you have to add them to the Cutelyst section. If you want to get other config sections than Cutelyst, you should use engine()->config("section").
Type: string
Default: empty
Absolute path to your home diretory, that is for example used by pathTo(). If this is empty (the default), it will be populated by the return value of QDir::currentPath().
Type: string
Default: empty
Absolute path to your web root directory that contains your template and/or static files. This might be used by plugins to serve static files or by Views like CuteleeView to find template files. If this is empty (the default), it will be populated as directory "root" below "home".
Definition at line 65 of file application.h.
|
explicit |
The constructor is used to setup the class configuration, subclasses should only use this for objects that do not require configuration to be ready.
A Web Engine will instantiate your application through this class, next it will load the settings file, and in the end it will call init() which is where your application should do it’s own setup.
Definition at line 48 of file application.cpp.
References Application(), QObject::QObject(), loadTranslations(), and QObject::parent().
Referenced by Application(), addTranslator(), addTranslators(), addXCutelystVersionHeader(), config(), config(), controllers(), createComponentPlugin(), defaultHeaders(), defaultLocale(), dispatcher(), dispatchers(), engine(), enginePostFork(), handleRequest(), inited(), plugins(), postForked(), preForked(), registerController(), registerDispatcher(), registerPlugin(), registerView(), setConfig(), setDefaultLocale(), setup(), shuttingDown(), translate(), and view().
|
virtual |
Destroys the Application object.
Definition at line 63 of file application.cpp.
| void Application::addTranslator | ( | const QLocale & | locale, |
| QTranslator * | translator ) |
Adds a translator for the specified locale.
You can add multiple translators for different application parts for every supported locale. The installed translators will then be used by Context::translate() and Context::qtTrId() (which itself will use Application::translate()) to translate strings according to the locale set by Context::setLocale().
Definition at line 433 of file application.cpp.
References Application().
Referenced by addTranslator(), loadTranslationsFromDir(), and loadTranslationsFromDirs().
| void Application::addTranslator | ( | const QString & | locale, |
| QTranslator * | translator ) |
Adds a translator for the specified locale.
The locale string has to be parseable by QLocale.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 445 of file application.cpp.
References addTranslator().
| void Application::addTranslators | ( | const QLocale & | locale, |
| const QVector< QTranslator * > & | translators ) |
Adds multiple translators for the specified locale.
Definition at line 450 of file application.cpp.
References Application(), QList::crbegin(), QList::crend(), and QList::empty().
|
protected |
Adds a X-Cutelyst header with our version on each request.
Definition at line 88 of file application.cpp.
References Application().
|
signal |
This signal is emitted right after the Action found by the dispatcher got executed.
References afterDispatch().
Referenced by afterDispatch(), handleRequest(), and Cutelyst::Session::setup().
|
signal |
This signal is emitted right after the Dispatcher returns the Action that will be executed.
References beforeDispatch().
Referenced by beforeDispatch(), handleRequest(), and Cutelyst::CSRFProtection::setup().
|
signal |
This signal is emitted before the Dispatcher is called to find an action. It’s useful if you need to intercept requests before they are dispatched. Always check skipMethod and return if it’s true. In case you want to stop further processing set skipMethod to true.
References beforePrepareAction().
Referenced by beforePrepareAction(), handleRequest(), Cutelyst::LangSelect::setup(), Cutelyst::StaticCompressed::setup(), Cutelyst::StaticSimple::setup(), and StaticMap::setup().
|
noexcept |
User configuration for the application. Returns a map with configuration settings that are read from the Cutelyst section of your application configuration file.
Cutelyst, you should use engine()->config("section").Definition at line 210 of file application.cpp.
References Application().
Referenced by pathTo().
Returns application config specified by key. If key is not found, defaultValue will be returned. These are the config entries in the Cutelyst section in your application configuration file.
Cutelyst, you should use engine()->config("section").value("key").Definition at line 182 of file application.cpp.
References Application().
Referenced by pathTo().
|
noexcept |
Returns a list with all registered controllers.
The list might only be complete after application has been setup.
Definition at line 170 of file application.cpp.
References Application().
Referenced by enginePostFork(), and setup().
Tries to load a plugin in Cutelyst default plugin directory with parent as it’s parent. A nullptr is returned in case of failure.
Definition at line 138 of file application.cpp.
References Application(), QObject::QObject(), Cutelyst::ComponentFactory::createComponent(), QString::fromLocal8Bit(), and QObject::parent().
|
staticnoexcept |
|
protectednoexcept |
This is the HTTP default response headers that each request gets.
Do not change it after the application has started.
Definition at line 82 of file application.cpp.
References Application().
|
nodiscardnoexcept |
Returns the default locale that will be set to the locale() of newly created Context objects. By default this will be QLocale(English, Latin, United States).
Definition at line 641 of file application.cpp.
References Application().
|
noexcept |
Returns the dispatcher class.
Definition at line 192 of file application.cpp.
References Application().
Referenced by registerDispatcher().
|
noexcept |
Returns a list with all registered dispatchers.
The list might only be complete after application has been setup.
Definition at line 198 of file application.cpp.
References Application().
|
noexcept |
Returns current engine that is generating requests.
Definition at line 234 of file application.cpp.
References Application().
Referenced by Cutelyst::MemcachedSessionStore::MemcachedSessionStore(), handleRequest(), setup(), Cutelyst::CSRFProtection::setup(), Cutelyst::LangSelect::setup(), Cutelyst::Memcached::setup(), Cutelyst::Session::setup(), Cutelyst::StaticCompressed::setup(), and shuttingDown().
|
protected |
Called by the Engine once post fork happened.
Definition at line 413 of file application.cpp.
References Application(), controllers(), postFork(), postForked(), and QObject::Q_EMIT().
Referenced by shuttingDown().
|
protected |
Called by the Engine to handle a new Request object.
Definition at line 368 of file application.cpp.
References Application(), afterDispatch(), beforeDispatch(), beforePrepareAction(), Cutelyst::EngineRequest::context, engine(), QObject::Q_EMIT(), and Cutelyst::EngineRequest::status.
Referenced by shuttingDown().
|
protectedvirtual |
Do your application initialization here, if your application should not proceed log some information that might help on debugging and return false.
For example if your application only works with PostgeSQL and the Qt driver is not available it makes sense to fail here. However you should not initialize resouces that cannot be shared among process.
true if your application was successfuly initted Definition at line 68 of file application.cpp.
References QCoreApplication::applicationPid().
Referenced by setup().
|
noexcept |
Returns true if the application has been inited.
Definition at line 228 of file application.cpp.
References Application().
| void Application::loadTranslations | ( | const QString & | filename, |
| const QString & | directory = {}, | ||
| const QString & | prefix = {}, | ||
| const QString & | suffix = {} ) |
Loads translations for a specific filename from a single directory.
This can be used to load translations for a specific component or application if the translation file names follow a common schema.
Let us assume you organised your translation files as follows:
/usr/share/myapp/translations/myapp_de.qm /usr/share/myapp/translations/myapp_pt_BR.qm ...You can then use loadTranslations() in your reimplementation of Application::init() as follows:
If directory is empty, the default directory, set by -DI18NDIR, will be used. prefix is the part between the file name and the locale part. In the example above it is "_", if it is not set the default "." will be used. The suffix is the file name suffix that defaults to ".qm".
Definition at line 524 of file application.cpp.
References loadTranslationsFromDir().
Referenced by Application(), Cutelyst::Validator::loadTranslations(), Cutelyst::CSRFProtection::setup(), and Cutelyst::Memcached::setup().
| QVector< QLocale > Application::loadTranslationsFromDir | ( | const QString & | filename, |
| const QString & | directory = QString(), | ||
| const QString & | prefix = QStringLiteral("."), | ||
| const QString & | suffix = QStringLiteral(".qm") ) |
Loads translations for a specific filename from a single directory and returns a list of added locales.
This can be used to load translations for a specific component or application if the translation file names follow a common schema.
Let us assume you organised your translation files as follows:
/usr/share/myapp/translations/myapp_de.qm /usr/share/myapp/translations/myapp_pt_BR.qm ...You can then use loadTranslationsFromDir() in your reimplementation of Application::init() as follows:
If directory is empty, the default directory, set by -DI18NDIR, will be used. prefix is the part between the file name and the locale part. In the example above it is "_", if it is not set the default "." will be used. The suffix is the file name suffix that defaults to ".qm".
Definition at line 532 of file application.cpp.
References addTranslator(), QList::append(), QLocale::C, QDir::entryInfoList(), QDir::exists(), QDir::Files, QString::indexOf(), QString::isEmpty(), QLocale::language(), QString::length(), QString::mid(), QList::reserve(), and QList::squeeze().
Referenced by loadTranslations().
| QVector< QLocale > Application::loadTranslationsFromDirs | ( | const QString & | directory, |
| const QString & | filename ) |
Loads translations for a specific filename from a directory structure under directory and returns a list of added locales.
This can be used to load translations for a specific component or application if the the translation files are organized in subdirectories named after locale codes. Let us assume you organised your translation files as follows:
/usr/share/locale/de/LC_MESSAGES/myapp.qm /usr/share/locale/pt_BR/LC_MESSAGES/myapp.qm ...You can then use loadTranslationsFromDirs() in your reimplementation of Application::init() as follows:
Definition at line 590 of file application.cpp.
References QFileInfo::absoluteFilePath(), QFileInfo::absolutePath(), addTranslator(), QDir::AllDirs, QList::append(), QFileInfo::baseName(), QLocale::C, QDir::entryList(), QDir::exists(), QString::isEmpty(), QLocale::language(), QList::reserve(), QList::squeeze(), and QFileInfo::suffix().
Merges path with config("home") and returns an absolute path.
Definition at line 216 of file application.cpp.
References QDir::absoluteFilePath(), and config().
| QString Cutelyst::Application::pathTo | ( | const QStringList & | path | ) | const |
Merges the path parts with config("home") and returns an absolute path.
Definition at line 222 of file application.cpp.
References QDir::absoluteFilePath(), config(), and QStringList::join().
|
inline |
Returns the registered plugin that casts to the template type T.
Definition at line 137 of file application.h.
References plugin(), plugins(), and QObject::qobject_cast().
Referenced by plugin(), registerPlugin(), setup(), and Cutelyst::CSRFProtection::setup().
Returns all registered plugins.
Definition at line 204 of file application.cpp.
References Application().
|
protectedvirtual |
This method is called after the engine forks
After the web engine forks itself it will call this function so that you can initialize resources that can’t be shared with the parent process, namely sockets and file descriptors.
A good example of usage of this function is when opening a connection to the database which can’t be shared with other processes and should probably make this function return false if it fails to open.
Default implementation returns true.
false if the engine should not use this process Definition at line 75 of file application.cpp.
References QCoreApplication::applicationPid().
Referenced by enginePostFork().
|
signal |
This signal is emitted after postFork() is called.
References Application(), and postForked().
Referenced by enginePostFork(), postForked(), Cutelyst::Authentication::setup(), Cutelyst::CSRFProtection::setup(), Cutelyst::LangSelect::setup(), Cutelyst::Memcached::setup(), Cutelyst::Session::setup(), and Cutelyst::StatusMessage::setup().
|
signal |
This signal is emitted right after application has been setup and before application forks and postFork() is called.
References Application(), and preForked().
Referenced by preForked(), and setup().
|
protected |
This method registers a Controller class which is responsible for handlying requests, since they are reused between multiple requests. Be aware of not storing data there, instead you might want to use a session plugin or the stash.
| controller | the Controller class |
true if succeeded Definition at line 104 of file application.cpp.
References Application(), QMetaObject::className(), QString::fromLatin1(), and QObject::metaObject().
|
protected |
Registers a custom DispatchType. If none is registered, all the built-in dispatcher types will be registered.
Definition at line 128 of file application.cpp.
References Application(), and dispatcher().
|
protected |
Registers a global plugin ie. one that doesn’t need to be created explicity for a single request and returns true on plugin->isApplicationPlugin();
true if the plugin could be registered Definition at line 94 of file application.cpp.
References Application(), and plugin().
|
protected |
This method registers a View class which is responsible for rendering requests.
true if succeeded Definition at line 116 of file application.cpp.
References Application(), and view().
Change the value of the configuration key.
This will change or add keys to the configuration read from the Cutelyst section of your configuration file. The set values are not written to the file.
Definition at line 240 of file application.cpp.
References Application().
Referenced by shuttingDown().
| void Application::setDefaultLocale | ( | const QLocale & | locale | ) |
Sets the default locale that will be set to the locale() of newly created Context objects. By default this will be QLocale(English, Latin, United States).
Definition at line 647 of file application.cpp.
References Application().
|
protected |
Called by the Engine to setup the internal data.
Definition at line 246 of file application.cpp.
References Application(), QList::append(), QCoreApplication::applicationName, QObject::children(), controllers(), cutelystVersion(), engine(), QString::fromLatin1(), init(), QFileInfo::isDir(), QList::isEmpty(), QString::isEmpty(), plugin(), plugins(), preForked(), QObject::Q_EMIT(), QStringList::sort(), and view().
Referenced by shuttingDown().
|
signal |
This signal is likely to be emitted when the worker process should stop. At this point the application has a limited time to finish it’s operations. If a timeout is reached the application will get killed.
References Application(), engine(), enginePostFork(), handleRequest(), setConfig(), setup(), and shuttingDown().
Referenced by shuttingDown().
| QString Application::translate | ( | const QLocale & | locale, |
| const char * | context, | ||
| const char * | sourceText, | ||
| const char * | disambiguation = nullptr, | ||
| int | n = -1 ) const |
Translates the sourceText into the target locale language.
This uses the installed translators for the specified locale to translate the sourceText for the given context into the target locale. Optionally you can use a disambiguation and/or the n parameter to translate a pluralized version.
Do not use this method directly but use Context::translate() or Context::qtTrId().
Definition at line 488 of file application.cpp.
References Application(), QList::empty(), QString::fromUtf8(), QString::isEmpty(), and QList::value().
| View * Application::view | ( | QStringView | name = {} | ) | const |
Returns the View specified by name, if no view is found nullptr is returned. The default view is nameless.
Definition at line 176 of file application.cpp.
References Application().
Referenced by Cutelyst::RenderView::init(), registerView(), and setup().
|
friend |
Definition at line 547 of file application.h.
|
friend |
Definition at line 546 of file application.h.