tested except for the changed event; refs #116

This commit is contained in:
Marc Wäckerlin
2012-04-18 09:40:57 +00:00
parent 60af8e2687
commit 9802fa5b1d
3 changed files with 43 additions and 12 deletions

View File

@@ -6,6 +6,7 @@
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
#include <QtCore/QFile>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QString>
@@ -35,18 +36,15 @@ class FileStorage: public Storage {
Q_OBJECT;
public:
FileStorage(QString file): _file(file) {
if (valid()) {
assert(connect(new QFileSystemWatcher(QStringList()<<_file.fileName(),
this),
SIGNAL(fileChanged(const QString&)),
SLOT(changed())));
}
if (valid()) setupWatcher();
}
bool readable() {
return QFileInfo(_file).exists();
}
bool writeable() {
return readable() || !_file.fileName().isEmpty();
return readable() ||
(!_file.fileName().isEmpty() &&
QFileInfo(_file).absoluteDir().exists());
}
QStringList read() {
QStringList res;
@@ -66,7 +64,23 @@ class FileStorage: public Storage {
}
return res;
}
private Q_SLOTS:
void setupWatcher() {
if (!_watcher) {
_watcher = new QFileSystemWatcher(this);
_watcher->addPath(QFileInfo(_file).absolutePath());
assert(connect(_watcher, SIGNAL(directoryChanged(const QString&)),
SLOT(setupWatcher())));
assert(connect(_watcher, SIGNAL(fileChanged(const QString&)),
SIGNAL(changed())));
}
if (readable()) {
_watcher->removePaths(_watcher->files());
_watcher->addPath(_file.fileName());
}
}
private:
QFile _file;
QFileSystemWatcher* _watcher;
};