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

View File

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

View File

@@ -752,7 +752,7 @@ void QtcProcess::start()
#endif
// Note: Arguments set with setNativeArgs will be appended to the ones
// passed with start() below.
QProcess::start(command, QStringList());
QProcess::start(command, QStringList(), m_openMode);
} else {
if (!success) {
setErrorString(tr("Error in command line."));
@@ -761,7 +761,7 @@ void QtcProcess::start()
emit errorOccurred(QProcess::UnknownError);
return;
}
QProcess::start(command, arguments.toUnixArgs());
QProcess::start(command, arguments.toUnixArgs(), m_openMode);
}
if (m_synchronous)
@@ -1275,6 +1275,22 @@ void QtcProcess::setSynchronous(bool 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()
{
// 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;
void setSynchronous(bool on);
void setOpenMode(OpenMode mode);
bool stopProcess();
private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void setupChildProcess() override;
@@ -163,6 +167,7 @@ private:
bool m_lowPriority = false;
bool m_synchronous = false;
OpenMode m_openMode = ReadWrite;
};
} // namespace Utils