Utils: Add FilePath::watch

Change-Id: I1e5706f22aaa072c68b97496896cfa6d244d9849
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-06-20 12:40:21 +02:00
parent 729c803758
commit a1a6ccbf1c
4 changed files with 52 additions and 1 deletions

View File

@@ -17,9 +17,11 @@
#endif #endif
#include <QCoreApplication> #include <QCoreApplication>
#include <QFileSystemWatcher>
#include <QOperatingSystemVersion> #include <QOperatingSystemVersion>
#include <QRandomGenerator> #include <QRandomGenerator>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStandardPaths>
#include <QStorageInfo> #include <QStorageInfo>
#include <QTemporaryFile> #include <QTemporaryFile>
@@ -33,7 +35,6 @@
#include <qplatformdefs.h> #include <qplatformdefs.h>
#endif #endif
#include <QStandardPaths>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
@@ -389,6 +390,13 @@ expected_str<FilePath> DeviceFileAccess::createTempFile(const FilePath &filePath
Tr::tr("createTempFile is not implemented for \"%1\".").arg(filePath.toUserOutput())); Tr::tr("createTempFile is not implemented for \"%1\".").arg(filePath.toUserOutput()));
} }
Utils::expected_str<std::unique_ptr<FilePathWatcher>> DeviceFileAccess::watch(
const FilePath &path) const
{
Q_UNUSED(path);
return make_unexpected(Tr::tr("watch is not implemented."));
}
// DesktopDeviceFileAccess // DesktopDeviceFileAccess
DesktopDeviceFileAccess::~DesktopDeviceFileAccess() = default; DesktopDeviceFileAccess::~DesktopDeviceFileAccess() = default;
@@ -767,6 +775,29 @@ expected_str<FilePath> DesktopDeviceFileAccess::createTempFile(const FilePath &f
return filePath.withNewPath(file.fileName()); return filePath.withNewPath(file.fileName());
} }
class DesktopFilePathWatcher : public FilePathWatcher
{
QFileSystemWatcher m_watcher;
public:
DesktopFilePathWatcher(const FilePath &path) {
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, [this, path] {
emit pathChanged(path);
});
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, [this, path] {
emit pathChanged(path);
});
m_watcher.addPath(path.path());
}
};
Utils::expected_str<std::unique_ptr<FilePathWatcher>> DesktopDeviceFileAccess::watch(
const FilePath &path) const
{
return std::make_unique<DesktopFilePathWatcher>(path);
}
QDateTime DesktopDeviceFileAccess::lastModified(const FilePath &filePath) const QDateTime DesktopDeviceFileAccess::lastModified(const FilePath &filePath) const
{ {
return QFileInfo(filePath.path()).lastModified(); return QFileInfo(filePath.path()).lastModified();

View File

@@ -72,6 +72,8 @@ protected:
const QByteArray &data) const; const QByteArray &data) const;
virtual expected_str<FilePath> createTempFile(const FilePath &filePath); virtual expected_str<FilePath> createTempFile(const FilePath &filePath);
virtual Utils::expected_str<std::unique_ptr<FilePathWatcher>> watch(const FilePath &path) const;
}; };
class QTCREATOR_UTILS_EXPORT DesktopDeviceFileAccess : public DeviceFileAccess class QTCREATOR_UTILS_EXPORT DesktopDeviceFileAccess : public DeviceFileAccess
@@ -128,6 +130,7 @@ protected:
expected_str<FilePath> createTempFile(const FilePath &filePath) override; expected_str<FilePath> createTempFile(const FilePath &filePath) override;
Utils::expected_str<std::unique_ptr<FilePathWatcher>> watch(const FilePath &path) const override;
}; };
class QTCREATOR_UTILS_EXPORT UnixDeviceFileAccess : public DeviceFileAccess class QTCREATOR_UTILS_EXPORT UnixDeviceFileAccess : public DeviceFileAccess

View File

@@ -313,6 +313,11 @@ bool FilePath::equalsCaseSensitive(const FilePath &other) const
return equals(*this, other, Qt::CaseSensitive); return equals(*this, other, Qt::CaseSensitive);
} }
Utils::expected_str<std::unique_ptr<FilePathWatcher>> FilePath::watch() const
{
return fileAccess()->watch(*this);
}
/*! /*!
Returns a QString for passing on to QString based APIs. Returns a QString for passing on to QString based APIs.

View File

@@ -55,6 +55,16 @@ public:
using FilePaths = QList<class FilePath>; using FilePaths = QList<class FilePath>;
class QTCREATOR_UTILS_EXPORT FilePathWatcher : public QObject
{
Q_OBJECT
public:
using QObject::QObject;
signals:
void pathChanged(const Utils::FilePath &path);
};
class QTCREATOR_UTILS_EXPORT FilePath class QTCREATOR_UTILS_EXPORT FilePath
{ {
public: public:
@@ -270,6 +280,8 @@ public:
bool equalsCaseSensitive(const FilePath &other) const; bool equalsCaseSensitive(const FilePath &other) const;
Utils::expected_str<std::unique_ptr<FilePathWatcher>> watch() const;
private: private:
// These are needed. // These are needed.
QTCREATOR_UTILS_EXPORT friend bool operator==(const FilePath &first, const FilePath &second); QTCREATOR_UTILS_EXPORT friend bool operator==(const FilePath &first, const FilePath &second);