18 #include "controller_p.h"
20 #include "application.h"
21 #include "dispatcher.h"
25 #include "context_p.h"
27 #include <QMetaClassInfo>
28 #include <QRegularExpression>
30 using namespace Cutelyst;
33 , d_ptr(new ControllerPrivate(this))
37 Controller::~Controller()
40 qDeleteAll(d->actionList);
53 Action *ret = d->actions.value(name);
57 return d->dispatcher->getAction(name, d->pathPrefix);
68 return !qstrcmp(
metaObject()->className(), className);
83 ControllerPrivate::ControllerPrivate(
Controller *parent) :
92 dispatcher = _dispatcher;
100 q->setObjectName(className);
102 bool namespaceFound =
false;
109 namespaceFound =
true;
114 if (!namespaceFound) {
116 bool lastWasUpper =
true;
118 for (
int i = 0; i < className.
length(); ++i) {
119 const QChar c = className.
at(i);
122 lastWasUpper =
false;
136 pathPrefix = controlerNS;
139 registerActionMethods(meta, q, app);
142 void ControllerPrivate::setupFinished()
146 const ActionList beginList = dispatcher->getActions(QStringLiteral(
"Begin"), pathPrefix);
151 beginAutoList.append(dispatcher->getActions(QStringLiteral(
"Auto"), pathPrefix));
153 const ActionList endList = dispatcher->getActions(QStringLiteral(
"End"), pathPrefix);
155 end = endList.
last();
158 const auto actions = actionList;
159 for (
Action *action : actions) {
160 action->dispatcherReady(dispatcher, q);
163 q->preFork(qobject_cast<Application *>(q->parent()));
172 int &asyncDetached = c->d_ptr->asyncDetached;
175 const auto beginAutoList = d->beginAutoList;
176 for (
Action *action : beginAutoList) {
178 c->d_ptr->pendingAsync.append(action);
179 }
else if (!action->dispatch(c)) {
188 c->d_ptr->pendingAsync.append(c->action());
189 }
else if (!c->action()->
dispatch(c)) {
197 c->d_ptr->pendingAsync.append(d->end);
198 }
else if (!d->end->dispatch(c)) {
206 Action *ControllerPrivate::actionClass(
const QVariantHash &args)
209 const QString actionClass = attributes.value(QStringLiteral(
"ActionClass"));
211 QObject *
object = instantiateClass(actionClass,
"Cutelyst::Action");
217 qCWarning(CUTELYST_CONTROLLER) <<
"ActionClass"
219 <<
"is not an ActionClass";
228 Action *action = actionClass(args);
234 for (
int i = 0; i < roles.
size(); ++i) {
236 code->
init(app, args);
242 action->
setName(args.value(QStringLiteral(
"name")).toString());
243 action->
setReverse(args.value(QStringLiteral(
"reverse")).toString());
267 if (name == classInfo.
name()) {
280 Action *action = createAction({
290 actions.insertMulti(action->
reverse(), action);
291 actionList.append(action);
299 std::vector<std::pair<QString, QString> > attributes;
306 int size = str.
size();
313 if (str.
at(pos) ==
':') {
314 int keyStart = ++pos;
317 if (str.
at(pos) ==
'(') {
319 int valueStart = ++pos;
321 if (str.
at(pos) ==
')') {
324 if (++pos < size && str.
at(pos) ==
':') {
329 }
else if (pos >= size) {
342 }
else if (str.
at(pos) ==
':') {
371 auto i = attributes.crbegin();
372 const auto end = attributes.crend();
377 key = QStringLiteral(
"Path");
380 key = QStringLiteral(
"Path");
383 value = parsePathAttr(value);
393 value = parseChainedAttr(value);
401 if (!ret.
contains(QStringLiteral(
"Args")) && !ret.
contains(QStringLiteral(
"CaptureArgs")) &&
402 (ret.
contains(QStringLiteral(
"AutoArgs")) || ret.
contains(QStringLiteral(
"AutoCaptureArgs")))) {
403 if (ret.
contains(QStringLiteral(
"AutoArgs")) && ret.
contains(QStringLiteral(
"AutoCaptureArgs"))) {
404 qFatal(
"Action '%s' has both AutoArgs and AutoCaptureArgs, which is not allowed", name.
constData());
407 if (ret.
contains(QStringLiteral(
"AutoArgs"))) {
408 ret.
remove(QStringLiteral(
"AutoArgs"));
409 parameterName = QStringLiteral(
"Args");
411 ret.
remove(QStringLiteral(
"AutoCaptureArgs"));
412 parameterName = QStringLiteral(
"CaptureArgs");
417 int parameterCount = 0;
420 if (typeId == QMetaType::QString) {
431 if (!ret.
contains(QStringLiteral(
"Private")) && method.
access() == QMetaMethod::Private) {
442 auto doesIt = attributes.
constFind(QStringLiteral(
"Does"));
444 QObject *
object = instantiateClass(doesIt.
value(), QByteArrayLiteral(
"Cutelyst::Component"));
446 roles.
push(qobject_cast<Component *>(
object));
466 QString ret = QStringLiteral(
"/");
471 if (attr == QStringLiteral(
".")) {
474 if (!pathPrefix.isEmpty()) {
500 if (!
id && !instanceName.
startsWith(QStringLiteral(
"Cutelyst::"))) {
509 if (!superIsClassName(metaObj->
superClass(), super)) {
510 qCWarning(CUTELYST_CONTROLLER)
513 <<
"is not a derived class of"
519 qCWarning(CUTELYST_CONTROLLER)
520 <<
"Could create a new instance of"
522 <<
"make sure it's default constructor is "
523 "marked with the Q_INVOKABLE macro";
529 Component *component = application->createComponentPlugin(name);
534 component = application->createComponentPlugin(instanceName);
541 qFatal(
"Could not create component '%s', you can register it with qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
542 qPrintable(instanceName), qPrintable(instanceName));
554 return superIsClassName(super->
superClass(), className);
559 #include "moc_controller.cpp"
void setName(const QString &name)
QString & append(QChar ch)
bool dispatch(Context *c)
bool contains(const Key &key) const
void append(const T &value)
void setReverse(const QString &reverse)
ActionList actions() const
virtual bool preFork(Application *app)
const_iterator constFind(const Key &key) const
QString & remove(int position, int n)
The Cutelyst Component base class.
This class represents a Cutelyst Action.
QString number(int n, int base)
Action * actionFor(const QString &name) const
Cutelyst Controller base class
iterator insertMulti(const Key &key, const T &value)
const_iterator constEnd() const
const char * constData() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
virtual bool init(Application *application, const QVariantHash &args)
bool _DISPATCH(Context *c)
void applyRoles(const QStack< Component * > &roles)
QByteArray mid(int pos, int len) const
QByteArray & append(char ch)
void setParent(QObject *parent)
QVariant fromValue(const T &value)
void setController(Controller *controller)
virtual bool postFork(Application *app)
const Key key(const T &value, const Key &defaultKey) const
const T & at(int i) const
QByteArray toLatin1() const
void setupAction(const QVariantHash &args, Application *app)
const QChar at(int position) const
QString fromLatin1(const char *str, int size)
bool operator==(const char *className)
The Cutelyst Application.
void setMethod(const QMetaMethod &method)
iterator insert(const Key &key, const T &value)
const T value(const Key &key, const T &defaultValue) const
int remove(const Key &key)
Controller(QObject *parent=nullptr)