use timer to check filesystem; refs #155
This commit is contained in:
@@ -9,9 +9,10 @@
|
|||||||
#include <QtCore/QFile>
|
#include <QtCore/QFile>
|
||||||
#include <QtCore/QDir>
|
#include <QtCore/QDir>
|
||||||
#include <QtCore/QFileInfo>
|
#include <QtCore/QFileInfo>
|
||||||
#include <QtCore/QFileSystemWatcher>
|
|
||||||
#include <QtCore/QString>
|
#include <QtCore/QString>
|
||||||
#include <QtCore/QStringList>
|
#include <QtCore/QStringList>
|
||||||
|
#include <QtCore/QTimer>
|
||||||
|
#include <QtCore/QDateTime>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@@ -59,24 +60,22 @@ class FileStorage: public Storage {
|
|||||||
Q_OBJECT;
|
Q_OBJECT;
|
||||||
public:
|
public:
|
||||||
/*! @param file full path to storage file */
|
/*! @param file full path to storage file */
|
||||||
FileStorage(QString file): _file(file) {
|
FileStorage(QString file, int msec=1000): _file(file), _fileInfo(_file) {
|
||||||
TRC; LOG<<"file: "<<file;
|
TRC; LOG<<"file: "<<file;
|
||||||
_watcher.addPath(QFileInfo(_file).absolutePath());
|
assert(connect(&_timer, SIGNAL(timeout()), SLOT(check())));
|
||||||
assert(connect(&_watcher, SIGNAL(directoryChanged(const QString&)),
|
_timer.setInterval(msec);
|
||||||
SLOT(setupWatcher())));
|
_timer.start();
|
||||||
assert(connect(&_watcher, SIGNAL(fileChanged(const QString&)),
|
|
||||||
SLOT(emitChanged())));
|
|
||||||
}
|
}
|
||||||
QString name() {
|
QString name() {
|
||||||
return _file.fileName();
|
return _file.fileName();
|
||||||
}
|
}
|
||||||
bool readable() {
|
bool readable() {
|
||||||
return QFileInfo(_file).exists();
|
return _fileInfo.exists();
|
||||||
}
|
}
|
||||||
bool writeable() {
|
bool writeable() {
|
||||||
return readable() ||
|
return readable() ||
|
||||||
(!_file.fileName().isEmpty() &&
|
(!_file.fileName().isEmpty() &&
|
||||||
QFileInfo(_file).absoluteDir().exists());
|
_fileInfo.absoluteDir().exists());
|
||||||
}
|
}
|
||||||
QStringList read() {
|
QStringList read() {
|
||||||
TRC;
|
TRC;
|
||||||
@@ -99,18 +98,17 @@ class FileStorage: public Storage {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void setupWatcher() {
|
void check() {
|
||||||
TRC;
|
TRC;
|
||||||
bool watching(_watcher.files().size());
|
if (_modified!=_fileInfo.lastModified()) {
|
||||||
if (watching) // remove watchlist if already existent
|
LOG<<"file has been modified";
|
||||||
_watcher.removePaths(_watcher.files());
|
_modified = _fileInfo.lastModified();
|
||||||
if (readable()) { // add file to watchlist
|
emitChanged();
|
||||||
_watcher.addPath(_file.fileName());
|
|
||||||
if (!watching) // send change event if file is initially created
|
|
||||||
emitChanged();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
QFile _file;
|
QFile _file;
|
||||||
QFileSystemWatcher _watcher;
|
QFileInfo _fileInfo;
|
||||||
|
QDateTime _modified;
|
||||||
|
QTimer _timer;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user