forked from qt-creator/qt-creator
Utils: Make Utils::withTildeHomePath a FilePath member
Gets .osType() right in remote cases. Change-Id: I6397996062d976d7db690c5729dd4faa1f459563 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
@@ -1720,6 +1720,28 @@ FilePath FilePath::cleanPath() const
|
|||||||
return withNewPath(doCleanPath(path()));
|
return withNewPath(doCleanPath(path()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
On Linux/Mac replace user's home path with ~ in the \c toString()
|
||||||
|
result for this path after cleaning.
|
||||||
|
|
||||||
|
If path is not sub of home path, or when running on Windows, returns the input
|
||||||
|
*/
|
||||||
|
QString FilePath::withTildeHomePath() const
|
||||||
|
{
|
||||||
|
if (osType() == OsTypeWindows)
|
||||||
|
return toString();
|
||||||
|
|
||||||
|
if (needsDevice())
|
||||||
|
return toString();
|
||||||
|
|
||||||
|
static const QString homePath = QDir::homePath();
|
||||||
|
|
||||||
|
QString outPath = cleanPath().absoluteFilePath().path();
|
||||||
|
if (outPath.startsWith(homePath))
|
||||||
|
outPath = '~' + outPath.mid(homePath.size());
|
||||||
|
return outPath;
|
||||||
|
}
|
||||||
|
|
||||||
QTextStream &operator<<(QTextStream &s, const FilePath &fn)
|
QTextStream &operator<<(QTextStream &s, const FilePath &fn)
|
||||||
{
|
{
|
||||||
return s << fn.toString();
|
return s << fn.toString();
|
||||||
|
@@ -195,6 +195,8 @@ public:
|
|||||||
QString displayName(const QString &args = {}) const;
|
QString displayName(const QString &args = {}) const;
|
||||||
QString nativePath() const;
|
QString nativePath() const;
|
||||||
QString shortNativePath() const;
|
QString shortNativePath() const;
|
||||||
|
QString withTildeHomePath() const;
|
||||||
|
|
||||||
bool startsWithDriveLetter() const;
|
bool startsWithDriveLetter() const;
|
||||||
|
|
||||||
static QString formatFilePaths(const FilePaths &files, const QString &separator);
|
static QString formatFilePaths(const FilePaths &files, const QString &separator);
|
||||||
|
@@ -73,25 +73,6 @@ QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings)
|
|||||||
return strings.at(0).left(commonLength);
|
return strings.at(0).left(commonLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
QTCREATOR_UTILS_EXPORT QString withTildeHomePath(const QString &path)
|
|
||||||
{
|
|
||||||
if (HostOsInfo::isWindowsHost())
|
|
||||||
return path;
|
|
||||||
|
|
||||||
if (FilePath::fromString(path).needsDevice())
|
|
||||||
return path;
|
|
||||||
|
|
||||||
static const QString homePath = QDir::homePath();
|
|
||||||
|
|
||||||
QFileInfo fi(QDir::cleanPath(path));
|
|
||||||
QString outPath = fi.absoluteFilePath();
|
|
||||||
if (outPath.startsWith(homePath))
|
|
||||||
outPath = '~' + outPath.mid(homePath.size());
|
|
||||||
else
|
|
||||||
outPath = path;
|
|
||||||
return outPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool validateVarName(const QString &varName)
|
static bool validateVarName(const QString &varName)
|
||||||
{
|
{
|
||||||
return !varName.startsWith("JS:");
|
return !varName.startsWith("JS:");
|
||||||
|
@@ -26,11 +26,6 @@ QTCREATOR_UTILS_EXPORT QString settingsKey(const QString &category);
|
|||||||
// "C:\foo\bar1" "C:\foo\bar2" -> "C:\foo\bar"
|
// "C:\foo\bar1" "C:\foo\bar2" -> "C:\foo\bar"
|
||||||
QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings);
|
QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings);
|
||||||
|
|
||||||
// On Linux/Mac replace user's home path with ~
|
|
||||||
// Uses cleaned path and tries to use absolute path of "path" if possible
|
|
||||||
// If path is not sub of home path, or when running on Windows, returns the input
|
|
||||||
QTCREATOR_UTILS_EXPORT QString withTildeHomePath(const QString &path);
|
|
||||||
|
|
||||||
// Removes first unescaped ampersand in text
|
// Removes first unescaped ampersand in text
|
||||||
QTCREATOR_UTILS_EXPORT QString stripAccelerator(const QString &text);
|
QTCREATOR_UTILS_EXPORT QString stripAccelerator(const QString &text);
|
||||||
// Quotes all ampersands
|
// Quotes all ampersands
|
||||||
|
@@ -3476,7 +3476,7 @@ void ProjectExplorerPluginPrivate::updateRecentProjectMenu()
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
const QString actionText = ActionManager::withNumberAccelerator(
|
const QString actionText = ActionManager::withNumberAccelerator(
|
||||||
Utils::withTildeHomePath(filePath.toUserOutput()), acceleratorKey);
|
filePath.withTildeHomePath(), acceleratorKey);
|
||||||
QAction *action = menu->addAction(actionText);
|
QAction *action = menu->addAction(actionText);
|
||||||
connect(action, &QAction::triggered, this, [this, filePath] {
|
connect(action, &QAction::triggered, this, [this, filePath] {
|
||||||
openRecentProject(filePath);
|
openRecentProject(filePath);
|
||||||
|
@@ -73,7 +73,7 @@ QVariant ProjectModel::data(const QModelIndex &index, int role) const
|
|||||||
case FilePathRole:
|
case FilePathRole:
|
||||||
return data.first.toVariant();
|
return data.first.toVariant();
|
||||||
case PrettyFilePathRole:
|
case PrettyFilePathRole:
|
||||||
return Utils::withTildeHomePath(data.first.toUserOutput()); // FIXME: FilePath::displayName() ?
|
return data.first.withTildeHomePath(); // FIXME: FilePath::displayName() ?
|
||||||
case ShortcutRole: {
|
case ShortcutRole: {
|
||||||
const Id projectBase = PROJECT_BASE_ID;
|
const Id projectBase = PROJECT_BASE_ID;
|
||||||
if (Command *cmd = ActionManager::command(projectBase.withSuffix(index.row() + 1)))
|
if (Command *cmd = ActionManager::command(projectBase.withSuffix(index.row() + 1)))
|
||||||
@@ -334,7 +334,7 @@ public:
|
|||||||
yy += 18;
|
yy += 18;
|
||||||
|
|
||||||
// Project path.
|
// Project path.
|
||||||
QString pathWithTilde = Utils::withTildeHomePath(projectPath.toUserOutput());
|
QString pathWithTilde = projectPath.withTildeHomePath();
|
||||||
painter->setPen(foregroundPrimaryColor);
|
painter->setPen(foregroundPrimaryColor);
|
||||||
painter->drawText(x1, yy, fm.elidedText(pathWithTilde, Qt::ElideMiddle, textSpace));
|
painter->drawText(x1, yy, fm.elidedText(pathWithTilde, Qt::ElideMiddle, textSpace));
|
||||||
yy += 22;
|
yy += 22;
|
||||||
@@ -485,7 +485,7 @@ public:
|
|||||||
|
|
||||||
painter->setPen(themeColor(Theme::Welcome_ForegroundPrimaryColor));
|
painter->setPen(themeColor(Theme::Welcome_ForegroundPrimaryColor));
|
||||||
painter->setFont(sizedFont(13, option.widget));
|
painter->setFont(sizedFont(13, option.widget));
|
||||||
QString pathWithTilde = Utils::withTildeHomePath(projectPath.toUserOutput());
|
QString pathWithTilde = projectPath.withTildeHomePath();
|
||||||
const QString pathWithTildeElided =
|
const QString pathWithTildeElided =
|
||||||
painter->fontMetrics().elidedText(pathWithTilde, Qt::ElideMiddle, textSpace);
|
painter->fontMetrics().elidedText(pathWithTilde, Qt::ElideMiddle, textSpace);
|
||||||
painter->drawText(x + TEXT_OFFSET_HORIZONTAL, secondBase, pathWithTildeElided);
|
painter->drawText(x + TEXT_OFFSET_HORIZONTAL, secondBase, pathWithTildeElided);
|
||||||
|
@@ -84,7 +84,7 @@ QStringList pathsToBaseNames(const QStringList &paths)
|
|||||||
QStringList pathsWithTildeHomePath(const QStringList &paths)
|
QStringList pathsWithTildeHomePath(const QStringList &paths)
|
||||||
{
|
{
|
||||||
return Utils::transform(paths, [](const QString &path) {
|
return Utils::transform(paths, [](const QString &path) {
|
||||||
return Utils::withTildeHomePath(QDir::toNativeSeparators(path));
|
return FilePath::fromString(path).withTildeHomePath();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -396,7 +396,7 @@ QVariant ProjectModel::data(const QModelIndex &index, int role) const
|
|||||||
case FilePathRole:
|
case FilePathRole:
|
||||||
return data.first.toVariant();
|
return data.first.toVariant();
|
||||||
case PrettyFilePathRole:
|
case PrettyFilePathRole:
|
||||||
return Utils::withTildeHomePath(data.first.absolutePath().toUserOutput());
|
return data.first.absolutePath().withTildeHomePath();
|
||||||
case PreviewUrl:
|
case PreviewUrl:
|
||||||
return QVariant(QStringLiteral("image://project_preview/") +
|
return QVariant(QStringLiteral("image://project_preview/") +
|
||||||
QmlProjectManager::ProjectFileContentTools::appQmlFile(
|
QmlProjectManager::ProjectFileContentTools::appQmlFile(
|
||||||
|
@@ -1,12 +1,15 @@
|
|||||||
// Copyright (C) 2016 The Qt Company Ltd.
|
// Copyright (C) 2016 The Qt Company Ltd.
|
||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
|
#include <utils/filepath.h>
|
||||||
#include <utils/stringutils.h>
|
#include <utils/stringutils.h>
|
||||||
|
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
|
|
||||||
//TESTED_COMPONENT=src/libs/utils
|
//TESTED_COMPONENT=src/libs/utils
|
||||||
|
|
||||||
|
using namespace Utils;
|
||||||
|
|
||||||
class TestMacroExpander : public Utils::AbstractMacroExpander
|
class TestMacroExpander : public Utils::AbstractMacroExpander
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -77,32 +80,30 @@ private:
|
|||||||
|
|
||||||
void tst_StringUtils::testWithTildeHomePath()
|
void tst_StringUtils::testWithTildeHomePath()
|
||||||
{
|
{
|
||||||
|
const QString home = QDir::homePath();
|
||||||
|
const FilePath homePath = FilePath::fromString(home);
|
||||||
|
|
||||||
#ifndef Q_OS_WIN
|
#ifndef Q_OS_WIN
|
||||||
// home path itself
|
// home path itself
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QString::fromLatin1("~"));
|
QCOMPARE(homePath.withTildeHomePath(), QString("~"));
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1Char('/')),
|
QCOMPARE(homePath.pathAppended("/").withTildeHomePath(), QString("~"));
|
||||||
QString::fromLatin1("~"));
|
QCOMPARE(FilePath::fromString("/unclean/../" + home).withTildeHomePath(), QString("~"));
|
||||||
QCOMPARE(Utils::withTildeHomePath(QString::fromLatin1("/unclean/..") + QDir::homePath()),
|
|
||||||
QString::fromLatin1("~"));
|
|
||||||
// sub of home path
|
// sub of home path
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/foo")),
|
QCOMPARE(homePath.pathAppended("/foo").withTildeHomePath(), QString("~/foo"));
|
||||||
QString::fromLatin1("~/foo"));
|
QCOMPARE(homePath.pathAppended("/foo/").withTildeHomePath(), QString("~/foo"));
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/foo/")),
|
QCOMPARE(homePath.pathAppended("/some/path/file.txt").withTildeHomePath(),
|
||||||
QString::fromLatin1("~/foo"));
|
QString("~/some/path/file.txt"));
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/some/path/file.txt")),
|
QCOMPARE(homePath.pathAppended("/some/unclean/../path/file.txt").withTildeHomePath(),
|
||||||
QString::fromLatin1("~/some/path/file.txt"));
|
QString("~/some/path/file.txt"));
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/some/unclean/../path/file.txt")),
|
|
||||||
QString::fromLatin1("~/some/path/file.txt"));
|
|
||||||
// not sub of home path
|
// not sub of home path
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/../foo")),
|
QCOMPARE(homePath.pathAppended("/../foo").withTildeHomePath(),
|
||||||
QString(QDir::homePath() + QString::fromLatin1("/../foo")));
|
QString(home + "/../foo"));
|
||||||
#else
|
#else
|
||||||
// windows: should return same as input
|
// windows: should return same as input
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QDir::homePath());
|
QCOMPARE(homePath.withTildeHomePath(), home);
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/foo")),
|
QCOMPARE(homePath.pathAppended("/foo").withTildeHomePath(), home + QString("/foo"));
|
||||||
QDir::homePath() + QString::fromLatin1("/foo"));
|
QCOMPARE(homePath.pathAppended("/../foo").withTildeHomePath(),
|
||||||
QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/../foo")),
|
homePath.pathAppended("/../foo").withTildeHomePath());
|
||||||
Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/../foo")));
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user