forked from qt-creator/qt-creator
Core: Make access to remote terminal easier in code
By re-routing through device hooks. Now basically a FilePath is sufficient as a handle. Change-Id: I0ee43c35096ada12114d50476d5156a6c1de1e70 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -32,15 +32,6 @@
|
||||
#include <QTextCodec>
|
||||
#include <QWidget>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstring>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
namespace Core {
|
||||
@@ -111,69 +102,10 @@ void FileUtils::showInFileSystemView(const FilePath &path)
|
||||
navWidget->syncWithFilePath(path);
|
||||
}
|
||||
|
||||
static void startTerminalEmulator(const QString &workingDir, const Environment &env)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
STARTUPINFO si;
|
||||
ZeroMemory(&si, sizeof(si));
|
||||
si.cb = sizeof(si);
|
||||
|
||||
PROCESS_INFORMATION pinfo;
|
||||
ZeroMemory(&pinfo, sizeof(pinfo));
|
||||
|
||||
static const auto quoteWinCommand = [](const QString &program) {
|
||||
const QChar doubleQuote = QLatin1Char('"');
|
||||
|
||||
// add the program as the first arg ... it works better
|
||||
QString programName = program;
|
||||
programName.replace(QLatin1Char('/'), QLatin1Char('\\'));
|
||||
if (!programName.startsWith(doubleQuote) && !programName.endsWith(doubleQuote)
|
||||
&& programName.contains(QLatin1Char(' '))) {
|
||||
programName.prepend(doubleQuote);
|
||||
programName.append(doubleQuote);
|
||||
}
|
||||
return programName;
|
||||
};
|
||||
const QString cmdLine = quoteWinCommand(qtcEnvironmentVariable("COMSPEC"));
|
||||
// cmdLine is assumed to be detached -
|
||||
// https://blogs.msdn.microsoft.com/oldnewthing/20090601-00/?p=18083
|
||||
|
||||
const QString totalEnvironment = env.toStringList().join(QChar(QChar::Null)) + QChar(QChar::Null);
|
||||
LPVOID envPtr = (env != Environment::systemEnvironment())
|
||||
? (WCHAR *)(totalEnvironment.utf16()) : nullptr;
|
||||
|
||||
const bool success = CreateProcessW(0, (WCHAR *)cmdLine.utf16(),
|
||||
0, 0, FALSE, CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT,
|
||||
envPtr, workingDir.isEmpty() ? 0 : (WCHAR *)workingDir.utf16(),
|
||||
&si, &pinfo);
|
||||
|
||||
if (success) {
|
||||
CloseHandle(pinfo.hThread);
|
||||
CloseHandle(pinfo.hProcess);
|
||||
}
|
||||
#else
|
||||
const TerminalCommand term = TerminalCommand::terminalEmulator();
|
||||
QProcess process;
|
||||
process.setProgram(term.command);
|
||||
process.setArguments(ProcessArgs::splitArgs(term.openArgs));
|
||||
process.setProcessEnvironment(env.toProcessEnvironment());
|
||||
process.setWorkingDirectory(workingDir);
|
||||
process.startDetached();
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileUtils::openTerminal(const FilePath &path)
|
||||
{
|
||||
openTerminal(path, Environment::systemEnvironment());
|
||||
}
|
||||
|
||||
void FileUtils::openTerminal(const FilePath &path, const Environment &env)
|
||||
{
|
||||
const QFileInfo fileInfo = path.toFileInfo();
|
||||
const QString workingDir = QDir::toNativeSeparators(fileInfo.isDir() ?
|
||||
fileInfo.absoluteFilePath() :
|
||||
fileInfo.absolutePath());
|
||||
startTerminalEmulator(workingDir, env);
|
||||
QTC_ASSERT(DeviceFileHooks::instance().openTerminal, return);
|
||||
DeviceFileHooks::instance().openTerminal(path, env);
|
||||
}
|
||||
|
||||
QString FileUtils::msgFindInDirectory()
|
||||
|
||||
Reference in New Issue
Block a user