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/fs-watcher/README.md b/fs-watcher/README.md
new file mode 100644
index 0000000..da229ca
--- /dev/null
+++ b/fs-watcher/README.md
@@ -0,0 +1,65 @@
+Overview:
+
+FileSystemWatcher reports changes that are made to a monitored directory by signaling a registered callback function and passing that function a list of changes that have occurred.  Each element of the list represents a file and the action that was performed on that file. 
+
+Example:
+
+    ADDED: /Users/jared/Desktop/test.txt
+
+The list is held in a vector of type sEventInfo, where sEventInfo is a struct defined as follows:
+
+    enum eEvent {
+        ADDED = 0,
+        MODIFIED,
+        DELETED
+    };
+
+    struct sEventInfo {
+        eEvent event;
+        std::string absFilePath;
+    };
+
+The eEvent enumerator specifies the action taken on the file and the string absFilePath is the absolute file path of the file.
+
+Usage:
+
+SimpleEventCatcher is a dummy class that serves as an example of how to register for signals from FileSystemWatcher.  These are the basic steps:
+
+    // invoke file system watcher on specified path
+    FileSystemWatcher watcher("/Users/jared/Desktop");
+
+    // pass the instance of FileSystemWatcher to the class
+    // that will register for event notifications
+    SimpleEventCatcher dirEventCatcher(&watcher);
+
+    // register for directory event signal (callback function)
+    QObject::connect(watcher, SIGNAL(dirEventSignal(std::vector<sEventInfo>)), this,
+                     SLOT(handleDirEvent(std::vector<sEventInfo>)));
+
+    // implement handleDirEvent
+    void SimpleEventCatcher::handleDirEvent(std::vector<sEventInfo>)
+    {
+        /* implementation here */
+    }
+
+Debug:
+
+The debug flag can be set in filesystemwatcher.h.  It is set to 1 by default and outputs the following information to the console:
+
+[BOOTSTRAP] 
+
+[TIMER] Triggered Path:  "/Users/jared/Desktop" 
+	 "ADDED: /Users/jared/Desktop/test2.txt" 
+	 "ADDED: /Users/jared/Desktop/test.txt" 
+
+[SIGNAL] From SimpleEventCatcher Slot: 
+	 "ADDED: /Users/jared/Desktop/test2.txt" 
+	 "ADDED: /Users/jared/Desktop/test.txt" 
+
+[\BOOTSTRAP] 
+
+[WATCHER] Triggered Path:  "/Users/jared/Desktop" 
+	 "DELETED: /Users/jared/Desktop/test2.txt" 
+
+[SIGNAL] From SimpleEventCatcher Slot: 
+	 "DELETED: /Users/jared/Desktop/test2.txt" 
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/fs-watcher/structs.h b/fs-watcher/structs.h
new file mode 100644
index 0000000..93008b1
--- /dev/null
+++ b/fs-watcher/structs.h
@@ -0,0 +1,35 @@
+/* -*- 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 STRUCTS_H
+#define STRUCTS_H
+
+enum eEvent {
+    ADDED = 0,
+    MODIFIED,
+    DELETED
+};
+
+struct sEventInfo {
+    eEvent event;
+    std::string absFilePath;
+};
+
+#endif // STRUCTS_H