diff --git a/src/libs/utils/devicefileaccess.cpp b/src/libs/utils/devicefileaccess.cpp index 303ffa6bf50..c8f920c07f7 100644 --- a/src/libs/utils/devicefileaccess.cpp +++ b/src/libs/utils/devicefileaccess.cpp @@ -17,9 +17,11 @@ #endif #include +#include #include #include #include +#include #include #include @@ -33,7 +35,6 @@ #include #endif -#include #include #include @@ -389,6 +390,13 @@ expected_str DeviceFileAccess::createTempFile(const FilePath &filePath Tr::tr("createTempFile is not implemented for \"%1\".").arg(filePath.toUserOutput())); } +Utils::expected_str> DeviceFileAccess::watch( + const FilePath &path) const +{ + Q_UNUSED(path); + return make_unexpected(Tr::tr("watch is not implemented.")); +} + // DesktopDeviceFileAccess DesktopDeviceFileAccess::~DesktopDeviceFileAccess() = default; @@ -767,6 +775,29 @@ expected_str DesktopDeviceFileAccess::createTempFile(const FilePath &f 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> DesktopDeviceFileAccess::watch( + const FilePath &path) const +{ + return std::make_unique(path); +} + QDateTime DesktopDeviceFileAccess::lastModified(const FilePath &filePath) const { return QFileInfo(filePath.path()).lastModified(); diff --git a/src/libs/utils/devicefileaccess.h b/src/libs/utils/devicefileaccess.h index 8a0884ec1d0..451fb2db4be 100644 --- a/src/libs/utils/devicefileaccess.h +++ b/src/libs/utils/devicefileaccess.h @@ -72,6 +72,8 @@ protected: const QByteArray &data) const; virtual expected_str createTempFile(const FilePath &filePath); + + virtual Utils::expected_str> watch(const FilePath &path) const; }; class QTCREATOR_UTILS_EXPORT DesktopDeviceFileAccess : public DeviceFileAccess @@ -128,6 +130,7 @@ protected: expected_str createTempFile(const FilePath &filePath) override; + Utils::expected_str> watch(const FilePath &path) const override; }; class QTCREATOR_UTILS_EXPORT UnixDeviceFileAccess : public DeviceFileAccess diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index c4d77a29efc..23a6ff24129 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -313,6 +313,11 @@ bool FilePath::equalsCaseSensitive(const FilePath &other) const return equals(*this, other, Qt::CaseSensitive); } +Utils::expected_str> FilePath::watch() const +{ + return fileAccess()->watch(*this); +} + /*! Returns a QString for passing on to QString based APIs. diff --git a/src/libs/utils/filepath.h b/src/libs/utils/filepath.h index e742bf1a0de..d1845571203 100644 --- a/src/libs/utils/filepath.h +++ b/src/libs/utils/filepath.h @@ -55,6 +55,16 @@ public: using FilePaths = QList; +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 { public: @@ -270,6 +280,8 @@ public: bool equalsCaseSensitive(const FilePath &other) const; + Utils::expected_str> watch() const; + private: // These are needed. QTCREATOR_UTILS_EXPORT friend bool operator==(const FilePath &first, const FilePath &second);