Cutelyst  1.11.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 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 CSESSION_H
19 #define CSESSION_H
20 
21 #include <QtCore/QVariant>
22 
23 #include <Cutelyst/plugin.h>
24 #include <Cutelyst/cutelyst_global.h>
25 
26 namespace Cutelyst {
27 
28 class Context;
29 class CUTELYST_PLUGIN_SESSION_EXPORT SessionStore : public QObject {
30  Q_OBJECT
31 public:
35  explicit SessionStore(QObject *parent = nullptr);
36 
40  virtual QVariant getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue = QVariant()) = 0;
41 
45  virtual bool storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value) = 0;
46 
50  virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) = 0;
51 
55  virtual bool deleteExpiredSessions(Context *c, quint64 expires) = 0;
56 };
57 
58 class SessionPrivate;
59 class CUTELYST_PLUGIN_SESSION_EXPORT Session : public Plugin
60 {
61  Q_OBJECT
62  Q_DECLARE_PRIVATE(Session)
63 public:
67  Session(Application *parent);
68  virtual ~Session();
69 
76  virtual bool setup(Application *app) final;
77 
81  void setStorage(SessionStore *store);
82 
86  SessionStore *storage() const;
87 
92  static QString id(Context *c);
93 
98  static quint64 expires(Context *c);
99 
105  static void changeExpires(Context *c, quint64 expires);
106 
113  static void deleteSession(Context *c, const QString &reason = QString());
114 
121  static QString deleteReason(Context *c);
122 
126  static QVariant value(Context *c, const QString &key, const QVariant &defaultValue = QVariant());
127 
131  static void setValue(Context *c, const QString &key, const QVariant &value);
132 
136  static void deleteValue(Context *c, const QString &key);
137 
141  static void deleteValues(Context *c, const QStringList &keys);
142 
146  static bool isValid(Context *c);
147 
148 protected:
149  SessionPrivate *d_ptr;
150 
151 private:
152  Q_PRIVATE_SLOT(d_func(), void _q_saveSession(Context*))
153  Q_PRIVATE_SLOT(d_func(), void _q_postFork(Application*))
154 };
155 
156 }
157 
158 #endif // CSESSION_H
The Cutelyst Context.
Definition: context.h:50
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
The Cutelyst Application.
Definition: application.h:54