improve executeProcess

This commit is contained in:
Syping 2019-09-30 20:15:26 +02:00
parent 7773d84176
commit 1ddf257a41

View file

@ -35,25 +35,49 @@ void LuaEngineOS::pushClass(LuaEngine *luaEngine)
int LuaEngineOS::executeProcess(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 1) {
QProcess *process = new QProcess;
int processReturn = 0;
bool runInBackground = false;
bool processExecuted = false;
if (getArgumentCount(L_p) >= 2) {
QVariantList arguments = getArguments(L_p);
QVariant lastArgument = arguments.last();
if (lastArgument.type() == QVariant::Bool) {
runInBackground = lastArgument.toBool();
arguments.removeLast();
}
if (arguments.length() >= 2) {
QStringList processArguments;
QString processPath = arguments.first().toString();
arguments.removeFirst();
for (const QVariant &argument : arguments) {
processArguments << argument.toString();
}
process->start(processPath, processArguments, QProcess::ReadOnly);
if (runInBackground) {
processExecuted = QProcess::startDetached(processPath, processArguments);
}
else {
process->start(getVariant(L_p, 1).toString(), QProcess::ReadOnly);
processReturn = QProcess::execute(processPath, processArguments);
}
QObject::connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater()));
pushVariant(L_p, true);
}
else {
if (runInBackground) {
processExecuted = QProcess::startDetached(arguments.first().toString());
}
else {
processReturn = QProcess::execute(arguments.first().toString());
}
}
}
else {
processReturn = QProcess::execute(getVariant(L_p, 1).toString());
}
if (runInBackground && !processExecuted) {
processReturn = -2;
}
pushVariant(L_p, processReturn);
return 1;
}
pushVariant(L_p, false);
pushVariant(L_p, -2);
return 1;
}