2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-12-25 23:38:53 +08:00
|
|
|
#include "environmentprogramswidget.h"
|
|
|
|
#include "ui_environmentprogramswidget.h"
|
|
|
|
#include "../settings.h"
|
|
|
|
#include "../iconsmanager.h"
|
2021-12-27 22:46:54 +08:00
|
|
|
#include "../systemconsts.h"
|
2023-09-05 19:14:08 +08:00
|
|
|
#include "../compiler/executablerunner.h"
|
2021-12-25 23:38:53 +08:00
|
|
|
|
|
|
|
#include <QFileDialog>
|
2023-09-20 10:52:54 +08:00
|
|
|
#include <QMessageBox>
|
2021-12-25 23:38:53 +08:00
|
|
|
|
|
|
|
EnvironmentProgramsWidget::EnvironmentProgramsWidget(const QString& name, const QString& group, QWidget *parent) :
|
|
|
|
SettingsWidget(name,group,parent),
|
|
|
|
ui(new Ui::EnvironmentProgramsWidget)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-09-20 10:52:54 +08:00
|
|
|
ui->labelCmdPreviewResult->setFont(QFont(DEFAULT_MONO_FONT));
|
|
|
|
#ifndef Q_OS_WINDOWS
|
|
|
|
ui->grpUseCustomTerminal->setCheckable(false);
|
2023-09-05 19:14:08 +08:00
|
|
|
#endif
|
2021-12-25 23:38:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EnvironmentProgramsWidget::~EnvironmentProgramsWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2023-09-20 10:52:54 +08:00
|
|
|
auto EnvironmentProgramsWidget::resolveExecArguments(const QString &terminalPath, const QString &argsPattern)
|
|
|
|
-> std::tuple<QString, QStringList, std::unique_ptr<QTemporaryFile>>
|
2023-09-05 19:14:08 +08:00
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
QString shell = defaultShell();
|
|
|
|
QStringList payloadArgs{shell, "-c", "echo hello; sleep 3"};
|
2023-09-21 16:31:22 +08:00
|
|
|
return wrapCommandForTerminalEmulator(terminalPath, argsPattern, payloadArgs);
|
2023-09-05 19:14:08 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 10:52:54 +08:00
|
|
|
void EnvironmentProgramsWidget::updateCommandPreview(const QString &terminalPath, const QString &argsPattern)
|
2023-09-05 19:14:08 +08:00
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
auto [filename, arguments, fileOwner] = resolveExecArguments(terminalPath, argsPattern);
|
|
|
|
for (auto &arg : arguments)
|
|
|
|
arg = escapeArgument(arg, false);
|
|
|
|
|
2023-09-21 16:31:22 +08:00
|
|
|
ui->labelCmdPreviewResult->setPlainText(escapeArgument(filename, true) + " " + arguments.join(' '));
|
2023-09-20 10:52:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentProgramsWidget::autoDetectAndUpdateArgumentsPattern(const QString &terminalPath)
|
|
|
|
{
|
|
|
|
const QString &executable = QFileInfo(terminalPath).fileName();
|
2023-09-21 08:17:07 +08:00
|
|
|
const QString &pattern = pSettings->environment().queryPredefinedTerminalArgumentsPattern(executable);
|
|
|
|
if (!pattern.isEmpty())
|
|
|
|
ui->txtArgsPattern->setText(pattern);
|
2023-09-20 10:52:54 +08:00
|
|
|
else
|
|
|
|
QMessageBox::warning(nullptr,
|
|
|
|
QObject::tr("Auto Detection Failed"),
|
|
|
|
QObject::tr("Failed to detect terminal arguments pattern for “%1”.").arg(executable),
|
|
|
|
QMessageBox::Ok);
|
2023-09-05 19:14:08 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 23:38:53 +08:00
|
|
|
void EnvironmentProgramsWidget::doLoad()
|
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
ui->grpUseCustomTerminal->setChecked(pSettings->environment().useCustomTerminal());
|
|
|
|
#endif
|
2021-12-25 23:38:53 +08:00
|
|
|
ui->txtTerminal->setText(pSettings->environment().terminalPath());
|
2023-09-20 10:52:54 +08:00
|
|
|
ui->txtArgsPattern->setText(pSettings->environment().terminalArgumentsPattern());
|
2021-12-25 23:38:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentProgramsWidget::doSave()
|
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
pSettings->environment().setUseCustomTerminal(ui->grpUseCustomTerminal->isChecked());
|
|
|
|
#endif
|
2021-12-25 23:38:53 +08:00
|
|
|
pSettings->environment().setTerminalPath(ui->txtTerminal->text());
|
2023-09-20 10:52:54 +08:00
|
|
|
pSettings->environment().setTerminalArgumentsPattern(ui->txtArgsPattern->text());
|
2021-12-25 23:38:53 +08:00
|
|
|
pSettings->environment().save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentProgramsWidget::updateIcons(const QSize &)
|
|
|
|
{
|
|
|
|
pIconsManager->setIcon(ui->btnChooseTerminal,IconsManager::ACTION_FILE_OPEN_FOLDER);
|
2023-09-20 10:52:54 +08:00
|
|
|
pIconsManager->setIcon(ui->btnAutoDetectArgsPattern,IconsManager::ACTION_EDIT_SEARCH);
|
|
|
|
pIconsManager->setIcon(ui->btnTest,IconsManager::ACTION_RUN_RUN);
|
2021-12-25 23:38:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentProgramsWidget::on_btnChooseTerminal_clicked()
|
|
|
|
{
|
|
|
|
QString filename = QFileDialog::getOpenFileName(
|
|
|
|
this,
|
|
|
|
tr("Choose Terminal Program"),
|
|
|
|
QString(),
|
2021-12-27 22:46:54 +08:00
|
|
|
tr("All files (%1)").arg(ALL_FILE_WILDCARD));
|
2021-12-25 23:38:53 +08:00
|
|
|
if (!filename.isEmpty() && fileExists(filename) ) {
|
|
|
|
ui->txtTerminal->setText(filename);
|
2023-09-20 10:52:54 +08:00
|
|
|
autoDetectAndUpdateArgumentsPattern(filename);
|
2021-12-25 23:38:53 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-05 19:14:08 +08:00
|
|
|
|
|
|
|
void EnvironmentProgramsWidget::on_txtTerminal_textChanged(const QString &terminalPath)
|
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
const QString &argsPattern = ui->txtArgsPattern->text();
|
|
|
|
updateCommandPreview(terminalPath, argsPattern);
|
2023-09-05 19:14:08 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 10:52:54 +08:00
|
|
|
void EnvironmentProgramsWidget::on_txtArgsPattern_textChanged(const QString &argsPattern)
|
2023-09-05 19:14:08 +08:00
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
const QString &terminalPath = ui->txtTerminal->text();
|
|
|
|
updateCommandPreview(terminalPath, argsPattern);
|
2023-09-05 19:14:08 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 10:52:54 +08:00
|
|
|
void EnvironmentProgramsWidget::on_btnAutoDetectArgsPattern_clicked()
|
2023-09-05 19:14:08 +08:00
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
const QString &terminalPath = ui->txtTerminal->text();
|
|
|
|
autoDetectAndUpdateArgumentsPattern(terminalPath);
|
2023-09-05 19:14:08 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 10:52:54 +08:00
|
|
|
void EnvironmentProgramsWidget::on_btnTest_clicked()
|
2023-09-05 19:14:08 +08:00
|
|
|
{
|
2023-09-20 10:52:54 +08:00
|
|
|
const QString &terminalPath = ui->txtTerminal->text();
|
|
|
|
const QString &argsPattern = ui->txtArgsPattern->text();
|
|
|
|
auto [filename, arguments, fileOwner] = resolveExecArguments(terminalPath, argsPattern);
|
|
|
|
ExecutableRunner runner(filename, arguments, "", nullptr);
|
|
|
|
runner.start();
|
|
|
|
runner.wait();
|
2023-09-05 19:14:08 +08:00
|
|
|
}
|