| 34 | } |
| 35 | |
| 36 | ChessPlayer* EngineBuilder::create(QObject* receiver, |
| 37 | const char* method, |
| 38 | QObject* parent, |
| 39 | QString* error) const |
| 40 | { |
| 41 | QString workDir = m_config.workingDirectory(); |
| 42 | QString cmd = m_config.command().trimmed(); |
| 43 | QString stderrFile = m_config.stderrFile(); |
| 44 | |
| 45 | if (cmd.isEmpty()) |
| 46 | { |
| 47 | setError(error, tr("Empty engine command")); |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
| 51 | if (!EngineFactory::protocols().contains(m_config.protocol())) |
| 52 | { |
| 53 | setError(error, tr("Unknown chess protocol: %1") |
| 54 | .arg(m_config.protocol())); |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | EngineProcess* process = new EngineProcess(); |
| 59 | |
| 60 | if (workDir.isEmpty()) |
| 61 | { |
| 62 | process->setWorkingDirectory(QDir::tempPath()); |
| 63 | |
| 64 | QFileInfo cmdInfo(cmd); |
| 65 | if (cmdInfo.isFile()) |
| 66 | cmd = cmdInfo.absoluteFilePath(); |
| 67 | } |
| 68 | else |
| 69 | process->setWorkingDirectory(workDir); |
| 70 | |
| 71 | if (!stderrFile.isEmpty()) |
| 72 | process->setStandardErrorFile(stderrFile, QIODevice::Append); |
| 73 | |
| 74 | process->start(cmd, m_config.arguments()); |
| 75 | |
| 76 | bool ok = process->waitForStarted(); |
| 77 | if (!ok) |
| 78 | { |
| 79 | setError(error, tr("Cannot execute command: %1") |
| 80 | .arg(m_config.command())); |
| 81 | delete process; |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | ChessEngine* engine = EngineFactory::create(m_config.protocol()); |
| 86 | Q_ASSERT(engine != nullptr); |
| 87 | |
| 88 | engine->setParent(parent); |
| 89 | if (receiver != nullptr && method != nullptr) |
| 90 | QObject::connect(engine, SIGNAL(debugMessage(QString)), |
| 91 | receiver, method); |
| 92 | engine->setDevice(process); |
| 93 | engine->applyConfiguration(m_config); |
nothing calls this directly
no test coverage detected