2014-11-27 18:31:09 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2024-07-26 22:53:52 +00:00
|
|
|
* Copyright (C) 2014 Symless Ltd.
|
2014-11-27 18:31:09 +00:00
|
|
|
*
|
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2014-11-27 18:31:09 +00:00
|
|
|
*
|
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "CommandProcess.h"
|
|
|
|
|
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
CommandProcess::CommandProcess(
|
2024-07-26 22:53:52 +00:00
|
|
|
QString command, QStringList arguments, QString input)
|
|
|
|
|
: m_Command(command),
|
2024-07-16 13:36:44 +00:00
|
|
|
m_Arguments(arguments),
|
|
|
|
|
m_Input(input) {}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
QString CommandProcess::run() {
|
|
|
|
|
QProcess process;
|
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
|
|
|
|
process.start(m_Command, m_Arguments);
|
|
|
|
|
bool success = process.waitForStarted();
|
|
|
|
|
|
2024-07-22 16:48:02 +00:00
|
|
|
QString output;
|
|
|
|
|
QString error;
|
2024-07-02 19:07:06 +00:00
|
|
|
if (success) {
|
|
|
|
|
if (!m_Input.isEmpty()) {
|
|
|
|
|
process.write(m_Input.toStdString().c_str());
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (process.waitForFinished()) {
|
|
|
|
|
output =
|
|
|
|
|
QString::fromLocal8Bit(process.readAllStandardOutput().trimmed());
|
|
|
|
|
error = process.readAllStandardError().trimmed();
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 16:48:02 +00:00
|
|
|
if (int code = process.exitCode(); !success || code != 0) {
|
|
|
|
|
qFatal(
|
2024-08-01 00:13:01 +00:00
|
|
|
"command failed: %s %s\ncode: %d\nerror: %s", qUtf8Printable(m_Command),
|
2024-07-22 16:48:02 +00:00
|
|
|
qUtf8Printable(m_Arguments.join(" ")), code,
|
2024-08-01 00:13:01 +00:00
|
|
|
error.isEmpty() ? "none" : qUtf8Printable(error));
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
emit finished();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return output;
|
2014-11-27 18:31:09 +00:00
|
|
|
}
|