Cutelyst  1.5.0
context.h
1 /*
2  * Copyright (C) 2013-2016 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 
68  bool error() const;
69  void error(const QString &error);
70  QStringList errors() const;
71 
75  bool state() const;
76 
77  void setState(bool state);
78 
82  Engine *engine() const;
83 
87  Application *app() const;
88 
92  Response *response() const;
93 
97  Response *res() const;
98 
102  Action *action() const;
103 
107  QString actionName() const;
108 
116  QString ns() const;
117 
122  Request *request() const;
123 
127  Request *req() const;
128 
132  Dispatcher *dispatcher() const;
133 
137  QString controllerName() const;
138 
142  Controller *controller() const;
143 
148  Controller *controller(const QString &name) const;
149 
155  View *view() const;
156 
162  View *view(const QString &name) const;
163 
175  bool setView(const QString &name);
176 
196  inline void stash(const QVariantHash &unite);
197 
209  QVariantHash &stash();
210 
214  QVariant stash(const QString &key) const;
215 
219  void setStash(const QString &key, const QVariant &value);
220 
224  QStack<Component *> stack() const;
225 
237  QUrl uriFor(const QString &path = QString(),
238  const QStringList &args = QStringList(),
239  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
240 
251  inline QUrl uriFor(const QString &path,
252  const ParamsMultiMap &queryValues) const;
253 
263  QUrl uriFor(Action *action,
264  const QStringList &captures = QStringList(),
265  const QStringList &args = QStringList(),
266  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
267 
273  inline QUrl uriFor(Action *action,
274  const ParamsMultiMap &queryValues) const;
275 
299  QUrl uriForAction(const QString &path,
300  const QStringList &captures = QStringList(),
301  const QStringList &args = QStringList(),
302  const ParamsMultiMap &queryValues = ParamsMultiMap()) const;
303 
307  inline QUrl uriForAction(const QString &path,
308  const ParamsMultiMap &queryValues) const;
309 
314  bool detached() const;
315 
321  void detach(Action *action = nullptr);
322 
341  bool forward(Component *component);
342 
361  bool forward(const QString &action);
362 
366  Action *getAction(const QString &action, const QString &ns = QString());
367 
371  QVector<Action *> getActions(const QString &action, const QString &ns = QString());
372 
373  QVector<Plugin *> plugins();
374 
375  template <typename T>
376  T plugin()
377  {
378  Q_FOREACH (Plugin *plugin, plugins()) {
379  if (qobject_cast<T>(plugin)) {
380  return qobject_cast<T>(plugin);
381  }
382  }
383  return 0;
384  }
385 
389  bool execute(Component *code);
390 
398  QLocale locale() const;
399 
414  void setLocale(const QLocale &locale);
415 
416  QVariant config(const QString &key, const QVariant &defaultValue = QVariant()) const;
417 
418  QVariantMap config() const;
419 
420  void *engineData();
421 
435  QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const;
436 
437 protected:
438  Context(ContextPrivate *priv);
439 
440  friend class Application;
441  friend class Action;
442  friend class DispatchType;
443  friend class Plugin;
444  friend class Engine;
445  ContextPrivate *d_ptr;
446 
447 private:
448  Q_DECLARE_PRIVATE(Context)
449 };
450 
451 inline void Context::stash(const QVariantHash &unite)
452 { stash().unite(unite); }
453 
454 inline QUrl Context::uriFor(const QString &path, const ParamsMultiMap &queryValues) const
455 { return uriFor(path, QStringList(), queryValues); }
456 
457 inline QUrl Context::uriFor(Action *action, const ParamsMultiMap &queryValues) const
458 { return uriFor(action, QStringList(), QStringList(), queryValues); }
459 
460 inline QUrl Context::uriForAction(const QString &path, const ParamsMultiMap &queryValues) const
461 { return uriForAction(path, QStringList(), QStringList(), queryValues); }
462 
463 }
464 
465 Q_DECLARE_METATYPE(Cutelyst::Context *)
466 
467 #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
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()
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
The Cutelyst Application.
Definition: application.h:55
The Cutelyst Dispatcher.
Definition: dispatcher.h:41