| 1 | /* |
| 2 | * File: UtilityExecutor.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "UtilityExecutor.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "UtilityRunner.h" |
| 28 | |
| 29 | #include <QProcess> |
| 30 | #include <QString> |
| 31 | #include <QStringList> |
| 32 | #include <cerrno> |
| 33 | #include <cstdio> |
| 34 | |
| 35 | UtilityExecutor::UtilityExecutor(QObject *parent) :QObject(parent) |
| 36 | { |
| 37 | mProcess = NULL; |
| 38 | } |
| 39 | |
| 40 | UtilityExecutor::~UtilityExecutor() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | //TODO convert all std::string parameters to QString |
| 45 | int UtilityExecutor::run(const std::string & pCommand, const std::string& pPath, std::vector<std::string> pParameters, UtilityRunner * runner, |
| 46 | UtilityRunner * errorRunner, const QString& workingDirectory) |
| 47 | { |
| 48 | QString program = pPath.c_str(); |
| 49 | if (pPath.size() > 0) program += "/"; |
| 50 | program += pCommand.c_str(); |
| 51 | QStringList arguments; |
| 52 | for (unsigned int i=0; i<pParameters.size(); i++) |
| 53 | { |
| 54 | arguments << pParameters[i].c_str(); |
| 55 | } |
| 56 | mProcess = new QProcess(); |
| 57 | if (runner != NULL) |
| 58 | { |
| 59 | runner->start(); |
| 60 | connect(mProcess, |
| 61 | SIGNAL(readyReadStandardOutput()), |
| 62 | runner, |
| 63 | SLOT(readStandardOutput())); |
| 64 | } |
| 65 | if (errorRunner != NULL) |
| 66 | { |
| 67 | errorRunner->start(); |
| 68 | connect(mProcess, |
| 69 | SIGNAL(readyReadStandardError()), |
| 70 | errorRunner, |
| 71 | SLOT(readStandardError())); |
| 72 | } |
| 73 | for (int i=0 ; i<arguments.size(); i++) |
| 74 | hOutDebug(1, " " << arguments.value(i).toStdString()); |
| 75 | if (!workingDirectory.isEmpty()) |
| 76 | mProcess->setWorkingDirectory(workingDirectory); |
| 77 | hOutDebug(1, mProcess->workingDirectory().toStdString()); |
| 78 | mProcess->start(program,arguments); |
| 79 | Q_PID pid = mProcess->pid(); |
| 80 | |
| 81 | if (pid != 0) |
| 82 | return 0; |
| 83 | else |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | bool UtilityExecutor::running() |
| 88 | { |
| 89 | if (mProcess != NULL) |
| 90 | return mProcess->state() == QProcess::Running; |
| 91 | else return false; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | void UtilityExecutor::terminate() |
| 96 | { |
| 97 | if (mProcess != NULL) |
| 98 | { |
| 99 | //mProcess->close(); |
| 100 | mProcess = NULL; |
| 101 | } |
| 102 | } |