forked from qt-creator/qt-creator
Utils: Replace FileUtils class by a FileUtils namespace
Change-Id: I33be0a11cef008d7cc6ed7472ffadf6c98b50797 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -311,21 +311,23 @@ TempFileSaver::~TempFileSaver()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\class Utils::FileUtils
|
\namespace Utils::FileUtils
|
||||||
\inmodule QtCreator
|
\inmodule QtCreator
|
||||||
|
|
||||||
\brief The FileUtils class contains file and directory related convenience
|
\brief The FileUtils namespace contains file and directory related convenience
|
||||||
functions.
|
functions.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
namespace FileUtils {
|
||||||
|
|
||||||
#ifdef QT_GUI_LIB
|
#ifdef QT_GUI_LIB
|
||||||
FileUtils::CopyAskingForOverwrite::CopyAskingForOverwrite(QWidget *dialogParent, const std::function<void (FilePath)> &postOperation)
|
CopyAskingForOverwrite::CopyAskingForOverwrite(QWidget *dialogParent,
|
||||||
|
const std::function<void (FilePath)> &postOperation)
|
||||||
: m_parent(dialogParent)
|
: m_parent(dialogParent)
|
||||||
, m_postOperation(postOperation)
|
, m_postOperation(postOperation)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
FileUtils::CopyHelper FileUtils::CopyAskingForOverwrite::operator()()
|
CopyHelper CopyAskingForOverwrite::operator()()
|
||||||
{
|
{
|
||||||
CopyHelper helperFunction = [this](const FilePath &src, const FilePath &dest, QString *error) {
|
CopyHelper helperFunction = [this](const FilePath &src, const FilePath &dest, QString *error) {
|
||||||
bool copyFile = true;
|
bool copyFile = true;
|
||||||
@@ -373,13 +375,13 @@ FileUtils::CopyHelper FileUtils::CopyAskingForOverwrite::operator()()
|
|||||||
return helperFunction;
|
return helperFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePaths FileUtils::CopyAskingForOverwrite::files() const
|
FilePaths CopyAskingForOverwrite::files() const
|
||||||
{
|
{
|
||||||
return m_files;
|
return m_files;
|
||||||
}
|
}
|
||||||
#endif // QT_GUI_LIB
|
#endif // QT_GUI_LIB
|
||||||
|
|
||||||
FilePath FileUtils::commonPath(const FilePaths &paths)
|
FilePath commonPath(const FilePaths &paths)
|
||||||
{
|
{
|
||||||
if (paths.isEmpty())
|
if (paths.isEmpty())
|
||||||
return {};
|
return {};
|
||||||
@@ -425,17 +427,6 @@ FilePath FileUtils::commonPath(const FilePaths &paths)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
template <>
|
|
||||||
void withNtfsPermissions(const std::function<void()> &task)
|
|
||||||
{
|
|
||||||
qt_ntfs_permission_lookup++;
|
|
||||||
task();
|
|
||||||
qt_ntfs_permission_lookup--;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef QT_WIDGETS_LIB
|
#ifdef QT_WIDGETS_LIB
|
||||||
|
|
||||||
static QUrl filePathToQUrl(const FilePath &filePath)
|
static QUrl filePathToQUrl(const FilePath &filePath)
|
||||||
@@ -511,7 +502,7 @@ FilePath firstOrEmpty(const FilePaths &filePaths)
|
|||||||
return filePaths.isEmpty() ? FilePath() : filePaths.first();
|
return filePaths.isEmpty() ? FilePath() : filePaths.first();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileUtils::hasNativeFileDialog()
|
bool hasNativeFileDialog()
|
||||||
{
|
{
|
||||||
static std::optional<bool> hasNative;
|
static std::optional<bool> hasNative;
|
||||||
if (!hasNative.has_value()) {
|
if (!hasNative.has_value()) {
|
||||||
@@ -524,7 +515,7 @@ bool FileUtils::hasNativeFileDialog()
|
|||||||
return *hasNative;
|
return *hasNative;
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath FileUtils::getOpenFilePath(QWidget *parent,
|
FilePath getOpenFilePath(QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir,
|
const FilePath &dir,
|
||||||
const QString &filter,
|
const QString &filter,
|
||||||
@@ -553,7 +544,7 @@ FilePath FileUtils::getOpenFilePath(QWidget *parent,
|
|||||||
QFileDialog::AcceptOpen));
|
QFileDialog::AcceptOpen));
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath FileUtils::getSaveFilePath(QWidget *parent,
|
FilePath getSaveFilePath(QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir,
|
const FilePath &dir,
|
||||||
const QString &filter,
|
const QString &filter,
|
||||||
@@ -576,7 +567,7 @@ FilePath FileUtils::getSaveFilePath(QWidget *parent,
|
|||||||
QFileDialog::AcceptSave));
|
QFileDialog::AcceptSave));
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath FileUtils::getExistingDirectory(QWidget *parent,
|
FilePath getExistingDirectory(QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir,
|
const FilePath &dir,
|
||||||
QFileDialog::Options options,
|
QFileDialog::Options options,
|
||||||
@@ -604,7 +595,7 @@ FilePath FileUtils::getExistingDirectory(QWidget *parent,
|
|||||||
QFileDialog::AcceptOpen));
|
QFileDialog::AcceptOpen));
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePaths FileUtils::getOpenFilePaths(QWidget *parent,
|
FilePaths getOpenFilePaths(QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir,
|
const FilePath &dir,
|
||||||
const QString &filter,
|
const QString &filter,
|
||||||
@@ -704,7 +695,7 @@ FilePathInfo::FileFlags fileInfoFlagsfromStatMode(const QString &hexString, int
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePathInfo FileUtils::filePathInfoFromTriple(const QString &infos, int modeBase)
|
FilePathInfo filePathInfoFromTriple(const QString &infos, int modeBase)
|
||||||
{
|
{
|
||||||
const QStringList parts = infos.split(' ', Qt::SkipEmptyParts);
|
const QStringList parts = infos.split(' ', Qt::SkipEmptyParts);
|
||||||
if (parts.size() != 3)
|
if (parts.size() != 3)
|
||||||
@@ -717,7 +708,7 @@ FilePathInfo FileUtils::filePathInfoFromTriple(const QString &infos, int modeBas
|
|||||||
return {size, flags, dt};
|
return {size, flags, dt};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileUtils::copyRecursively(const FilePath &srcFilePath,
|
bool copyRecursively(const FilePath &srcFilePath,
|
||||||
const FilePath &tgtFilePath,
|
const FilePath &tgtFilePath,
|
||||||
QString *error,
|
QString *error,
|
||||||
CopyHelper copyHelper)
|
CopyHelper copyHelper)
|
||||||
@@ -753,7 +744,7 @@ bool FileUtils::copyRecursively(const FilePath &srcFilePath,
|
|||||||
Returns whether the operation succeeded.
|
Returns whether the operation succeeded.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Result FileUtils::copyIfDifferent(const FilePath &srcFilePath, const FilePath &tgtFilePath)
|
Result copyIfDifferent(const FilePath &srcFilePath, const FilePath &tgtFilePath)
|
||||||
{
|
{
|
||||||
if (!srcFilePath.exists())
|
if (!srcFilePath.exists())
|
||||||
return Result::Error(Tr::tr("File %1 does not exist.").arg(srcFilePath.toUserOutput()));
|
return Result::Error(Tr::tr("File %1 does not exist.").arg(srcFilePath.toUserOutput()));
|
||||||
@@ -779,7 +770,7 @@ Result FileUtils::copyIfDifferent(const FilePath &srcFilePath, const FilePath &t
|
|||||||
return srcFilePath.copyFile(tgtFilePath);
|
return srcFilePath.copyFile(tgtFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString FileUtils::fileSystemFriendlyName(const QString &name)
|
QString fileSystemFriendlyName(const QString &name)
|
||||||
{
|
{
|
||||||
QString result = name;
|
QString result = name;
|
||||||
static const QRegularExpression nonWordEx("\\W");
|
static const QRegularExpression nonWordEx("\\W");
|
||||||
@@ -795,13 +786,13 @@ QString FileUtils::fileSystemFriendlyName(const QString &name)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int FileUtils::indexOfQmakeUnfriendly(const QString &name, int startpos)
|
int indexOfQmakeUnfriendly(const QString &name, int startpos)
|
||||||
{
|
{
|
||||||
static const QRegularExpression checkRegExp(QLatin1String("[^a-zA-Z0-9_.-]"));
|
static const QRegularExpression checkRegExp(QLatin1String("[^a-zA-Z0-9_.-]"));
|
||||||
return checkRegExp.match(name, startpos).capturedStart();
|
return checkRegExp.match(name, startpos).capturedStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString FileUtils::qmakeFriendlyName(const QString &name)
|
QString qmakeFriendlyName(const QString &name)
|
||||||
{
|
{
|
||||||
QString result = name;
|
QString result = name;
|
||||||
|
|
||||||
@@ -814,14 +805,14 @@ QString FileUtils::qmakeFriendlyName(const QString &name)
|
|||||||
return fileSystemFriendlyName(result);
|
return fileSystemFriendlyName(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileUtils::makeWritable(const FilePath &path)
|
bool makeWritable(const FilePath &path)
|
||||||
{
|
{
|
||||||
return path.setPermissions(path.permissions() | QFile::WriteUser);
|
return path.setPermissions(path.permissions() | QFile::WriteUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
// makes sure that capitalization of directories is canonical on Windows and macOS.
|
// makes sure that capitalization of directories is canonical on Windows and macOS.
|
||||||
// This mimics the logic in QDeclarative_isFileCaseCorrect
|
// This mimics the logic in QDeclarative_isFileCaseCorrect
|
||||||
QString FileUtils::normalizedPathName(const QString &name)
|
QString normalizedPathName(const QString &name)
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
const QString nativeSeparatorName(QDir::toNativeSeparators(name));
|
const QString nativeSeparatorName(QDir::toNativeSeparators(name));
|
||||||
@@ -842,7 +833,7 @@ QString FileUtils::normalizedPathName(const QString &name)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath FileUtils::commonPath(const FilePath &oldCommonPath, const FilePath &filePath)
|
FilePath commonPath(const FilePath &oldCommonPath, const FilePath &filePath)
|
||||||
{
|
{
|
||||||
FilePath newCommonPath = oldCommonPath;
|
FilePath newCommonPath = oldCommonPath;
|
||||||
while (!newCommonPath.isEmpty() && !filePath.isChildOf(newCommonPath))
|
while (!newCommonPath.isEmpty() && !filePath.isChildOf(newCommonPath))
|
||||||
@@ -850,12 +841,12 @@ FilePath FileUtils::commonPath(const FilePath &oldCommonPath, const FilePath &fi
|
|||||||
return newCommonPath.canonicalPath();
|
return newCommonPath.canonicalPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath FileUtils::homePath()
|
FilePath homePath()
|
||||||
{
|
{
|
||||||
return FilePath::fromUserInput(QDir::homePath());
|
return FilePath::fromUserInput(QDir::homePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
expected_str<FilePath> FileUtils::scratchBufferFilePath(const QString &pattern)
|
expected_str<FilePath> scratchBufferFilePath(const QString &pattern)
|
||||||
{
|
{
|
||||||
QString tmp = pattern;
|
QString tmp = pattern;
|
||||||
QFileInfo fi(tmp);
|
QFileInfo fi(tmp);
|
||||||
@@ -876,12 +867,12 @@ expected_str<FilePath> FileUtils::scratchBufferFilePath(const QString &pattern)
|
|||||||
return FilePath::fromString(file.fileName());
|
return FilePath::fromString(file.fileName());
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePaths FileUtils::toFilePathList(const QStringList &paths)
|
FilePaths toFilePathList(const QStringList &paths)
|
||||||
{
|
{
|
||||||
return transform(paths, &FilePath::fromString);
|
return transform(paths, &FilePath::fromString);
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 FileUtils::bytesAvailableFromDFOutput(const QByteArray &dfOutput)
|
qint64 bytesAvailableFromDFOutput(const QByteArray &dfOutput)
|
||||||
{
|
{
|
||||||
const auto lines = filtered(dfOutput.split('\n'),
|
const auto lines = filtered(dfOutput.split('\n'),
|
||||||
[](const QByteArray &line) { return line.size() > 0; });
|
[](const QByteArray &line) { return line.size() > 0; });
|
||||||
@@ -903,7 +894,7 @@ qint64 FileUtils::bytesAvailableFromDFOutput(const QByteArray &dfOutput)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePaths FileUtils::usefulExtraSearchPaths()
|
FilePaths usefulExtraSearchPaths()
|
||||||
{
|
{
|
||||||
if (HostOsInfo::isMacHost()) {
|
if (HostOsInfo::isMacHost()) {
|
||||||
return {"/opt/homebrew/bin"};
|
return {"/opt/homebrew/bin"};
|
||||||
@@ -917,4 +908,16 @@ FilePaths FileUtils::usefulExtraSearchPaths()
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace FileUtils
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
template <>
|
||||||
|
void withNtfsPermissions(const std::function<void()> &task)
|
||||||
|
{
|
||||||
|
qt_ntfs_permission_lookup++;
|
||||||
|
task();
|
||||||
|
qt_ntfs_permission_lookup--;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
@@ -21,7 +21,6 @@
|
|||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QDataStream;
|
class QDataStream;
|
||||||
class QTextStream;
|
class QTextStream;
|
||||||
class QWidget;
|
|
||||||
class QXmlStreamWriter;
|
class QXmlStreamWriter;
|
||||||
|
|
||||||
// for withNtfsPermissions
|
// for withNtfsPermissions
|
||||||
@@ -31,12 +30,8 @@ extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
|
|||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
namespace FileUtils {
|
||||||
|
|
||||||
class CommandLine;
|
|
||||||
|
|
||||||
class QTCREATOR_UTILS_EXPORT FileUtils
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using CopyHelper = std::function<bool(const FilePath &, const FilePath &, QString *)>;
|
using CopyHelper = std::function<bool(const FilePath &, const FilePath &, QString *)>;
|
||||||
#ifdef QT_GUI_LIB
|
#ifdef QT_GUI_LIB
|
||||||
class QTCREATOR_UTILS_EXPORT CopyAskingForOverwrite
|
class QTCREATOR_UTILS_EXPORT CopyAskingForOverwrite
|
||||||
@@ -56,38 +51,39 @@ public:
|
|||||||
};
|
};
|
||||||
#endif // QT_GUI_LIB
|
#endif // QT_GUI_LIB
|
||||||
|
|
||||||
static bool copyRecursively(
|
QTCREATOR_UTILS_EXPORT bool copyRecursively(
|
||||||
const FilePath &srcFilePath,
|
const FilePath &srcFilePath,
|
||||||
const FilePath &tgtFilePath,
|
const FilePath &tgtFilePath,
|
||||||
QString *error,
|
QString *error,
|
||||||
CopyHelper helper);
|
CopyHelper helper);
|
||||||
|
|
||||||
static Result copyIfDifferent(const FilePath &srcFilePath,
|
QTCREATOR_UTILS_EXPORT Result copyIfDifferent(const FilePath &srcFilePath,
|
||||||
const FilePath &tgtFilePath);
|
const FilePath &tgtFilePath);
|
||||||
static QString fileSystemFriendlyName(const QString &name);
|
QTCREATOR_UTILS_EXPORT QString fileSystemFriendlyName(const QString &name);
|
||||||
static int indexOfQmakeUnfriendly(const QString &name, int startpos = 0);
|
QTCREATOR_UTILS_EXPORT int indexOfQmakeUnfriendly(const QString &name, int startpos = 0);
|
||||||
static QString qmakeFriendlyName(const QString &name);
|
QTCREATOR_UTILS_EXPORT QString qmakeFriendlyName(const QString &name);
|
||||||
static bool makeWritable(const FilePath &path);
|
QTCREATOR_UTILS_EXPORT bool makeWritable(const FilePath &path);
|
||||||
static QString normalizedPathName(const QString &name);
|
QTCREATOR_UTILS_EXPORT QString normalizedPathName(const QString &name);
|
||||||
|
|
||||||
static FilePath commonPath(const FilePath &oldCommonPath, const FilePath &fileName);
|
QTCREATOR_UTILS_EXPORT FilePath commonPath(const FilePath &oldCommonPath, const FilePath &fileName);
|
||||||
static FilePath commonPath(const FilePaths &paths);
|
QTCREATOR_UTILS_EXPORT FilePath commonPath(const FilePaths &paths);
|
||||||
static FilePath homePath();
|
QTCREATOR_UTILS_EXPORT FilePath homePath();
|
||||||
static expected_str<FilePath> scratchBufferFilePath(const QString &pattern);
|
QTCREATOR_UTILS_EXPORT expected_str<FilePath> scratchBufferFilePath(const QString &pattern);
|
||||||
|
|
||||||
static FilePaths toFilePathList(const QStringList &paths);
|
QTCREATOR_UTILS_EXPORT FilePaths toFilePathList(const QStringList &paths);
|
||||||
|
|
||||||
static qint64 bytesAvailableFromDFOutput(const QByteArray &dfOutput);
|
QTCREATOR_UTILS_EXPORT qint64 bytesAvailableFromDFOutput(const QByteArray &dfOutput);
|
||||||
|
|
||||||
static FilePathInfo filePathInfoFromTriple(const QString &infos, int modeBase);
|
QTCREATOR_UTILS_EXPORT FilePathInfo filePathInfoFromTriple(const QString &infos, int modeBase);
|
||||||
|
|
||||||
//! Returns known paths like /opt/homebrew on macOS that might not be in PATH
|
//! Returns known paths like /opt/homebrew on macOS that might not be in PATH
|
||||||
static FilePaths usefulExtraSearchPaths();
|
QTCREATOR_UTILS_EXPORT FilePaths usefulExtraSearchPaths();
|
||||||
|
|
||||||
#ifdef QT_WIDGETS_LIB
|
#ifdef QT_WIDGETS_LIB
|
||||||
static bool hasNativeFileDialog();
|
QTCREATOR_UTILS_EXPORT bool hasNativeFileDialog();
|
||||||
|
|
||||||
static FilePath getOpenFilePath(QWidget *parent,
|
QTCREATOR_UTILS_EXPORT FilePath getOpenFilePath(
|
||||||
|
QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir = {},
|
const FilePath &dir = {},
|
||||||
const QString &filter = {},
|
const QString &filter = {},
|
||||||
@@ -96,7 +92,8 @@ public:
|
|||||||
bool fromDeviceIfShiftIsPressed = false,
|
bool fromDeviceIfShiftIsPressed = false,
|
||||||
bool forceNonNativeDialog = false);
|
bool forceNonNativeDialog = false);
|
||||||
|
|
||||||
static FilePath getSaveFilePath(QWidget *parent,
|
QTCREATOR_UTILS_EXPORT FilePath getSaveFilePath(
|
||||||
|
QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir = {},
|
const FilePath &dir = {},
|
||||||
const QString &filter = {},
|
const QString &filter = {},
|
||||||
@@ -104,21 +101,24 @@ public:
|
|||||||
QFileDialog::Options options = {},
|
QFileDialog::Options options = {},
|
||||||
bool forceNonNativeDialog = false);
|
bool forceNonNativeDialog = false);
|
||||||
|
|
||||||
static FilePath getExistingDirectory(QWidget *parent,
|
QTCREATOR_UTILS_EXPORT FilePath getExistingDirectory(
|
||||||
|
QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir = {},
|
const FilePath &dir = {},
|
||||||
QFileDialog::Options options = QFileDialog::ShowDirsOnly,
|
QFileDialog::Options options = QFileDialog::ShowDirsOnly,
|
||||||
bool fromDeviceIfShiftIsPressed = false,
|
bool fromDeviceIfShiftIsPressed = false,
|
||||||
bool forceNonNativeDialog = false);
|
bool forceNonNativeDialog = false);
|
||||||
|
|
||||||
static FilePaths getOpenFilePaths(QWidget *parent,
|
QTCREATOR_UTILS_EXPORT FilePaths getOpenFilePaths(
|
||||||
|
QWidget *parent,
|
||||||
const QString &caption,
|
const QString &caption,
|
||||||
const FilePath &dir = {},
|
const FilePath &dir = {},
|
||||||
const QString &filter = {},
|
const QString &filter = {},
|
||||||
QString *selectedFilter = nullptr,
|
QString *selectedFilter = nullptr,
|
||||||
QFileDialog::Options options = {});
|
QFileDialog::Options options = {});
|
||||||
#endif
|
#endif
|
||||||
};
|
|
||||||
|
} // namespace FileUtils
|
||||||
|
|
||||||
// for actually finding out if e.g. directories are writable on Windows
|
// for actually finding out if e.g. directories are writable on Windows
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
|
Reference in New Issue
Block a user