Cutelyst  1.8.0
session.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 CSESSION_H
21 #define CSESSION_H
22 
23 #include <QtCore/QVariant>
24 
25 #include <Cutelyst/plugin.h>
26 #include <Cutelyst/cutelyst_global.h>
27 
28 namespace Cutelyst {
29 
30 class Context;
31 class CUTELYST_PLUGIN_SESSION_EXPORT SessionStore : public QObject {
32  Q_OBJECT
33 public:
37  explicit SessionStore(QObject *parent = nullptr);
38 
42  virtual QVariant getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue = QVariant()) = 0;
43 
47  virtual bool storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value) = 0;
48 
52  virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) = 0;
53 
57  virtual bool deleteExpiredSessions(Context *c, quint64 expires) = 0;
58 };
59 
60 class SessionPrivate;
61 class CUTELYST_PLUGIN_SESSION_EXPORT Session : public Plugin
62 {
63  Q_OBJECT
64  Q_DECLARE_PRIVATE(Session)
65 public:
69  Session(Application *parent);
70  virtual ~Session();
71 
78  virtual bool setup(Application *app) final;
79 
83  void setStorage(SessionStore *store);
84 
88  SessionStore *storage() const;
89 
94  static QString id(Context *c);
95 
100  static quint64 expires(Context *c);
101 
107  static void changeExpires(Context *c, quint64 expires);
108 
115  static void deleteSession(Context *c, const QString &reason = QString());
116 
123  static QString deleteReason(Context *c);
124 
128  static QVariant value(Context *c, const QString &key, const QVariant &defaultValue = QVariant());
129 
133  static void setValue(Context *c, const QString &key, const QVariant &value);
134 
138  static void deleteValue(Context *c, const QString &key);
139 
143  static void deleteValues(Context *c, const QStringList &keys);
144 
148  static bool isValid(Context *c);
149 
150 protected:
151  SessionPrivate *d_ptr;
152 
153 private:
154  Q_PRIVATE_SLOT(d_func(), void _q_saveSession(Context*))
155  Q_PRIVATE_SLOT(d_func(), void _q_postFork(Application*))
156 };
157 
158 }
159 
160 #endif // CSESSION_H
The Cutelyst Context.
Definition: context.h:52
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
The Cutelyst Application.
Definition: application.h:56