TreeFrogFramework  2.8
tabstractcontroller.h
Go to the documentation of this file.
1 #pragma once
2 #include <QObject>
3 #include <QVariant>
4 #include <TGlobal>
5 
6 class THttpRequest;
7 class TSession;
8 class TCookie;
9 class TFormValidator;
10 class TActionContext;
11 
12 
13 class T_CORE_EXPORT TAbstractController : public QObject {
14 public:
16  virtual ~TAbstractController() { }
17  virtual QString className() const;
18  virtual QString name() const = 0;
19  virtual QString activeAction() const = 0;
20  virtual QStringList arguments() const { return QStringList(); }
21  virtual const THttpRequest &httpRequest() const;
22  virtual const THttpRequest &request() const;
23  virtual const TSession &session() const;
24  virtual QString getRenderingData(const QString &templateName, const QVariantMap &vars = QVariantMap());
25  virtual QByteArray authenticityToken() const { return QByteArray(); }
26  virtual void setFlash(const QString &name, const QVariant &value);
27  void exportVariant(const QString &name, const QVariant &value, bool overwrite = true);
28  virtual bool isUserLoggedIn() const;
29  const TActionContext *context() const { return _context; }
30  TActionContext *context() { return _context; }
31  void setContext(TActionContext *context) { _context = context; }
32  static QThread *currentThread() { return QThread::currentThread(); }
33 
34 protected:
35  virtual TSession &session();
36  virtual bool addCookie(const TCookie &cookie);
37  virtual bool addCookie(const QByteArray &name, const QByteArray &value, const QDateTime &expire = QDateTime(), const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false, const QByteArray &sameSite = "Lax");
38  virtual bool addCookie(const QByteArray &name, const QByteArray &value, int64_t maxAge, const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false, const QByteArray &sameSite = "Lax");
39  virtual void reset() { }
40 
41  QVariant variant(const QString &name) const;
42  void exportVariants(const QVariantMap &map);
43  void exportValidationErrors(const TFormValidator &validator, const QString &prefix = QString("err_"));
44  bool hasVariant(const QString &name) const;
45  const QVariantMap &allVariants() const { return _exportVars; }
46  QString viewClassName(const QString &action = QString()) const;
47  QString viewClassName(const QString &contoller, const QString &action) const;
48 
49 private:
50  QVariantMap _exportVars;
51  TActionContext *_context {nullptr};
52 
55  friend class TDirectView;
56 };
57 
58 
63 inline QString TAbstractController::className() const
64 {
65  return QString(metaObject()->className());
66 }
67 
68 inline QVariant TAbstractController::variant(const QString &name) const
69 {
70  return _exportVars.value(name);
71 }
72 
73 inline bool TAbstractController::hasVariant(const QString &name) const
74 {
75  return _exportVars.contains(name);
76 }
The TAbstractController class is the abstract base class of controllers, providing functionality comm...
Definition: tabstractcontroller.h:13
virtual QStringList arguments() const
Definition: tabstractcontroller.h:20
virtual ~TAbstractController()
Definition: tabstractcontroller.h:16
static QThread * currentThread()
Definition: tabstractcontroller.h:32
bool hasVariant(const QString &name) const
Returns true if a variable with the name is exported for views; otherwise returns false.
Definition: tabstractcontroller.h:73
void setContext(TActionContext *context)
Definition: tabstractcontroller.h:31
const QVariantMap & allVariants() const
Returns all the exported variables.
Definition: tabstractcontroller.h:45
virtual QByteArray authenticityToken() const
Definition: tabstractcontroller.h:25
const TActionContext * context() const
Definition: tabstractcontroller.h:29
QVariant variant(const QString &name) const
Returns the exported variable with the value associated with the name.
Definition: tabstractcontroller.h:68
TActionContext * context()
Definition: tabstractcontroller.h:30
virtual QString name() const =0
This function is reimplemented in subclasses to return a controller name.
virtual void reset()
Definition: tabstractcontroller.h:39
virtual QString activeAction() const =0
This function is reimplemented in subclasses to return a active action name.
The TCookie class holds one network cookie.
Definition: tcookie.h:7
The TFormValidator class provides form validation for a map-table-based dictionary.
Definition: tformvalidator.h:9
The THttpRequest class contains request information for HTTP.
Definition: thttprequest.h:34
The TSession class holds information associated with individual visitors.
Definition: tsession.h:7
#define T_CORE_EXPORT
Definition: tdeclexport.h:28
#define T_DISABLE_COPY(Class)
Definition: tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition: tdeclexport.h:41