Cutelyst  2.5.0
statusmessage.cpp
1 /*
2  * Copyright (C) 2016-2018 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 #include "statusmessage.h"
19 
20 #include <Cutelyst/Application>
21 #include <Cutelyst/Plugins/Session/session.h>
22 
23 #include <QtCore/QDateTime>
24 #include <QtCore/QLoggingCategory>
25 
26 using namespace Cutelyst;
27 
28 Q_LOGGING_CATEGORY(C_STATUSMESSAGE, "cutelyst.plugins.statusmessage", QtWarningMsg)
29 
30 static thread_local StatusMessage *m_instance = nullptr;
31 
32 namespace Cutelyst {
33 
35 {
36 public:
37  static void _q_postFork(Application *app);
38 
39  QString sessionPrefix = QStringLiteral("status_msg");
40  QString tokenParam = QStringLiteral("mid");
41  QString statusMsgStashKey = QStringLiteral("status_msg");
42  QString errorMsgStashKey = QStringLiteral("error_msg");
43 };
44 
45 void StatusMessagePrivate::_q_postFork(Application *app)
46 {
47  m_instance = app->plugin<StatusMessage *>();
48 }
49 
50 }
51 
53 {
54  qsrand(QDateTime::currentMSecsSinceEpoch());
55  m_instance = this;
56 }
57 
58 StatusMessage::~StatusMessage()
59 {
60  delete d_ptr;
61 }
62 
64 {
65  Q_D(const StatusMessage);
66  return d->sessionPrefix;
67 }
68 
70 {
71  Q_D(StatusMessage);
72  d->sessionPrefix = sessionPrefix;
73 }
74 
76 {
77  Q_D(const StatusMessage);
78  return d->tokenParam;
79 }
80 
82 {
83  Q_D(StatusMessage);
84  d->tokenParam = tokenParam;
85 }
86 
88 {
89  Q_D(const StatusMessage);
90  return d->statusMsgStashKey;
91 }
92 
94 {
95  Q_D(StatusMessage);
96  d->statusMsgStashKey = statusMsgStashKey;
97 }
98 
100 {
101  Q_D(const StatusMessage);
102  return d->errorMsgStashKey;
103 }
104 
106 {
107  Q_D(StatusMessage);
108  d->errorMsgStashKey = errorMgStashKey;
109 }
110 
112 {
113  if (Q_UNLIKELY(!m_instance)) {
114  qCCritical(C_STATUSMESSAGE, "StatusMessage plugin not registered");
115  return;
116  }
117  StatusMessagePrivate *priv = m_instance->d_ptr;
118 
119  const QString token = c->request()->queryParam(priv->tokenParam);
120  if (token.isEmpty()) {
121  return;
122  }
123 
124  QStringList deleteKeys;
125  const QString statusKey = priv->sessionPrefix + QLatin1String("status") + token;
126  const QVariant statusValue = Session::value(c, statusKey);
127  if (!statusValue.isNull()) {
128  deleteKeys.append(statusKey);
129  c->setStash(priv->statusMsgStashKey, statusValue);
130  }
131 
132  const QString errorKey = priv->sessionPrefix + QLatin1String("error") + token;
133  const QVariant errorValue = Session::value(c, errorKey);
134  if (!errorValue.isNull()) {
135  deleteKeys.append(errorKey);
136  c->setStash(priv->errorMsgStashKey, errorValue);
137  }
138 
139  if (!deleteKeys.isEmpty()) {
140  Session::deleteValues(c, deleteKeys);
141  }
142 }
143 
144 inline QString createToken()
145 {
146  return QString::number(qrand() % 99999999).rightJustified(8, QLatin1Char('0'), true);
147 }
148 
149 QString StatusMessage::error(Context *c, const QString &msg)
150 {
151  QString token;
152  if (Q_UNLIKELY(!m_instance)) {
153  qCCritical(C_STATUSMESSAGE, "StatusMessage plugin not registered");
154  return token;
155  }
156 
157  token = createToken();
158  Session::setValue(c, m_instance->d_ptr->sessionPrefix + QLatin1String("error") + token, msg);
159  return token;
160 }
161 
163 {
164  ParamsMultiMap map(query);
165  if (Q_UNLIKELY(!m_instance)) {
166  qCCritical(C_STATUSMESSAGE, "StatusMessage plugin not registered");
167  return map;
168  }
169  StatusMessagePrivate *priv = m_instance->d_ptr;
170 
171  const QString token = createToken();
172  Session::setValue(c, priv->sessionPrefix + QLatin1String("error") + token, msg);
173  map.insert(priv->tokenParam, token);
174  return map;
175 }
176 
177 QString StatusMessage::status(Context *c, const QString &msg)
178 {
179  QString token;
180  if (Q_UNLIKELY(!m_instance)) {
181  qCCritical(C_STATUSMESSAGE, "StatusMessage plugin not registered");
182  return token;
183  }
184 
185  token = createToken();
186  Session::setValue(c, m_instance->d_ptr->sessionPrefix + QLatin1String("status") + token, msg);
187  return token;
188 }
189 
191 {
192  ParamsMultiMap map(query);
193  if (Q_UNLIKELY(!m_instance)) {
194  qCCritical(C_STATUSMESSAGE, "StatusMessage plugin not registered");
195  return map;
196  }
197  StatusMessagePrivate *priv = m_instance->d_ptr;
198 
199  const QString token = createToken();
200  Session::setValue(c, priv->sessionPrefix + QLatin1String("status") + token, msg);
201  map.insert(priv->tokenParam, token);
202  return map;
203 }
204 
206 {
207  connect(app, &Application::postForked, this, &StatusMessagePrivate::_q_postFork);
208  return true;
209 }
210 
211 #include "moc_statusmessage.cpp"
QMap< QString, QString > ParamsMultiMap
void setTokenParam(const QString &tokenParam)
static QString status(Context *c, const QString &msg)
QString sessionPrefix() const
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:207
T plugin()
Returns the registered plugin that casts to the template type T.
Definition: application.h:115
static ParamsMultiMap statusQuery(Context *c, const QString &msg, ParamsMultiMap query=ParamsMultiMap())
static void load(Context *c)
The Cutelyst Context.
Definition: context.h:50
StatusMessage(Application *parent)
QString tokenParam() const
static void setValue(Context *c, const QString &key, const QVariant &value)
Definition: session.cpp:176
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void setStatusMsgStashKey(const QString &statusMsgStashKey)
virtual bool setup(Application *app) override
void postForked(Application *app)
QString errorMgStashKey() const
static void deleteValues(Context *c, const QStringList &keys)
Definition: session.cpp:222
QString statusMsgStashKey() const
static QVariant value(Context *c, const QString &key, const QVariant &defaultValue=QVariant())
Definition: session.cpp:161
The Cutelyst Application.
Definition: application.h:55
static ParamsMultiMap errorQuery(Context *c, const QString &msg, ParamsMultiMap query=ParamsMultiMap())
static QString error(Context *c, const QString &msg)
void setErrorMgStashKey(const QString &errorMgStashKey)
void setSessionPrefix(const QString &sessionPrefix)
QString queryParam(const QString &key, const QString &defaultValue=QString()) const
Definition: request.h:545