Cutelyst  1.11.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 Plugin;
46 class Headers;
47 class ApplicationPrivate;
48 
54 class CUTELYST_LIBRARY Application : public QObject
55 {
56  Q_OBJECT
57  Q_DECLARE_PRIVATE(Application)
58 public:
73  explicit Application(QObject *parent = nullptr);
74  virtual ~Application();
75 
81  QVector<Controller *> controllers() const;
82 
86  View *view(const QString &name = QString()) const;
87 
91  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
92 
96  Dispatcher *dispatcher() const;
97 
103  QVector<DispatchType *> dispatchers() const;
104 
108  QVector<Plugin *> plugins() const;
109 
113  template <typename T>
114  T plugin()
115  {
116  const auto pluginsConst = plugins();
117  for (Plugin *plugin : pluginsConst) {
118  auto p = qobject_cast<T>(plugin);
119  if (p) {
120  return p;
121  }
122  }
123  return nullptr;
124  }
125 
130  QVariantMap config() const;
131 
135  QString pathTo(const QString &path) const;
136 
140  QString pathTo(const QStringList &path) const;
141 
145  bool inited() const;
146 
150  Engine *engine() const;
151 
156  Component *createComponentPlugin(const QString &name, QObject *parent = nullptr);
157 
161  static const char *cutelystVersion();
162 
189  void addTranslator(const QLocale &locale, QTranslator *translator);
190 
200  void addTranslator(const QString &locale, QTranslator *translator);
201 
209  void addTranslators(const QLocale &locale, const QVector<QTranslator *> &translators);
210 
222  QString translate(const QLocale &locale, const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
223 
224 protected:
238  virtual bool init();
239 
257  virtual bool postFork();
258 
264  Headers &defaultHeaders();
265 
273  bool registerPlugin(Plugin *plugin);
274 
285  bool registerController(Controller *controller);
286 
294  bool registerView(View *view);
295 
300  bool registerDispatcher(DispatchType *dispatcher);
301 
302 Q_SIGNALS:
312  void beforePrepareAction(Context *c, bool *skipMethod);
313 
318  void beforeDispatch(Context *c);
319 
324  void afterDispatch(Context *c);
325 
330  void preForked(Application *app);
331 
335  void postForked(Application *app);
336 
337 protected:
344  void setConfig(const QString &key, const QVariant &value);
345 
346  friend class Engine;
347  friend class Context;
348 
352  bool setup(Engine *engine);
353 
357  Q_DECL_DEPRECATED
358  void handleRequest(Request *req);
359 
363  Context *handleRequest2(Request *req);
364 
368  bool enginePostFork();
369 
370  ApplicationPrivate *d_ptr;
371 };
372 
373 }
374 
375 #define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
376 
377 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
378 
379 #endif // CUTELYST_APPLICATION_H
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:114
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:54
The Cutelyst Engine.
Definition: engine.h:33
The Cutelyst Dispatcher.
Definition: dispatcher.h:40