|
|
@ -5,6 +5,7 @@ |
|
|
|
// 1 2 3 4 5 6 7 8
|
|
|
|
// 1 2 3 4 5 6 7 8
|
|
|
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
|
|
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <qbrowserlib/log.hxx> |
|
|
|
#include <QtCore/QFile> |
|
|
|
#include <QtCore/QFile> |
|
|
|
#include <QtCore/QDir> |
|
|
|
#include <QtCore/QDir> |
|
|
|
#include <QtCore/QFileInfo> |
|
|
|
#include <QtCore/QFileInfo> |
|
|
@ -22,7 +23,12 @@ class Storage: public QObject { |
|
|
|
//! Emitted if file content has changed.
|
|
|
|
//! Emitted if file content has changed.
|
|
|
|
void changed(); |
|
|
|
void changed(); |
|
|
|
public: |
|
|
|
public: |
|
|
|
Storage() {} |
|
|
|
Storage() { |
|
|
|
|
|
|
|
TRC; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
~Storage() { |
|
|
|
|
|
|
|
TRC; |
|
|
|
|
|
|
|
} |
|
|
|
/*! @return true if readable or writable */ |
|
|
|
/*! @return true if readable or writable */ |
|
|
|
bool valid() { |
|
|
|
bool valid() { |
|
|
|
return readable() || writeable(); |
|
|
|
return readable() || writeable(); |
|
|
@ -41,6 +47,11 @@ class Storage: public QObject { |
|
|
|
virtual QStringList read() = 0; |
|
|
|
virtual QStringList read() = 0; |
|
|
|
//! Writes storage content from string list.
|
|
|
|
//! Writes storage content from string list.
|
|
|
|
virtual bool write(const QStringList& out) = 0; |
|
|
|
virtual bool write(const QStringList& out) = 0; |
|
|
|
|
|
|
|
public Q_SLOTS: |
|
|
|
|
|
|
|
void emitChanged() { |
|
|
|
|
|
|
|
TRC; |
|
|
|
|
|
|
|
changed(); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
//! Implement @ref Storage for files.
|
|
|
|
//! Implement @ref Storage for files.
|
|
|
@ -49,11 +60,12 @@ class FileStorage: public Storage { |
|
|
|
public: |
|
|
|
public: |
|
|
|
/*! @param file full path to storage file */ |
|
|
|
/*! @param file full path to storage file */ |
|
|
|
FileStorage(QString file): _file(file) { |
|
|
|
FileStorage(QString file): _file(file) { |
|
|
|
|
|
|
|
TRC; LOG<<"file: "<<file; |
|
|
|
_watcher.addPath(QFileInfo(_file).absolutePath()); |
|
|
|
_watcher.addPath(QFileInfo(_file).absolutePath()); |
|
|
|
assert(connect(&_watcher, SIGNAL(directoryChanged(const QString&)), |
|
|
|
assert(connect(&_watcher, SIGNAL(directoryChanged(const QString&)), |
|
|
|
SLOT(setupWatcher()))); |
|
|
|
SLOT(setupWatcher()))); |
|
|
|
assert(connect(&_watcher, SIGNAL(fileChanged(const QString&)), |
|
|
|
assert(connect(&_watcher, SIGNAL(fileChanged(const QString&)), |
|
|
|
SIGNAL(changed()))); |
|
|
|
SLOT(emitChanged()))); |
|
|
|
} |
|
|
|
} |
|
|
|
QString name() { |
|
|
|
QString name() { |
|
|
|
return _file.fileName(); |
|
|
|
return _file.fileName(); |
|
|
@ -67,6 +79,7 @@ class FileStorage: public Storage { |
|
|
|
QFileInfo(_file).absoluteDir().exists()); |
|
|
|
QFileInfo(_file).absoluteDir().exists()); |
|
|
|
} |
|
|
|
} |
|
|
|
QStringList read() { |
|
|
|
QStringList read() { |
|
|
|
|
|
|
|
TRC; |
|
|
|
QStringList res; |
|
|
|
QStringList res; |
|
|
|
if (readable()) { |
|
|
|
if (readable()) { |
|
|
|
if (_file.open(QIODevice::ReadOnly)) |
|
|
|
if (_file.open(QIODevice::ReadOnly)) |
|
|
@ -76,6 +89,7 @@ class FileStorage: public Storage { |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
bool write(const QStringList& out) { |
|
|
|
bool write(const QStringList& out) { |
|
|
|
|
|
|
|
TRC; |
|
|
|
bool res(false); |
|
|
|
bool res(false); |
|
|
|
if (writeable()) { |
|
|
|
if (writeable()) { |
|
|
|
if (_file.open(QIODevice::WriteOnly)) |
|
|
|
if (_file.open(QIODevice::WriteOnly)) |
|
|
@ -86,13 +100,14 @@ class FileStorage: public Storage { |
|
|
|
} |
|
|
|
} |
|
|
|
private Q_SLOTS: |
|
|
|
private Q_SLOTS: |
|
|
|
void setupWatcher() { |
|
|
|
void setupWatcher() { |
|
|
|
|
|
|
|
TRC; |
|
|
|
bool watching(_watcher.files().size()); |
|
|
|
bool watching(_watcher.files().size()); |
|
|
|
if (watching) // remove watchlist if already existent
|
|
|
|
if (watching) // remove watchlist if already existent
|
|
|
|
_watcher.removePaths(_watcher.files()); |
|
|
|
_watcher.removePaths(_watcher.files()); |
|
|
|
if (readable()) { // add file to watchlist
|
|
|
|
if (readable()) { // add file to watchlist
|
|
|
|
_watcher.addPath(_file.fileName()); |
|
|
|
_watcher.addPath(_file.fileName()); |
|
|
|
if (!watching) // send change event if file is initially created
|
|
|
|
if (!watching) // send change event if file is initially created
|
|
|
|
changed(); |
|
|
|
emitChanged(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
private: |
|
|
|
private: |
|
|
|