18 #include "controller_p.h"
20 #include "application.h"
21 #include "dispatcher.h"
25 #include "context_p.h"
27 #include <QMetaClassInfo>
28 #include <QRegularExpression>
33 , d_ptr(new ControllerPrivate(this))
37 Controller::~Controller()
40 qDeleteAll(d->actionList);
53 auto it = d->actions.constFind(name);
54 if (it != d->actions.constEnd()) {
57 return d->dispatcher->getAction(name, d->pathPrefix);
63 auto it = d->actions.constFind(name);
64 if (it != d->actions.constEnd()) {
67 return d->dispatcher->getAction(name.
toString(), d->pathPrefix);
78 return !qstrcmp(
metaObject()->className(), className);
93 ControllerPrivate::ControllerPrivate(
Controller *parent) :
102 dispatcher = _dispatcher;
110 q->setObjectName(className);
112 bool namespaceFound =
false;
117 pathPrefix.remove(0, 1);
119 namespaceFound =
true;
124 if (!namespaceFound) {
126 bool lastWasUpper =
true;
128 for (
int i = 0; i < className.
length(); ++i) {
129 const QChar c = className.
at(i);
132 lastWasUpper =
false;
146 pathPrefix = controlerNS;
149 registerActionMethods(meta, q, app);
152 void ControllerPrivate::setupFinished()
156 const ActionList beginList = dispatcher->getActions(QStringLiteral(
"Begin"), pathPrefix);
161 beginAutoList.append(dispatcher->getActions(QStringLiteral(
"Auto"), pathPrefix));
163 const ActionList endList = dispatcher->getActions(QStringLiteral(
"End"), pathPrefix);
165 end = endList.
last();
168 const auto actions = actionList;
169 for (
Action *action : actions) {
170 action->dispatcherReady(dispatcher, q);
173 q->preFork(qobject_cast<Application *>(q->parent()));
182 int &asyncDetached = c->d_ptr->asyncDetached;
185 const auto beginAutoList = d->beginAutoList;
186 for (
Action *action : beginAutoList) {
188 c->d_ptr->pendingAsync.append(action);
189 }
else if (!action->dispatch(c)) {
198 c->d_ptr->pendingAsync.append(c->action());
207 c->d_ptr->pendingAsync.append(d->end);
208 }
else if (!d->end->dispatch(c)) {
214 c->d_ptr->engineRequest->status |= EngineRequest::Async;
220 Action *ControllerPrivate::actionClass(
const QVariantHash &args)
222 const auto attributes = args.value(QStringLiteral(
"attributes")).value<
ParamsMultiMap>();
223 const QString actionClass = attributes.value(QStringLiteral(
"ActionClass"));
225 QObject *
object = instantiateClass(actionClass,
"Cutelyst::Action");
227 Action *action = qobject_cast<Action*>(
object);
231 qCWarning(CUTELYST_CONTROLLER) <<
"ActionClass"
233 <<
"is not an ActionClass";
242 Action *action = actionClass(args);
248 for (
int i = 0; i < roles.
size(); ++i) {
250 code->
init(app, args);
256 action->
setName(args.value(QStringLiteral(
"name")).toString());
257 action->
setReverse(args.value(QStringLiteral(
"reverse")).toString());
281 if (name == classInfo.
name()) {
285 ParamsMultiMap attrs = parseAttributes(method, attributeArray, name);
294 Action *action = createAction({
305 actionList.append(action);
313 std::vector<std::pair<QString, QString> > attributes;
320 int size = str.
size();
327 if (str.
at(pos) ==
':') {
328 int keyStart = ++pos;
331 if (str.
at(pos) ==
'(') {
333 int valueStart = ++pos;
335 if (str.
at(pos) ==
')') {
338 if (++pos < size && str.
at(pos) ==
':') {
343 }
else if (pos >= size) {
356 }
else if (str.
at(pos) ==
':') {
368 if (!value.isEmpty()) {
372 value.remove(value.size() - 1, 1);
377 attributes.emplace_back(std::make_pair(key, value));
385 for (
const auto &pair : attributes) {
388 if (key.
compare(u
"Global") == 0) {
389 key = QStringLiteral(
"Path");
391 }
else if (key.
compare(u
"Local") == 0) {
392 key = QStringLiteral(
"Path");
394 }
else if (key.
compare(u
"Path") == 0) {
395 value = parsePathAttr(value);
396 }
else if (key.
compare(u
"Args") == 0) {
401 }
else if (key.
compare(u
"CaptureArgs") == 0) {
404 }
else if (key.
compare(u
"Chained") == 0) {
405 value = parseChainedAttr(value);
412 if (!ret.
contains(QStringLiteral(
"Args")) && !ret.
contains(QStringLiteral(
"CaptureArgs")) &&
413 (ret.
contains(QStringLiteral(
"AutoArgs")) || ret.
contains(QStringLiteral(
"AutoCaptureArgs")))) {
414 if (ret.
contains(QStringLiteral(
"AutoArgs")) && ret.
contains(QStringLiteral(
"AutoCaptureArgs"))) {
415 qFatal(
"Action '%s' has both AutoArgs and AutoCaptureArgs, which is not allowed", name.
constData());
418 if (ret.
contains(QStringLiteral(
"AutoArgs"))) {
419 ret.
remove(QStringLiteral(
"AutoArgs"));
420 parameterName = QStringLiteral(
"Args");
422 ret.
remove(QStringLiteral(
"AutoCaptureArgs"));
423 parameterName = QStringLiteral(
"CaptureArgs");
428 int parameterCount = 0;
453 auto doesIt = attributes.
constFind(QStringLiteral(
"Does"));
454 while (doesIt != attributes.constEnd() && doesIt.
key().compare(u
"Does") == 0) {
455 QObject *
object = instantiateClass(doesIt.
value(), QByteArrayLiteral(
"Cutelyst::Component"));
457 roles.
push(qobject_cast<Component *>(
object));
469 }
else if (!value.isEmpty()) {
477 QString ret = QStringLiteral(
"/");
485 if (!pathPrefix.isEmpty()) {
504 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
511 id = QMetaType::fromName(instanceName.
toLatin1().
data());
512 if (!
id.isValid() && !instanceName.
startsWith(u
"Cutelyst::")) {
514 id = QMetaType::fromName(instanceName.
toLatin1().
data());
521 if (!superIsClassName(metaObj->
superClass(), super)) {
522 qCWarning(CUTELYST_CONTROLLER)
525 <<
"is not a derived class of"
531 qCWarning(CUTELYST_CONTROLLER)
532 <<
"Could create a new instance of"
534 <<
"make sure it's default constructor is "
535 "marked with the Q_INVOKABLE macro";
541 Component *component = application->createComponentPlugin(name);
546 component = application->createComponentPlugin(instanceName);
553 qFatal(
"Could not create component '%s', you can register it with qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
554 qPrintable(instanceName), qPrintable(instanceName));
565 if (!
id && !instanceName.
startsWith(u
"Cutelyst::")) {
574 if (!superIsClassName(metaObj->
superClass(), super)) {
575 qCWarning(CUTELYST_CONTROLLER)
578 <<
"is not a derived class of"
584 qCWarning(CUTELYST_CONTROLLER)
585 <<
"Could create a new instance of"
587 <<
"make sure it's default constructor is "
588 "marked with the Q_INVOKABLE macro";
594 Component *component = application->createComponentPlugin(name);
599 component = application->createComponentPlugin(instanceName);
606 qFatal(
"Could not create component '%s', you can register it with qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
607 qPrintable(instanceName), qPrintable(instanceName));
620 return superIsClassName(super->
superClass(), className);
625 #include "moc_controller.cpp"
This class represents a Cutelyst Action.
void setupAction(const QVariantHash &args, Application *app)
bool dispatch(Context *c)
void setMethod(const QMetaMethod &method)
void setController(Controller *controller)
The Cutelyst Application.
The Cutelyst Component base class.
void setReverse(const QString &reverse)
virtual bool init(Application *application, const QVariantHash &args)
void applyRoles(const QStack< Component * > &roles)
void setName(const QString &name)
Cutelyst Controller base class
virtual bool preFork(Application *app)
Action * actionFor(const QString &name) const
ActionList actions() const
virtual bool postFork(Application *app)
bool operator==(const char *className)
bool _DISPATCH(Context *c)
Controller(QObject *parent=nullptr)
The Cutelyst namespace holds all public Cutelyst API.
QByteArray & append(char ch)
char at(int i) const const
const char * constData() const const
QByteArray mid(int pos, int len) const const
bool isDigit() const const
bool isLower() const const
QChar toLower() const const
const Key key(const T &value, const Key &defaultKey) const const
const T value(const Key &key, const T &defaultValue) const const
typename QMap< Key, T >::const_iterator constFind(const Key &key, const T &value) const const
bool contains(const Key &key, const T &value) const const
typename QMap< Key, T >::iterator insert(const Key &key, const T &value)
int remove(const Key &key, const T &value)
typename QMap< Key, T >::iterator replace(const Key &key, const T &value)
void setParent(QObject *parent)
QString & append(QChar ch)
const QChar at(int position) const const
int compare(const QString &other, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString fromLatin1(const char *str, int size)
bool isEmpty() const const
QString number(int n, int base)
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QByteArray toLatin1() const const
QString toString() const const
QVariant fromValue(const T &value)
void append(const T &value)
const T & at(int i) const const
bool isEmpty() const const
T value(int i) const const