Cutelyst  2.13.0
application.h
1 /*
2  * Copyright (C) 2013-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CUTELYST_APPLICATION_H
19 #define CUTELYST_APPLICATION_H
20 
21 #include <QtCore/QObject>
22 #include <QtCore/QVariant>
23 #include <QtCore/QLocale>
24 #include <QtCore/QVector>
25 
26 #include <Cutelyst/cutelyst_global.h>
27 
28 class QTranslator;
29 
30 namespace Cutelyst {
31 
32 #define CUTELYST_APPLICATION(x) \
33  Q_PLUGIN_METADATA(x) \
34  Q_INTERFACES(Cutelyst::Application)
35 
36 class Context;
37 class Controller;
38 class Component;
39 class View;
40 class Dispatcher;
41 class DispatchType;
42 class Request;
43 class Response;
44 class Engine;
45 class EngineRequest;
46 class Plugin;
47 class Headers;
48 class ApplicationPrivate;
49 
55 class CUTELYST_LIBRARY Application : public QObject
56 {
57  Q_OBJECT
58  Q_DECLARE_PRIVATE(Application)
59 public:
74  explicit Application(QObject *parent = nullptr);
75  virtual ~Application();
76 
82  QVector<Controller *> controllers() const;
83 
87  View *view(const QString &name = QString()) const;
88 
92  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
93 
97  Dispatcher *dispatcher() const;
98 
104  QVector<DispatchType *> dispatchers() const;
105 
109  QVector<Plugin *> plugins() const;
110 
114  template <typename T>
115  T plugin()
116  {
117  const auto pluginsConst = plugins();
118  for (Plugin *plugin : pluginsConst) {
119  auto p = qobject_cast<T>(plugin);
120  if (p) {
121  return p;
122  }
123  }
124  return nullptr;
125  }
126 
131  QVariantMap config() const;
132 
136  QString pathTo(const QString &path) const;
137 
141  QString pathTo(const QStringList &path) const;
142 
146  bool inited() const;
147 
151  Engine *engine() const;
152 
157  Component *createComponentPlugin(const QString &name, QObject *parent = nullptr);
158 
162  static const char *cutelystVersion();
163 
192  void addTranslator(const QLocale &locale, QTranslator *translator);
193 
203  void addTranslator(const QString &locale, QTranslator *translator);
204 
212  void addTranslators(const QLocale &locale, const QVector<QTranslator *> &translators);
213 
225  QString translate(const QLocale &locale, const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
226 
252  void loadTranslations(const QString &filename, const QString &directory = QString(), const QString &prefix = QString(), const QString &suffix = QString());
253 
279  QVector<QLocale> loadTranslationsFromDir(const QString &filename, const QString &directory = QString(), const QString &prefix = QStringLiteral("."), const QString &suffix = QStringLiteral(".qm"));
280 
302  QVector<QLocale> loadTranslationsFromDirs(const QString &directory, const QString &filename);
303 
304 protected:
318  virtual bool init();
319 
337  virtual bool postFork();
338 
344  Headers &defaultHeaders();
345 
349  void addXCutelystVersionHeader();
350 
358  bool registerPlugin(Plugin *plugin);
359 
370  bool registerController(Controller *controller);
371 
379  bool registerView(View *view);
380 
385  bool registerDispatcher(DispatchType *dispatcher);
386 
387 Q_SIGNALS:
397  void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod);
398 
403  void beforeDispatch(Cutelyst::Context *c);
404 
409  void afterDispatch(Cutelyst::Context *c);
410 
415  void preForked(Cutelyst::Application *app);
416 
420  void postForked(Cutelyst::Application *app);
421 
427  void shuttingDown(Cutelyst::Application *app);
428 
429 protected:
436  void setConfig(const QString &key, const QVariant &value);
437 
438  friend class Engine;
439  friend class Context;
440 
444  bool setup(Engine *engine);
445 
449  void handleRequest(Cutelyst::EngineRequest *request);
450 
454  bool enginePostFork();
455 
456  ApplicationPrivate *d_ptr;
457 };
458 
459 }
460 
461 #define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
462 
463 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
464 
465 #endif // CUTELYST_APPLICATION_H
Cutelyst::Controller
Cutelyst Controller base class
Definition: controller.h:102
Cutelyst::Plugin
Definition: plugin.h:30
Cutelyst::Application::plugin
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:115
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Dispatcher
The Cutelyst Dispatcher.
Definition: dispatcher.h:40
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::Component
The Cutelyst Component base class.
Definition: component.h:38
Cutelyst::EngineRequest
Definition: enginerequest.h:31
Cutelyst::DispatchType
Definition: dispatchtype.h:31
Cutelyst::Headers
Definition: headers.h:29
Cutelyst::View
Cutelyst View abstract view component
Definition: view.h:34
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Engine
The Cutelyst Engine.
Definition: engine.h:33