add logging for kimmo; refs #155

This commit is contained in:
Marc Wäckerlin
2012-05-16 09:49:32 +00:00
parent ef46a79170
commit 6fb037ec6c
3 changed files with 98 additions and 3 deletions

View File

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