2023-02-23 12:47:39 +01:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
#include "terminalhooks.h"
|
|
|
|
|
2023-05-25 10:56:07 +02:00
|
|
|
#include "externalterminalprocessimpl.h"
|
2023-02-23 12:47:39 +01:00
|
|
|
#include "filepath.h"
|
2024-02-27 16:08:45 +01:00
|
|
|
#include "qtcprocess.h"
|
2023-10-06 08:51:17 +02:00
|
|
|
#include "utilstr.h"
|
2023-03-07 17:55:38 +01:00
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
#include <QMutex>
|
2023-02-23 12:47:39 +01:00
|
|
|
|
|
|
|
namespace Utils::Terminal {
|
|
|
|
|
2023-10-06 08:51:17 +02:00
|
|
|
expected_str<FilePath> defaultShellForDevice(const FilePath &deviceRoot)
|
2023-03-01 09:57:43 +01:00
|
|
|
{
|
2023-03-14 09:09:55 +01:00
|
|
|
if (deviceRoot.osType() == OsTypeWindows)
|
|
|
|
return deviceRoot.withNewPath("cmd.exe").searchInPath();
|
2023-03-01 09:57:43 +01:00
|
|
|
|
2023-10-06 08:51:17 +02:00
|
|
|
const expected_str<Environment> env = deviceRoot.deviceEnvironmentWithError();
|
|
|
|
if (!env)
|
|
|
|
return make_unexpected(env.error());
|
2023-08-10 06:44:27 +02:00
|
|
|
|
2023-10-06 08:51:17 +02:00
|
|
|
FilePath shell = FilePath::fromUserInput(env->value_or("SHELL", "/bin/sh"));
|
2023-03-01 09:57:43 +01:00
|
|
|
|
|
|
|
if (!shell.isAbsolutePath())
|
2023-10-06 08:51:17 +02:00
|
|
|
shell = env->searchInPath(shell.nativePath());
|
2023-03-01 09:57:43 +01:00
|
|
|
|
|
|
|
if (shell.isEmpty())
|
2023-10-18 09:59:28 +02:00
|
|
|
return make_unexpected(Tr::tr("Could not find any shell."));
|
2023-03-01 09:57:43 +01:00
|
|
|
|
2023-03-29 13:45:42 +02:00
|
|
|
return deviceRoot.withNewMappedPath(shell);
|
2023-03-01 09:57:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
class HooksPrivate
|
2023-02-23 12:47:39 +01:00
|
|
|
{
|
2023-03-22 16:54:53 +01:00
|
|
|
public:
|
2023-02-23 12:47:39 +01:00
|
|
|
HooksPrivate()
|
2023-03-22 16:54:53 +01:00
|
|
|
{
|
|
|
|
auto openTerminal = [](const OpenTerminalParameters ¶meters) {
|
2023-02-23 12:47:39 +01:00
|
|
|
DeviceFileHooks::instance().openTerminal(parameters.workingDirectory.value_or(
|
|
|
|
FilePath{}),
|
|
|
|
parameters.environment.value_or(Environment{}));
|
2023-03-22 16:54:53 +01:00
|
|
|
};
|
2023-12-12 12:12:36 +01:00
|
|
|
auto createProcessInterface = [] { return new ExternalTerminalProcessImpl; };
|
2023-03-22 16:54:53 +01:00
|
|
|
|
|
|
|
addCallbackSet("External", {openTerminal, createProcessInterface});
|
|
|
|
}
|
|
|
|
|
|
|
|
void addCallbackSet(const QString &name, const Hooks::CallbackSet &callbackSet)
|
|
|
|
{
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
m_callbackSets.push_back(qMakePair(name, callbackSet));
|
|
|
|
|
|
|
|
m_createTerminalProcessInterface
|
|
|
|
= m_callbackSets.back().second.createTerminalProcessInterface;
|
|
|
|
m_openTerminal = m_callbackSets.back().second.openTerminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeCallbackSet(const QString &name)
|
|
|
|
{
|
|
|
|
if (name == "External")
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
m_callbackSets.removeIf([name](const auto &pair) { return pair.first == name; });
|
|
|
|
|
|
|
|
m_createTerminalProcessInterface
|
|
|
|
= m_callbackSets.back().second.createTerminalProcessInterface;
|
|
|
|
m_openTerminal = m_callbackSets.back().second.openTerminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hooks::CreateTerminalProcessInterface createTerminalProcessInterface()
|
|
|
|
{
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
return m_createTerminalProcessInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hooks::OpenTerminal openTerminal()
|
|
|
|
{
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
return m_openTerminal;
|
|
|
|
}
|
2023-02-23 12:47:39 +01:00
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
private:
|
|
|
|
Hooks::OpenTerminal m_openTerminal;
|
|
|
|
Hooks::CreateTerminalProcessInterface m_createTerminalProcessInterface;
|
|
|
|
|
|
|
|
QMutex m_mutex;
|
|
|
|
QList<QPair<QString, Hooks::CallbackSet>> m_callbackSets;
|
2023-02-23 12:47:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Hooks &Hooks::instance()
|
|
|
|
{
|
|
|
|
static Hooks manager;
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hooks::Hooks()
|
|
|
|
: d(new HooksPrivate())
|
|
|
|
{}
|
|
|
|
|
|
|
|
Hooks::~Hooks() = default;
|
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
void Hooks::openTerminal(const OpenTerminalParameters ¶meters) const
|
2023-02-23 12:47:39 +01:00
|
|
|
{
|
2023-03-22 16:54:53 +01:00
|
|
|
d->openTerminal()(parameters);
|
2023-02-23 12:47:39 +01:00
|
|
|
}
|
2023-02-25 10:47:21 +01:00
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
ProcessInterface *Hooks::createTerminalProcessInterface() const
|
2023-02-23 12:47:39 +01:00
|
|
|
{
|
2023-03-22 16:54:53 +01:00
|
|
|
return d->createTerminalProcessInterface()();
|
2023-02-23 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
void Hooks::addCallbackSet(const QString &name, const CallbackSet &callbackSet)
|
|
|
|
{
|
|
|
|
d->addCallbackSet(name, callbackSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hooks::removeCallbackSet(const QString &name)
|
|
|
|
{
|
|
|
|
d->removeCallbackSet(name);
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:47:39 +01:00
|
|
|
} // namespace Utils::Terminal
|