Terminal: Add remote devices to shell selection

Change-Id: Id28471aaf3e91ef493f48ab28207230f3fb513c2
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-02-25 10:47:21 +01:00
parent 1381bd0963
commit d4ac8aeaa6
7 changed files with 199 additions and 74 deletions

View File

@@ -3,6 +3,7 @@
#include "terminalpane.h"
#include "shellmodel.h"
#include "terminaltr.h"
#include "terminalwidget.h"
@@ -13,7 +14,6 @@
#include <utils/environment.h>
#include <utils/utilsicons.h>
#include <QFileIconProvider>
#include <QMenu>
#include <QStandardPaths>
@@ -21,59 +21,6 @@ namespace Terminal {
using namespace Utils;
FilePaths availableShells()
{
if (Utils::HostOsInfo::isWindowsHost()) {
FilePaths shells;
FilePath comspec = FilePath::fromUserInput(qtcEnvironmentVariable("COMSPEC"));
shells << comspec;
if (comspec.fileName() != "cmd.exe") {
FilePath cmd = FilePath::fromUserInput(QStandardPaths::findExecutable("cmd.exe"));
shells << cmd;
}
FilePath powershell = FilePath::fromUserInput(
QStandardPaths::findExecutable("powershell.exe"));
if (powershell.exists())
shells << powershell;
FilePath bash = FilePath::fromUserInput(QStandardPaths::findExecutable("bash.exe"));
if (bash.exists())
shells << bash;
FilePath git_bash = FilePath::fromUserInput(QStandardPaths::findExecutable("git.exe"));
if (git_bash.exists())
shells << git_bash.parentDir().parentDir().pathAppended("usr/bin/bash.exe");
FilePath msys2_bash = FilePath::fromUserInput(QStandardPaths::findExecutable("msys2.exe"));
if (msys2_bash.exists())
shells << msys2_bash.parentDir().pathAppended("usr/bin/bash.exe");
return shells;
} else {
FilePath shellsFile = FilePath::fromString("/etc/shells");
const auto shellFileContent = shellsFile.fileContents();
QTC_ASSERT_EXPECTED(shellFileContent, return {});
QString shellFileContentString = QString::fromUtf8(*shellFileContent);
// Filter out comments ...
const QStringList lines
= Utils::filtered(shellFileContentString.split('\n', Qt::SkipEmptyParts),
[](const QString &line) { return !line.trimmed().startsWith('#'); });
// Convert lines to file paths ...
const FilePaths shells = Utils::transform(lines, [](const QString &line) {
return FilePath::fromUserInput(line.trimmed());
});
// ... and filter out non-existing shells.
return Utils::filtered(shells, [](const FilePath &shell) { return shell.exists(); });
}
}
TerminalPane::TerminalPane(QObject *parent)
: Core::IOutputPane(parent)
{
@@ -92,32 +39,30 @@ TerminalPane::TerminalPane(QObject *parent)
removeTab(m_tabWidget->currentIndex());
});
//Core::Command *cmd = Core::ActionManager::registerAction(m_newTerminal, Constants::STOP);
//cmd->setDescription(m_newTerminal->toolTip());
m_newTerminalButton = new QToolButton();
QMenu *shellMenu = new QMenu(m_newTerminalButton);
Internal::ShellModel *shellModel = new Internal::ShellModel(shellMenu);
connect(shellMenu, &QMenu::aboutToShow, shellMenu, [shellMenu, shellModel, pane = this] {
shellMenu->clear();
const FilePaths shells = availableShells();
const auto addItems = [shellMenu, pane](const QList<Internal::ShellModelItem> &items) {
for (const Internal::ShellModelItem &item : items) {
QAction *action = new QAction(item.icon, item.name, shellMenu);
QFileIconProvider iconProvider;
connect(action, &QAction::triggered, action, [item, pane]() {
pane->openTerminal(item.openParameters);
});
// Create an action for each available shell ...
for (const FilePath &shell : shells) {
const QIcon icon = iconProvider.icon(shell.toFileInfo());
shellMenu->addAction(action);
}
};
QAction *action = new QAction(icon, shell.toUserOutput(), shellMenu);
action->setData(shell.toVariant());
shellMenu->addAction(action);
}
connect(shellMenu, &QMenu::triggered, this, [this](QAction *action) {
openTerminal(
Utils::Terminal::OpenTerminalParameters{CommandLine{FilePath::fromVariant(action->data()), {}},
std::nullopt,
std::nullopt,
Utils::Terminal::ExitBehavior::Close});
addItems(shellModel->local());
shellMenu->addSection(Tr::tr("Devices"));
addItems(shellModel->remote());
});
m_newTerminal.setMenu(shellMenu);
m_newTerminalButton->setDefaultAction(&m_newTerminal);
@@ -132,6 +77,8 @@ void TerminalPane::openTerminal(const Utils::Terminal::OpenTerminalParameters &p
m_tabWidget->setCurrentIndex(
m_tabWidget->addTab(new TerminalWidget(m_tabWidget, parameters), Tr::tr("Terminal")));
m_tabWidget->currentWidget()->setFocus();
m_closeTerminal.setEnabled(m_tabWidget->count() > 1);
emit navigateStateUpdate();
}