Utils: Copy some SynchronousProcess function to QtcProcess

Start using it in Utils::Archive.

The idea is to merge SynchronousProcess into QtcProcess (or avoid its
use) to have fewer classes to make "remote-aware".

Change-Id: Ieb08f6f66eab63fd058b75e3bafa79bfbf140387
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-04-29 17:15:48 +02:00
parent fe8fbf1a4a
commit dcba96d16e
4 changed files with 32 additions and 10 deletions

View File

@@ -30,7 +30,7 @@
#include "environment.h" #include "environment.h"
#include "mimetypes/mimedatabase.h" #include "mimetypes/mimedatabase.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "synchronousprocess.h" #include "qtcprocess.h"
#include <QDir> #include <QDir>
#include <QPushButton> #include <QPushButton>
@@ -208,7 +208,7 @@ Archive *Archive::unarchive(const FilePath &src, const FilePath &dest)
const QString workingDirectory = dest.toFileInfo().absoluteFilePath(); const QString workingDirectory = dest.toFileInfo().absoluteFilePath();
QDir(workingDirectory).mkpath("."); QDir(workingDirectory).mkpath(".");
archive->m_process = new QProcess; archive->m_process = new QtcProcess;
archive->m_process->setProcessChannelMode(QProcess::MergedChannels); archive->m_process->setProcessChannelMode(QProcess::MergedChannels);
QObject::connect( QObject::connect(
archive->m_process, archive->m_process,
@@ -265,15 +265,15 @@ Archive *Archive::unarchive(const FilePath &src, const FilePath &dest)
archive->m_process->setArguments(tool->arguments); archive->m_process->setArguments(tool->arguments);
#endif #endif
archive->m_process->setWorkingDirectory(workingDirectory); archive->m_process->setWorkingDirectory(workingDirectory);
archive->m_process->start(QProcess::ReadOnly); archive->m_process->setOpenMode(QProcess::ReadOnly);
archive->m_process->start();
return archive; return archive;
} }
void Archive::cancel() void Archive::cancel()
{ {
if (!m_process) if (m_process)
return; m_process->stopProcess();
SynchronousProcess::stopProcess(*m_process);
} }
} // namespace Utils } // namespace Utils

View File

@@ -30,10 +30,11 @@
#include "fileutils.h" #include "fileutils.h"
#include <QObject> #include <QObject>
#include <QProcess>
namespace Utils { namespace Utils {
class QtcProcess;
class QTCREATOR_UTILS_EXPORT Archive : public QObject class QTCREATOR_UTILS_EXPORT Archive : public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -51,7 +52,7 @@ signals:
private: private:
Archive() = default; Archive() = default;
QProcess *m_process = nullptr; QtcProcess *m_process = nullptr;
}; };
} // namespace Utils } // namespace Utils

View File

@@ -752,7 +752,7 @@ void QtcProcess::start()
#endif #endif
// Note: Arguments set with setNativeArgs will be appended to the ones // Note: Arguments set with setNativeArgs will be appended to the ones
// passed with start() below. // passed with start() below.
QProcess::start(command, QStringList()); QProcess::start(command, QStringList(), m_openMode);
} else { } else {
if (!success) { if (!success) {
setErrorString(tr("Error in command line.")); setErrorString(tr("Error in command line."));
@@ -761,7 +761,7 @@ void QtcProcess::start()
emit errorOccurred(QProcess::UnknownError); emit errorOccurred(QProcess::UnknownError);
return; return;
} }
QProcess::start(command, arguments.toUnixArgs()); QProcess::start(command, arguments.toUnixArgs(), m_openMode);
} }
if (m_synchronous) if (m_synchronous)
@@ -1275,6 +1275,22 @@ void QtcProcess::setSynchronous(bool on)
m_synchronous = on; m_synchronous = on;
} }
void QtcProcess::setOpenMode(OpenMode mode)
{
m_openMode = mode;
}
bool QtcProcess::stopProcess()
{
if (state() == QProcess::NotRunning)
return true;
terminate();
if (waitForFinished(300))
return true;
kill();
return waitForFinished(300);
}
bool QtcProcess::ArgIterator::next() bool QtcProcess::ArgIterator::next()
{ {
// We delay the setting of m_prev so we can still delete the last argument // We delay the setting of m_prev so we can still delete the last argument

View File

@@ -149,6 +149,10 @@ public:
bool isSynchronous() const; bool isSynchronous() const;
void setSynchronous(bool on); void setSynchronous(bool on);
void setOpenMode(OpenMode mode);
bool stopProcess();
private: private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void setupChildProcess() override; void setupChildProcess() override;
@@ -163,6 +167,7 @@ private:
bool m_lowPriority = false; bool m_lowPriority = false;
bool m_synchronous = false; bool m_synchronous = false;
OpenMode m_openMode = ReadWrite;
}; };
} // namespace Utils } // namespace Utils