use timer to check filesystem; refs #155
This commit is contained in:
		| @@ -60,7 +60,7 @@ class FileStorage: public Storage { | ||||
|     Q_OBJECT; | ||||
|   public: | ||||
|     /*! @param file full path to storage file */ | ||||
|     FileStorage(QString file, int msec=1000): _file(file), _fileInfo(_file) { | ||||
|     FileStorage(QString file, int msec=1000): _file(file) { | ||||
|       TRC; LOG<<"file: "<<file; | ||||
|       assert(connect(&_timer, SIGNAL(timeout()), SLOT(check()))); | ||||
|       _timer.setInterval(msec); | ||||
| @@ -70,12 +70,12 @@ class FileStorage: public Storage { | ||||
|       return _file.fileName(); | ||||
|     } | ||||
|     bool readable() { | ||||
|       return _fileInfo.exists(); | ||||
|       return QFileInfo(_file).exists(); | ||||
|     } | ||||
|     bool writeable() { | ||||
|       return readable() || | ||||
|         (!_file.fileName().isEmpty() && | ||||
|          _fileInfo.absoluteDir().exists()); | ||||
|          QFileInfo(_file).absoluteDir().exists()); | ||||
|     } | ||||
|     QStringList read() { | ||||
|       TRC; | ||||
| @@ -99,16 +99,15 @@ class FileStorage: public Storage { | ||||
|     } | ||||
|   private Q_SLOTS: | ||||
|     void check() { | ||||
|       if (_modified!=QFileInfo(_file).lastModified()) { | ||||
|         TRC; | ||||
|       if (_modified!=_fileInfo.lastModified()) { | ||||
|         LOG<<"file has been modified"; | ||||
|         _modified = _fileInfo.lastModified(); | ||||
|         _modified = QFileInfo(_file).lastModified(); | ||||
|         LOG<<"file has been modified"<<_modified.toString(); | ||||
|         emitChanged(); | ||||
|       } | ||||
|     } | ||||
|   private: | ||||
|     QFile _file; | ||||
|     QFileInfo _fileInfo; | ||||
|     QDateTime _modified; | ||||
|     QTimer _timer; | ||||
| }; | ||||
|   | ||||
| @@ -8,9 +8,10 @@ | ||||
| #ifndef QBROWSERLIB_LOG | ||||
| #define QBROWSERLIB_LOG | ||||
|  | ||||
| #ifdef QT_GUI_LIB | ||||
| #include <qbrowserlib/ui_log.h> | ||||
|  | ||||
| #include <QtGui/QDialog> | ||||
| #endif | ||||
|  | ||||
| #include <sstream> | ||||
| #include <iostream> | ||||
| @@ -82,15 +83,23 @@ namespace qbrowserlib { | ||||
|       unsigned long _line; | ||||
|   }; | ||||
|  | ||||
|   class LogDialog: public QDialog, public Ui::LogDialog { | ||||
|   class LogDialog: | ||||
| #ifdef QT_GUI_LIB | ||||
|       public QDialog, public Ui::LogDialog | ||||
| #else | ||||
|       public QObject | ||||
| #endif | ||||
|   { | ||||
|  | ||||
|       Q_OBJECT; | ||||
|            | ||||
|     public: | ||||
|            | ||||
|       LogDialog(QWidget* p): QDialog(p) { | ||||
| #ifdef QT_GUI_LIB | ||||
|       LogDialog(QWidget* p=0): QDialog(p) { | ||||
|         setupUi(this); | ||||
|       } | ||||
| #endif | ||||
|            | ||||
|       template<typename TYPE> LogDialog& append(const Log& log, TYPE* arg) { | ||||
|         std::ostringstream ss; | ||||
| @@ -99,6 +108,7 @@ namespace qbrowserlib { | ||||
|       } | ||||
|        | ||||
|       template<typename TYPE> LogDialog& append(const Log& log, TYPE arg) { | ||||
| #ifdef QT_GUI_LIB | ||||
|         int pos(_logs->rowCount()); | ||||
|         _logs->insertRow(pos); | ||||
|         _logs->setItem | ||||
| @@ -116,11 +126,13 @@ namespace qbrowserlib { | ||||
|         _logs->setItem | ||||
|           (pos, MESSAGE, | ||||
|            new QTableWidgetItem(QVariant(arg).toString())); | ||||
| #endif | ||||
|         return *this; | ||||
|       } | ||||
|  | ||||
|     protected Q_SLOTS: | ||||
|  | ||||
| #ifdef QT_GUI_LIB | ||||
|       void on__fileLine_toggled(bool checked) { | ||||
|         if (checked) { | ||||
|           _logs->showColumn(FILE); | ||||
| @@ -166,6 +178,7 @@ namespace qbrowserlib { | ||||
|         if (!items.isEmpty()) _logs->scrollToItem(items[0]); | ||||
|         unsetCursor(); | ||||
|       } | ||||
| #endif | ||||
|           | ||||
|     private: | ||||
|  | ||||
| @@ -181,7 +194,7 @@ namespace qbrowserlib { | ||||
|     init(ss); | ||||
|     indent(ss)<<"   → "<<arg; | ||||
|     std::clog<<close(ss).str()<<std::endl; | ||||
|     if (!_dialog) _dialog = new LogDialog(0); | ||||
|     if (!_dialog) _dialog = new LogDialog; | ||||
|     _dialog->append(*this, arg); | ||||
|     return *this; | ||||
|   } | ||||
|   | ||||
| @@ -10,6 +10,7 @@ | ||||
| #include <QtCore/QCoreApplication> | ||||
|  | ||||
| int main(int argc, char** argv) { | ||||
|   qbrowserlib::Log::DEBUG = true; | ||||
|   QCoreApplication app(argc, argv); | ||||
|   TestFileWatch test("testfile"); | ||||
|   return app.exec(); | ||||
|   | ||||
| @@ -3,6 +3,9 @@ QMAKE_LIBDIR += @top_builddir@/src/qbrowserlib | ||||
| QMAKE_LIBDIR += @top_builddir@/src/qbrowserlib/release | ||||
| QMAKE_LIBS += -lqbrowserlib | ||||
|  | ||||
| QT = core | ||||
| CONFIG += debug | ||||
|  | ||||
| macx { | ||||
|   CONFIG += x86 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user