Move TerminalCommand into a separate file

This will ease the ConsoleProcess and QtcProcess
unification.

Change-Id: Idda9ad393d184088c3e3a734389761d7176bd0c0
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2022-01-21 14:38:50 +01:00
parent 74f522e0de
commit 1f0ecd600e
8 changed files with 263 additions and 186 deletions

View File

@@ -33,7 +33,6 @@
#include <qtsingleapplication.h>
#include <utils/algorithm.h>
#include <utils/consoleprocess.h>
#include <utils/environment.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
@@ -41,6 +40,7 @@
#include <utils/qtcsettings.h>
#include <utils/singleton.h>
#include <utils/temporarydirectory.h>
#include <utils/terminalcommand.h>
#include <QDebug>
#include <QDir>
@@ -552,7 +552,7 @@ int main(int argc, char **argv)
QSettings::SystemScope,
QLatin1String(Core::Constants::IDE_SETTINGSVARIANT_STR),
QLatin1String(Core::Constants::IDE_CASED_ID));
Utils::ConsoleProcess::setSettings(settings);
Utils::TerminalCommand::setSettings(settings);
loadFonts();
if (Utils::HostOsInfo::isWindowsHost()

View File

@@ -164,6 +164,7 @@ add_qtc_library(Utils
templateengine.cpp templateengine.h
temporarydirectory.cpp temporarydirectory.h
temporaryfile.cpp temporaryfile.h
terminalcommand.cpp terminalcommand.h
textfieldcheckbox.cpp textfieldcheckbox.h
textfieldcombobox.cpp textfieldcombobox.h
textfileformat.cpp textfileformat.h

View File

@@ -25,22 +25,20 @@
#include "consoleprocess.h"
#include <utils/algorithm.h>
#include <utils/environment.h>
#include <utils/hostosinfo.h>
#include <utils/commandline.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/terminalcommand.h>
#include <utils/winutils.h>
#include <QAbstractEventDispatcher>
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QLocalServer>
#include <QLocalSocket>
#include <QRegularExpression>
#include <QSettings>
#include <QTemporaryFile>
#include <QTextCodec>
#include <QTimer>
@@ -118,18 +116,6 @@ static QString msgCannotExecute(const QString & p, const QString &why)
return ConsoleProcess::tr("Cannot execute \"%1\": %2").arg(p, why);
}
static QSettings *s_settings = nullptr;
// TerminalCommand
TerminalCommand::TerminalCommand(const QString &command, const QString &openArgs, const QString &executeArgs, bool needsQuotes)
: command(command)
, openArgs(openArgs)
, executeArgs(executeArgs)
, needsQuotes(needsQuotes)
{
}
// ConsoleProcessPrivate
class ConsoleProcessPrivate
@@ -196,126 +182,6 @@ const CommandLine &ConsoleProcess::commandLine() const
return d->m_commandLine;
}
void ConsoleProcess::setSettings(QSettings *settings)
{
s_settings = settings;
}
Q_GLOBAL_STATIC_WITH_ARGS(const QVector<TerminalCommand>, knownTerminals, (
{
{"x-terminal-emulator", "", "-e"},
{"xdg-terminal", "", "", true},
{"xterm", "", "-e"},
{"aterm", "", "-e"},
{"Eterm", "", "-e"},
{"rxvt", "", "-e"},
{"urxvt", "", "-e"},
{"xfce4-terminal", "", "-x"},
{"konsole", "--separate --workdir .", "-e"},
{"gnome-terminal", "", "--"}
}));
TerminalCommand ConsoleProcess::defaultTerminalEmulator()
{
static TerminalCommand defaultTerm;
if (defaultTerm.command.isEmpty()) {
if (HostOsInfo::isMacHost()) {
const QString termCmd = QCoreApplication::applicationDirPath()
+ "/../Resources/scripts/openTerminal.py";
if (QFileInfo::exists(termCmd))
defaultTerm = {termCmd, "", ""};
else
defaultTerm = {"/usr/X11/bin/xterm", "", "-e"};
} else if (HostOsInfo::isAnyUnixHost()) {
defaultTerm = {"xterm", "", "-e"};
const Environment env = Environment::systemEnvironment();
for (const TerminalCommand &term : *knownTerminals) {
const QString result = env.searchInPath(term.command).toString();
if (!result.isEmpty()) {
defaultTerm = {result, term.openArgs, term.executeArgs, term.needsQuotes};
break;
}
}
}
}
return defaultTerm;
}
QVector<TerminalCommand> ConsoleProcess::availableTerminalEmulators()
{
QVector<TerminalCommand> result;
if (HostOsInfo::isAnyUnixHost()) {
const Environment env = Environment::systemEnvironment();
for (const TerminalCommand &term : *knownTerminals) {
const QString command = env.searchInPath(term.command).toString();
if (!command.isEmpty())
result.push_back({command, term.openArgs, term.executeArgs});
}
// sort and put default terminal on top
const TerminalCommand defaultTerm = defaultTerminalEmulator();
result.removeAll(defaultTerm);
sort(result);
result.prepend(defaultTerm);
}
return result;
}
const char kTerminalVersion[] = "4.8";
const char kTerminalVersionKey[] = "General/Terminal/SettingsVersion";
const char kTerminalCommandKey[] = "General/Terminal/Command";
const char kTerminalOpenOptionsKey[] = "General/Terminal/OpenOptions";
const char kTerminalExecuteOptionsKey[] = "General/Terminal/ExecuteOptions";
TerminalCommand ConsoleProcess::terminalEmulator()
{
if (s_settings && HostOsInfo::isAnyUnixHost()) {
if (s_settings->value(kTerminalVersionKey).toString() == kTerminalVersion) {
if (s_settings->contains(kTerminalCommandKey))
return {s_settings->value(kTerminalCommandKey).toString(),
s_settings->value(kTerminalOpenOptionsKey).toString(),
s_settings->value(kTerminalExecuteOptionsKey).toString()};
} else {
// TODO remove reading of old settings some time after 4.8
const QString value = s_settings->value("General/TerminalEmulator").toString().trimmed();
if (!value.isEmpty()) {
// split off command and options
const QStringList splitCommand = ProcessArgs::splitArgs(value);
if (QTC_GUARD(!splitCommand.isEmpty())) {
const QString command = splitCommand.first();
const QStringList quotedArgs = Utils::transform(splitCommand.mid(1),
&ProcessArgs::quoteArgUnix);
const QString options = quotedArgs.join(' ');
return {command, "", options};
}
}
}
}
return defaultTerminalEmulator();
}
void ConsoleProcess::setTerminalEmulator(const TerminalCommand &term)
{
if (s_settings && HostOsInfo::isAnyUnixHost()) {
s_settings->setValue(kTerminalVersionKey, kTerminalVersion);
if (term == defaultTerminalEmulator()) {
s_settings->remove(kTerminalCommandKey);
s_settings->remove(kTerminalOpenOptionsKey);
s_settings->remove(kTerminalExecuteOptionsKey);
} else {
s_settings->setValue(kTerminalCommandKey, term.command);
s_settings->setValue(kTerminalOpenOptionsKey, term.openArgs);
s_settings->setValue(kTerminalExecuteOptionsKey, term.executeArgs);
}
}
}
static QString quoteWinCommand(const QString &program)
{
const QChar doubleQuote = QLatin1Char('"');
@@ -373,7 +239,6 @@ QString createWinCommandline(const QString &program, const QString &args)
return programName;
}
bool ConsoleProcess::startTerminalEmulator(const QString &workingDir, const Environment &env)
{
#ifdef Q_OS_WIN
@@ -405,7 +270,7 @@ bool ConsoleProcess::startTerminalEmulator(const QString &workingDir, const Envi
return success;
#else
const TerminalCommand term = terminalEmulator();
const TerminalCommand term = TerminalCommand::terminalEmulator();
QProcess process;
process.setProgram(term.command);
process.setArguments(ProcessArgs::splitArgs(term.openArgs));
@@ -567,7 +432,7 @@ void ConsoleProcess::start()
}
ProcessArgs::SplitError qerr;
const TerminalCommand terminal = terminalEmulator();
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
const ProcessArgs terminalArgs = ProcessArgs::prepareArgs(terminal.executeArgs,
&qerr,
HostOsInfo::hostOs(),
@@ -998,20 +863,4 @@ void ConsoleProcess::emitError(QProcess::ProcessError err, const QString &errorS
emit errorOccurred(err);
}
bool TerminalCommand::operator==(const TerminalCommand &other) const
{
return other.command == command && other.openArgs == openArgs
&& other.executeArgs == executeArgs;
}
bool TerminalCommand::operator<(const TerminalCommand &other) const
{
if (command == other.command) {
if (openArgs == other.openArgs)
return executeArgs < other.executeArgs;
return openArgs < other.openArgs;
}
return command < other.command;
}
} // Utils

View File

@@ -28,11 +28,6 @@
#include "utils_global.h"
#include <QProcess>
#include <QVector>
QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace Utils {
@@ -40,21 +35,6 @@ class CommandLine;
class Environment;
class FilePath;
class QTCREATOR_UTILS_EXPORT TerminalCommand
{
public:
TerminalCommand() = default;
TerminalCommand(const QString &command, const QString &openArgs, const QString &executeArgs, bool needsQuotes = false);
bool operator==(const TerminalCommand &other) const;
bool operator<(const TerminalCommand &other) const;
QString command;
QString openArgs;
QString executeArgs;
bool needsQuotes = false;
};
class QTCREATOR_UTILS_EXPORT ConsoleProcess : public QObject
{
Q_OBJECT
@@ -99,11 +79,6 @@ public:
int exitCode() const;
QProcess::ExitStatus exitStatus() const;
static void setSettings(QSettings *settings);
static TerminalCommand defaultTerminalEmulator();
static QVector<TerminalCommand> availableTerminalEmulators();
static TerminalCommand terminalEmulator();
static void setTerminalEmulator(const TerminalCommand &term);
static bool startTerminalEmulator(const QString &workingDir, const Utils::Environment &env);
signals:
@@ -131,5 +106,3 @@ private:
};
} // Utils
Q_DECLARE_METATYPE(Utils::TerminalCommand)

View File

@@ -0,0 +1,186 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "terminalcommand.h"
#include <utils/algorithm.h>
#include <utils/commandline.h>
#include <utils/environment.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
#include <QFileInfo>
#include <QSettings>
namespace Utils {
static QSettings *s_settings = nullptr;
TerminalCommand::TerminalCommand(const QString &command, const QString &openArgs,
const QString &executeArgs, bool needsQuotes)
: command(command)
, openArgs(openArgs)
, executeArgs(executeArgs)
, needsQuotes(needsQuotes)
{
}
bool TerminalCommand::operator==(const TerminalCommand &other) const
{
return other.command == command && other.openArgs == openArgs
&& other.executeArgs == executeArgs;
}
bool TerminalCommand::operator<(const TerminalCommand &other) const
{
if (command == other.command) {
if (openArgs == other.openArgs)
return executeArgs < other.executeArgs;
return openArgs < other.openArgs;
}
return command < other.command;
}
void TerminalCommand::setSettings(QSettings *settings)
{
s_settings = settings;
}
Q_GLOBAL_STATIC_WITH_ARGS(const QVector<TerminalCommand>, knownTerminals, (
{
{"x-terminal-emulator", "", "-e"},
{"xdg-terminal", "", "", true},
{"xterm", "", "-e"},
{"aterm", "", "-e"},
{"Eterm", "", "-e"},
{"rxvt", "", "-e"},
{"urxvt", "", "-e"},
{"xfce4-terminal", "", "-x"},
{"konsole", "--separate --workdir .", "-e"},
{"gnome-terminal", "", "--"}
}));
TerminalCommand TerminalCommand::defaultTerminalEmulator()
{
static TerminalCommand defaultTerm;
if (defaultTerm.command.isEmpty()) {
if (HostOsInfo::isMacHost()) {
const QString termCmd = QCoreApplication::applicationDirPath()
+ "/../Resources/scripts/openTerminal.py";
if (QFileInfo::exists(termCmd))
defaultTerm = {termCmd, "", ""};
else
defaultTerm = {"/usr/X11/bin/xterm", "", "-e"};
} else if (HostOsInfo::isAnyUnixHost()) {
defaultTerm = {"xterm", "", "-e"};
const Environment env = Environment::systemEnvironment();
for (const TerminalCommand &term : *knownTerminals) {
const QString result = env.searchInPath(term.command).toString();
if (!result.isEmpty()) {
defaultTerm = {result, term.openArgs, term.executeArgs, term.needsQuotes};
break;
}
}
}
}
return defaultTerm;
}
QVector<TerminalCommand> TerminalCommand::availableTerminalEmulators()
{
QVector<TerminalCommand> result;
if (HostOsInfo::isAnyUnixHost()) {
const Environment env = Environment::systemEnvironment();
for (const TerminalCommand &term : *knownTerminals) {
const QString command = env.searchInPath(term.command).toString();
if (!command.isEmpty())
result.push_back({command, term.openArgs, term.executeArgs});
}
// sort and put default terminal on top
const TerminalCommand defaultTerm = defaultTerminalEmulator();
result.removeAll(defaultTerm);
sort(result);
result.prepend(defaultTerm);
}
return result;
}
const char kTerminalVersion[] = "4.8";
const char kTerminalVersionKey[] = "General/Terminal/SettingsVersion";
const char kTerminalCommandKey[] = "General/Terminal/Command";
const char kTerminalOpenOptionsKey[] = "General/Terminal/OpenOptions";
const char kTerminalExecuteOptionsKey[] = "General/Terminal/ExecuteOptions";
TerminalCommand TerminalCommand::terminalEmulator()
{
if (s_settings && HostOsInfo::isAnyUnixHost()) {
if (s_settings->value(kTerminalVersionKey).toString() == kTerminalVersion) {
if (s_settings->contains(kTerminalCommandKey))
return {s_settings->value(kTerminalCommandKey).toString(),
s_settings->value(kTerminalOpenOptionsKey).toString(),
s_settings->value(kTerminalExecuteOptionsKey).toString()};
} else {
// TODO remove reading of old settings some time after 4.8
const QString value = s_settings->value("General/TerminalEmulator").toString().trimmed();
if (!value.isEmpty()) {
// split off command and options
const QStringList splitCommand = ProcessArgs::splitArgs(value);
if (QTC_GUARD(!splitCommand.isEmpty())) {
const QString command = splitCommand.first();
const QStringList quotedArgs = Utils::transform(splitCommand.mid(1),
&ProcessArgs::quoteArgUnix);
const QString options = quotedArgs.join(' ');
return {command, "", options};
}
}
}
}
return defaultTerminalEmulator();
}
void TerminalCommand::setTerminalEmulator(const TerminalCommand &term)
{
if (s_settings && HostOsInfo::isAnyUnixHost()) {
s_settings->setValue(kTerminalVersionKey, kTerminalVersion);
if (term == defaultTerminalEmulator()) {
s_settings->remove(kTerminalCommandKey);
s_settings->remove(kTerminalOpenOptionsKey);
s_settings->remove(kTerminalExecuteOptionsKey);
} else {
s_settings->setValue(kTerminalCommandKey, term.command);
s_settings->setValue(kTerminalOpenOptionsKey, term.openArgs);
s_settings->setValue(kTerminalExecuteOptionsKey, term.executeArgs);
}
}
}
} // Utils

View File

@@ -0,0 +1,65 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
#include <QMetaType>
#include <QVector>
QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace Utils {
class Environment;
class QTCREATOR_UTILS_EXPORT TerminalCommand
{
public:
TerminalCommand() = default;
TerminalCommand(const QString &command, const QString &openArgs,
const QString &executeArgs, bool needsQuotes = false);
bool operator==(const TerminalCommand &other) const;
bool operator<(const TerminalCommand &other) const;
QString command;
QString openArgs;
QString executeArgs;
bool needsQuotes = false;
static void setSettings(QSettings *settings);
static TerminalCommand defaultTerminalEmulator();
static QVector<TerminalCommand> availableTerminalEmulators();
static TerminalCommand terminalEmulator();
static void setTerminalEmulator(const TerminalCommand &term);
};
} // Utils
Q_DECLARE_METATYPE(Utils::TerminalCommand)

View File

@@ -292,6 +292,8 @@ Project {
"temporarydirectory.h",
"temporaryfile.cpp",
"temporaryfile.h",
"terminalcommand.cpp"
"terminalcommand.h"
"textfieldcheckbox.cpp",
"textfieldcheckbox.h",
"textfieldcombobox.cpp",

View File

@@ -43,6 +43,7 @@
#include <utils/environment.h>
#include <utils/environmentdialog.h>
#include <utils/hostosinfo.h>
#include <utils/terminalcommand.h>
#include <utils/unixutils.h>
#include <QCoreApplication>
@@ -90,10 +91,10 @@ public:
m_ui.reloadBehavior->setCurrentIndex(EditorManager::reloadSetting());
if (HostOsInfo::isAnyUnixHost()) {
const QVector<TerminalCommand> availableTerminals = ConsoleProcess::availableTerminalEmulators();
const QVector<TerminalCommand> availableTerminals = TerminalCommand::availableTerminalEmulators();
for (const TerminalCommand &term : availableTerminals)
m_ui.terminalComboBox->addItem(term.command, QVariant::fromValue(term));
updateTerminalUi(ConsoleProcess::terminalEmulator());
updateTerminalUi(TerminalCommand::terminalEmulator());
connect(m_ui.terminalComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
@@ -265,7 +266,7 @@ void SystemSettingsWidget::apply()
QtcSettings *settings = ICore::settings();
EditorManager::setReloadSetting(IDocument::ReloadSetting(m_ui.reloadBehavior->currentIndex()));
if (HostOsInfo::isAnyUnixHost()) {
ConsoleProcess::setTerminalEmulator({m_ui.terminalComboBox->lineEdit()->text(),
TerminalCommand::setTerminalEmulator({m_ui.terminalComboBox->lineEdit()->text(),
m_ui.terminalOpenArgs->text(),
m_ui.terminalExecuteArgs->text()});
if (!HostOsInfo::isMacHost()) {