* @brief Opens the data channel (spawns process or starts pipe reader thread). */
| 172 | * @brief Opens the data channel (spawns process or starts pipe reader thread). |
| 173 | */ |
| 174 | bool IO::Drivers::Process::open(const QIODevice::OpenMode mode) |
| 175 | { |
| 176 | (void)mode; |
| 177 | |
| 178 | doClose(); |
| 179 | |
| 180 | if (m_mode == Mode::Launch) { |
| 181 | const QString resolved = resolveExecutable(m_executable); |
| 182 | if (resolved.isEmpty()) { |
| 183 | Misc::Utilities::showMessageBox(tr("Failed to start process"), |
| 184 | tr("Executable \"%1\" not found in PATH.").arg(m_executable), |
| 185 | QMessageBox::Warning); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | const QStringList args = QProcess::splitCommand(m_arguments); |
| 190 | |
| 191 | m_process = new QProcess(this); |
| 192 | m_process->setProcessChannelMode(QProcess::MergedChannels); |
| 193 | |
| 194 | auto env = QProcessEnvironment::systemEnvironment(); |
| 195 | const QStringList extra = extraSearchPaths(); |
| 196 | if (!extra.isEmpty()) { |
| 197 | #ifdef Q_OS_WIN |
| 198 | const QChar sep = QLatin1Char(';'); |
| 199 | #else |
| 200 | const QChar sep = QLatin1Char(':'); |
| 201 | #endif |
| 202 | const QString current = env.value(QStringLiteral("PATH")); |
| 203 | env.insert(QStringLiteral("PATH"), current + sep + extra.join(sep)); |
| 204 | m_process->setProcessEnvironment(env); |
| 205 | } |
| 206 | |
| 207 | connect(m_process, &QProcess::readyRead, this, &Process::onReadyRead); |
| 208 | connect(m_process, |
| 209 | QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), |
| 210 | this, |
| 211 | &Process::onProcessFinished); |
| 212 | connect(m_process, &QProcess::errorOccurred, this, &Process::onProcessError); |
| 213 | |
| 214 | if (!m_workingDir.isEmpty()) |
| 215 | m_process->setWorkingDirectory(m_workingDir); |
| 216 | |
| 217 | m_process->start(resolved, args); |
| 218 | |
| 219 | if (!m_process->waitForStarted(3000)) { |
| 220 | const QString detail = m_process->errorString(); |
| 221 | m_process->deleteLater(); |
| 222 | m_process = nullptr; |
| 223 | |
| 224 | Misc::Utilities::showMessageBox(tr("Failed to start process"), detail, QMessageBox::Warning); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | m_pipeRunning = true; |
no test coverage detected