Cutelyst  1.8.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 Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef CUTELYST_APPLICATION_H
21 #define CUTELYST_APPLICATION_H
22 
23 #include <QtCore/QObject>
24 #include <QtCore/QVariant>
25 #include <QtCore/QLocale>
26 #include <QtCore/QVector>
27 
28 #include <Cutelyst/cutelyst_global.h>
29 
30 class QTranslator;
31 
32 namespace Cutelyst {
33 
34 #define CUTELYST_APPLICATION(x) \
35  Q_PLUGIN_METADATA(x) \
36  Q_INTERFACES(Cutelyst::Application)
37 
38 class Context;
39 class Controller;
40 class Component;
41 class View;
42 class Dispatcher;
43 class DispatchType;
44 class Request;
45 class Response;
46 class Engine;
47 class Plugin;
48 class Headers;
49 class ApplicationPrivate;
50 
56 class CUTELYST_LIBRARY Application : public QObject
57 {
58  Q_OBJECT
59  Q_DECLARE_PRIVATE(Application)
60 public:
75  explicit Application(QObject *parent = nullptr);
76  virtual ~Application();
77 
83  QVector<Controller *> controllers() const;
84 
88  View *view(const QString &name = QString()) const;
89 
93  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
94 
98  Dispatcher *dispatcher() const;
99 
105  QVector<DispatchType *> dispatchers() const;
106 
110  QVector<Plugin *> plugins() const;
111 
115  template <typename T>
116  T plugin()
117  {
118  const auto pluginsConst = plugins();
119  for (Plugin *plugin : pluginsConst) {
120  auto p = qobject_cast<T>(plugin);
121  if (p) {
122  return p;
123  }
124  }
125  return 0;
126  }
127 
132  QVariantMap config() const;
133 
137  QString pathTo(const QString &path) const;
138 
142  QString pathTo(const QStringList &path) const;
143 
147  bool inited() const;
148 
152  Engine *engine() const;
153 
158  Component *createComponentPlugin(const QString &name, QObject *parent = nullptr);
159 
163  static const char *cutelystVersion();
164 
191  void addTranslator(const QLocale &locale, QTranslator *translator);
192 
202  void addTranslator(const QString &locale, QTranslator *translator);
203 
211  void addTranslators(const QLocale &locale, const QVector<QTranslator *> &translators);
212 
224  QString translate(const QLocale &locale, const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
225 
226 protected:
240  virtual bool init();
241 
259  virtual bool postFork();
260 
266  Headers &defaultHeaders();
267 
275  bool registerPlugin(Plugin *plugin);
276 
287  bool registerController(Controller *controller);
288 
296  bool registerView(View *view);
297 
302  bool registerDispatcher(DispatchType *dispatcher);
303 
304 Q_SIGNALS:
314  void beforePrepareAction(Context *c, bool *skipMethod);
315 
320  void beforeDispatch(Context *c);
321 
326  void afterDispatch(Context *c);
327 
332  void preForked(Application *app);
333 
337  void postForked(Application *app);
338 
339 protected:
346  void setConfig(const QString &key, const QVariant &value);
347 
348  friend class Engine;
349 
353  bool setup(Engine *engine);
354 
358  Q_DECL_DEPRECATED
359  void handleRequest(Request *req);
360 
364  Context *handleRequest2(Request *req);
365 
369  bool enginePostFork();
370 
371  ApplicationPrivate *d_ptr;
372 };
373 
374 }
375 
376 #define CutelystApplicationInterface_iid "org.cutelyst.CutelystApplicationInterface"
377 
378 Q_DECLARE_INTERFACE(Cutelyst::Application, CutelystApplicationInterface_iid)
379 
380 #endif // CUTELYST_APPLICATION_H
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:116
The Cutelyst Component base class.
Definition: component.h:40
The Cutelyst Context.
Definition: context.h:52
Cutelyst Controller base class
Definition: controller.h:104
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst View abstract view component
Definition: view.h:35
The Cutelyst Application.
Definition: application.h:56
The Cutelyst Dispatcher.
Definition: dispatcher.h:41