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 
15 class QTranslator;
16 
17 namespace Cutelyst {
18 
19 #define CUTELYST_APPLICATION(x) \
20  Q_PLUGIN_METADATA(x) \
21  Q_INTERFACES(Cutelyst::Application)
22 
23 class Context;
24 class Controller;
25 class Component;
26 class View;
27 class Dispatcher;
28 class DispatchType;
29 class Request;
30 class Response;
31 class Engine;
32 class EngineRequest;
33 class Plugin;
34 class Headers;
35 class ApplicationPrivate;
36 
42 class CUTELYST_LIBRARY Application : public QObject
43 {
44  Q_OBJECT
45  Q_DECLARE_PRIVATE(Application)
46 public:
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>
107  T plugin()
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 
317 protected:
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 
400 Q_SIGNALS:
410  void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod);
411 
416  void beforeDispatch(Cutelyst::Context *c);
417 
422  void afterDispatch(Cutelyst::Context *c);
423 
428  void preForked(Cutelyst::Application *app);
429 
433  void postForked(Cutelyst::Application *app);
434 
440  void shuttingDown(Cutelyst::Application *app);
441 
442 protected:
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 
476 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
477 
478 #endif // CUTELYST_APPLICATION_H
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:107
The Cutelyst Component base class.
Definition: component.h:25
The Cutelyst Context.
Definition: context.h:38
Cutelyst Controller base class
Definition: controller.h:87
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst View abstract view component
Definition: view.h:21
The Cutelyst Application.
Definition: application.h:42
The Cutelyst Engine
Definition: engine.h:20
The Cutelyst Dispatcher.
Definition: dispatcher.h:27