Cutelyst  3.4.0
context.h
1 /*
2  * Copyright (C) 2013-2022 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/async.h>
28 #include <Cutelyst/request.h>
29 #include <Cutelyst/cutelyst_global.h>
30 
31 namespace Cutelyst {
32 
33 class Action;
34 class Application;
35 class Component;
36 class Engine;
37 class Response;
38 class Dispatcher;
39 class Controller;
40 class View;
41 class Stats;
42 class Plugin;
43 class ContextPrivate;
44 
51 class CUTELYST_LIBRARY Context : public QObject
52 {
53  Q_OBJECT
54  Q_PROPERTY(Action* action READ action CONSTANT)
55  Q_PROPERTY(QString actionName READ actionName CONSTANT)
56  Q_PROPERTY(QString ns READ ns CONSTANT)
57  Q_PROPERTY(QString namespace READ ns CONSTANT)
58  Q_PROPERTY(Request *req READ request CONSTANT)
59  Q_PROPERTY(Request *request READ request CONSTANT)
60  Q_PROPERTY(Controller *controller READ controller CONSTANT)
61  Q_PROPERTY(QString controllerName READ controllerName CONSTANT)
62  Q_PROPERTY(QVariantMap config READ config CONSTANT)
63  Q_PROPERTY(bool state READ state CONSTANT)
64 public:
70  Context(Application *app);
71  virtual ~Context();
72 
76  bool error() const noexcept;
77 
81  void error(const QString &error);
82 
86  QStringList errors() const noexcept;
87 
91  bool state() const noexcept;
92 
97  void setState(bool state) noexcept;
98 
102  Engine *engine() const noexcept;
103 
107  Application *app() const noexcept;
108 
112  Response *response() const noexcept;
113 
117  Response *res() const noexcept;
118 
122  Action *action() const noexcept;
123 
127  QString actionName() const noexcept;
128 
136  QString ns() const noexcept;
137 
142  Request *request() const noexcept;
143 
147  Request *req() const noexcept;
148 
152  Dispatcher *dispatcher() const noexcept;
153 
157  QString controllerName() const;
158 
162  Controller *controller() const noexcept;
163 
168  Controller *controller(const QString &name) const;
169 
173  View *view(const QString &name = QString()) const;
174 
180  View *customView() const noexcept;
181 
193  bool setCustomView(const QString &name);
194 
216  void stash(const QVariantHash &unite);
217 
229  QVariantHash &stash();
230 
234  QVariant stash(const QString &key) const;
235 
239  QVariant stash(const QString &key, const QVariant &defaultValue) const;
240 
246  QVariant stashTake(const QString &key);
247 
252  bool stashRemove(const QString &key);
253 
257  void setStash(const QString &key, const QVariant &value);
258 
262  void setStash(const QString &key, const ParamsMultiMap &map);
263 
267  QStack<Component *> stack() const noexcept;
268 
280  QUrl uriFor(const QString &path = QString(),
281  const QStringList &args = QStringList(),
282  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
283 
294  inline QUrl uriFor(const QString &path,
295  const ParamsMultiMap &queryValues) const;
296 
306  QUrl uriFor(Action *action,
307  const QStringList &captures = QStringList(),
308  const QStringList &args = QStringList(),
309  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
310 
316  inline QUrl uriFor(Action *action,
317  const ParamsMultiMap &queryValues) const;
318 
342  QUrl uriForAction(const QString &path,
343  const QStringList &captures = QStringList(),
344  const QStringList &args = QStringList(),
345  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
346 
350  inline QUrl uriForAction(const QString &path,
351  const ParamsMultiMap &queryValues) const;
352 
357  bool detached() const noexcept;
358 
364  void detach(Action *action = nullptr);
365 
381  void detachAsync() noexcept;
382 
390  void attachAsync();
391 
410  bool forward(Component *component);
411 
430  bool forward(const QString &action);
431 
435  Action *getAction(const QString &action, const QString &ns = QString()) const;
436 
440  QVector<Action *> getActions(const QString &action, const QString &ns = QString()) const;
441 
445  QVector<Plugin *> plugins() const;
446 
450  template <typename T>
451  T plugin()
452  {
453  const auto pluginsConst = plugins();
454  for (Plugin *plugin : pluginsConst) {
455  auto p = qobject_cast<T>(plugin);
456  if (p) {
457  return p;
458  }
459  }
460  return nullptr;
461  }
462 
466  bool execute(Component *code);
467 
475  QLocale locale() const noexcept;
476 
491  void setLocale(const QLocale &locale);
492 
496  QVariant config(const QString &key, const QVariant &defaultValue = {}) const;
497 
501  QVariantMap config() const noexcept;
502 
516  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
517 
518 public Q_SLOTS:
523  void finalize();
524 
525 protected:
529  Context(ContextPrivate *priv);
530 
531  friend class Application;
532  friend class Action;
533  friend class ActionChain;
534  friend class DispatchType;
535  friend class Plugin;
536  friend class Engine;
537  friend class Controller;
538  friend class Async;
539  ContextPrivate *d_ptr;
540 
541 private:
542  Q_DECLARE_PRIVATE(Context)
543 };
544 
545 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
546 { return uriFor(path, QStringList(), queryValues); }
547 
548 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
549 { return uriFor(action, QStringList(), QStringList(), queryValues); }
550 
551 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
552 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
553 
554 }
555 
556 Q_DECLARE_METATYPE(Cutelyst::Context *)
557 
558 #endif // CUTELYST_CONTEXT_H
This class represents a Cutelyst Action.
Definition: action.h:48
The Cutelyst Application.
Definition: application.h:56
The Cutelyst Component base class.
Definition: component.h:39
The Cutelyst Context.
Definition: context.h:52
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:243
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:325
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: context.h:451
Cutelyst Controller base class
Definition: controller.h:103
The Cutelyst Dispatcher.
Definition: dispatcher.h:41
The Cutelyst Engine.
Definition: engine.h:34
Cutelyst View abstract view component
Definition: view.h:35
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap