cutelyst  4.5.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_export.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 
65 class CUTELYST_EXPORT Application : public QObject
66 {
67  Q_OBJECT
68  Q_DECLARE_PRIVATE(Application)
69 public:
84  explicit Application(QObject *parent = nullptr);
85 
89  virtual ~Application();
90 
96  QVector<Controller *> controllers() const noexcept;
97 
102  View *view(QStringView name = {}) const;
103 
114  QVariant config(const QString &key, const QVariant &defaultValue = {}) const;
115 
119  Dispatcher *dispatcher() const noexcept;
120 
126  QVector<DispatchType *> dispatchers() const noexcept;
127 
131  QVector<Plugin *> plugins() const noexcept;
132 
136  template <typename T>
137  T plugin()
138  {
139  const auto pluginsConst = plugins();
140  for (Plugin *plugin : pluginsConst) {
141  auto p = qobject_cast<T>(plugin);
142  if (p) {
143  return p;
144  }
145  }
146  return nullptr;
147  }
148 
159  QVariantMap config() const noexcept;
160 
167  QString pathTo(const QString &path) const;
168 
176  QString pathTo(const QStringList &path) const;
177 
181  bool inited() const noexcept;
182 
186  Engine *engine() const noexcept;
187 
192  Component *createComponentPlugin(const QString &name, QObject *parent = nullptr);
193 
197  static const char *cutelystVersion() noexcept;
198 
234  void addTranslator(const QLocale &locale, QTranslator *translator);
235 
247  void addTranslator(const QString &locale, QTranslator *translator);
248 
261  void addTranslators(const QLocale &locale, const QVector<QTranslator *> &translators);
262 
277  QString translate(const QLocale &locale,
278  const char *context,
279  const char *sourceText,
280  const char *disambiguation = nullptr,
281  int n = -1) const;
282 
315  void loadTranslations(const QString &filename,
316  const QString &directory = {},
317  const QString &prefix = {},
318  const QString &suffix = {});
319 
353  QVector<QLocale> loadTranslationsFromDir(const QString &filename,
354  const QString &directory = QString(),
355  const QString &prefix = QStringLiteral("."),
356  const QString &suffix = QStringLiteral(".qm"));
357 
384  QVector<QLocale> loadTranslationsFromDirs(const QString &directory, const QString &filename);
385 
394  [[nodiscard]] QLocale defaultLocale() const noexcept;
395 
404  void setDefaultLocale(const QLocale &locale);
405 
406 protected:
422  virtual bool init();
423 
441  virtual bool postFork();
442 
448  Headers &defaultHeaders() noexcept;
449 
453  void addXCutelystVersionHeader();
454 
462  bool registerPlugin(Plugin *plugin);
463 
474  bool registerController(Controller *controller);
475 
483  bool registerView(View *view);
484 
489  bool registerDispatcher(DispatchType *dispatcher);
490 
491 Q_SIGNALS:
501  void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod);
502 
507  void beforeDispatch(Cutelyst::Context *c);
508 
513  void afterDispatch(Cutelyst::Context *c);
514 
519  void preForked(Cutelyst::Application *app);
520 
524  void postForked(Cutelyst::Application *app);
525 
531  void shuttingDown(Cutelyst::Application *app);
532 
533 protected:
544  void setConfig(const QString &key, const QVariant &value);
545 
546  friend class Engine;
547  friend class Context;
548 
552  bool setup(Engine *engine);
553 
557  void handleRequest(Cutelyst::EngineRequest *request);
558 
562  bool enginePostFork();
563 
564  ApplicationPrivate *d_ptr;
565 };
566 
567 } // namespace Cutelyst
568 
569 #define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
570 
571 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
572 
573 #endif // CUTELYST_APPLICATION_H
The Cutelyst application.
Definition: application.h:66
The Cutelyst Component base class.
Definition: component.h:30
The Cutelyst Context.
Definition: context.h:42
The Cutelyst Dispatcher.
Definition: dispatcher.h:29
The Cutelyst Engine.
Definition: engine.h:20
Base class for Cutelyst Plugins.
Definition: plugin.h:25
Abstract View component for Cutelyst.
Definition: view.h:25
The Cutelyst namespace holds all public Cutelyst API.