Cutelyst  2.13.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  QVariant stash(const QString &key, const QVariant &defaultValue) const;
237 
243  QVariant stashTake(const QString &key);
244 
249  bool stashRemove(const QString &key);
250 
254  void setStash(const QString &key, const QVariant &value);
255 
259  void setStash(const QString &key, const ParamsMultiMap &map);
260 
264  QStack<Component *> stack() const;
265 
277  QUrl uriFor(const QString &path = QString(),
278  const QStringList &args = QStringList(),
279  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
280 
291  inline QUrl uriFor(const QString &path,
292  const ParamsMultiMap &queryValues) const;
293 
303  QUrl uriFor(Action *action,
304  const QStringList &captures = QStringList(),
305  const QStringList &args = QStringList(),
306  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
307 
313  inline QUrl uriFor(Action *action,
314  const ParamsMultiMap &queryValues) const;
315 
339  QUrl uriForAction(const QString &path,
340  const QStringList &captures = QStringList(),
341  const QStringList &args = QStringList(),
342  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
343 
347  inline QUrl uriForAction(const QString &path,
348  const ParamsMultiMap &queryValues) const;
349 
354  bool detached() const;
355 
361  void detach(Action *action = nullptr);
362 
376  void detachAsync();
377 
383  void attachAsync();
384 
403  bool forward(Component *component);
404 
423  bool forward(const QString &action);
424 
428  Action *getAction(const QString &action, const QString &ns = QString()) const;
429 
433  QVector<Action *> getActions(const QString &action, const QString &ns = QString()) const;
434 
438  QVector<Plugin *> plugins() const;
439 
443  template <typename T>
444  T plugin()
445  {
446  const auto pluginsConst = plugins();
447  for (Plugin *plugin : pluginsConst) {
448  auto p = qobject_cast<T>(plugin);
449  if (p) {
450  return p;
451  }
452  }
453  return nullptr;
454  }
455 
459  bool execute(Component *code);
460 
468  QLocale locale() const;
469 
484  void setLocale(const QLocale &locale);
485 
489  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
490 
494  QVariantMap config() const;
495 
509  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
510 
523  Q_DECL_DEPRECATED bool wait(uint count = 1);
524 
525 public Q_SLOTS:
536  void next(bool force = false);
537 
538 protected:
542  Context(ContextPrivate *priv);
543 
544  void finalize();
545 
546  friend class Application;
547  friend class Action;
548  friend class DispatchType;
549  friend class Plugin;
550  friend class Engine;
551  friend class Controller;
552  ContextPrivate *d_ptr;
553 
554 private:
555  Q_DECLARE_PRIVATE(Context)
556 };
557 
558 inline void Context::stash(const QVariantHash &unite)
559 { stash().unite(unite); }
560 
561 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
562 { return uriFor(path, QStringList(), queryValues); }
563 
564 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
565 { return uriFor(action, QStringList(), QStringList(), queryValues); }
566 
567 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
568 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
569 
570 }
571 
572 Q_DECLARE_METATYPE(Cutelyst::Context *)
573 
574 #endif // CUTELYST_CONTEXT_H
Cutelyst::Controller
Cutelyst Controller base class
Definition: controller.h:102
Cutelyst::Plugin
Definition: plugin.h:30
Cutelyst::ParamsMultiMap
QMap< QString, QString > ParamsMultiMap
Definition: paramsmultimap.h:36
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Dispatcher
The Cutelyst Dispatcher.
Definition: dispatcher.h:40
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::Context::stash
QVariantHash & stash()
Definition: context.cpp:195
Cutelyst::Context::plugin
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: context.h:444
Cutelyst::Component
The Cutelyst Component base class.
Definition: component.h:38
Cutelyst::Request
Definition: request.h:42
Cutelyst::DispatchType
Definition: dispatchtype.h:31
Cutelyst::View
Cutelyst View abstract view component
Definition: view.h:34
Cutelyst::Context::uriFor
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:243
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Context::uriForAction
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:325
Cutelyst::Engine
The Cutelyst Engine.
Definition: engine.h:33
Cutelyst::Action
This class represents a Cutelyst Action.
Definition: action.h:47
Cutelyst::Response
Definition: response.h:34