Cutelyst  1.8.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  void setStash(const QString &key, const ParamsMultiMap &map);
240 
244  QStack<Component *> stack() const;
245 
257  QUrl uriFor(const QString &path = QString(),
258  const QStringList &args = QStringList(),
259  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
260 
271  inline QUrl uriFor(const QString &path,
272  const ParamsMultiMap &queryValues) const;
273 
283  QUrl uriFor(Action *action,
284  const QStringList &captures = QStringList(),
285  const QStringList &args = QStringList(),
286  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
287 
293  inline QUrl uriFor(Action *action,
294  const ParamsMultiMap &queryValues) const;
295 
319  QUrl uriForAction(const QString &path,
320  const QStringList &captures = QStringList(),
321  const QStringList &args = QStringList(),
322  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
323 
327  inline QUrl uriForAction(const QString &path,
328  const ParamsMultiMap &queryValues) const;
329 
334  bool detached() const;
335 
341  void detach(Action *action = nullptr);
342 
361  bool forward(Component *component);
362 
381  bool forward(const QString &action);
382 
386  // TODO C2 mark as const
387  Action *getAction(const QString &action, const QString &ns = QString());
388 
392  // TODO C2 mark as const
393  QVector<Action *> getActions(const QString &action, const QString &ns = QString());
394 
398  // TODO C2 mark as const
399  QVector<Plugin *> plugins();
400 
404  template <typename T>
405  T plugin()
406  {
407  const auto pluginsConst = plugins();
408  for (Plugin *plugin : pluginsConst) {
409  auto p = qobject_cast<T>(plugin);
410  if (p) {
411  return p;
412  }
413  }
414  return 0;
415  }
416 
420  bool execute(Component *code);
421 
429  QLocale locale() const;
430 
445  void setLocale(const QLocale &locale);
446 
450  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
451 
455  QVariantMap config() const;
456 
461  void *engineData();
462 
476  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
477 
478 protected:
482  Context(ContextPrivate *priv);
483 
484  friend class Application;
485  friend class Action;
486  friend class DispatchType;
487  friend class Plugin;
488  friend class Engine;
489  ContextPrivate *d_ptr;
490 
491 private:
492  Q_DECLARE_PRIVATE(Context)
493 };
494 
495 inline void Context::stash(const QVariantHash &unite)
496 { stash().unite(unite); }
497 
498 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
499 { return uriFor(path, QStringList(), queryValues); }
500 
501 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
502 { return uriFor(action, QStringList(), QStringList(), queryValues); }
503 
504 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
505 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
506 
507 }
508 
509 Q_DECLARE_METATYPE(Cutelyst::Context *)
510 
511 #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:405
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