documented; refs #116

master
Marc Wäckerlin 12 years ago
parent 9802fa5b1d
commit b7279a5eab
  1. 34
      src/qbrowserlib/filestorage.hxx

@ -14,29 +14,44 @@
#include <cassert>
//! Store string lists.
/** Abstract storage interface to string lists, such as bookmarks. */
class Storage: public QObject {
Q_OBJECT;
Q_SIGNALS:
//! Emitted if file content has changed.
void changed();
public:
Storage() {}
/*! @return true if readable or writable */
bool valid() {
return readable() || writeable();
}
/*! @return true if valid */
operator bool() {
return valid();
}
/*! @return true if storage object already exists */
virtual bool readable() = 0;
/*! @return true if storage object exists or could be created */
virtual bool writeable() = 0;
/*! @return storage content as string list */
virtual QStringList read() = 0;
//! Writes storage content from string list.
virtual bool write(const QStringList& out) = 0;
};
//! Implement @ref Storage for files.
class FileStorage: public Storage {
Q_OBJECT;
public:
/*! @param file full path to storage file */
FileStorage(QString file): _file(file) {
if (valid()) setupWatcher();
_watcher.addPath(QFileInfo(_file).absolutePath());
assert(connect(&_watcher, SIGNAL(directoryChanged(const QString&)),
SLOT(setupWatcher())));
assert(connect(&_watcher, SIGNAL(fileChanged(const QString&)),
SIGNAL(changed())));
}
bool readable() {
return QFileInfo(_file).exists();
@ -66,21 +81,10 @@ class FileStorage: public Storage {
}
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());
}
_watcher.removePaths(_watcher.files());
if (readable()) _watcher.addPath(_file.fileName());
}
private:
QFile _file;
QFileSystemWatcher* _watcher;
QFileSystemWatcher _watcher;
};

Loading…
Cancel
Save