Skip to content

Commit

Permalink
improve filesystemwatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmolka committed Oct 6, 2018
1 parent 50358ad commit e6831be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/core/filesystem/filesystemwatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "filesystemwatcher.hpp"

#include <QDirIterator>
#include <QFileInfo>

FileSystemWatcher::FileSystemWatcher(QObject *parent)
: QObject(parent)
, m_watcher(this)
, m_timer(this)
, m_ignoreDir(false)
, m_lastSize(0)
{
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, &FileSystemWatcher::onFileChanged);
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemWatcher::onDirectoryChanged);
Expand Down Expand Up @@ -55,9 +57,30 @@ void FileSystemWatcher::onDirectoryChanged(const Path &dir)

void FileSystemWatcher::onTimerTimeout()
{
const qint64 size = dirSize(m_bufferedDir);
if (size != m_lastSize)
{
m_lastSize = size;
m_timer.start(s_bufferDuration);
return;
}

if (!m_ignoreDir)
emit directoryChanged(m_bufferedDir);

m_bufferedDir = Path();
m_ignoreDir = false;
m_lastSize = 0;
}

qint64 FileSystemWatcher::dirSize(const Path &dir)
{
qint64 size = 0;
QDirIterator dirIter(dir, QDir::Files, QDirIterator::Subdirectories);
while (dirIter.hasNext())
{
dirIter.next();
size += dirIter.fileInfo().size();
}
return size;
}
5 changes: 4 additions & 1 deletion src/core/filesystem/filesystemwatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ private slots:
void onTimerTimeout();

private:
static constexpr int s_bufferDuration{1000};
static qint64 dirSize(const Path &dir);

static constexpr int s_bufferDuration{500};

QFileSystemWatcher m_watcher;
QTimer m_timer;
Path m_bufferedDir;
bool m_ignoreDir;
qint64 m_lastSize;
};

#endif // FILESYSTEMWATCHER_HPP

0 comments on commit e6831be

Please sign in to comment.