aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/archive.cpp
diff options
context:
space:
mode:
authorJarek Kobus <jaroslaw.kobus@qt.io>2022-03-24 10:29:02 +0100
committerJarek Kobus <jaroslaw.kobus@qt.io>2022-03-25 12:49:19 +0000
commited94e0c0662aa01f97af327b8e269613806e853d (patch)
tree9563d40b04cfe020f7ff14c9605ae45648f8052f /src/libs/utils/archive.cpp
parent41f9962ea6a21441534308f1835117a0d494e1ef (diff)
Archive: Avoid calling blocking stopProcess
Make Archive constructor public. Make the caller responsible for deleting the Archive object. Don't automatially run the unarchive process when constructing Archive object. Provide a start() method, to be called after the caller has connected to Archive signals. Add Archive::isValid() method. Remove Archive::unarchive() gui overload, as it's unused. Make sure we don't leak the Archive object in AndroidSdkDownloader. Change-Id: Idf67262554cdfef50aef4a2234b6a5089110f9a2 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Diffstat (limited to 'src/libs/utils/archive.cpp')
-rw-r--r--src/libs/utils/archive.cpp120
1 files changed, 32 insertions, 88 deletions
diff --git a/src/libs/utils/archive.cpp b/src/libs/utils/archive.cpp
index 3883fa3f192..fcef2c22b04 100644
--- a/src/libs/utils/archive.cpp
+++ b/src/libs/utils/archive.cpp
@@ -26,16 +26,11 @@
#include "archive.h"
#include "algorithm.h"
-#include "checkablemessagebox.h"
-#include "environment.h"
#include "mimeutils.h"
#include "qtcassert.h"
#include "qtcprocess.h"
-#include <QDir>
-#include <QPushButton>
#include <QSettings>
-#include <QTimer>
namespace Utils {
@@ -163,99 +158,48 @@ bool Archive::supportsFile(const FilePath &filePath, QString *reason)
return true;
}
-bool Archive::unarchive(const FilePath &src, const FilePath &dest, QWidget *parent)
+Archive::Archive(const FilePath &src, const FilePath &dest)
{
- Archive *archive = unarchive(src, dest);
- QTC_ASSERT(archive, return false);
-
- CheckableMessageBox box(parent);
- box.setIcon(QMessageBox::Information);
- box.setWindowTitle(tr("Unarchiving File"));
- box.setText(tr("Unzipping \"%1\" to \"%2\".").arg(src.toUserOutput(), dest.toUserOutput()));
- box.setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
- box.button(QDialogButtonBox::Ok)->setEnabled(false);
- box.setCheckBoxVisible(false);
- QObject::connect(archive, &Archive::outputReceived, &box, [&box](const QString &output) {
- box.setDetailedText(box.detailedText() + output);
- });
- bool success = false;
- QObject::connect(archive, &Archive::finished, [&box, &success](bool ret) {
- box.button(QDialogButtonBox::Ok)->setEnabled(true);
- box.button(QDialogButtonBox::Cancel)->setEnabled(false);
- success = ret;
- });
- QObject::connect(&box, &QMessageBox::rejected, archive, &Archive::cancel);
- box.exec();
- return success;
+ const Utils::optional<Tool> tool = unzipTool(src, dest);
+ if (!tool)
+ return;
+ m_commandLine = tool->command;
+ m_workingDirectory = dest.absoluteFilePath();
}
-Archive *Archive::unarchive(const FilePath &src, const FilePath &dest)
-{
- const Utils::optional<Tool> tool = unzipTool(src, dest);
- QTC_ASSERT(tool, return nullptr);
+Archive::~Archive() = default;
- auto archive = new Archive;
+bool Archive::isValid() const
+{
+ return !m_commandLine.isEmpty();
+}
- const FilePath workingDirectory = dest.absolutePath();
- workingDirectory.ensureWritableDir();
+void Archive::unarchive()
+{
+ QTC_ASSERT(isValid(), return);
+ QTC_ASSERT(!m_process, return);
- archive->m_process = new QtcProcess;
- archive->m_process->setProcessChannelMode(QProcess::MergedChannels);
- QObject::connect(
- archive->m_process,
- &QtcProcess::readyReadStandardOutput,
- archive,
- [archive]() {
- if (!archive->m_process)
- return;
- emit archive->outputReceived(QString::fromUtf8(
- archive->m_process->readAllStandardOutput()));
- },
- Qt::QueuedConnection);
- QObject::connect(
- archive->m_process,
- &QtcProcess::finished,
- archive,
- [archive] {
- if (!archive->m_process)
- return;
- emit archive->finished(archive->m_process->result() == ProcessResult::FinishedWithSuccess);
- archive->m_process->deleteLater();
- archive->m_process = nullptr;
- archive->deleteLater();
- },
- Qt::QueuedConnection);
- QObject::connect(
- archive->m_process,
- &QtcProcess::errorOccurred,
- archive,
- [archive](QProcess::ProcessError) {
- if (!archive->m_process)
- return;
- emit archive->outputReceived(tr("Command failed."));
- emit archive->finished(false);
- archive->m_process->deleteLater();
- archive->m_process = nullptr;
- archive->deleteLater();
- },
- Qt::QueuedConnection);
+ m_workingDirectory.ensureWritableDir();
- QTimer::singleShot(0, archive, [archive, tool, workingDirectory] {
- emit archive->outputReceived(
- tr("Running %1\nin \"%2\".\n\n", "Running <cmd> in <workingdirectory>")
- .arg(tool->command.toUserOutput(), workingDirectory.toUserOutput()));
+ m_process.reset(new QtcProcess);
+ m_process->setProcessChannelMode(QProcess::MergedChannels);
+ QObject::connect(m_process.get(), &QtcProcess::readyReadStandardOutput, this, [this] {
+ emit outputReceived(QString::fromUtf8(m_process->readAllStandardOutput()));
+ });
+ QObject::connect(m_process.get(), &QtcProcess::finished, this, [this] {
+ emit finished(m_process->result() == ProcessResult::FinishedWithSuccess);
+ });
+ QObject::connect(m_process.get(), &QtcProcess::errorOccurred, this, [this] {
+ emit outputReceived(tr("Command failed."));
+ emit finished(false);
});
- archive->m_process->setCommand(tool->command);
- archive->m_process->setWorkingDirectory(workingDirectory);
- archive->m_process->start();
- return archive;
-}
+ emit outputReceived(tr("Running %1\nin \"%2\".\n\n", "Running <cmd> in <workingdirectory>")
+ .arg(m_commandLine.toUserOutput(), m_workingDirectory.toUserOutput()));
-void Archive::cancel()
-{
- if (m_process)
- m_process->stopProcess();
+ m_process->setCommand(m_commandLine);
+ m_process->setWorkingDirectory(m_workingDirectory);
+ m_process->start();
}
} // namespace Utils