forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtactionhelper.cpp
189 lines (161 loc) · 6.78 KB
/
tactionhelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Copyright (c) 2010-2017, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include <TActionHelper>
#include <TActionController>
#include <THttpUtility>
#include <TSystemGlobal>
#include "turlroute.h"
/*!
\class TActionHelper
\brief The TActionHelper class is the base class of all helpers.
*/
/*!
Returns a QUrl to \a action of \a controller with arguments \a args.
The current controller name is used, if the \a controller is a empty string.
The current action name is used, if the \a action is a empty string.
If \a query is not empty, sets the query string to an encoded version
of \a query.
*/
QUrl TActionHelper::url(const QString &controller, const QString &action,
const QStringList &args, const QVariantMap &query) const
{
QString querystr;
for (QMapIterator<QString, QVariant> it(query); it.hasNext(); ) {
it.next();
if (!it.key().isEmpty()) {
querystr += it.key();
querystr += QLatin1Char('=');
querystr += THttpUtility::toUrlEncoding(it.value().toString());
querystr += QLatin1Char('&');
}
}
querystr.chop(1);
return url(controller, action, args, querystr);
}
/*!
Returns a QUrl to \a action of \a controller with arguments \a args.
The current controller name is used, if the \a controller is a empty string.
The current action name is used, if the \a action is a empty string.
If \a query is not empty, sets the query string to \a query.
*/
QUrl TActionHelper::url(const QString &controller, const QString &action,
const QStringList &arguments, const QString &query) const
{
Q_ASSERT(this->controller());
const QString ctrl = (controller.isEmpty()) ? this->controller()->name() : controller;
const QString act = (action.isEmpty()) ? this->controller()->activeAction() : action;
const QStringList args = (controller.isEmpty() && action.isEmpty() && arguments.isEmpty()) ? this->controller()->arguments() : arguments;
QString path = TUrlRoute::instance().findUrl(ctrl, act, args);
if (path.isEmpty()) {
path.append('/').append(ctrl).append('/').append(act);
}
for (auto &a : args) {
path.append('/').append(a);
}
// appends query items
if (!query.isEmpty()) {
if (!query.startsWith('?')) {
path += QLatin1Char('?');
}
path += query;
}
return QUrl(path);
}
/*!
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
QUrl TActionHelper::url(const QString &controller, const QString &action, const QVariant &arg) const
{
if (arg.canConvert(QVariant::StringList)) {
return url(controller, action, arg.toStringList());
} else {
return url(controller, action, arg.toString());
}
}
/*!
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
QUrl TActionHelper::urla(const QString &action, const QVariant &arg) const
{
if (arg.canConvert(QVariant::StringList)) {
return urla(action, arg.toStringList());
} else {
return urla(action, arg.toString());
}
}
/*!
\fn QUrl TActionHelper::url(const QString &controller, const QString &action, int arg) const
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::url(const QString &controller, const QString &action, uint arg) const
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::url(const QString &controller, const QString &action, qint64 arg) const
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::url(const QString &controller, const QString &action, quint64 arg) const
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::url(const QString &controller, const QString &action, const QString &arg) const
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::url(const QString &controller, const QString &action, const QString &arg, const QVariantMap &) const
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::urla(const QString &action=QString(), const QStringList &args=QStringList(), const QVariantMap &query = QVariantMap()) const
Returns a QUrl to \a action of the current controller with arguments \a args.
The current action name is used, if the \a action is a empty string.
If \a query is not empty, sets the query string to an encoded version
of \a query.
*/
/*!
\fn TActionHelper::urla(const QString &action, const QStringList &args, const QString &query) const
Returns a QUrl to \a action of the current controller with arguments \a args.
The current action name is used, if the \a action is a empty string.
If \a query is not empty, sets the query string to an encoded version
of \a query.
*/
/*!
\fn TActionHelper::urla(const QString &action, const QString &arg) const
This function overloads urla(const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::urla(const QString &action, int arg) const
This function overloads urla(const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::urla(const QString &action, uint arg) const
This function overloads urla(const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::urla(const QString &action, qint64 arg) const
This function overloads urla(const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::urla(const QString &action, quint64 arg) const
This function overloads urla(const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::urla(const QString &action, const QVariantMap &query) const
This function overloads urla(const QString &, const QStringList &, const QVariantMap &) const.
*/
/*!
\fn TActionHelper::urlq(const QVariantMap &query) const
This function overloads url(const QString &, const QString &, const QStringList &, const QVariantMap &) const.
Equivalent to url(QString(), QString(), QStringList(), query).
*/
/*!
\fn TActionHelper::urlq(const QString &query) const
This function overloads url(const QString &, const QString &, const QStringList &, const QString &) const.
Equivalent to url(QString(), QString(), QStringList(), query).
*/