Merge remote-tracking branch 'git.irl/master'
diff --git a/fs-watcher/README.md b/fs-watcher/README.md
deleted file mode 100644
index da229ca..0000000
--- a/fs-watcher/README.md
+++ /dev/null
@@ -1,65 +0,0 @@
-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/simpleeventcatcher.cpp b/fs-watcher/simpleeventcatcher.cpp
deleted file mode 100644
index 610903c..0000000
--- a/fs-watcher/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/fs-watcher/simpleeventcatcher.h b/fs-watcher/simpleeventcatcher.h
deleted file mode 100644
index a99d1f8..0000000
--- a/fs-watcher/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/fs-watcher/structs.h b/fs-watcher/structs.h
deleted file mode 100644
index 93008b1..0000000
--- a/fs-watcher/structs.h
+++ /dev/null
@@ -1,35 +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 STRUCTS_H
-#define STRUCTS_H
-
-enum eEvent {
-    ADDED = 0,
-    MODIFIED,
-    DELETED
-};
-
-struct sEventInfo {
-    eEvent event;
-    std::string absFilePath;
-};
-
-#endif // STRUCTS_H
diff --git a/gui/chronosharegui.cpp b/gui/chronosharegui.cpp
index 9fca3b3..f310594 100644
--- a/gui/chronosharegui.cpp
+++ b/gui/chronosharegui.cpp
@@ -20,6 +20,12 @@
 
 #include "chronosharegui.h"
 #include "logging.h"
+#include "ccnx-wrapper.h"
+
+#include <boost/make_shared.hpp>
+
+using namespace boost;
+using namespace Ccnx;
 
 INIT_LOGGER ("Gui");
 
@@ -81,14 +87,19 @@
   //            const std::string &sharedFolder, const boost::filesystem::path &rootDir,
   //            Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
 
+  m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (),
+                                 m_dirPath.toStdString (), make_shared<CcnxWrapper> ());
 
   // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
-  m_watcher = new FsWatcher (m_dirPath);
+  m_watcher = new FsWatcher (m_dirPath,
+                             bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1),
+                             bind (&Dispatcher::Did_LocalFile_Delete,      m_dispatcher, _1));
 }
 
 ChronoShareGui::~ChronoShareGui()
 {
   delete m_watcher; // stop filewatching ASAP
+  delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
 
   // cleanup
   delete m_trayIcon;
diff --git a/gui/chronosharegui.h b/gui/chronosharegui.h
index 098a48e..bf42515 100644
--- a/gui/chronosharegui.h
+++ b/gui/chronosharegui.h
@@ -35,6 +35,7 @@
 #include <QApplication>
 
 #include "fs-watcher.h"
+#include "dispatcher.h"
 
 class ChronoShareGui : public QDialog
 {
@@ -101,7 +102,8 @@
   QString m_username; // username
   QString m_sharedFolderName; // shared folder name
 
-  FsWatcher* m_watcher;
+  FsWatcher  *m_watcher;
+  Dispatcher *m_dispatcher;
 
   QLabel* labelUsername;
   QPushButton* button;
diff --git a/fs-watcher/fs-watcher.cc b/gui/fs-watcher.cc
similarity index 96%
rename from fs-watcher/fs-watcher.cc
rename to gui/fs-watcher.cc
index 86c0f80..2c6f8a2 100644
--- a/fs-watcher/fs-watcher.cc
+++ b/gui/fs-watcher.cc
@@ -33,11 +33,15 @@
 
 INIT_LOGGER ("FsWatcher");
 
-FsWatcher::FsWatcher (QString dirPath, QObject* parent)
+FsWatcher::FsWatcher (QString dirPath,
+                      LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete,
+                      QObject* parent)
   : QObject(parent)
   , m_watcher (new QFileSystemWatcher())
   , m_executor (1)
   , m_dirPath (dirPath)
+  , m_onChange (onChange)
+  , m_onDelete (onDelete)
 {
   _LOG_DEBUG ("Monitor dir: " << m_dirPath.toStdString ());
   // add main directory to monitor
diff --git a/fs-watcher/fs-watcher.h b/gui/fs-watcher.h
similarity index 85%
rename from fs-watcher/fs-watcher.h
rename to gui/fs-watcher.h
index 278a630..6ba6671 100644
--- a/fs-watcher/fs-watcher.h
+++ b/gui/fs-watcher.h
@@ -24,8 +24,7 @@
 
 #include <vector>
 #include <QFileSystemWatcher>
-
-#include "structs.h"
+#include <boost/filesystem.hpp>
 
 #include "executor.h"
 
@@ -34,8 +33,12 @@
   Q_OBJECT
 
 public:
+  typedef boost::function<void (const boost::filesystem::path &)> LocalFile_Change_Callback;
+
   // constructor
-  FsWatcher (QString dirPath, QObject* parent = 0);
+  FsWatcher (QString dirPath,
+             LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete,
+             QObject* parent = 0);
 
   // destructor
   ~FsWatcher ();
@@ -76,6 +79,9 @@
   Executor m_executor;
 
   QString m_dirPath; // monitored path
+
+  LocalFile_Change_Callback m_onChange;
+  LocalFile_Change_Callback m_onDelete;
 };
 
 #endif // FILESYSTEMWATCHER_H
diff --git a/src/object-db.cc b/src/object-db.cc
index 6c2b786..7567c83 100644
--- a/src/object-db.cc
+++ b/src/object-db.cc
@@ -67,7 +67,7 @@
   res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
   if (res != SQLITE_OK && errmsg != 0)
     {
-      _LOG_DEBUG ("Init error: " << errmsg);
+      // _LOG_TRACE ("Init \"error\": " << errmsg);
       sqlite3_free (errmsg);
     }
 
@@ -98,7 +98,7 @@
           int countAll = sqlite3_column_int (stmt, 0);
           int countNonNull = sqlite3_column_int (stmt, 1);
 
-          _LOG_DEBUG ("Total segments: " << countAll << ", non-empty segments: " << countNonNull);
+          _LOG_TRACE ("Total segments: " << countAll << ", non-empty segments: " << countNonNull);
 
           if (countAll > 0 && countAll==countNonNull)
             {
diff --git a/wscript b/wscript
index 17e1e07..2300f7f 100644
--- a/wscript
+++ b/wscript
@@ -102,15 +102,6 @@
           includes = "ccnx scheduler src",
           )
 
-    qt = bld (
-        target = "fs-watcher",
-        features = "qt4 cxx",
-        defines = "WAF",
-          source = bld.path.ant_glob(['fs-watcher/*.cc']),
-        includes = "fs-watcher . src ",
-        use = "QTCORE QTGUI LOG4CXX"
-        )
-
     app_plist = '''<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
 <plist version="0.9">
@@ -136,7 +127,7 @@
         mac_plist = app_plist % "ChronoShare",
 	features = "qt4 cxx cxxprogram",
 	defines = "WAF",
-	source = bld.path.ant_glob(['gui/*.cpp', 'gui/*.qrc']),
-	includes = "src gui fs-watcher src . ",
+	source = bld.path.ant_glob(['gui/*.cpp', 'gui/*.cc', 'gui/*.qrc']),
+	includes = "ccnx scheduler src gui src . ",
 	use = "QTCORE QTGUI LOG4CXX fs-watcher ccnx database chronoshare"
 	)