cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends | List of all members
Cutelyst::Application Class Reference

The Cutelyst Application. More...

#include <Cutelyst/Application>

Inheritance diagram for Cutelyst::Application:
Inheritance graph
[legend]

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)
 
void addTranslator (const QLocale &locale, QTranslator *translator)
 
void addTranslator (const QString &locale, QTranslator *translator)
 
void addTranslators (const QLocale &locale, const QVector< QTranslator *> &translators)
 
QVariant config (const QString &key, const QVariant &defaultValue={}) const
 
QVariantMap config () const noexcept
 
QVector< Controller * > controllers () const noexcept
 
ComponentcreateComponentPlugin (const QString &name, QObject *parent=nullptr)
 
Dispatcherdispatcher () const noexcept
 
QVector< DispatchType * > dispatchers () const noexcept
 
Engineengine () const noexcept
 
bool inited () const noexcept
 
void loadTranslations (const QString &filename, const QString &directory=QString(), const QString &prefix=QString(), const QString &suffix=QString())
 
QVector< QLocaleloadTranslationsFromDir (const QString &filename, const QString &directory=QString(), const QString &prefix=QStringLiteral("."), const QString &suffix=QStringLiteral(".qm"))
 
QVector< QLocaleloadTranslationsFromDirs (const QString &directory, const QString &filename)
 
QString pathTo (const QString &path) const
 
QString pathTo (const QStringList &path) const
 
template<typename T >
plugin ()
 Returns the registered plugin that casts to the template type T.
 
QVector< Plugin * > plugins () const noexcept
 
QString translate (const QLocale &locale, const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
 
Viewview (const QString &name) const
 
Viewview (QStringView name={}) const
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
bool blockSignals (bool block)
 
const QObjectListchildren () 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 ()
 
void dumpObjectInfo () const const
 
void dumpObjectTree ()
 
void dumpObjectTree () const const
 
QList< QByteArraydynamicPropertyNames () const const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
bool inherits (const char *className) const const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
QVariant property (const char *name) const const
 
 Q_CLASSINFO (Name, Value)
 
 Q_DISABLE_COPY (Class)
 
 Q_DISABLE_COPY_MOVE (Class)
 
 Q_DISABLE_MOVE (Class)
 
 Q_EMIT Q_EMIT
 
 Q_ENUM (...)
 
 Q_ENUM_NS (...)
 
 Q_ENUMS (...)
 
 Q_FLAG (...)
 
 Q_FLAG_NS (...)
 
 Q_FLAGS (...)
 
 Q_GADGET Q_GADGET
 
 Q_INTERFACES (...)
 
 Q_INVOKABLE Q_INVOKABLE
 
 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
 
qFindChild (const QObject *obj, const QString &name)
 
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
 
QList< T > qFindChildren (const QObject *obj, const QString &name)
 
qobject_cast (QObject *object)
 
qobject_cast (const QObject *object)
 
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const const
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
QThreadthread () 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 *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
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, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 

Protected Member Functions

void addXCutelystVersionHeader ()
 
HeadersdefaultHeaders () noexcept
 
bool enginePostFork ()
 Called by the Engine once post fork happened.
 
void handleRequest (Cutelyst::EngineRequest *request)
 Called by the Engine to handle a new Request object.
 
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)
 Called by the Engine to setup the internal data.
 
- 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
 
QObjectsender () const const
 
int senderSignalIndex () const const
 
virtual void timerEvent (QTimerEvent *event)
 

Friends

class Context
 
class Engine
 

Additional Inherited Members

- Public Attributes inherited from QObject
typedef QObjectList
 
- Properties inherited from QObject
 objectName
 

Detailed Description

This is the main class of a Cutelyst appplication

Definition at line 42 of file application.h.

Constructor & Destructor Documentation

◆ Application()

Application::Application ( QObject parent = nullptr)
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.

Warning
DO NOT register your controllers, plugins or anything that might want to use config() in here, do that in init()

Definition at line 46 of file application.cpp.

References loadTranslations().

Member Function Documentation

◆ addTranslator() [1/2]

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() (what itself will use Application::translate()) to translate strings according to the locale set by Context::setLocale().

