| 212 | |
| 213 | |
| 214 | QMap<QString, QVariant> ScriptAPI::system(const QString& cmdline, bool waitForResult) |
| 215 | { |
| 216 | QMap<QString, QVariant> retVal; |
| 217 | |
| 218 | retVal[QString::fromLatin1("status")] = SystemAccess_PermissionDenied; |
| 219 | retVal[QString::fromLatin1("result")] = QVariant(); |
| 220 | retVal[QString::fromLatin1("message")] = QVariant(); |
| 221 | retVal[QString::fromLatin1("output")] = QVariant(); |
| 222 | |
| 223 | // Paranoia |
| 224 | if (!m_script) { |
| 225 | retVal[QString::fromLatin1("message")] = tr("Internal error"); |
| 226 | return retVal; |
| 227 | } |
| 228 | |
| 229 | if (mayExecuteSystemCommand(cmdline, m_target)) { |
| 230 | Tw::Utils::SystemCommand * process = new Tw::Utils::SystemCommand(this, waitForResult, !waitForResult); |
| 231 | if (waitForResult) { |
| 232 | process->setProcessChannelMode(QProcess::MergedChannels); |
| 233 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 234 | process->start(cmdline); |
| 235 | #else |
| 236 | process->startCommand(cmdline); |
| 237 | #endif |
| 238 | // make sure events (in particular GUI update events that should |
| 239 | // inform the user of the progress) are processed before we make a |
| 240 | // call that possibly blocks for a considerable amount of time |
| 241 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100); |
| 242 | if (!process->waitForStarted()) { |
| 243 | retVal[QString::fromLatin1("status")] = SystemAccess_Failed; |
| 244 | retVal[QString::fromLatin1("message")] = tr("Failed to execute system command: %1").arg(cmdline); |
| 245 | process->deleteLater(); |
| 246 | return retVal; |
| 247 | } |
| 248 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100); |
| 249 | if (!process->waitForFinished()) { |
| 250 | retVal[QString::fromLatin1("status")] = SystemAccess_Failed; |
| 251 | retVal[QString::fromLatin1("result")] = process->exitCode(); |
| 252 | retVal[QString::fromLatin1("output")] = process->getResult(); |
| 253 | retVal[QString::fromLatin1("message")] = tr("Error executing system command: %1").arg(cmdline); |
| 254 | process->deleteLater(); |
| 255 | return retVal; |
| 256 | } |
| 257 | retVal[QString::fromLatin1("status")] = SystemAccess_OK; |
| 258 | retVal[QString::fromLatin1("result")] = process->exitCode(); |
| 259 | retVal[QString::fromLatin1("output")] = process->getResult(); |
| 260 | process->deleteLater(); |
| 261 | } |
| 262 | else { |
| 263 | process->closeReadChannel(QProcess::StandardOutput); |
| 264 | process->closeReadChannel(QProcess::StandardError); |
| 265 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 266 | process->start(cmdline); |
| 267 | #else |
| 268 | process->startCommand(cmdline); |
| 269 | #endif |
| 270 | retVal[QString::fromLatin1("status")] = SystemAccess_OK; |
| 271 | } |
no test coverage detected