forked from quickshell/quickshell
io/fileview: add support for watching changes
This commit is contained in:
parent
ccf885081c
commit
69d13967c9
2 changed files with 71 additions and 3 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue