GUI/FS-watcher update
There are no guarantees that we detect all changes and some changes may
trigger several events. Should be good-enough for now
diff --git a/filesystemwatcher/filesystemwatcher.cpp b/filesystemwatcher/filesystemwatcher.cpp
deleted file mode 100644
index b887292..0000000
--- a/filesystemwatcher/filesystemwatcher.cpp
+++ /dev/null
@@ -1,271 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2013 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Jared Lindblom <lindblom@cs.ucla.edu>
- */
-
-#include "filesystemwatcher.h"
-
-FileSystemWatcher::FileSystemWatcher(QString dirPath, QObject* parent) :
- QObject(parent),
- m_watcher(new QFileSystemWatcher()),
- m_timer(new QTimer()),
- m_dirPath(dirPath)
-{
- // add main directory to monitor
- m_watcher->addPath(m_dirPath);
-
- // register signals (callback functions)
- connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(watcherCallbackSlot(QString)));
- connect(m_timer, SIGNAL(timeout()), this, SLOT(timerCallbackSlot()));
-
- // bootstrap
- QTimer::singleShot(1000, this, SLOT(bootstrap()));
-
- // start timer
- m_timer->start(300000);
-}
-
-FileSystemWatcher::~FileSystemWatcher()
-{
- // clean up
- delete m_watcher;
- delete m_timer;
-}
-
-void FileSystemWatcher::bootstrap()
-{
- // bootstrap specific steps
-#if DEBUG
- qDebug() << endl << "[BOOTSTRAP]";
-#endif
- timerCallbackSlot();
-#if DEBUG
- qDebug() << endl << "[\\BOOTSTRAP]";
-#endif
-}
-
-void FileSystemWatcher::watcherCallbackSlot(QString dirPath)
-{
- // watcher specific steps
-#if DEBUG
- qDebug() << endl << "[WATCHER] Triggered Path: " << dirPath;
-#endif
- handleCallback(dirPath);
-}
-
-void FileSystemWatcher::timerCallbackSlot()
-{
- // timer specific steps
-#if DEBUG
- qDebug() << endl << "[TIMER] Triggered Path: " << m_dirPath;
-#endif
- handleCallback(m_dirPath);
-}
-
-void FileSystemWatcher::handleCallback(QString dirPath)
-{
- // scan directory and populate file list
- QHash<QString, qint64> currentState = scanDirectory(dirPath);
-
- // reconcile directory and report changes
- std::vector<sEventInfo> dirChanges = reconcileDirectory(currentState, dirPath);
-#if DEBUG
- // DEBUG: Print Changes
- printChanges(dirChanges);
-#endif
- // emit the signal if not empty
- if(!dirChanges.empty())
- emit dirEventSignal(dirChanges);
-}
-
-QHash<QString, qint64> FileSystemWatcher::scanDirectory(QString dirPath)
-{
- // list of files in directory
- QHash<QString, qint64> currentState;
-
- // directory iterator (recursive)
- QDirIterator dirIterator(dirPath, QDirIterator::Subdirectories |
- QDirIterator::FollowSymlinks);
-
- // iterate through directory recursively
- while(dirIterator.hasNext())
- {
- // Get Next File/Dir
- dirIterator.next();
-
- // Get FileInfo
- QFileInfo fileInfo = dirIterator.fileInfo();
-
- // if not this directory or previous directory
- if(fileInfo.absoluteFilePath() != ".." && fileInfo.absoluteFilePath() != ".")
- {
- QString absFilePath = fileInfo.absoluteFilePath();
-
- // if this is a directory
- if(fileInfo.isDir())
- {
- QStringList dirList = m_watcher->directories();
-
- // if the directory is not already being watched
- if (absFilePath.startsWith(m_dirPath) && !dirList.contains(absFilePath))
- {
- // add this directory to the watch list
- m_watcher->addPath(absFilePath);
- }
- }
- else
- {
- // add this file to the file list
- currentState.insert(absFilePath, fileInfo.created().toMSecsSinceEpoch());
- }
- }
- }
-
- return currentState;
-}
-
-std::vector<sEventInfo> FileSystemWatcher::reconcileDirectory(QHash<QString, qint64> currentState, QString dirPath)
-{
- // list of files changed
- std::vector<sEventInfo> dirChanges;
-
- // compare result (database/stored snapshot) to fileList (current snapshot)
- QMutableHashIterator<QString, qint64> i(m_storedState);
-
- while(i.hasNext())
- {
- i.next();
-
- QString absFilePath = i.key();
- qint64 storedCreated = i.value();
-
- // if this file is in a level higher than
- // this directory, ignore
- if(!absFilePath.startsWith(dirPath))
- {
- continue;
- }
-
- // check file existence
- if(currentState.contains(absFilePath))
- {
- qint64 currentCreated = currentState.value(absFilePath);
-
- if(storedCreated != currentCreated)
- {
- // update stored state
- i.setValue(currentCreated);
-
- // this file has been modified
- sEventInfo eventInfo;
- eventInfo.event = MODIFIED;
- eventInfo.absFilePath = absFilePath.toStdString();
- dirChanges.push_back(eventInfo);
- }
-
- // delete this file from fileList we have processed it
- currentState.remove(absFilePath);
- }
- else
- {
- // delete from stored state
- i.remove();
-
- // this file has been deleted
- sEventInfo eventInfo;
- eventInfo.event = DELETED;
- eventInfo.absFilePath = absFilePath.toStdString();
- dirChanges.push_back(eventInfo);
- }
- }
-
- // any files left in fileList have been added
- for(QHash<QString, qint64>::iterator i = currentState.begin(); i != currentState.end(); ++i)
- {
- QString absFilePath = i.key();
- qint64 currentCreated = i.value();
-
- m_storedState.insert(absFilePath, currentCreated);
-
- // this file has been added
- sEventInfo eventInfo;
- eventInfo.event = ADDED;
- eventInfo.absFilePath = absFilePath.toStdString();
- dirChanges.push_back(eventInfo);
- }
-
- return dirChanges;
-}
-
-QByteArray FileSystemWatcher::calcChecksum(QString absFilePath)
-{
- // initialize checksum
- QCryptographicHash crypto(QCryptographicHash::Md5);
-
- // open file
- QFile file(absFilePath);
- file.open(QFile::ReadOnly);
-
- // calculate checksum
- while(!file.atEnd())
- {
- crypto.addData(file.read(8192));
- }
-
- return crypto.result();
-}
-
-void FileSystemWatcher::printChanges(std::vector<sEventInfo> dirChanges)
-{
- if(!dirChanges.empty())
- {
- for(size_t i = 0; i < dirChanges.size(); i++)
- {
- QString tempString;
-
- eEvent event = dirChanges[i].event;
- QString absFilePath = QString::fromStdString(dirChanges[i].absFilePath);
-
- switch(event)
- {
- case ADDED:
- tempString.append("ADDED: ");
- break;
- case MODIFIED:
- tempString.append("MODIFIED: ");
- break;
- case DELETED:
- tempString.append("DELETED: ");
- break;
- }
-
- tempString.append(absFilePath);
-
- qDebug() << "\t" << tempString;
- }
- }
- else
- {
- qDebug() << "\t[EMPTY]";
- }
-}
-
-#if WAF
-#include "filesystemwatcher.moc"
-#include "filesystemwatcher.cpp.moc"
-#endif
diff --git a/filesystemwatcher/filesystemwatcher.h b/filesystemwatcher/filesystemwatcher.h
deleted file mode 100644
index 543fba6..0000000
--- a/filesystemwatcher/filesystemwatcher.h
+++ /dev/null
@@ -1,84 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2013 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Jared Lindblom <lindblom@cs.ucla.edu>
- */
-#ifndef FILESYSTEMWATCHER_H
-#define FILESYSTEMWATCHER_H
-
-#include <QFileSystemWatcher>
-#include <QCryptographicHash>
-#include <QDirIterator>
-#include <QFileInfo>
-#include <QDateTime>
-#include <QTimer>
-#include <QDebug>
-#include <QHash>
-#include <structs.h>
-
-#define DEBUG 1
-
-class FileSystemWatcher : public QObject
-{
- Q_OBJECT
-
-public:
- // constructor
- FileSystemWatcher(QString dirPath, QObject* parent = 0);
-
- // destructor
- ~FileSystemWatcher();
-
-signals:
- // directory event signal
- void dirEventSignal(std::vector<sEventInfo> dirChanges);
-
-private slots:
- // handle callback from watcher
- void watcherCallbackSlot(QString dirPath);
-
- // handle callback from timer
- void timerCallbackSlot();
-
- // bootstrap
- void bootstrap();
-
-private:
- // handle callback from either the watcher or timer
- void handleCallback(QString dirPath);
-
- // scan directory and populate file list
- QHash<QString, qint64> scanDirectory(QString dirPath);
-
- // reconcile directory, find changes
- std::vector<sEventInfo> reconcileDirectory(QHash<QString, qint64> fileList, QString dirPath);
-
- // calculate checksum
- QByteArray calcChecksum(QString absFilePath);
-
- // print Changes (DEBUG)
- void printChanges(std::vector<sEventInfo> dirChanges);
-
-private:
- QFileSystemWatcher* m_watcher; // filesystem watcher
- QTimer* m_timer; // timer
-
- QString m_dirPath; // monitored path
- QHash<QString, qint64> m_storedState; // stored state of directory
-};
-
-#endif // FILESYSTEMWATCHER_H
diff --git a/filesystemwatcher/main.cpp b/filesystemwatcher/main.cpp
deleted file mode 100644
index 213a27e..0000000
--- a/filesystemwatcher/main.cpp
+++ /dev/null
@@ -1,37 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2013 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Jared Lindblom <lindblom@cs.ucla.edu>
- */
-
-#include "filesystemwatcher.h"
-#include "simpleeventcatcher.h"
-#include <QApplication>
-#include <iostream>
-
-int main(int argc, char *argv[])
-{
- QApplication app(argc, argv);
-
- // invoke file system watcher on specified path
- FileSystemWatcher watcher("/Users/jared/Desktop");
-
- // class that will utilize these signals
- SimpleEventCatcher dirEventCatcher(&watcher);
-
- return app.exec();
-}
diff --git a/filesystemwatcher/simpleeventcatcher.cpp b/filesystemwatcher/simpleeventcatcher.cpp
deleted file mode 100644
index f1061fb..0000000
--- a/filesystemwatcher/simpleeventcatcher.cpp
+++ /dev/null
@@ -1,70 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2013 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Jared Lindblom <lindblom@cs.ucla.edu>
- */
-
-#include "simpleeventcatcher.h"
-
-SimpleEventCatcher::SimpleEventCatcher(FileSystemWatcher* watcher, QObject *parent) :
- QObject(parent)
-{
- // register for directory event signal (callback function)
- QObject::connect(watcher, SIGNAL(dirEventSignal(std::vector<sEventInfo>)), this, SLOT(handleDirEvent(std::vector<sEventInfo>)));
-}
-
-void SimpleEventCatcher::handleDirEvent(std::vector<sEventInfo> dirChanges)
-{
- qDebug() << endl << "[SIGNAL] From SimpleEventCatcher Slot:";
-
- if(!dirChanges.empty())
- {
- for(size_t i = 0; i < dirChanges.size(); i++)
- {
- QString tempString;
-
- eEvent event = dirChanges[i].event;
- QString absFilePath = QString::fromStdString(dirChanges[i].absFilePath);
-
- switch(event)
- {
- case ADDED:
- tempString.append("ADDED: ");
- break;
- case MODIFIED:
- tempString.append("MODIFIED: ");
- break;
- case DELETED:
- tempString.append("DELETED: ");
- break;
- }
-
- tempString.append(absFilePath);
-
- qDebug() << "\t" << tempString;
- }
- }
- else
- {
- qDebug() << "\t[EMPTY]";
- }
-}
-
-#if WAF
-#include "simpleeventcatcher.moc"
-#include "simpleeventcatcher.cpp.moc"
-#endif
diff --git a/filesystemwatcher/simpleeventcatcher.h b/filesystemwatcher/simpleeventcatcher.h
deleted file mode 100644
index 2a858fd..0000000
--- a/filesystemwatcher/simpleeventcatcher.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2013 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Jared Lindblom <lindblom@cs.ucla.edu>
- */
-
-#ifndef SIMPLEEVENTCATCHER_H
-#define SIMPLEEVENTCATCHER_H
-
-#include "filesystemwatcher.h"
-#include <QObject>
-#include <QDebug>
-#include <vector>
-#include <structs.h>
-
-class SimpleEventCatcher : public QObject
-{
- Q_OBJECT
-public:
- explicit SimpleEventCatcher(FileSystemWatcher* watcher, QObject *parent = 0);
-
-public slots:
- // handle signal
- void handleDirEvent(std::vector<sEventInfo> dirChanges);
-};
-
-#endif // SIMPLEEVENTCATCHER_H
diff --git a/filesystemwatcher/README.md b/fs-watcher/README.md
similarity index 100%
rename from filesystemwatcher/README.md
rename to fs-watcher/README.md
diff --git a/fs-watcher/fs-watcher.cc b/fs-watcher/fs-watcher.cc
new file mode 100644
index 0000000..cdbff3c
--- /dev/null
+++ b/fs-watcher/fs-watcher.cc
@@ -0,0 +1,276 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Jared Lindblom <lindblom@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include "fs-watcher.h"
+#include "logging.h"
+
+#include <boost/bind.hpp>
+
+#include <QDirIterator>
+#include <QRegExp>
+
+using namespace std;
+using namespace boost;
+
+INIT_LOGGER ("FsWatcher");
+
+FsWatcher::FsWatcher (QString dirPath, QObject* parent)
+ : QObject(parent)
+ , m_watcher (new QFileSystemWatcher())
+ , m_executor (1)
+ , m_dirPath (dirPath)
+{
+ _LOG_DEBUG ("Monitor dir: " << m_dirPath.toStdString ());
+ // add main directory to monitor
+ m_watcher->addPath (m_dirPath);
+
+ // register signals (callback functions)
+ connect (m_watcher, SIGNAL (directoryChanged (QString)), this, SLOT (DidDirectoryChanged (QString)));
+ connect (m_watcher, SIGNAL (fileChanged (QString)), this, SLOT (DidFileChanged (QString)));
+
+ m_executor.execute (bind (&FsWatcher::ScanDirectory_Notify_Execute, this, m_dirPath));
+}
+
+FsWatcher::~FsWatcher()
+{
+ delete m_watcher;
+}
+
+void
+FsWatcher::DidDirectoryChanged (QString dirPath)
+{
+ _LOG_DEBUG ("Triggered DirPath: " << dirPath.toStdString ());
+
+ m_executor.execute (bind (&FsWatcher::ScanDirectory_Notify_Execute, this, dirPath));
+}
+
+void
+FsWatcher::DidFileChanged (QString filePath)
+{
+ _LOG_DEBUG ("Triggered FilePath: " << filePath.toStdString ());
+}
+
+
+void FsWatcher::DidDirectoryChanged_Execute (QString dirPath)
+{
+// // scan directory and populate file list
+// QHash<QString, qint64> currentState = scanDirectory(dirPath);
+
+// // reconcile directory and report changes
+// std::vector<sEventInfo> dirChanges = reconcileDirectory(currentState, dirPath);
+// #ifdef _DEBUG
+// // DEBUG: Print Changes
+// printChanges(dirChanges);
+// #endif
+// // emit the signal if not empty
+// if(!dirChanges.empty())
+// emit dirEventSignal(dirChanges);
+}
+
+void
+FsWatcher::ScanDirectory_Notify_Execute (QString dirPath)
+{
+ QRegExp exclude ("^(\\.|\\.\\.|\\.chronoshare)$");
+
+ QDirIterator dirIterator (dirPath,
+ QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot,
+ QDirIterator::Subdirectories); // directory iterator (recursive)
+
+ // iterate through directory recursively
+ while (dirIterator.hasNext ())
+ {
+ dirIterator.next ();
+
+ // Get FileInfo
+ QFileInfo fileInfo = dirIterator.fileInfo ();
+
+ QString name = fileInfo.fileName ();
+
+ if (!exclude.exactMatch (name))
+ {
+ // _LOG_DEBUG ("Not excluded file/dir: " << fileInfo.absoluteFilePath ().toStdString ());
+ QString absFilePath = fileInfo.absoluteFilePath ();
+
+ // _LOG_DEBUG ("Attempt to add path to watcher: " << absFilePath.toStdString ());
+ m_watcher->addPath (absFilePath);
+
+ if (fileInfo.isFile ())
+ {
+ DidFileChanged (absFilePath);
+ }
+ // // if this is a directory
+ // if(fileInfo.isDir())
+ // {
+ // QStringList dirList = m_watcher->directories();
+
+ // // if the directory is not already being watched
+ // if (absFilePath.startsWith(m_dirPath) && !dirList.contains(absFilePath))
+ // {
+ // _LOG_DEBUG ("Add new dir to watchlist: " << absFilePath.toStdString ());
+ // // add this directory to the watch list
+ // m_watcher->addPath(absFilePath);
+ // }
+ // }
+ // else
+ // {
+ // _LOG_DEBUG ("Found file: " << absFilePath.toStdString ());
+ // // add this file to the file list
+ // // currentState.insert(absFilePath, fileInfo.created().toMSecsSinceEpoch());
+ // }
+ }
+ else
+ {
+ // _LOG_DEBUG ("Excluded file/dir: " << fileInfo.filePath ().toStdString ());
+ }
+ }
+}
+
+// std::vector<sEventInfo> FsWatcher::reconcileDirectory(QHash<QString, qint64> currentState, QString dirPath)
+// {
+// // list of files changed
+// std::vector<sEventInfo> dirChanges;
+
+// // compare result (database/stored snapshot) to fileList (current snapshot)
+// QMutableHashIterator<QString, qint64> i(m_storedState);
+
+// while(i.hasNext())
+// {
+// i.next();
+
+// QString absFilePath = i.key();
+// qint64 storedCreated = i.value();
+
+// // if this file is in a level higher than
+// // this directory, ignore
+// if(!absFilePath.startsWith(dirPath))
+// {
+// continue;
+// }
+
+// // check file existence
+// if(currentState.contains(absFilePath))
+// {
+// qint64 currentCreated = currentState.value(absFilePath);
+
+// if(storedCreated != currentCreated)
+// {
+// // update stored state
+// i.setValue(currentCreated);
+
+// // this file has been modified
+// sEventInfo eventInfo;
+// eventInfo.event = MODIFIED;
+// eventInfo.absFilePath = absFilePath.toStdString();
+// dirChanges.push_back(eventInfo);
+// }
+
+// // delete this file from fileList we have processed it
+// currentState.remove(absFilePath);
+// }
+// else
+// {
+// // delete from stored state
+// i.remove();
+
+// // this file has been deleted
+// sEventInfo eventInfo;
+// eventInfo.event = DELETED;
+// eventInfo.absFilePath = absFilePath.toStdString();
+// dirChanges.push_back(eventInfo);
+// }
+// }
+
+// // any files left in fileList have been added
+// for(QHash<QString, qint64>::iterator i = currentState.begin(); i != currentState.end(); ++i)
+// {
+// QString absFilePath = i.key();
+// qint64 currentCreated = i.value();
+
+// m_storedState.insert(absFilePath, currentCreated);
+
+// // this file has been added
+// sEventInfo eventInfo;
+// eventInfo.event = ADDED;
+// eventInfo.absFilePath = absFilePath.toStdString();
+// dirChanges.push_back(eventInfo);
+// }
+
+// return dirChanges;
+// }
+
+// QByteArray FsWatcher::calcChecksum(QString absFilePath)
+// {
+// // initialize checksum
+// QCryptographicHash crypto(QCryptographicHash::Md5);
+
+// // open file
+// QFile file(absFilePath);
+// file.open(QFile::ReadOnly);
+
+// // calculate checksum
+// while(!file.atEnd())
+// {
+// crypto.addData(file.read(8192));
+// }
+
+// return crypto.result();
+// }
+
+// void FsWatcher::printChanges(std::vector<sEventInfo> dirChanges)
+// {
+// if(!dirChanges.empty())
+// {
+// for(size_t i = 0; i < dirChanges.size(); i++)
+// {
+// QString tempString;
+
+// eEvent event = dirChanges[i].event;
+// QString absFilePath = QString::fromStdString(dirChanges[i].absFilePath);
+
+// switch(event)
+// {
+// case ADDED:
+// tempString.append("ADDED: ");
+// break;
+// case MODIFIED:
+// tempString.append("MODIFIED: ");
+// break;
+// case DELETED:
+// tempString.append("DELETED: ");
+// break;
+// }
+
+// tempString.append(absFilePath);
+
+// _LOG_DEBUG ("\t" << tempString.toStdString ());
+// }
+// }
+// else
+// {
+// _LOG_DEBUG ("\t[EMPTY]");
+// }
+// }
+
+#if WAF
+#include "fs-watcher.moc"
+#include "fs-watcher.cc.moc"
+#endif
diff --git a/fs-watcher/fs-watcher.h b/fs-watcher/fs-watcher.h
new file mode 100644
index 0000000..278a630
--- /dev/null
+++ b/fs-watcher/fs-watcher.h
@@ -0,0 +1,81 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Jared Lindblom <lindblom@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+#ifndef FS_WATCHER_H
+#define FS_WATCHER_H
+
+#include <vector>
+#include <QFileSystemWatcher>
+
+#include "structs.h"
+
+#include "executor.h"
+
+class FsWatcher : public QObject
+{
+ Q_OBJECT
+
+public:
+ // constructor
+ FsWatcher (QString dirPath, QObject* parent = 0);
+
+ // destructor
+ ~FsWatcher ();
+
+private slots:
+ // handle callback from watcher
+ void
+ DidDirectoryChanged (QString dirPath);
+
+ /**
+ * @brief This even will be triggered either by actual file change or via directory change event
+ * (i.e., can happen twice in a row, as well as trigger false alarm)
+ */
+ void
+ DidFileChanged (QString filePath);
+
+private:
+ // handle callback from the watcher
+ void
+ DidDirectoryChanged_Execute (QString dirPath);
+
+ // scan directory and notify callback about any file changes
+ void
+ ScanDirectory_Notify_Execute (QString dirPath);
+
+ // // reconcile directory, find changes
+ // std::vector<sEventInfo>
+ // reconcileDirectory (QHash<QString, qint64> fileList, QString dirPath);
+
+ // // calculate checksum
+ // QByteArray calcChecksum(QString absFilePath);
+
+ // // print Changes (DEBUG)
+ // void printChanges(std::vector<sEventInfo> dirChanges);
+
+private:
+ QFileSystemWatcher* m_watcher; // filesystem watcher
+ Executor m_executor;
+
+ QString m_dirPath; // monitored path
+};
+
+#endif // FILESYSTEMWATCHER_H
diff --git a/fs-watcher/simpleeventcatcher.cpp b/fs-watcher/simpleeventcatcher.cpp
new file mode 100644
index 0000000..610903c
--- /dev/null
+++ b/fs-watcher/simpleeventcatcher.cpp
@@ -0,0 +1,70 @@
+// /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+// /*
+// * Copyright (c) 2012-2013 University of California, Los Angeles
+// *
+// * This program is free software; you can redistribute it and/or modify
+// * it under the terms of the GNU General Public License version 2 as
+// * published by the Free Software Foundation;
+// *
+// * This program is distributed in the hope that it will be useful,
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// * GNU General Public License for more details.
+// *
+// * You should have received a copy of the GNU General Public License
+// * along with this program; if not, write to the Free Software
+// * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// *
+// * Author: Jared Lindblom <lindblom@cs.ucla.edu>
+// */
+
+// #include "simpleeventcatcher.h"
+
+// SimpleEventCatcher::SimpleEventCatcher(FileSystemWatcher* watcher, QObject *parent) :
+// QObject(parent)
+// {
+// // register for directory event signal (callback function)
+// QObject::connect(watcher, SIGNAL(dirEventSignal(std::vector<sEventInfo>)), this, SLOT(handleDirEvent(std::vector<sEventInfo>)));
+// }
+
+// void SimpleEventCatcher::handleDirEvent(std::vector<sEventInfo> dirChanges)
+// {
+// qDebug() << endl << "[SIGNAL] From SimpleEventCatcher Slot:";
+
+// if(!dirChanges.empty())
+// {
+// for(size_t i = 0; i < dirChanges.size(); i++)
+// {
+// QString tempString;
+
+// eEvent event = dirChanges[i].event;
+// QString absFilePath = QString::fromStdString(dirChanges[i].absFilePath);
+
+// switch(event)
+// {
+// case ADDED:
+// tempString.append("ADDED: ");
+// break;
+// case MODIFIED:
+// tempString.append("MODIFIED: ");
+// break;
+// case DELETED:
+// tempString.append("DELETED: ");
+// break;
+// }
+
+// tempString.append (absFilePath);
+
+// qDebug() << "\t" << tempString;
+// }
+// }
+// else
+// {
+// qDebug() << "\t[EMPTY]";
+// }
+// }
+
+// #if WAF
+// #include "simpleeventcatcher.moc"
+// #include "simpleeventcatcher.cpp.moc"
+// #endif
diff --git a/fs-watcher/simpleeventcatcher.h b/fs-watcher/simpleeventcatcher.h
new file mode 100644
index 0000000..a99d1f8
--- /dev/null
+++ b/fs-watcher/simpleeventcatcher.h
@@ -0,0 +1,41 @@
+// /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+// /*
+// * Copyright (c) 2012-2013 University of California, Los Angeles
+// *
+// * This program is free software; you can redistribute it and/or modify
+// * it under the terms of the GNU General Public License version 2 as
+// * published by the Free Software Foundation;
+// *
+// * This program is distributed in the hope that it will be useful,
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// * GNU General Public License for more details.
+// *
+// * You should have received a copy of the GNU General Public License
+// * along with this program; if not, write to the Free Software
+// * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// *
+// * Author: Jared Lindblom <lindblom@cs.ucla.edu>
+// */
+
+// #ifndef SIMPLEEVENTCATCHER_H
+// #define SIMPLEEVENTCATCHER_H
+
+// #include "filesystemwatcher.h"
+// #include <QObject>
+// #include <QDebug>
+// #include <vector>
+// #include <structs.h>
+
+// class SimpleEventCatcher : public QObject
+// {
+// Q_OBJECT
+// public:
+// explicit SimpleEventCatcher(FileSystemWatcher* watcher, QObject *parent = 0);
+
+// public slots:
+// // handle signal
+// void handleDirEvent(std::vector<sEventInfo> dirChanges);
+// };
+
+// #endif // SIMPLEEVENTCATCHER_H
diff --git a/filesystemwatcher/structs.h b/fs-watcher/structs.h
similarity index 100%
rename from filesystemwatcher/structs.h
rename to fs-watcher/structs.h
diff --git a/gui/chronosharegui.cpp b/gui/chronosharegui.cpp
index 8f46363..13c3a51 100644
--- a/gui/chronosharegui.cpp
+++ b/gui/chronosharegui.cpp
@@ -21,10 +21,13 @@
#include "chronosharegui.h"
#include "logging.h"
+
+
INIT_LOGGER ("Gui");
ChronoShareGui::ChronoShareGui(QWidget *parent)
: QWidget(parent)
+ , m_watcher (0)
// , m_settingsFilePath(QDir::homePath() + "/.chronoshare")
{
// load settings
@@ -46,10 +49,21 @@
// show tray icon
m_trayIcon->show();
+
+ // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
+ // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
+ // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
+
+ m_watcher = new FsWatcher (m_dirPath);
}
ChronoShareGui::~ChronoShareGui()
{
+ if (!m_watcher)
+ {
+ delete m_watcher;
+ }
+
// cleanup
delete m_trayIcon;
delete m_trayIconMenu;
@@ -158,9 +172,8 @@
{
// prompt user for new directory
QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
- m_dirPath, QFileDialog::ShowDirsOnly |
- QFileDialog::DontResolveSymlinks);
- QFileInfo qFileInfo(tempPath);
+ m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
+ QFileInfo qFileInfo (tempPath);
if(qFileInfo.isDir())
m_dirPath = tempPath;
diff --git a/gui/chronosharegui.h b/gui/chronosharegui.h
index f0cbe76..b0d459e 100644
--- a/gui/chronosharegui.h
+++ b/gui/chronosharegui.h
@@ -33,6 +33,8 @@
#include <QMessageBox>
#include <QApplication>
+#include "fs-watcher.h"
+
class ChronoShareGui : public QWidget
{
Q_OBJECT
@@ -93,6 +95,7 @@
QString m_dirPath; // shared directory
+ FsWatcher *m_watcher;
// QString m_settingsFilePath; // settings file path
// QString m_settings;
};
diff --git a/wscript b/wscript
index df1e2d3..b7d5d1a 100644
--- a/wscript
+++ b/wscript
@@ -62,6 +62,7 @@
conf.env.append_value('CXXFLAGS', ['-O3', '-g', '-Qunused-arguments'])
if conf.options._test:
+ conf.define ('_TESTS', 1)
conf.env.TEST = 1
conf.write_config_header('src/config.h')
@@ -102,11 +103,11 @@
)
qt = bld (
- target = "filewatcher",
- features = "qt4 cxx cxxprogram",
+ target = "fs-watcher",
+ features = "qt4 cxx",
defines = "WAF",
- source = bld.path.ant_glob(['filesystemwatcher/*.cpp']),
- includes = "filesystemwatcher . ",
+ source = bld.path.ant_glob(['fs-watcher/*.cc']),
+ includes = "fs-watcher . src ",
use = "QTCORE QTGUI LOG4CXX"
)
@@ -136,6 +137,6 @@
features = "qt4 cxx cxxprogram",
defines = "WAF",
source = bld.path.ant_glob(['gui/*.cpp', 'gui/*.qrc']),
- includes = "src gui . ",
- use = "QTCORE QTGUI LOG4CXX ccnx database chronoshare"
+ includes = "src gui fs-watcher src . ",
+ use = "QTCORE QTGUI LOG4CXX fs-watcher ccnx database chronoshare"
)