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
#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();