Files
surfer/src/qbrowserlib/filestorage.hxx

97 lines
2.8 KiB
C++
Raw Normal View History

2012-04-17 14:50:35 +00:00
/*! @file
@id $Id$
*/
// 1 2 3 4 5 6 7 8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
#include <QtCore/QFile>
#include <QtCore/QDir>
2012-04-17 14:50:35 +00:00
#include <QtCore/QFileInfo>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <cassert>
2012-04-18 11:33:57 +00:00
//! Store string lists.
/** Abstract storage interface to string lists, such as bookmarks. */
2012-04-17 14:50:35 +00:00
class Storage: public QObject {
Q_OBJECT;
Q_SIGNALS:
2012-04-18 11:33:57 +00:00
//! Emitted if file content has changed.
2012-04-17 14:50:35 +00:00
void changed();
public:
Storage() {}
2012-04-18 11:33:57 +00:00
/*! @return true if readable or writable */
2012-04-17 14:50:35 +00:00
bool valid() {
return readable() || writeable();
}
2012-04-18 11:33:57 +00:00
/*! @return true if valid */
2012-04-17 14:50:35 +00:00
operator bool() {
return valid();
}
2012-04-18 11:33:57 +00:00
/*! @return true if storage object already exists */
2012-04-17 14:50:35 +00:00
virtual bool readable() = 0;
2012-04-18 11:33:57 +00:00
/*! @return true if storage object exists or could be created */
2012-04-17 14:50:35 +00:00
virtual bool writeable() = 0;
2012-04-18 11:33:57 +00:00
/*! @return storage content as string list */
2012-04-17 14:50:35 +00:00
virtual QStringList read() = 0;
2012-04-18 11:33:57 +00:00
//! Writes storage content from string list.
2012-04-17 14:50:35 +00:00
virtual bool write(const QStringList& out) = 0;
};
2012-04-18 11:33:57 +00:00
//! Implement @ref Storage for files.
2012-04-17 14:50:35 +00:00
class FileStorage: public Storage {
Q_OBJECT;
public:
2012-04-18 11:33:57 +00:00
/*! @param file full path to storage file */
2012-04-17 14:50:35 +00:00
FileStorage(QString file): _file(file) {
2012-04-18 11:33:57 +00:00
_watcher.addPath(QFileInfo(_file).absolutePath());
assert(connect(&_watcher, SIGNAL(directoryChanged(const QString&)),
SLOT(setupWatcher())));
assert(connect(&_watcher, SIGNAL(fileChanged(const QString&)),
SIGNAL(changed())));
2012-04-17 14:50:35 +00:00
}
bool readable() {
return QFileInfo(_file).exists();
}
bool writeable() {
return readable() ||
(!_file.fileName().isEmpty() &&
QFileInfo(_file).absoluteDir().exists());
2012-04-17 14:50:35 +00:00
}
QStringList read() {
QStringList res;
if (readable()) {
if (_file.open(QIODevice::ReadOnly))
res=QString::fromUtf8(_file.readAll()).split("\n");
_file.close();
}
return res;
}
bool write(const QStringList& out) {
bool res(false);
if (writeable()) {
if (_file.open(QIODevice::WriteOnly))
if (_file.write(out.join("\n").toUtf8())>=0) res=true;
_file.close();
}
return res;
}
private Q_SLOTS:
void setupWatcher() {
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
changed();
}
}
2012-04-17 14:50:35 +00:00
private:
QFile _file;
2012-04-18 11:33:57 +00:00
QFileSystemWatcher _watcher;
2012-04-17 14:50:35 +00:00
};