Cutelyst  1.11.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 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)
54  Q_PROPERTY(QString actionName READ actionName)
55  Q_PROPERTY(QString ns READ ns)
56  Q_PROPERTY(QString namespace READ ns)
57  Q_PROPERTY(Request *req READ request)
58  Q_PROPERTY(Request *request READ request)
59  Q_PROPERTY(Controller *controller READ controller)
60  Q_PROPERTY(QString controllerName READ controllerName)
61  Q_PROPERTY(QVariantMap config READ config)
62  Q_PROPERTY(bool state READ state)
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 
174  View *view() const;
175 
179  // TODO find a new name for the above method
180  View *view(const QString &name) const;
181 
193  bool setView(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  void setStash(const QString &key, const QVariant &value);
238 
242  void setStash(const QString &key, const ParamsMultiMap &map);
243 
247  QStack<Component *> stack() const;
248 
260  QUrl uriFor(const QString &path = QString(),
261  const QStringList &args = QStringList(),
262  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
263 
274  inline QUrl uriFor(const QString &path,
275  const ParamsMultiMap &queryValues) const;
276 
286  QUrl uriFor(Action *action,
287  const QStringList &captures = QStringList(),
288  const QStringList &args = QStringList(),
289  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
290 
296  inline QUrl uriFor(Action *action,
297  const ParamsMultiMap &queryValues) const;
298 
322  QUrl uriForAction(const QString &path,
323  const QStringList &captures = QStringList(),
324  const QStringList &args = QStringList(),
325  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
326 
330  inline QUrl uriForAction(const QString &path,
331  const ParamsMultiMap &queryValues) const;
332 
337  bool detached() const;
338 
344  void detach(Action *action = nullptr);
345 
364  bool forward(Component *component);
365 
384  bool forward(const QString &action);
385 
389  // TODO C2 mark as const
390  Action *getAction(const QString &action, const QString &ns = QString());
391 
395  // TODO C2 mark as const
396  QVector<Action *> getActions(const QString &action, const QString &ns = QString());
397 
401  // TODO C2 mark as const
402  QVector<Plugin *> plugins();
403 
407  template <typename T>
408  T plugin()
409  {
410  const auto pluginsConst = plugins();
411  for (Plugin *plugin : pluginsConst) {
412  auto p = qobject_cast<T>(plugin);
413  if (p) {
414  return p;
415  }
416  }
417  return nullptr;
418  }
419 
423  bool execute(Component *code);
424 
432  QLocale locale() const;
433 
448  void setLocale(const QLocale &locale);
449 
453  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
454 
458  QVariantMap config() const;
459 
464  Q_DECL_DEPRECATED
465  void *engineData();
466 
480  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
481 
482 protected:
486  Context(ContextPrivate *priv);
487 
488  friend class Application;
489  friend class Action;
490  friend class DispatchType;
491  friend class Plugin;
492  friend class Engine;
493  ContextPrivate *d_ptr;
494 
495 private:
496  Q_DECLARE_PRIVATE(Context)
497 };
498 
499 inline void Context::stash(const QVariantHash &unite)
500 { stash().unite(unite); }
501 
502 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
503 { return uriFor(path, QStringList(), queryValues); }
504 
505 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
506 { return uriFor(action, QStringList(), QStringList(), queryValues); }
507 
508 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
509 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
510 
511 }
512 
513 Q_DECLARE_METATYPE(Cutelyst::Context *)
514 
515 #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:298
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:50
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:226
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: context.h:408
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
QVariantHash & stash()
Definition: context.cpp:196
Cutelyst View abstract view component
Definition: view.h:33
The Cutelyst Application.
Definition: application.h:54
The Cutelyst Engine.
Definition: engine.h:33
The Cutelyst Dispatcher.
Definition: dispatcher.h:40