Utils: Split FilePath off fileutils.h

This now rather central class deserves a suitably named file(pair).

The rarer used declarations including otherwise not needed includes are
still in fileutils.h.

Change-Id: Ib18d72f16322c03f76023fdefcd5f6da35311b8b
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-07-16 11:16:45 +02:00
parent 805d19d1ed
commit fd677101a9
12 changed files with 1617 additions and 1499 deletions

View File

@@ -52,6 +52,7 @@ add_qtc_library(Utils
filecrumblabel.cpp filecrumblabel.h
fileinprojectfinder.cpp fileinprojectfinder.h
filenamevalidatinglineedit.cpp filenamevalidatinglineedit.h
filepath.cpp filepath.h
filesearch.cpp filesearch.h
filesystemwatcher.cpp filesystemwatcher.h
fileutils.cpp fileutils.h

1329
src/libs/utils/filepath.cpp Normal file

File diff suppressed because it is too large Load Diff

198
src/libs/utils/filepath.h Normal file
View File

@@ -0,0 +1,198 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
#include "hostosinfo.h"
#include <QDir>
#include <QMetaType>
#include <functional>
#include <memory>
QT_BEGIN_NAMESPACE
class QDateTime;
class QDebug;
class QFileInfo;
class QUrl;
QT_END_NAMESPACE
class tst_fileutils; // This becomes a friend of Utils::FilePath for testing private methods.
namespace Utils {
class Environment;
class QTCREATOR_UTILS_EXPORT FilePath
{
public:
FilePath();
static FilePath fromString(const QString &filepath);
static FilePath fromFileInfo(const QFileInfo &info);
static FilePath fromStringWithExtension(const QString &filepath, const QString &defaultExtension);
static FilePath fromUserInput(const QString &filepath);
static FilePath fromUtf8(const char *filepath, int filepathSize = -1);
static FilePath fromVariant(const QVariant &variant);
QString toString() const;
FilePath onDevice(const FilePath &deviceTemplate) const;
FilePath withNewPath(const QString &newPath) const;
QFileInfo toFileInfo() const;
QVariant toVariant() const;
QDir toDir() const;
QString toUserOutput() const;
QString shortNativePath() const;
QString fileName() const;
QString fileNameWithPathComponents(int pathComponents) const;
QString baseName() const;
QString completeBaseName() const;
QString suffix() const;
QString completeSuffix() const;
QString scheme() const { return m_scheme; }
void setScheme(const QString &scheme);
QString host() const { return m_host; }
void setHost(const QString &host);
QString path() const { return m_data; }
void setPath(const QString &path) { m_data = path; }
bool needsDevice() const;
bool exists() const;
bool isWritablePath() const { return isWritableDir(); } // Remove.
bool isWritableDir() const;
bool isWritableFile() const;
bool ensureWritableDir() const;
bool ensureExistingFile() const;
bool isExecutableFile() const;
bool isReadableFile() const;
bool isReadableDir() const;
bool isRelativePath() const;
bool isAbsolutePath() const { return !isRelativePath(); }
bool isFile() const;
bool isDir() const;
bool createDir() const;
QList<FilePath> dirEntries(const QStringList &nameFilters,
QDir::Filters filters,
QDir::SortFlags sort = QDir::NoSort) const;
QList<FilePath> dirEntries(QDir::Filters filters) const;
QByteArray fileContents(qint64 maxSize = -1, qint64 offset = 0) const;
bool writeFileContents(const QByteArray &data) const;
FilePath parentDir() const;
FilePath absolutePath() const;
FilePath absoluteFilePath() const;
FilePath absoluteFilePath(const FilePath &tail) const;
FilePath absoluteFromRelativePath(const FilePath &anchor) const;
bool operator==(const FilePath &other) const;
bool operator!=(const FilePath &other) const;
bool operator<(const FilePath &other) const;
bool operator<=(const FilePath &other) const;
bool operator>(const FilePath &other) const;
bool operator>=(const FilePath &other) const;
FilePath operator+(const QString &s) const;
bool isChildOf(const FilePath &s) const;
bool isChildOf(const QDir &dir) const;
bool startsWith(const QString &s) const;
bool endsWith(const QString &s) const;
bool isNewerThan(const QDateTime &timeStamp) const;
QDateTime lastModified() const;
QFile::Permissions permissions() const;
OsType osType() const;
bool removeFile() const;
bool removeRecursively(QString *error = nullptr) const;
bool copyFile(const FilePath &target) const;
bool renameFile(const FilePath &target) const;
Qt::CaseSensitivity caseSensitivity() const;
FilePath relativeChildPath(const FilePath &parent) const;
FilePath relativePath(const FilePath &anchor) const;
FilePath pathAppended(const QString &str) const;
FilePath stringAppended(const QString &str) const;
FilePath resolvePath(const QString &fileName) const;
FilePath cleanPath() const;
FilePath canonicalPath() const;
FilePath symLinkTarget() const;
FilePath resolveSymlinks() const;
FilePath withExecutableSuffix() const;
FilePath operator/(const QString &str) const;
void clear();
bool isEmpty() const;
uint hash(uint seed) const;
// NOTE: Most FilePath operations on FilePath created from URL currently
// do not work. Among the working are .toVariant() and .toUrl().
static FilePath fromUrl(const QUrl &url);
QUrl toUrl() const;
FilePath searchOnDevice(const QList<FilePath> &dirs) const;
Environment deviceEnvironment() const;
static QString formatFilePaths(const QList<FilePath> &files, const QString &separator);
static void removeDuplicates(QList<FilePath> &files);
static void sort(QList<FilePath> &files);
static QList<FilePath> filterEntriesHelper(const FilePath &base,
const QStringList &entries,
const QStringList &nameFilters,
QDir::Filters filters,
QDir::SortFlags sort);
private:
friend class ::tst_fileutils;
static QString calcRelativePath(const QString &absolutePath, const QString &absoluteAnchorPath);
QString m_scheme;
QString m_host;
QString m_data;
};
using FilePaths = QList<FilePath>;
} // namespace Utils
QT_BEGIN_NAMESPACE
QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug dbg, const Utils::FilePath &c);
QT_END_NAMESPACE
Q_DECLARE_METATYPE(Utils::FilePath)

