io/fileview: add support for watching changes

This commit is contained in:
outfoxxed 2024-12-06 02:32:19 -08:00
parent ccf885081c
commit 69d13967c9
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
2 changed files with 71 additions and 3 deletions

View file

@ -6,6 +6,7 @@
#include <qdir.h>
#include <qfiledevice.h>
#include <qfileinfo.h>
#include <qfilesystemwatcher.h>
#include <qlogging.h>
#include <qloggingcategory.h>
#include <qmutex.h>
@ -280,7 +281,6 @@ void FileViewWriter::write(
if (shouldCancel.loadAcquire()) return;
if (doAtomicWrite) {
qDebug() << "Atomic commit";
if (!reinterpret_cast<QSaveFile*>(file.get())->commit()) { // NOLINT
qmlWarning(view) << "Write of " << state.path << " failed: Atomic commit failed.";
}
@ -477,6 +477,49 @@ void FileView::updatePath() {
} else {
this->emitDataChanged();
}
this->updateWatchedFiles();
}
void FileView::updateWatchedFiles() {
delete this->watcher;
if (!this->targetPath.isEmpty() && this->bWatchChanges) {
qCDebug(logFileView) << "Creating watcher for" << this << "at" << this->targetPath;
this->watcher = new QFileSystemWatcher(this);
this->watcher->addPath(this->targetPath);
this->watcher->addPath(QDir(this->targetPath).dirName());
QObject::connect(
this->watcher,
&QFileSystemWatcher::fileChanged,
this,
&FileView::onWatchedFileChanged
);
QObject::connect(
this->watcher,
&QFileSystemWatcher::directoryChanged,
this,
&FileView::onWatchedDirectoryChanged
);
}
}
void FileView::onWatchedFileChanged() {
if (!this->watcher->files().contains(this->targetPath)) {
this->watcher->addPath(this->targetPath);
}
emit this->fileChanged();
}
void FileView::onWatchedDirectoryChanged() {
if (!this->watcher->files().contains(this->targetPath) && QFileInfo(this->targetPath).exists()) {
// the file was just created
this->watcher->addPath(this->targetPath);
emit this->fileChanged();
}
}
bool FileView::shouldBlockRead() const {