Usage example:
bool MyCutelystApp::init()
{
// ...
auto trans = new QTranslator(this);
if (trans->load(deDE, QStringLiteral("mycutelystapp"), QStringLiteral("."),
QStringLiteral("/usr/share/mycutelystapp/l10n")) { addTranslator(deDE, trans);
}
// ...
}
See also
loadTranslations()
Since
Cutelyst 1.5.0

Definition at line 438 of file application.cpp.

Referenced by addTranslator(), loadTranslationsFromDir(), and loadTranslationsFromDirs().

◆ addTranslator() [2/2]

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.

Since
Cutelyst 1.5.0

Definition at line 450 of file application.cpp.

References addTranslator().

◆ addTranslators()

void Application::addTranslators ( const QLocale locale,
const QVector< QTranslator *> &  translators 
)

Adds multiple translators for the specified locale.

See also
addTranslator()
Since
Cutelyst 1.5.0

Definition at line 455 of file application.cpp.

References QVector::crbegin(), QVector::crend(), and QVector::empty().

◆ addXCutelystVersionHeader()

void Application::addXCutelystVersionHeader ( )
protected

Adds a X-Cutelyst header with our version on each request

Definition at line 89 of file application.cpp.

◆ afterDispatch

void Cutelyst::Application::afterDispatch ( Cutelyst::Context c)
signal

This signal is emitted right after the Action found by the dispatcher got executed.

Referenced by handleRequest(), and Cutelyst::Session::setup().

◆ beforeDispatch

void Cutelyst::Application::beforeDispatch ( Cutelyst::Context c)
signal

This signal is emitted right after the Dispatcher returns the Action that will be executed.

Referenced by handleRequest(), and Cutelyst::CSRFProtection::setup().

◆ beforePrepareAction

void Cutelyst::Application::beforePrepareAction ( Cutelyst::Context c,
bool *  skipMethod 
)
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 dispached. Always check skipMethod and return if it's true. In case you want to stop further processing set skipMethod to true.

Referenced by handleRequest(), Cutelyst::StaticSimple::setup(), Cutelyst::StaticCompressed::setup(), and Cutelyst::LangSelect::setup().

◆ config() [1/2]

QVariant Application::config ( const QString key,
const QVariant defaultValue = {} 
) const

Returns application config specified by key, with a possible default value.

Definition at line 188 of file application.cpp.

◆ config() [2/2]

QVariantMap Application::config ( ) const
noexcept

User configuration for the application

Returns
A variant hash with configuration settings

Definition at line 216 of file application.cpp.

Referenced by pathTo().

◆ controllers()

QVector< Cutelyst::Controller * > Application::controllers ( ) const
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.

Referenced by enginePostFork(), and setup().

◆ createComponentPlugin()

Component * Application::createComponentPlugin ( const QString name,
QObject parent = nullptr 
)

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 139 of file application.cpp.

References Cutelyst::ComponentFactory::createComponent(), QString::fromLocal8Bit(), and QObject::parent().

◆ cutelystVersion()

const char * Application::cutelystVersion ( )
staticnoexcept

Returns cutelyst version.

Definition at line 165 of file application.cpp.

Referenced by setup().

◆ defaultHeaders()

Headers & Application::defaultHeaders ( )
protectednoexcept

This is the HTTP default response headers that each request gets

Do not change it after the application has started.

Definition at line 83 of file application.cpp.

Referenced by Cutelyst::Context::Context().

◆ dispatcher()

Dispatcher * Application::dispatcher ( ) const
noexcept

Returns the dispatcher class.

Definition at line 198 of file application.cpp.

Referenced by registerDispatcher().

◆ dispatchers()

QVector< Cutelyst::DispatchType * > Application::dispatchers ( ) const
noexcept

Returns a list with all registered dispachers.

The list might only be complete after application has been setup.

Definition at line 204 of file application.cpp.

◆ engine()

Cutelyst::Engine * Cutelyst::Application::engine ( ) const
noexcept

◆ init()

bool Application::init ( )
protectedvirtual

Do your application initialization here, if your application should not proceed log some information that might help on debuggin 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.

See also
postFork
Returns
true if your application was successfuly initted

Definition at line 69 of file application.cpp.

References QCoreApplication::applicationPid().

Referenced by setup().

◆ inited()

bool Cutelyst::Application::inited ( ) const
noexcept

Returns true if the application has been inited.

Definition at line 234 of file application.cpp.

◆ loadTranslations()

void Application::loadTranslations ( const QString filename,
const QString directory = QString(),
const QString prefix = QString(),
const QString suffix = QString() 
)

Loads translations for a specific filename from a single directory.

This can be used to load translations for a specific component 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:

bool MyApp::init()
{
loadTranslations(QStringLiteral("myapp"),
QStringLiteral("/usr/share/myapp/translations"), QStringLiteral("_"));
}

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".

See also
addTranslator(), loadTranslationsFromDir(), loadTranslationsFromDirs()
Since
Cuteylst 2.0.0

Definition at line 529 of file application.cpp.

References loadTranslationsFromDir().

Referenced by Application(), Cutelyst::Validator::loadTranslations(), Cutelyst::CSRFProtection::setup(), and Cutelyst::Memcached::setup().

◆ loadTranslationsFromDir()

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 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:

bool MyApp::init()
{
loadTranslationsFromDir(QStringLiteral("myapp"),
QStringLiteral("/usr/share/myapp/translations"), QStringLiteral("_"));
}

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".

See also
addTranslator(), loadTranslationsFromDirs()
Since
Cuteylst 2.1.0

Definition at line 537 of file application.cpp.

References addTranslator(), QVector::append(), QLocale::C, QDir::entryInfoList(), QDir::exists(), QDir::Files, QString::indexOf(), QString::isEmpty(), QLocale::language(), QString::length(), QString::mid(), QVector::reserve(), and QVector::squeeze().

Referenced by loadTranslations().

◆ loadTranslationsFromDirs()

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:

bool MyApp::init()
{
loadTranslationsFromDirs(QStringLiteral("/usr/share/locale"),
QStringLiteral("LC_MESSAGES/myapp.qm"));
}
See also
addTranslator(), loadTranslationsFromDir()
Since
Cutelyst 2.1.0

Definition at line 595 of file application.cpp.

References QFileInfo::absoluteFilePath(), QFileInfo::absolutePath(), addTranslator(), QDir::AllDirs, QVector::append(), QFileInfo::baseName(), QLocale::C, QDir::entryList(), QDir::exists(), QString::isEmpty(), QLocale::language(), QVector::reserve(), QVector::squeeze(), and QFileInfo::suffix().

◆ pathTo() [1/2]

QString Application::pathTo ( const QString path) const

Merges path with config("HOME") and returns an absolute path.

Definition at line 222 of file application.cpp.

References QDir::absoluteFilePath(), and config().

◆ pathTo() [2/2]

QString Cutelyst::Application::pathTo ( const QStringList path) const

Merges path with config("HOME") and returns an absolute path.

Definition at line 228 of file application.cpp.

References QDir::absoluteFilePath(), and QStringList::join().

◆ plugins()

QVector< Plugin * > Application::plugins ( ) const
noexcept

Returns all registered plugins

Definition at line 210 of file application.cpp.

Referenced by setup().

◆ postFork()

bool Application::postFork ( )
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 openning a connection to the database which can't be shared with other process and should probably make this function return false if it fails to open.

Default implementation returns true.

Returns
False if the engine should not use this process

Definition at line 76 of file application.cpp.

References QCoreApplication::applicationPid().

Referenced by enginePostFork().

◆ postForked

void Cutelyst::Application::postForked ( Cutelyst::Application app)
signal

◆ preForked

void Cutelyst::Application::preForked ( Cutelyst::Application app)
signal

This signal is emitted right after application has been setup and before application forks and

See also
postFork() is called.

Referenced by setup().

◆ registerController()

bool Application::registerController ( Controller controller)
protected

This method registers a Controller class which is responsible for handlying Requests, since they are reused between multiple requests beaware of not storing data there, instead you might want to use a session plugin or the stash.

Parameters
controllerthe Controller class
Returns
true if succeeded

Definition at line 105 of file application.cpp.

References QString::append(), QMetaObject::className(), QString::fromLatin1(), QString::insert(), and QObject::metaObject().

◆ registerDispatcher()

bool Application::registerDispatcher ( DispatchType dispatcher)
protected

Registers a custom DispatchType, if none is registered all the built-in dispatchers types will be registered

Definition at line 129 of file application.cpp.

References dispatcher().

◆ registerPlugin()

bool Application::registerPlugin ( Plugin plugin)
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();

Returns
true if the plugin could be registered

Definition at line 95 of file application.cpp.

References plugin().

◆ registerView()

bool Application::registerView ( View view)
protected

This method registers a View class which is responsible for rendering requests.

Parameters
viewthe View class
Returns
true if succeeded

Definition at line 117 of file application.cpp.

References QMetaObject::className(), QString::insert(), QObject::metaObject(), Cutelyst::Component::name(), and view().

◆ setConfig()

void Application::setConfig ( const QString key,
const QVariant value 
)
protected

Change the value of the configuration key You should never call this from random parts of the code as a way to store shareable data, it should only be called by a subclass

Definition at line 246 of file application.cpp.

◆ shuttingDown

void Cutelyst::Application::shuttingDown ( Cutelyst::Application app)
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.

◆ translate()

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.

See also
Context::translate(), QTranslator::translate()
Since
Cutelyst 1.5.0

Definition at line 493 of file application.cpp.

References QVector::empty(), QString::fromUtf8(), QString::isEmpty(), and QVector::value().

◆ view() [1/2]

View * Application::view ( const QString name) const

Returns the view specified by name, if no view is found nullptr is returned.

Definition at line 176 of file application.cpp.

Referenced by Cutelyst::RenderView::init(), registerView(), and setup().

◆ view() [2/2]

View * Application::view ( QStringView  name = {}) const

Returns the view specified by name, if no view is found nullptr is returned.

Definition at line 182 of file application.cpp.