Cutelyst  3.5.0
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 = QString()) const;
75 
79  QVariant config(const QString &key, const QVariant &defaultValue = {}) const;
80 
84  Dispatcher *dispatcher() const noexcept;
85 
91  QVector<DispatchType *> dispatchers() const noexcept;
92 
96  QVector<Plugin *> plugins() const noexcept;
97 
101  template <typename T>
102  T plugin()
103  {
104  const auto pluginsConst = plugins();
105  for (Plugin *plugin : pluginsConst) {
106  auto p = qobject_cast<T>(plugin);
107  if (p) {
108  return p;
109  }
110  }
111  return nullptr;
112  }
113 
118  QVariantMap config() const noexcept;
119 
123  QString pathTo(const QString &path) const;
124 
128  QString pathTo(const QStringList &path) const;
129 
133  bool inited() const noexcept;
134 
138  Engine *engine() const noexcept;
139 
144  Component *createComponentPlugin(const QString &name, QObject *parent = nullptr);
145 
149  static const char *cutelystVersion() noexcept;
150 
179  void addTranslator(const QLocale &locale, QTranslator *translator);
180 
190  void addTranslator(const QString &locale, QTranslator *translator);
191 
199  void addTranslators(const QLocale &locale, const QVector<QTranslator *> &translators);
200 
212  QString translate(const QLocale &locale, const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
213 
239  void loadTranslations(const QString &filename, const QString &directory = QString(), const QString &prefix = QString(), const QString &suffix = QString());
240 
266  QVector<QLocale> loadTranslationsFromDir(const QString &filename, const QString &directory = QString(), const QString &prefix = QStringLiteral("."), const QString &suffix = QStringLiteral(".qm"));
267 
289  QVector<QLocale> loadTranslationsFromDirs(const QString &directory, const QString &filename);
290 
291 protected:
305  virtual bool init();
306 
324  virtual bool postFork();
325 
331  Headers &defaultHeaders() noexcept;
332 
336  void addXCutelystVersionHeader();
337 
345  bool registerPlugin(Plugin *plugin);
346 
357  bool registerController(Controller *controller);
358 
366  bool registerView(View *view);
367 
372  bool registerDispatcher(DispatchType *dispatcher);
373 
374 Q_SIGNALS:
384  void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod);
385 
390  void beforeDispatch(Cutelyst::Context *c);
391 
396  void afterDispatch(Cutelyst::Context *c);
397 
402  void preForked(Cutelyst::Application *app);
403 
407  void postForked(Cutelyst::Application *app);
408 
414  void shuttingDown(Cutelyst::Application *app);
415 
416 protected:
423  void setConfig(const QString &key, const QVariant &value);
424 
425  friend class Engine;
426  friend class Context;
427 
431  bool setup(Engine *engine);
432 
436  void handleRequest(Cutelyst::EngineRequest *request);
437 
441  bool enginePostFork();
442 
443  ApplicationPrivate *d_ptr;
444 };
445 
446 }
447 
448 #define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
449 
450 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
451 
452 #endif // CUTELYST_APPLICATION_H
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:102
The Cutelyst Component base class.
Definition: component.h:25
The Cutelyst Context.
Definition: context.h:38
Cutelyst Controller base class
Definition: controller.h:89
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