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