Cutelyst  1.5.0
session.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 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:
34  explicit SessionStore(QObject *parent = nullptr);
35 
36  virtual QVariant getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue = QVariant()) = 0;
37  virtual bool storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value) = 0;
38  virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) = 0;
39  virtual bool deleteExpiredSessions(Context *c, quint64 expires) = 0;
40 };
41 
42 class SessionPrivate;
43 class CUTELYST_PLUGIN_SESSION_EXPORT Session : public Plugin
44 {
45  Q_OBJECT
46  Q_DECLARE_PRIVATE(Session)
47 public:
48  Session(Application *parent);
49  virtual ~Session();
50 
57  virtual bool setup(Application *app) final;
58 
59  void setStorage(SessionStore *store);
60  SessionStore *storage() const;
61 
66  static QString id(Context *c);
67 
72  static quint64 expires(Context *c);
73 
79  static void changeExpires(Context *c, quint64 expires);
80 
87  static void deleteSession(Context *c, const QString &reason = QString());
88 
95  static QString deleteReason(Context *c);
96 
97  static QVariant value(Context *c, const QString &key, const QVariant &defaultValue = QVariant());
98  static void setValue(Context *c, const QString &key, const QVariant &value);
99  static void deleteValue(Context *c, const QString &key);
100  static void deleteValues(Context *c, const QStringList &keys);
101 
102  static bool isValid(Context *c);
103 
104 protected:
105  SessionPrivate *d_ptr;
106 
107 private:
108  Q_PRIVATE_SLOT(d_func(), void _q_saveSession(Context*))
109 };
110 
111 }
112 
113 #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:55