File diff suppressed because it is too large Load Diff

View File

@@ -27,6 +27,7 @@
#include "utils_global.h"
#include "filepath.h"
#include "hostosinfo.h"
#include <QCoreApplication>
@@ -40,33 +41,17 @@
#include <functional>
#include <memory>
namespace Utils {
class Environment;
class FilePath;
} // Utils
QT_BEGIN_NAMESPACE
class QDataStream;
class QDateTime;
class QDir;
class QFile;
class QFileInfo;
class QTemporaryFile;
class QTextStream;
class QWidget;
QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug dbg, const Utils::FilePath &c);
// for withNtfsPermissions
#ifdef Q_OS_WIN
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
#endif
QT_END_NAMESPACE
// tst_fileutils becomes a friend of Utils::FilePath for testing private method
class tst_fileutils;
namespace Utils {
class DeviceFileHooks
@@ -99,151 +84,6 @@ public:
std::function<Environment(const FilePath &)> environment;
};
class QTCREATOR_UTILS_EXPORT FilePath
{
public:
FilePath();
static FilePath fromString(const QString &filepath);
static FilePath fromFileInfo(const QFileInfo &info);
static FilePath fromStringWithExtension(const QString &filepath, const QString &defaultExtension);
static FilePath fromUserInput(const QString &filepath);
static FilePath fromUtf8(const char *filepath, int filepathSize = -1);
static FilePath fromVariant(const QVariant &variant);
QString toString() const;
FilePath onDevice(const FilePath &deviceTemplate) const;
FilePath withNewPath(const QString &newPath) const;
QFileInfo toFileInfo() const;
QVariant toVariant() const;
QDir toDir() const;
QString toUserOutput() const;
QString shortNativePath() const;
QString fileName() const;
QString fileNameWithPathComponents(int pathComponents) const;
QString baseName() const;
QString completeBaseName() const;
QString suffix() const;
QString completeSuffix() const;
QString scheme() const { return m_scheme; }
void setScheme(const QString &scheme);
QString host() const { return m_host; }
void setHost(const QString &host);
QString path() const { return m_data; }
void setPath(const QString &path) { m_data = path; }
bool needsDevice() const;
bool exists() const;
bool isWritablePath() const { return isWritableDir(); } // Remove.
bool isWritableDir() const;
bool isWritableFile() const;
bool ensureWritableDir() const;
bool ensureExistingFile() const;
bool isExecutableFile() const;
bool isReadableFile() const;
bool isReadableDir() const;
bool isRelativePath() const;
bool isAbsolutePath() const { return !isRelativePath(); }
bool isFile() const;
bool isDir() const;
bool createDir() const;
QList<FilePath> dirEntries(const QStringList &nameFilters,
QDir::Filters filters,
QDir::SortFlags sort = QDir::NoSort) const;
QList<FilePath> dirEntries(QDir::Filters filters) const;
QByteArray fileContents(qint64 maxSize = -1, qint64 offset = 0) const;
bool writeFileContents(const QByteArray &data) const;
FilePath parentDir() const;
FilePath absolutePath() const;
FilePath absoluteFilePath() const;
FilePath absoluteFilePath(const FilePath &tail) const;
FilePath absoluteFromRelativePath(const FilePath &anchor) const;
bool operator==(const FilePath &other) const;
bool operator!=(const FilePath &other) const;
bool operator<(const FilePath &other) const;
bool operator<=(const FilePath &other) const;
bool operator>(const FilePath &other) const;
bool operator>=(const FilePath &other) const;
FilePath operator+(const QString &s) const;
bool isChildOf(const FilePath &s) const;
bool isChildOf(const QDir &dir) const;
bool startsWith(const QString &s) const;
bool endsWith(const QString &s) const;
bool isNewerThan(const QDateTime &timeStamp) const;
QDateTime lastModified() const;
QFile::Permissions permissions() const;
OsType osType() const;
bool removeFile() const;
bool removeRecursively(QString *error = nullptr) const;
bool copyFile(const FilePath &target) const;
bool renameFile(const FilePath &target) const;
Qt::CaseSensitivity caseSensitivity() const;
FilePath relativeChildPath(const FilePath &parent) const;
FilePath relativePath(const FilePath &anchor) const;
FilePath pathAppended(const QString &str) const;
FilePath stringAppended(const QString &str) const;
FilePath resolvePath(const QString &fileName) const;
FilePath cleanPath() const;
FilePath canonicalPath() const;
FilePath symLinkTarget() const;
FilePath resolveSymlinks() const;
FilePath withExecutableSuffix() const;
FilePath operator/(const QString &str) const;
void clear();
bool isEmpty() const;
uint hash(uint seed) const;
// NOTE: Most FilePath operations on FilePath created from URL currently
// do not work. Among the working are .toVariant() and .toUrl().
static FilePath fromUrl(const QUrl &url);
QUrl toUrl() const;
static void setDeviceFileHooks(const DeviceFileHooks &hooks);
FilePath searchOnDevice(const QList<FilePath> &dirs) const;
Environment deviceEnvironment() const;
static QString formatFilePaths(const QList<FilePath> &files, const QString &separator);
static void removeDuplicates(QList<FilePath> &files);
static void sort(QList<FilePath> &files);
static QList<FilePath> filterEntriesHelper(const FilePath &base,
const QStringList &entries,
const QStringList &nameFilters,
QDir::Filters filters,
QDir::SortFlags sort);
private:
friend class ::tst_fileutils;
static QString calcRelativePath(const QString &absolutePath, const QString &absoluteAnchorPath);
QString m_scheme;
QString m_host;
QString m_data;
};
QTCREATOR_UTILS_EXPORT QTextStream &operator<<(QTextStream &s, const FilePath &fn);
using FilePaths = QList<FilePath>;
class QTCREATOR_UTILS_EXPORT FileUtils {
public:
#ifdef QT_GUI_LIB
@@ -286,6 +126,8 @@ public:
static QByteArray fileId(const FilePath &fileName);
static FilePath homePath();
static bool renameFile(const FilePath &srcFilePath, const FilePath &tgtFilePath);
static void setDeviceFileHooks(const DeviceFileHooks &hooks);
};
template<typename T>
@@ -431,6 +273,8 @@ private:
bool m_autoRemove = true;
};
QTCREATOR_UTILS_EXPORT QTextStream &operator<<(QTextStream &s, const FilePath &fn);
inline uint qHash(const Utils::FilePath &a, uint seed = 0) { return a.hash(seed); }
} // namespace Utils
@@ -444,4 +288,3 @@ template<> struct QTCREATOR_UTILS_EXPORT hash<Utils::FilePath>
};
} // namespace std
Q_DECLARE_METATYPE(Utils::FilePath)

