Cutelyst  2.14.2
context.h
1 /*
2  * Copyright (C) 2013-2020 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;
77 
81  void error(const QString &error);
82 
86  QStringList errors() const;
87 
91  bool state() const;
92 
97  void setState(bool state);
98 
102  Engine *engine() const;
103 
107  Application *app() const;
108 
112  Response *response() const;
113 
117  Response *res() const;
118 
122  Action *action() const;
123 
127  QString actionName() const;
128 
136  QString ns() const;
137 
142  Request *request() const;
143 
147  Request *req() const;
148 
152  Dispatcher *dispatcher() const;
153 
157  QString controllerName() const;
158 
162  Controller *controller() const;
163 
168  Controller *controller(const QString &name) const;
169 
173  View *view(const QString &name = QString()) const;
174 
180  View *customView() const;
181 
193  bool setCustomView(const QString &name);
194 
214  inline void stash(const QVariantHash &unite);
215 
227  QVariantHash &stash();
228 
232  QVariant stash(const QString &key) const;
233 
237  QVariant stash(const QString &key, const QVariant &defaultValue) const;
238 
244  QVariant stashTake(const QString &key);
245 
250  bool stashRemove(const QString &key);
251 
255  void setStash(const QString &key, const QVariant &value);
256 
260  void setStash(const QString &key, const ParamsMultiMap &map);
261 
265  QStack<Component *> stack() const;
266 
278  QUrl uriFor(const QString &path = QString(),
279  const QStringList &args = QStringList(),
280  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
281 
292  inline QUrl uriFor(const QString &path,
293  const ParamsMultiMap &queryValues) const;
294 
304  QUrl uriFor(Action *action,
305  const QStringList &captures = QStringList(),
306  const QStringList &args = QStringList(),
307  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
308 
314  inline QUrl uriFor(Action *action,
315  const ParamsMultiMap &queryValues) const;
316 
340  QUrl uriForAction(const QString &path,
341  const QStringList &captures = QStringList(),
342  const QStringList &args = QStringList(),
343  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
344 
348  inline QUrl uriForAction(const QString &path,
349  const ParamsMultiMap &queryValues) const;
350 
355  bool detached() const;
356 
362  void detach(Action *action = nullptr);
363 
379  void detachAsync();
380 
388  void attachAsync();
389 
408  bool forward(Component *component);
409 
428  bool forward(const QString &action);
429 
433  Action *getAction(const QString &action, const QString &ns = QString()) const;
434 
438  QVector<Action *> getActions(const QString &action, const QString &ns = QString()) const;
439 
443  QVector<Plugin *> plugins() const;
444 
448  template <typename T>
449  T plugin()
450  {
451  const auto pluginsConst = plugins();
452  for (Plugin *plugin : pluginsConst) {
453  auto p = qobject_cast<T>(plugin);
454  if (p) {
455  return p;
456  }
457  }
458  return nullptr;
459  }
460 
464  bool execute(Component *code);
465 
473  QLocale locale() const;
474 
489  void setLocale(const QLocale &locale);
490 
494  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
495 
499  QVariantMap config() const;
500 
514  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
515 
528  Q_DECL_DEPRECATED bool wait(uint count = 1);
529 
530 public Q_SLOTS:
541  void next(bool force = false);
542 
547  void finalize();
548 
549 protected:
553  Context(ContextPrivate *priv);
554 
555  friend class Application;
556  friend class Action;
557  friend class DispatchType;
558  friend class Plugin;
559  friend class Engine;
560  friend class Controller;
561  friend class Async;
562  ContextPrivate *d_ptr;
563 
564 private:
565  Q_DECLARE_PRIVATE(Context)
566 };
567 
568 inline void Context::stash(const QVariantHash &unite)
569 { stash().unite(unite); }
570 
571 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
572 { return uriFor(path, QStringList(), queryValues); }
573 
574 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
575 { return uriFor(action, QStringList(), QStringList(), queryValues); }
576 
577 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
578 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
579 
580 }
581 
582 Q_DECLARE_METATYPE(Cutelyst::Context *)
583 
584 #endif // CUTELYST_CONTEXT_H
QMap< QString, QString > ParamsMultiMap
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:325
The Cutelyst Component base class.
Definition: component.h:38
This class represents a Cutelyst Action.
Definition: action.h:47
The Cutelyst Context.
Definition: context.h:51
Cutelyst Controller base class
Definition: controller.h:102
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:243
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: context.h:449
QVariantHash & stash()
Definition: context.cpp:195
Cutelyst View abstract view component
Definition: view.h:34
The Cutelyst Application.
Definition: application.h:55
The Cutelyst Engine.
Definition: engine.h:33
The Cutelyst Dispatcher.
Definition: dispatcher.h:40