forked from qt-creator/qt-creator
Utils: Add FilePath::watch
Change-Id: I1e5706f22aaa072c68b97496896cfa6d244d9849 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -17,9 +17,11 @@
|
||||
#endif
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QFileSystemWatcher>
|
||||
#include <QOperatingSystemVersion>
|
||||
#include <QRandomGenerator>
|
||||
#include <QRegularExpression>
|
||||
#include <QStandardPaths>
|
||||
#include <QStorageInfo>
|
||||
#include <QTemporaryFile>
|
||||
|
||||
@@ -33,7 +35,6 @@
|
||||
#include <qplatformdefs.h>
|
||||
#endif
|
||||
|
||||
#include <QStandardPaths>
|
||||
#include <algorithm>
|
||||
#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()));
|
||||
}
|
||||
|
||||
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() = default;
|
||||
@@ -767,6 +775,29 @@ expected_str<FilePath> 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<std::unique_ptr<FilePathWatcher>> DesktopDeviceFileAccess::watch(
|
||||
const FilePath &path) const
|
||||
{
|
||||
return std::make_unique<DesktopFilePathWatcher>(path);
|
||||
}
|
||||
|
||||
QDateTime DesktopDeviceFileAccess::lastModified(const FilePath &filePath) const
|
||||
{
|
||||
return QFileInfo(filePath.path()).lastModified();
|
||||
|
@@ -72,6 +72,8 @@ protected:
|
||||
const QByteArray &data) const;
|
||||
|
||||
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
|
||||
@@ -128,6 +130,7 @@ protected:
|
||||
|
||||
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
|
||||
|
@@ -313,6 +313,11 @@ bool FilePath::equalsCaseSensitive(const FilePath &other) const
|
||||
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.
|
||||
|
||||
|
@@ -55,6 +55,16 @@ public:
|
||||
|
||||
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
|
||||
{
|
||||
public:
|
||||
@@ -270,6 +280,8 @@ public:
|
||||
|
||||
bool equalsCaseSensitive(const FilePath &other) const;
|
||||
|
||||
Utils::expected_str<std::unique_ptr<FilePathWatcher>> watch() const;
|
||||
|
||||
private:
|
||||
// These are needed.
|
||||
QTCREATOR_UTILS_EXPORT friend bool operator==(const FilePath &first, const FilePath &second);
|
||||
|
Reference in New Issue
Block a user