Cutelyst  2.5.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 
353  bool registerPlugin(Plugin *plugin);
354 
365  bool registerController(Controller *controller);
366 
374  bool registerView(View *view);
375 
380  bool registerDispatcher(DispatchType *dispatcher);
381 
382 Q_SIGNALS:
392  void beforePrepareAction(Context *c, bool *skipMethod);
393 
398  void beforeDispatch(Context *c);
399 
404  void afterDispatch(Context *c);
405 
410  void preForked(Application *app);
411 
415  void postForked(Application *app);
416 
417 protected:
424  void setConfig(const QString &key, const QVariant &value);
425 
426  friend class Engine;
427  friend class Context;
428 
432  bool setup(Engine *engine);
433 
437  void handleRequest(Cutelyst::EngineRequest *request);
438 
442  bool enginePostFork();
443 
444  ApplicationPrivate *d_ptr;
445 };
446 
447 }
448 
449 #define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
450 
451 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
452 
453 #endif // CUTELYST_APPLICATION_H
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:115
The Cutelyst Component base class.
Definition: component.h:38
The Cutelyst Context.
Definition: context.h:50
Cutelyst Controller base class
Definition: controller.h:102
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst View abstract view component
Definition: view.h:33
The Cutelyst Application.
Definition: application.h:55
The Cutelyst Engine.
Definition: engine.h:33
The Cutelyst Dispatcher.
Definition: dispatcher.h:40