View File

@@ -62,6 +62,7 @@ SOURCES += \
$$PWD/fancylineedit.cpp \
$$PWD/qtcolorbutton.cpp \
$$PWD/savefile.cpp \
$$PWD/filepath.cpp \
$$PWD/fileutils.cpp \
$$PWD/textfileformat.cpp \
$$PWD/consoleprocess.cpp \
@@ -193,6 +194,7 @@ HEADERS += \
$$PWD/qtcolorbutton.h \
$$PWD/consoleprocess.h \
$$PWD/savefile.h \
$$PWD/filepath.h \
$$PWD/fileutils.h \
$$PWD/textfileformat.h \
$$PWD/uncommentselection.h \

View File

@@ -115,6 +115,8 @@ Project {
"fileinprojectfinder.h",
"filenamevalidatinglineedit.cpp",
"filenamevalidatinglineedit.h",
"filepath.cpp",
"filepath.h",
"filesearch.cpp",
"filesearch.h",
"filesystemwatcher.cpp",

View File

@@ -525,7 +525,7 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
return device->systemEnvironment();
};
FilePath::setDeviceFileHooks(deviceHooks);
FileUtils::setDeviceFileHooks(deviceHooks);
DeviceProcessHooks processHooks;

View File

@@ -79,6 +79,7 @@ extend_qtc_executable(sdktool
DEFINES QTCREATOR_UTILS_STATIC_LIB
SOURCES
environment.cpp environment.h
filepath.cpp filepath.h
fileutils.cpp fileutils.h
hostosinfo.cpp hostosinfo.h
namevaluedictionary.cpp namevaluedictionary.h

View File

@@ -32,6 +32,7 @@ SOURCES += \
rmtoolchainoperation.cpp \
settings.cpp \
$$UTILS/environment.cpp \
$$UTILS/filepath.cpp \
$$UTILS/fileutils.cpp \
$$UTILS/hostosinfo.cpp \
$$UTILS/namevaluedictionary.cpp \
@@ -65,6 +66,7 @@ HEADERS += \
rmtoolchainoperation.h \
settings.h \
$$UTILS/environment.h \
$$UTILS/filepath.h \
$$UTILS/fileutils.h \
$$UTILS/hostosinfo.h \
$$UTILS/namevaluedictionary.h \

View File

@@ -70,6 +70,7 @@ QtcTool {
files: [
"commandline.cpp", "commandline.h",
"environment.cpp", "environment.h",
"filepath.cpp", "filepath.h",
"fileutils.cpp", "fileutils.h",
"hostosinfo.cpp", "hostosinfo.h",
"namevaluedictionary.cpp", "namevaluedictionary.h",

View File

@@ -19,6 +19,7 @@ HEADERS += \
SOURCES += \
$$UTILSDIR/commandline.cpp \
$$UTILSDIR/environment.cpp \
$$UTILSDIR/filepath.cpp \
$$UTILSDIR/fileutils.cpp \
$$UTILSDIR/hostosinfo.cpp \
$$UTILSDIR/namevaluedictionary.cpp \
@@ -31,6 +32,7 @@ HEADERS += \
HEADERS += \
$$UTILSDIR/commandline.h \
$$UTILSDIR/environment.h \
$$UTILSDIR/filepath.h \
$$UTILSDIR/fileutils.h \
$$UTILSDIR/hostosinfo.h \
$$UTILSDIR/namevaluedictionary.h \