cutelyst  3.7.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 <QtCore/QObject>
9 #include <QtCore/QVariant>
10 #include <QtCore/QLocale>
11 #include <QtCore/QVector>
12 
13 #include <Cutelyst/cutelyst_global.h>
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, const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
218 
244  void loadTranslations(const QString &filename, const QString &directory = QString(), const QString &prefix = QString(), const QString &suffix = QString());
245 
271  QVector<QLocale> loadTranslationsFromDir(const QString &filename, const QString &directory = QString(), const QString &prefix = QStringLiteral("."), const QString &suffix = QStringLiteral(".qm"));
272 
294  QVector<QLocale> loadTranslationsFromDirs(const QString &directory, const QString &filename);
295 
296 protected:
310  virtual bool init();
311 
329  virtual bool postFork();
330 
336  Headers &defaultHeaders() noexcept;
337 
341  void addXCutelystVersionHeader();
342 
350  bool registerPlugin(Plugin *plugin);
351 
362  bool registerController(Controller *controller);
363 
371  bool registerView(View *view);
372 
377  bool registerDispatcher(DispatchType *dispatcher);
378 
379 Q_SIGNALS:
389  void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod);
390 
395  void beforeDispatch(Cutelyst::Context *c);
396 
401  void afterDispatch(Cutelyst::Context *c);
402 
407  void preForked(Cutelyst::Application *app);
408 
412  void postForked(Cutelyst::Application *app);
413 
419  void shuttingDown(Cutelyst::Application *app);
420 
421 protected:
428  void setConfig(const QString &key, const QVariant &value);
429 
430  friend class Engine;
431  friend class Context;
432 
436  bool setup(Engine *engine);
437 
441  void handleRequest(Cutelyst::EngineRequest *request);
442 
446  bool enginePostFork();
447 
448  ApplicationPrivate *d_ptr;
449 };
450 
451 }
452 
453 #define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
454 
455 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
456 
457 #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:107
The Cutelyst Component base class.
Definition: component.h:26
The Cutelyst Context.
Definition: context.h:39
Cutelyst Controller base class
Definition: controller.h:90
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