aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJarek Kobus <jaroslaw.kobus@qt.io>2023-06-04 19:34:21 +0200
committerJarek Kobus <jaroslaw.kobus@qt.io>2023-06-09 07:28:22 +0000
commita4b60e5f13c2b768e6c4ca42209b6261d3671984 (patch)
treec364152e756cbe004c91d1c02adc0b8a4e6a319e
parent8e75381fce29504cd3f278f90cfcda22b9e47b4d (diff)
Utils: Introduce UnarchiverTask
Transform the Archive class into Unarchiver class. The Unarchiver class is prepared to work with TaskTree. The Unarchiver is going to replace the Archive class. Provide the task tree adapter for the Unarchiver class. Register the task inside the Tasking namespace under the UnarchiverTask name. Change-Id: Ib8f95a80c411d5afd18aa0e2ca428914430641ad Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
-rw-r--r--src/libs/utils/archive.cpp78
-rw-r--r--src/libs/utils/archive.h46
2 files changed, 120 insertions, 4 deletions
diff --git a/src/libs/utils/archive.cpp b/src/libs/utils/archive.cpp
index 5e62835a20..b86a7ab0b8 100644
--- a/src/libs/utils/archive.cpp
+++ b/src/libs/utils/archive.cpp
@@ -5,7 +5,6 @@
#include "algorithm.h"
#include "mimeutils.h"
-#include "process.h"
#include "qtcassert.h"
#include "utilstr.h"
@@ -180,4 +179,81 @@ void Archive::unarchive()
m_process->start();
}
+expected_str<Unarchiver::SourceAndCommand> Unarchiver::sourceAndCommand(const FilePath &sourceFile)
+{
+ const QVector<Tool> tools = toolsForFilePath(sourceFile);
+ if (tools.isEmpty())
+ return make_unexpected(Tr::tr("File format not supported."));
+
+ for (const Tool &tool : tools) {
+ const std::optional<Tool> resolvedTool = resolveTool(tool);
+ if (resolvedTool)
+ return SourceAndCommand(sourceFile, resolvedTool->command);
+ }
+
+ const QStringList execs = transform<QStringList>(tools, [](const Tool &tool) {
+ return tool.command.executable().toUserOutput();
+ });
+ return make_unexpected(Tr::tr("Could not find any unarchiving executable in PATH (%1).")
+ .arg(execs.join(", ")));
+}
+
+static CommandLine unarchiveCommand(const CommandLine &commandTemplate, const FilePath &sourceFile,
+ const FilePath &destDir)
+{
+ CommandLine command = commandTemplate;
+ command.setArguments(command.arguments().replace("%{src}", sourceFile.path())
+ .replace("%{dest}", destDir.path()));
+ return command;
+}
+
+void Unarchiver::start()
+{
+ QTC_ASSERT(!m_process, emit done(false); return);
+
+ if (!m_sourceAndCommand) {
+ emit outputReceived(Tr::tr("No source file set."));
+ emit done(false);
+ return;
+ }
+ if (m_destDir.isEmpty()) {
+ emit outputReceived(Tr::tr("No destination directory set."));
+ emit done(false);
+ return;
+ }
+
+ const CommandLine command = unarchiveCommand(m_sourceAndCommand->m_commandTemplate,
+ m_sourceAndCommand->m_sourceFile, m_destDir);
+ m_destDir.ensureWritableDir();
+
+ m_process.reset(new Process);
+ m_process->setProcessChannelMode(QProcess::MergedChannels);
+ QObject::connect(m_process.get(), &Process::readyReadStandardOutput, this, [this] {
+ emit outputReceived(m_process->readAllStandardOutput());
+ });
+ QObject::connect(m_process.get(), &Process::done, this, [this] {
+ const bool success = m_process->result() == ProcessResult::FinishedWithSuccess;
+ if (!success)
+ emit outputReceived(Tr::tr("Command failed."));
+ emit done(success);
+ });
+
+ emit outputReceived(Tr::tr("Running %1\nin \"%2\".\n\n", "Running <cmd> in <workingdirectory>")
+ .arg(command.toUserOutput(), m_destDir.toUserOutput()));
+
+ m_process->setCommand(command);
+ m_process->setWorkingDirectory(m_destDir);
+ m_process->start();
+}
+
+UnarchiverTaskAdapter::UnarchiverTaskAdapter()
+{
+ connect(task(), &Unarchiver::done, this, &Tasking::TaskInterface::done);
+}
+
+void UnarchiverTaskAdapter::start()
+{
+ task()->start();
+}
+
} // namespace Utils
diff --git a/src/libs/utils/archive.h b/src/libs/utils/archive.h
index ccf62d3885..b40d26c7a4 100644
--- a/src/libs/utils/archive.h
+++ b/src/libs/utils/archive.h
@@ -6,14 +6,14 @@
#include "utils_global.h"
#include "commandline.h"
+#include "process.h"
+
+#include <solutions/tasking/tasktree.h>
#include <QObject>
namespace Utils {
-class FilePath;
-class Process;
-
class QTCREATOR_UTILS_EXPORT Archive : public QObject
{
Q_OBJECT
@@ -36,4 +36,44 @@ private:
std::unique_ptr<Process> m_process;
};
+class QTCREATOR_UTILS_EXPORT Unarchiver : public QObject
+{
+ Q_OBJECT
+public:
+ class SourceAndCommand
+ {
+ private:
+ friend class Unarchiver;
+ SourceAndCommand(const FilePath &sourceFile, const CommandLine &commandTemplate)
+ : m_sourceFile(sourceFile), m_commandTemplate(commandTemplate) {}
+ FilePath m_sourceFile;
+ CommandLine m_commandTemplate;
+ };
+
+ static expected_str<SourceAndCommand> sourceAndCommand(const FilePath &sourceFile);
+
+ void setSourceAndCommand(const SourceAndCommand &data) { m_sourceAndCommand = data; }
+ void setDestDir(const FilePath &destDir) { m_destDir = destDir; }
+
+ void start();
+
+signals:
+ void outputReceived(const QString &output);
+ void done(bool success);
+
+private:
+ std::optional<SourceAndCommand> m_sourceAndCommand;
+ FilePath m_destDir;
+ std::unique_ptr<Process> m_process;
+};
+
+class QTCREATOR_UTILS_EXPORT UnarchiverTaskAdapter : public Tasking::TaskAdapter<Unarchiver>
+{
+public:
+ UnarchiverTaskAdapter();
+ void start() final;
+};
+
} // namespace Utils
+
+TASKING_DECLARE_TASK(UnarchiverTask, Utils::UnarchiverTaskAdapter);