From a987bf24570e097dd040d5e922b7de9228c60eed Mon Sep 17 00:00:00 2001
From: xiaoming <2014500726@smail.xtu.edu.cn>
Date: Thu, 22 Aug 2024 13:36:54 +0800
Subject: [PATCH] Add custom Local Shell Profile path feature for Windows
Signed-off-by: xiaoming <2014500726@smail.xtu.edu.cn>
---
CHANGELOG.md | 2 +
docs/changelog.md | 1 +
.../globaloptionsadvancedwidget.cpp | 11 +++++
.../globaloptionsadvancedwidget.ui | 43 +++++++++++++++++--
src/globaloptions/globaloptionswindow.cpp | 34 +++++++++++++++
src/globaloptions/globaloptionswindow.h | 3 ++
src/mainwindow.cpp | 10 ++++-
src/sessionswindow/sessionswindow.cpp | 16 ++++---
src/sessionswindow/sessionswindow.h | 6 +--
9 files changed, 113 insertions(+), 13 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f863ba5d..3591a22e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ en-US:
- Add custom color feature to the highlight
- Allow editing the text to be pasted when confirming multiple lines of paste
- Allow the tab title to display a custom session name
+- Add custom Local Shell Profile path feature for Windows
- Fix the issue that clicking the new tab button in split screen mode may not create the session correctly or be located under the wrong tab group
- Fix the issue that the SSH connection cannot be reconnected by tapping the Enter key in some cases
- Fix the issue that the target session object is inaccurate when locking/unlocking the session
@@ -25,6 +26,7 @@ zh-CN:
- 高亮功能增加自定义颜色功能
- 多行粘贴确认时允许编辑待粘贴文本
- 标签栏标题允许显示自定义会话名称
+- Windows增加自定义Local Shell Profile路径功能
- 修复分屏模式下某些情况点击新标签按钮会话未正确创建或位于错误的标签页组下
- 修复ssh连接部分情况下无法通过敲击回车键发起重连的问题
- 修复锁定/解锁会话时目标会话对象不准确
diff --git a/docs/changelog.md b/docs/changelog.md
index df9eb9cf..4c8495b1 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -11,6 +11,7 @@
- Add custom color feature to the highlight
- Allow editing the text to be pasted when confirming multiple lines of paste
- Allow the tab title to display a custom session name
+- Add custom Local Shell Profile path feature for Windows
- Fix the issue that clicking the new tab button in split screen mode may not create the session correctly or be located under the wrong tab group
- Fix the issue that the SSH connection cannot be reconnected by tapping the Enter key in some cases
- Fix the issue that the target session object is inaccurate when locking/unlocking the session
diff --git a/src/globaloptions/globaloptionsadvancedwidget.cpp b/src/globaloptions/globaloptionsadvancedwidget.cpp
index cebce97b..cccc1ab9 100644
--- a/src/globaloptions/globaloptionsadvancedwidget.cpp
+++ b/src/globaloptions/globaloptionsadvancedwidget.cpp
@@ -31,6 +31,17 @@ GlobalOptionsAdvancedWidget::GlobalOptionsAdvancedWidget(QWidget *parent) :
#else
ui->checkBoxEnableCtrlC->setVisible(false);
#endif
+#if defined(Q_OS_WIN)
+ ui->labelPowerShellProfile->setVisible(true);
+ ui->lineEditPowerShellProfile->setVisible(true);
+ ui->toolButtonPowerShellProfile->setVisible(true);
+ ui->pushButtonPowerShellProfile->setVisible(true);
+#else
+ ui->labelPowerShellProfile->setVisible(false);
+ ui->lineEditPowerShellProfile->setVisible(false);
+ ui->toolButtonPowerShellProfile->setVisible(false);
+ ui->pushButtonPowerShellProfile->setVisible(false);
+#endif
}
GlobalOptionsAdvancedWidget::~GlobalOptionsAdvancedWidget()
diff --git a/src/globaloptions/globaloptionsadvancedwidget.ui b/src/globaloptions/globaloptionsadvancedwidget.ui
index 437e9f5e..d2224f0b 100644
--- a/src/globaloptions/globaloptionsadvancedwidget.ui
+++ b/src/globaloptions/globaloptionsadvancedwidget.ui
@@ -82,6 +82,41 @@
+ -
+
+
-
+
+
+ PowerShell Profile
+
+
+
+ -
+
+
+ Set the PowerShell profile path
+
+
+
+ -
+
+
+ Choose the PowerShell profile path
+
+
+ ...
+
+
+
+ -
+
+
+ Open
+
+
+
+
+
-
@@ -186,12 +221,12 @@
-
-
- Enable Ctrl+C for copy mode
-
Enable Ctrl+C for copy when characters are selected
+
+ Enable Ctrl+C for copy mode
+
-
@@ -217,7 +252,7 @@
-
- Qt::Vertical
+ Qt::Orientation::Vertical
diff --git a/src/globaloptions/globaloptionswindow.cpp b/src/globaloptions/globaloptionswindow.cpp
index ed1bd3f3..5f3aa610 100644
--- a/src/globaloptions/globaloptionswindow.cpp
+++ b/src/globaloptions/globaloptionswindow.cpp
@@ -157,6 +157,9 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) :
globalOptionsTerminalWidget->ui->checkBoxEcho->setChecked(settings.value("Echo", false).toBool());
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->setChecked(settings.value("EnableCtrlC", false).toBool());
+#endif
+#if defined(Q_OS_WIN)
+ globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(settings.value("PowerShellProfile", QApplication::applicationDirPath() + "/Profile.ps1").toString());
#endif
QString defaultLocalShell = settings.value("DefaultLocalShell",
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
@@ -348,6 +351,24 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) :
#endif
}
});
+#if defined(Q_OS_WIN)
+ connect(globalOptionsAdvancedWidget->ui->toolButtonPowerShellProfile, &QToolButton::clicked, this, [&](){
+ QString profile = FileDialog::getOpenFileName(this, tr("Select PowerShell Profile"), globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text(), tr("PowerShell Files (*.ps1)"));
+ if (!profile.isEmpty()) {
+ QFileInfo profileInfo(profile);
+ if(!profileInfo.exists() || !profileInfo.isFile()) {
+ QMessageBox::warning(this, tr("Warning"), tr("The PowerShell Profile is not a valid file!"));
+ return;
+ }
+ globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(profile);
+ } else {
+ globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(QApplication::applicationDirPath() + "/Profile.ps1");
+ }
+ });
+ connect(globalOptionsAdvancedWidget->ui->pushButtonPowerShellProfile, &QPushButton::clicked, this, [&](){
+ QDesktopServices::openUrl(QUrl::fromLocalFile(globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text()));
+ });
+#endif
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &GlobalOptionsWindow::buttonBoxAccepted);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &GlobalOptionsWindow::buttonBoxRejected);
@@ -627,6 +648,13 @@ bool GlobalOptionsWindow::getEnableCtrlC(void)
}
#endif
+#if defined(Q_OS_WIN)
+QString GlobalOptionsWindow::getPowerShellProfile(void)
+{
+ return globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text();
+}
+#endif
+
void GlobalOptionsWindow::buttonBoxAccepted(void)
{
GlobalSetting settings;
@@ -684,6 +712,9 @@ void GlobalOptionsWindow::buttonBoxAccepted(void)
settings.setValue("Echo", globalOptionsTerminalWidget->ui->checkBoxEcho->isChecked());
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
settings.setValue("EnableCtrlC", globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->isChecked());
+#endif
+#if defined(Q_OS_WIN)
+ settings.setValue("PowerShellProfile", globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text());
#endif
QString defaultLocalShell = globalOptionsAdvancedWidget->ui->lineEditDefaultLocalShell->text();
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
@@ -768,6 +799,9 @@ void GlobalOptionsWindow::buttonBoxRejected(void)
globalOptionsTerminalWidget->ui->checkBoxEcho->setChecked(settings.value("Echo", false).toBool());
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->setChecked(settings.value("EnableCtrlC", false).toBool());
+#endif
+#if defined(Q_OS_WIN)
+ globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(settings.value("PowerShellProfile", QApplication::applicationDirPath() + "/Profile.ps1").toString());
#endif
globalOptionsAdvancedWidget->ui->lineEditDefaultLocalShell->setText(settings.value("DefaultLocalShell",
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
diff --git a/src/globaloptions/globaloptionswindow.h b/src/globaloptions/globaloptionswindow.h
index cdfde227..5aaed2e3 100644
--- a/src/globaloptions/globaloptionswindow.h
+++ b/src/globaloptions/globaloptionswindow.h
@@ -78,6 +78,9 @@ class GlobalOptionsWindow : public QDialog
QColor getCursorColor(void);
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
bool getEnableCtrlC(void);
+#endif
+#if defined(Q_OS_WIN)
+ QString getPowerShellProfile(void);
#endif
bool updateColorButtons(const QString &text);
void switchTheme(void);
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index feb0eced..4ddce5fb 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -4190,7 +4190,11 @@ QString CentralWidget::startLocalShellSession(MainWidgetGroup *group, int groupI
sessionsWindow->setName(name);
QFileInfo workingDirectoryInfo(workingDirectory);
sessionsWindow->setWorkingDirectory(workingDirectoryInfo.isDir()?workingDirectory:QDir::homePath());
+#if defined(Q_OS_WIN)
+ sessionsWindow->startLocalShellSession(command,globalOptionsWindow->getPowerShellProfile());
+#else
sessionsWindow->startLocalShellSession(command);
+#endif
sessionList.push_back(sessionsWindow);
connect(sessionsWindow, &SessionsWindow::titleChanged, this, [=](int title,const QString& newTitle){
if(title == 0 || title == 2) {
@@ -4241,7 +4245,7 @@ QString CentralWidget::startWslSession(MainWidgetGroup *group, int groupIndex, c
sessionsWindow->setName(name);
QFileInfo workingDirectoryInfo(workingDirectory);
sessionsWindow->setWorkingDirectory(workingDirectoryInfo.isDir()?workingDirectory:QDir::homePath());
- sessionsWindow->startLocalShellSession(command,SessionsWindow::WSL);
+ sessionsWindow->startLocalShellSession(command,globalOptionsWindow->getPowerShellProfile(),SessionsWindow::WSL);
sessionList.push_back(sessionsWindow);
connect(sessionsWindow, &SessionsWindow::titleChanged, this, [=](int title,const QString& newTitle){
if(title == 0 || title == 2) {
@@ -4539,7 +4543,11 @@ int CentralWidget::cloneTargetSession(MainWidgetGroup *group, QString name,Sessi
checkSessionName(name);
}
sessionsWindowClone->setName(name);
+ #if defined(Q_OS_WIN)
+ sessionsWindowClone->cloneSession(sessionsWindow,globalOptionsWindow->getPowerShellProfile());
+ #else
sessionsWindowClone->cloneSession(sessionsWindow);
+ #endif
sessionList.push_back(sessionsWindowClone);
connect(sessionsWindowClone, &SessionsWindow::titleChanged, this, [=](int title,const QString& newTitle){
if(title == 0 || title == 2) {
diff --git a/src/sessionswindow/sessionswindow.cpp b/src/sessionswindow/sessionswindow.cpp
index 56b80c06..87cf4a63 100644
--- a/src/sessionswindow/sessionswindow.cpp
+++ b/src/sessionswindow/sessionswindow.cpp
@@ -756,11 +756,11 @@ void SessionsWindow::preprocesseData(QByteArray &data) {
}
}
-void SessionsWindow::cloneSession(SessionsWindow *src) {
+void SessionsWindow::cloneSession(SessionsWindow *src, QString profile) {
switch(src->getSessionType()) {
case LocalShell: {
setWorkingDirectory(src->getWorkingDirectory());
- startLocalShellSession(src->m_command, src->getShellType());
+ startLocalShellSession(src->m_command, profile, src->getShellType());
break;
case Telnet:
startTelnetSession(src->m_hostname, src->m_port, src->m_type);
@@ -788,19 +788,20 @@ void SessionsWindow::cloneSession(SessionsWindow *src) {
}
}
-int SessionsWindow::startLocalShellSession(const QString &command, ShellType sTp) {
+int SessionsWindow::startLocalShellSession(const QString &command,QString profile, ShellType sTp) {
QString shellPath;
QStringList args;
shellType = sTp;
if(command.isEmpty()) {
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
+ Q_UNUSED(profile);
GlobalSetting settings;
QString defaultLocalShell = settings.value("Global/Options/DefaultLocalShell", "ENV:SHELL").toString();
if(defaultLocalShell != "ENV:SHELL") {
QFileInfo shellInfo(defaultLocalShell);
if(shellInfo.isExecutable()) {
shellPath = defaultLocalShell;
- }
+ }
}
if(shellPath.isEmpty()) shellPath = qEnvironmentVariable("SHELL");
if(shellPath.isEmpty()) shellPath = "/bin/sh";
@@ -825,8 +826,13 @@ int SessionsWindow::startLocalShellSession(const QString &command, ShellType sTp
"-NoProfile",
"-NoExit",
"-File",
- "\"" + QApplication::applicationDirPath() + "/Profile.ps1\""
};
+ if(profile.isEmpty) {
+ profile = QString("\"") + QApplication::applicationDirPath() + "/Profile.ps1\"";
+ } else {
+ profile = QString("\"") + profile + "\"";
+ }
+ args.append(profile);
break;
}
case WSL:
diff --git a/src/sessionswindow/sessionswindow.h b/src/sessionswindow/sessionswindow.h
index 5ed696d0..e22896c2 100644
--- a/src/sessionswindow/sessionswindow.h
+++ b/src/sessionswindow/sessionswindow.h
@@ -124,11 +124,11 @@ class SessionsWindow : public QObject
SessionsWindow(SessionType tp, QWidget *parent = nullptr);
~SessionsWindow();
- void cloneSession(SessionsWindow *src);
+ void cloneSession(SessionsWindow *src, QString profile = QString());
#if defined(Q_OS_WIN)
- int startLocalShellSession(const QString &command, ShellType sTp = PowerShell);
+ int startLocalShellSession(const QString &command, QString profile = QString(), ShellType sTp = PowerShell);
#else
- int startLocalShellSession(const QString &command, ShellType sTp = UnixShell);
+ int startLocalShellSession(const QString &command, QString profile = QString(), ShellType sTp = UnixShell);
#endif
int startTelnetSession(const QString &hostname, quint16 port, QTelnet::SocketType type);
int startSerialSession(const QString &portName, uint32_t baudRate,