Allow to set environment for "Open Terminal"

Change-Id: If6e82ef8a7bbbaa947b41e0f6746d2b2aa770c20
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Tobias Hunger
2018-08-31 10:54:47 +02:00
parent a1ba378d8c
commit 626665b7a4
5 changed files with 30 additions and 7 deletions

View File

@@ -116,7 +116,8 @@ public:
static void setTerminalEmulator(QSettings *, const TerminalCommand &) {} static void setTerminalEmulator(QSettings *, const TerminalCommand &) {}
#endif #endif
static bool startTerminalEmulator(QSettings *settings, const QString &workingDir); static bool startTerminalEmulator(QSettings *settings, const QString &workingDir,
const Utils::Environment &env);
signals: signals:
void error(QProcess::ProcessError error); void error(QProcess::ProcessError error);

View File

@@ -431,10 +431,18 @@ void ConsoleProcess::setTerminalEmulator(QSettings *settings, const TerminalComm
} }
} }
bool ConsoleProcess::startTerminalEmulator(QSettings *settings, const QString &workingDir) bool ConsoleProcess::startTerminalEmulator(QSettings *settings, const QString &workingDir,
const Utils::Environment &env)
{ {
const TerminalCommand term = terminalEmulator(settings); const TerminalCommand term = terminalEmulator(settings);
return QProcess::startDetached(term.command, QtcProcess::splitArgs(term.openArgs), workingDir);
QProcess process;
process.setProgram(term.command);
process.setArguments(QtcProcess::splitArgs(term.openArgs));
process.setProcessEnvironment(env.toProcessEnvironment());
process.setWorkingDirectory(workingDir);
return process.startDetached();
} }
} // namespace Utils } // namespace Utils

View File

@@ -33,6 +33,7 @@
#include <QAbstractEventDispatcher> #include <QAbstractEventDispatcher>
#include <stdlib.h> #include <stdlib.h>
#include <cstring>
namespace Utils { namespace Utils {
@@ -361,7 +362,8 @@ QString ConsoleProcess::createWinCommandline(const QString &program, const QStri
return programName; return programName;
} }
bool ConsoleProcess::startTerminalEmulator(QSettings *, const QString &workingDir) bool ConsoleProcess::startTerminalEmulator(QSettings *, const QString &workingDir,
const Utils::Environment &env)
{ {
STARTUPINFO si; STARTUPINFO si;
ZeroMemory(&si, sizeof(si)); ZeroMemory(&si, sizeof(si));
@@ -375,9 +377,13 @@ bool ConsoleProcess::startTerminalEmulator(QSettings *, const QString &workingDi
// cmdLine is assumed to be detached - // cmdLine is assumed to be detached -
// https://blogs.msdn.microsoft.com/oldnewthing/20090601-00/?p=18083 // https://blogs.msdn.microsoft.com/oldnewthing/20090601-00/?p=18083
QString totalEnvironment = env.toStringList().join('\0') + '\0';
LPVOID envPtr = (env != Utils::Environment::systemEnvironment())
? (WCHAR *)(totalEnvironment.utf16()) : nullptr;
bool success = CreateProcessW(0, (WCHAR *)cmdLine.utf16(), bool success = CreateProcessW(0, (WCHAR *)cmdLine.utf16(),
0, 0, FALSE, CREATE_NEW_CONSOLE, 0, 0, FALSE, CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT,
0, workingDir.isEmpty() ? 0 : (WCHAR *)workingDir.utf16(), envPtr, workingDir.isEmpty() ? 0 : (WCHAR *)workingDir.utf16(),
&si, &pinfo); &si, &pinfo);
if (success) { if (success) {

View File

@@ -108,12 +108,17 @@ void FileUtils::showInGraphicalShell(QWidget *parent, const QString &pathIn)
} }
void FileUtils::openTerminal(const QString &path) void FileUtils::openTerminal(const QString &path)
{
openTerminal(path, Environment::systemEnvironment());
}
void FileUtils::openTerminal(const QString &path, const Environment &env)
{ {
const QFileInfo fileInfo(path); const QFileInfo fileInfo(path);
const QString pwd = QDir::toNativeSeparators(fileInfo.isDir() ? const QString pwd = QDir::toNativeSeparators(fileInfo.isDir() ?
fileInfo.absoluteFilePath() : fileInfo.absoluteFilePath() :
fileInfo.absolutePath()); fileInfo.absolutePath());
ConsoleProcess::startTerminalEmulator(ICore::settings(), pwd); ConsoleProcess::startTerminalEmulator(ICore::settings(), pwd, env);
} }
QString FileUtils::msgFindInDirectory() QString FileUtils::msgFindInDirectory()

View File

@@ -31,6 +31,8 @@ QT_BEGIN_NAMESPACE
class QWidget; class QWidget;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { class Environment; }
namespace Core { namespace Core {
struct CORE_EXPORT FileUtils struct CORE_EXPORT FileUtils
@@ -38,6 +40,7 @@ struct CORE_EXPORT FileUtils
// Helpers for common directory browser options. // Helpers for common directory browser options.
static void showInGraphicalShell(QWidget *parent, const QString &path); static void showInGraphicalShell(QWidget *parent, const QString &path);
static void openTerminal(const QString &path); static void openTerminal(const QString &path);
static void openTerminal(const QString &path, const Utils::Environment &env);
static QString msgFindInDirectory(); static QString msgFindInDirectory();
// Platform-dependent action descriptions // Platform-dependent action descriptions
static QString msgGraphicalShellAction(); static QString msgGraphicalShellAction();