Cutelyst  1.6.0
context.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_CONTEXT_H
21 #define CUTELYST_CONTEXT_H
22 
23 #include <QtCore/QObject>
24 #include <QtCore/QVariant>
25 #include <QtCore/QUrl>
26 #include <QtCore/QStringList>
27 #include <QtCore/QStack>
28 
29 #include <Cutelyst/request.h>
30 #include <Cutelyst/cutelyst_global.h>
31 
32 namespace Cutelyst {
33 
34 class Action;
35 class Application;
36 class Component;
37 class Engine;
38 class Response;
39 class Dispatcher;
40 class Controller;
41 class View;
42 class Stats;
43 class Plugin;
44 class ContextPrivate;
45 
52 class CUTELYST_LIBRARY Context : public QObject
53 {
54  Q_OBJECT
55  Q_PROPERTY(Action* action READ action)
56  Q_PROPERTY(QString actionName READ actionName)
57  Q_PROPERTY(QString ns READ ns)
58  Q_PROPERTY(QString namespace READ ns)
59  Q_PROPERTY(Request *req READ request)
60  Q_PROPERTY(Request *request READ request)
61  Q_PROPERTY(Controller *controller READ controller)
62  Q_PROPERTY(QString controllerName READ controllerName)
63  Q_PROPERTY(QVariantMap config READ config)
64  Q_PROPERTY(bool state READ state)
65 public:
66  virtual ~Context();
67 
71  bool error() const;
72 
76  void error(const QString &error);
77 
81  QStringList errors() const;
82 
86  bool state() const;
87 
92  void setState(bool state);
93 
97  Engine *engine() const;
98 
102  Application *app() const;
103 
107  Response *response() const;
108 
112  Response *res() const;
113 
117  Action *action() const;
118 
122  QString actionName() const;
123 
131  QString ns() const;
132 
137  Request *request() const;
138 
142  Request *req() const;
143 
147  Dispatcher *dispatcher() const;
148 
152  QString controllerName() const;
153 
157  Controller *controller() const;
158 
163  Controller *controller(const QString &name) const;
164 
170  View *view() const;
171 
177  View *view(const QString &name) const;
178 
190  bool setView(const QString &name);
191 
211  inline void stash(const QVariantHash &unite);
212 
224  QVariantHash &stash();
225 
229  QVariant stash(const QString &key) const;
230 
234  void setStash(const QString &key, const QVariant &value);
235 
239  QStack<Component *> stack() const;
240 
252  QUrl uriFor(const QString &path = QString(),
253  const QStringList &args = QStringList(),
254  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
255 
266  inline QUrl uriFor(const QString &path,
267  const ParamsMultiMap &queryValues) const;
268 
278  QUrl uriFor(Action *action,
279  const QStringList &captures = QStringList(),
280  const QStringList &args = QStringList(),
281  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
282 
288  inline QUrl uriFor(Action *action,
289  const ParamsMultiMap &queryValues) const;
290 
314  QUrl uriForAction(const QString &path,
315  const QStringList &captures = QStringList(),
316  const QStringList &args = QStringList(),
317  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
318 
322  inline QUrl uriForAction(const QString &path,
323  const ParamsMultiMap &queryValues) const;
324 
329  bool detached() const;
330 
336  void detach(Action *action = nullptr);
337 
356  bool forward(Component *component);
357 
376  bool forward(const QString &action);
377 
381  // TODO C2 mark as const
382  Action *getAction(const QString &action, const QString &ns = QString());
383 
387  // TODO C2 mark as const
388  QVector<Action *> getActions(const QString &action, const QString &ns = QString());
389 
393  // TODO C2 mark as const
394  QVector<Plugin *> plugins();
395 
399  template <typename T>
400  T plugin()
401  {
402  const auto pluginsConst = plugins();
403  for (Plugin *plugin : pluginsConst) {
404  auto p = qobject_cast<T>(plugin);
405  if (p) {
406  return p;
407  }
408  }
409  return 0;
410  }
411 
415  bool execute(Component *code);
416 
424  QLocale locale() const;
425 
440  void setLocale(const QLocale &locale);
441 
445  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
446 
450  QVariantMap config() const;
451 
456  void *engineData();
457 
471  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
472 
473 protected:
477  Context(ContextPrivate *priv);
478 
479  friend class Application;
480  friend class Action;
481  friend class DispatchType;
482  friend class Plugin;
483  friend class Engine;
484  ContextPrivate *d_ptr;
485 
486 private:
487  Q_DECLARE_PRIVATE(Context)
488 };
489 
490 inline void Context::stash(const QVariantHash &unite)
491 { stash().unite(unite); }
492 
493 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
494 { return uriFor(path, QStringList(), queryValues); }
495 
496 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
497 { return uriFor(action, QStringList(), QStringList(), queryValues); }
498 
499 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
500 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
501 
502 }
503 
504 Q_DECLARE_METATYPE(Cutelyst::Context *)
505 
506 #endif // CUTELYST_CONTEXT_H
QMap< QString, QString > ParamsMultiMap
The Cutelyst Component base class.
Definition: component.h:40
This class represents a Cutelyst Action.
Definition: action.h:49
The Cutelyst Context.
Definition: context.h:52
Cutelyst Controller base class
Definition: controller.h:104
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: context.h:400
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
QVariantHash & stash()
Cutelyst View abstract view component
Definition: view.h:35
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
The Cutelyst Application.
Definition: application.h:56
The Cutelyst Dispatcher.
Definition: dispatcher.h:41