Utils: Introduce FilePath::displayName()

To produce "<filepath> on <devicename>" which is nicer to read than
"<scheme>://<deviceid>/<filepath>" in some places.

Change-Id: Ife47bbf49382cf73dd4709d826bdeaa8730f88be
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2022-06-02 17:05:46 +02:00
parent 51aae3bb1f
commit 31f0ba4ef9
7 changed files with 39 additions and 2 deletions

View File

@@ -1508,6 +1508,11 @@ QString CommandLine::toUserOutput() const
return res; return res;
} }
QString CommandLine::displayName() const
{
return m_executable.displayName(m_arguments);
}
QStringList CommandLine::splitArguments() const QStringList CommandLine::splitArguments() const
{ {
return ProcessArgs::splitArgs(m_arguments, m_executable.osType()); return ProcessArgs::splitArgs(m_arguments, m_executable.osType());

View File

@@ -152,6 +152,7 @@ public:
void addCommandLineAsArgs(const CommandLine &cmd, RawType); void addCommandLineAsArgs(const CommandLine &cmd, RawType);
QString toUserOutput() const; QString toUserOutput() const;
QString displayName() const;
FilePath executable() const { return m_executable; } FilePath executable() const { return m_executable; }
void setExecutable(const FilePath &executable) { m_executable = executable; } void setExecutable(const FilePath &executable) { m_executable = executable; }

View File

@@ -921,6 +921,29 @@ FilePath FilePath::normalizedPathName() const
return result; return result;
} }
QString FilePath::displayName(const QString &args) const
{
QString deviceName;
if (needsDevice()) {
QTC_ASSERT(s_deviceHooks.deviceDisplayName, return m_data);
deviceName = s_deviceHooks.deviceDisplayName(*this);
}
if (args.isEmpty()) {
if (deviceName.isEmpty())
return m_data;
return QCoreApplication::translate("Utils::FileUtils", "%1 on %2", "File on device")
.arg(m_data, deviceName);
}
if (deviceName.isEmpty())
return m_data + ' ' + args;
return QCoreApplication::translate("Utils::FileUtils", "%1 %2 on %3", "File and args on device")
.arg(m_data, args, deviceName);
}
/// Constructs a FilePath from \a filename /// Constructs a FilePath from \a filename
/// \a filename is not checked for validity. /// \a filename is not checked for validity.
FilePath FilePath::fromString(const QString &filepath) FilePath FilePath::fromString(const QString &filepath)

View File

@@ -175,6 +175,7 @@ public:
// on Windows and macOS. This is rarely needed. // on Windows and macOS. This is rarely needed.
[[nodiscard]] FilePath normalizedPathName() const; [[nodiscard]] FilePath normalizedPathName() const;
QString displayName(const QString &args = {}) const;
QString nativePath() const; QString nativePath() const;
QString shortNativePath() const; QString shortNativePath() const;
bool startsWithDriveLetter() const; bool startsWithDriveLetter() const;

View File

@@ -86,6 +86,7 @@ public:
std::function<Environment(const FilePath &)> environment; std::function<Environment(const FilePath &)> environment;
std::function<qint64(const FilePath &)> fileSize; std::function<qint64(const FilePath &)> fileSize;
std::function<qint64(const FilePath &)> bytesAvailable; std::function<qint64(const FilePath &)> bytesAvailable;
std::function<QString(const FilePath &)> deviceDisplayName;
template <class ...Args> using Continuation = std::function<void(Args...)>; template <class ...Args> using Continuation = std::function<void(Args...)>;
std::function<void(const Continuation<bool> &, const FilePath &, const FilePath &)> asyncCopyFile; std::function<void(const Continuation<bool> &, const FilePath &, const FilePath &)> asyncCopyFile;

View File

@@ -595,6 +595,12 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
return device->bytesAvailable(filePath); return device->bytesAvailable(filePath);
}; };
deviceHooks.deviceDisplayName = [](const FilePath &filePath) {
auto device = DeviceManager::deviceForPath(filePath);
QTC_ASSERT(device, return QString());
return device->displayName();
};
FileUtils::setDeviceFileHooks(deviceHooks); FileUtils::setDeviceFileHooks(deviceHooks);
DeviceProcessHooks processHooks; DeviceProcessHooks processHooks;

View File

@@ -1520,7 +1520,7 @@ void SimpleTargetRunnerPrivate::forwardDone()
{ {
if (m_stopReported) if (m_stopReported)
return; return;
const QString executable = m_command.executable().toUserOutput(); const QString executable = m_command.executable().displayName();
QString msg = tr("%1 exited with code %2").arg(executable).arg(m_resultData.m_exitCode); QString msg = tr("%1 exited with code %2").arg(executable).arg(m_resultData.m_exitCode);
if (m_resultData.m_exitStatus == QProcess::CrashExit) if (m_resultData.m_exitStatus == QProcess::CrashExit)
msg = tr("%1 crashed.").arg(executable); msg = tr("%1 crashed.").arg(executable);
@@ -1570,7 +1570,7 @@ void SimpleTargetRunner::start()
d->m_process.setTerminalMode(useTerminal ? Utils::TerminalMode::On : Utils::TerminalMode::Off); d->m_process.setTerminalMode(useTerminal ? Utils::TerminalMode::On : Utils::TerminalMode::Off);
d->m_runAsRoot = runAsRoot; d->m_runAsRoot = runAsRoot;
const QString msg = RunControl::tr("Starting %1...").arg(d->m_command.toUserOutput()); const QString msg = RunControl::tr("Starting %1...").arg(d->m_command.displayName());
appendMessage(msg, NormalMessageFormat); appendMessage(msg, NormalMessageFormat);
const bool isDesktop = !d->m_command.executable().needsDevice(); const bool isDesktop = !d->m_command.executable().needsDevice();