Cutelyst  2.5.0
context.h
1 /*
2  * Copyright (C) 2013-2018 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_CONTEXT_H
19 #define CUTELYST_CONTEXT_H
20 
21 #include <QtCore/QObject>
22 #include <QtCore/QVariant>
23 #include <QtCore/QUrl>
24 #include <QtCore/QStringList>
25 #include <QtCore/QStack>
26 
27 #include <Cutelyst/request.h>
28 #include <Cutelyst/cutelyst_global.h>
29 
30 namespace Cutelyst {
31 
32 class Action;
33 class Application;
34 class Component;
35 class Engine;
36 class Response;
37 class Dispatcher;
38 class Controller;
39 class View;
40 class Stats;
41 class Plugin;
42 class ContextPrivate;
43 
50 class CUTELYST_LIBRARY Context : public QObject
51 {
52  Q_OBJECT
53  Q_PROPERTY(Action* action READ action CONSTANT)
54  Q_PROPERTY(QString actionName READ actionName CONSTANT)
55  Q_PROPERTY(QString ns READ ns CONSTANT)
56  Q_PROPERTY(QString namespace READ ns CONSTANT)
57  Q_PROPERTY(Request *req READ request CONSTANT)
58  Q_PROPERTY(Request *request READ request CONSTANT)
59  Q_PROPERTY(Controller *controller READ controller CONSTANT)
60  Q_PROPERTY(QString controllerName READ controllerName CONSTANT)
61  Q_PROPERTY(QVariantMap config READ config CONSTANT)
62  Q_PROPERTY(bool state READ state CONSTANT)
63 public:
69  Context(Application *app);
70  virtual ~Context();
71 
75  bool error() const;
76 
80  void error(const QString &error);
81 
85  QStringList errors() const;
86 
90  bool state() const;
91 
96  void setState(bool state);
97 
101  Engine *engine() const;
102 
106  Application *app() const;
107 
111  Response *response() const;
112 
116  Response *res() const;
117 
121  Action *action() const;
122 
126  QString actionName() const;
127 
135  QString ns() const;
136 
141  Request *request() const;
142 
146  Request *req() const;
147 
151  Dispatcher *dispatcher() const;
152 
156  QString controllerName() const;
157 
161  Controller *controller() const;
162 
167  Controller *controller(const QString &name) const;
168 
172  View *view(const QString &name = QString()) const;
173 
179  View *customView() const;
180 
192  bool setCustomView(const QString &name);
193 
213  inline void stash(const QVariantHash &unite);
214 
226  QVariantHash &stash();
227 
231  QVariant stash(const QString &key) const;
232 
236  void setStash(const QString &key, const QVariant &value);
237 
241  void setStash(const QString &key, const ParamsMultiMap &map);
242 
246  QStack<Component *> stack() const;
247 
259  QUrl uriFor(const QString &path = QString(),
260  const QStringList &args = QStringList(),
261  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
262 
273  inline QUrl uriFor(const QString &path,
274  const ParamsMultiMap &queryValues) const;
275 
285  QUrl uriFor(Action *action,
286  const QStringList &captures = QStringList(),
287  const QStringList &args = QStringList(),
288  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
289 
295  inline QUrl uriFor(Action *action,
296  const ParamsMultiMap &queryValues) const;
297 
321  QUrl uriForAction(const QString &path,
322  const QStringList &captures = QStringList(),
323  const QStringList &args = QStringList(),
324  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
325 
329  inline QUrl uriForAction(const QString &path,
330  const ParamsMultiMap &queryValues) const;
331 
336  bool detached() const;
337 
343  void detach(Action *action = nullptr);
344 
363  bool forward(Component *component);
364 
383  bool forward(const QString &action);
384 
388  Action *getAction(const QString &action, const QString &ns = QString()) const;
389 
393  QVector<Action *> getActions(const QString &action, const QString &ns = QString()) const;
394 
398  QVector<Plugin *> plugins() const;
399 
403  template <typename T>
404  T plugin()
405  {
406  const auto pluginsConst = plugins();
407  for (Plugin *plugin : pluginsConst) {
408  auto p = qobject_cast<T>(plugin);
409  if (p) {
410  return p;
411  }
412  }
413  return nullptr;
414  }
415 
419  bool execute(Component *code);
420 
428  QLocale locale() const;
429 
444  void setLocale(const QLocale &locale);
445 
449  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
450 
454  QVariantMap config() const;
455 
469  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
470 
483  Q_DECL_DEPRECATED bool wait(uint count = 1);
484 
485 public Q_SLOTS:
496  void next(bool force = false);
497 
498 protected:
502  Context(ContextPrivate *priv);
503 
504  friend class Application;
505  friend class Action;
506  friend class DispatchType;
507  friend class Plugin;
508  friend class Engine;
509  ContextPrivate *d_ptr;
510 
511 private:
512  Q_DECLARE_PRIVATE(Context)
513 };
514 
515 inline void Context::stash(const QVariantHash &unite)
516 { stash().unite(unite); }
517 
518 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
519 { return uriFor(path, QStringList(), queryValues); }
520 
521 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
522 { return uriFor(action, QStringList(), QStringList(), queryValues); }
523 
524 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
525 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
526 
527 }
528 
529 Q_DECLARE_METATYPE(Cutelyst::Context *)
530 
531 #endif // CUTELYST_CONTEXT_H
QMap< QString, QString > ParamsMultiMap
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:225
The Cutelyst Component base class.
Definition: component.h:38
This class represents a Cutelyst Action.
Definition: action.h:47
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:296
The Cutelyst Context.
Definition: context.h:50
Cutelyst Controller base class
Definition: controller.h:102
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: context.h:404
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
QVariantHash & stash()
Definition: context.cpp:195
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