getCommandResult():
reset result if necessary (for instance for showing current result in GUI.
This commit is contained in:
		@@ -5,6 +5,7 @@
 | 
				
			|||||||
#include <QDir>
 | 
					#include <QDir>
 | 
				
			||||||
#include <QRegularExpression>
 | 
					#include <QRegularExpression>
 | 
				
			||||||
#include <QDateTime>
 | 
					#include <QDateTime>
 | 
				
			||||||
 | 
					#include <QMutexLocker>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Command::Command(QString const &command, int start_timeout, int finish_timeout)
 | 
					Command::Command(QString const &command, int start_timeout, int finish_timeout)
 | 
				
			||||||
    : m_command(command.trimmed())
 | 
					    : m_command(command.trimmed())
 | 
				
			||||||
@@ -14,14 +15,23 @@ Command::Command(QString const &command, int start_timeout, int finish_timeout)
 | 
				
			|||||||
    , m_exitCode(-1) {
 | 
					    , m_exitCode(-1) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QString Command::getCommandResult() const {
 | 
					QString Command::getCommandResult(bool reset) const {
 | 
				
			||||||
    return m_commandResult;
 | 
					    QMutexLocker locker(&m_mtx);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (reset == false) {
 | 
				
			||||||
 | 
					        return m_commandResult;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QString commandResult = m_commandResult;
 | 
				
			||||||
 | 
					    m_commandResult.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return commandResult;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Command::readyReadStandardOutput() {
 | 
					void Command::readyReadStandardOutput() {
 | 
				
			||||||
 | 
					    QMutexLocker locker(&m_mtx);
 | 
				
			||||||
    QProcess *p = (QProcess *)sender();
 | 
					    QProcess *p = (QProcess *)sender();
 | 
				
			||||||
    m_commandResult += p->readAllStandardOutput();
 | 
					    m_commandResult += p->readAllStandardOutput();
 | 
				
			||||||
    // qCritical() << m_commandResult;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Command::readyReadStandardError() {
 | 
					void Command::readyReadStandardError() {
 | 
				
			||||||
@@ -35,6 +45,7 @@ void Command::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) {
 | 
				
			|||||||
    // read all remaining data sent to the process, just in case
 | 
					    // read all remaining data sent to the process, just in case
 | 
				
			||||||
    QString d = p->readAllStandardOutput();
 | 
					    QString d = p->readAllStandardOutput();
 | 
				
			||||||
    if (!d.isEmpty()) {
 | 
					    if (!d.isEmpty()) {
 | 
				
			||||||
 | 
					        QMutexLocker locker(&m_mtx);
 | 
				
			||||||
        m_commandResult += d;
 | 
					        m_commandResult += d;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput()));
 | 
					    disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput()));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,22 +7,24 @@
 | 
				
			|||||||
#include <QString>
 | 
					#include <QString>
 | 
				
			||||||
#include <QStringList>
 | 
					#include <QStringList>
 | 
				
			||||||
#include <QProcess>
 | 
					#include <QProcess>
 | 
				
			||||||
 | 
					#include <QMutex>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Command : public QObject {
 | 
					class Command : public QObject {
 | 
				
			||||||
    Q_OBJECT
 | 
					    Q_OBJECT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QString m_command;
 | 
					    QString m_command;
 | 
				
			||||||
    QString m_commandResult;
 | 
					    mutable QString m_commandResult;
 | 
				
			||||||
    int m_waitForStartTimeout;
 | 
					    int m_waitForStartTimeout;
 | 
				
			||||||
    int m_waitForFinishTimeout;
 | 
					    int m_waitForFinishTimeout;
 | 
				
			||||||
    int m_exitCode;
 | 
					    int m_exitCode;
 | 
				
			||||||
 | 
					    mutable QMutex m_mtx;
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    explicit Command(QString const &command,
 | 
					    explicit Command(QString const &command,
 | 
				
			||||||
                     int start_timeout = 100000,
 | 
					                     int start_timeout = 100000,
 | 
				
			||||||
                     int finish_timeout = 100000);
 | 
					                     int finish_timeout = 100000);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QString getCommandResult() const;
 | 
					    QString getCommandResult(bool reset = false) const;
 | 
				
			||||||
    QString command() const { return m_command; }
 | 
					    QString command() const { return m_command; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    bool execute(QString workingDirectory, QStringList args = QStringList());
 | 
					    bool execute(QString workingDirectory, QStringList args = QStringList());
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user