From ed0ee4fd96875451519cc76fa31ee7df0553e58e Mon Sep 17 00:00:00 2001
From: Krisjanis Rijnieks <krisjanis.rijnieks@gmail.com>
Date: Sat, 26 Nov 2016 22:46:19 +0100
Subject: [PATCH] React to image added events

---
 src/Gui/Widgets/SourcesEditorWidget.cpp | 23 ++++++++++++++++++-----
 src/Gui/Widgets/SourcesEditorWidget.h   |  3 +++
 src/MediaServer/DirectoryWatcher.h      |  3 +++
 src/MediaServer/MediaServer.cpp         |  3 +++
 4 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/Gui/Widgets/SourcesEditorWidget.cpp b/src/Gui/Widgets/SourcesEditorWidget.cpp
index b122e9b..5dd7517 100644
--- a/src/Gui/Widgets/SourcesEditorWidget.cpp
+++ b/src/Gui/Widgets/SourcesEditorWidget.cpp
@@ -5,13 +5,17 @@ namespace piMapper {
 
 SourcesEditorWidget::SourcesEditorWidget(){
 	mediaServer = 0;
-	addMediaServerListeners();
+}
+
+void SourcesEditorWidget::setup(){
+	createSelectors();
+}
+
+void SourcesEditorWidget::createSelectors(){
 	imageSelector = new RadioList();
 	videoSelector = new RadioList();
 	fboSelector = new RadioList();
-}
 
-void SourcesEditorWidget::setup(){
 	int numImages = mediaServer->getNumImages();
 	int numVideos = mediaServer->getNumVideos();
 	int numFbos = mediaServer->getNumFboSources();
@@ -48,7 +52,6 @@ void SourcesEditorWidget::setup(){
 	if(numFbos){
 		fboSelector->setPosition(menuPosX, 20);
 	}
-
 }
 
 void SourcesEditorWidget::draw(){
@@ -119,8 +122,13 @@ void SourcesEditorWidget::setMediaServer(MediaServer * newMediaServer){
 		ofLogFatalError("SourcesEditorWidget") << "New media server is 0";
 		exit(EXIT_FAILURE);
 	}
+	
+	if(mediaServer != 0){
+		removeMediaServerListeners();
+	}
 	clearMediaServer();
 	mediaServer = newMediaServer;
+	addMediaServerListeners();
 }
 
 MediaServer * SourcesEditorWidget::getMediaServer(){
@@ -317,7 +325,12 @@ void SourcesEditorWidget::clearMediaServer(){
 }
 
 // TODO: There is no need for those at the moment. They add too much overhead.
-void SourcesEditorWidget::handleImageAdded(string & path){}
+void SourcesEditorWidget::handleImageAdded(string & path){
+	ofLogNotice("SourcesEditorWidget::handleImageAdded")
+		<< "Image path: "
+		<< path;
+}
+
 void SourcesEditorWidget::handleImageRemoved(string & path){}
 void SourcesEditorWidget::handleVideoAdded(string & path){}
 void SourcesEditorWidget::handleVideoRemoved(string & path){}
diff --git a/src/Gui/Widgets/SourcesEditorWidget.h b/src/Gui/Widgets/SourcesEditorWidget.h
index 7ef6aeb..b5dbaea 100644
--- a/src/Gui/Widgets/SourcesEditorWidget.h
+++ b/src/Gui/Widgets/SourcesEditorWidget.h
@@ -44,6 +44,9 @@ class SourcesEditorWidget {
 		RadioList * videoSelector;
 		RadioList * fboSelector;
 		CmdManager * _cmdManager;
+	
+		// Methods for creating and destroying the source selectors
+		void createSelectors();
 
 		// Methods for adding and removing listeners to the media server
 		void addMediaServerListeners();
diff --git a/src/MediaServer/DirectoryWatcher.h b/src/MediaServer/DirectoryWatcher.h
index 1df5710..c302cb1 100644
--- a/src/MediaServer/DirectoryWatcher.h
+++ b/src/MediaServer/DirectoryWatcher.h
@@ -66,6 +66,9 @@ class DirectoryWatcher {
 			if(!filter->accept(pocoPath)){
 				return;
 			}
+			ofLogNotice("DirectoryWatcher::onDirectoryWatcherItemAdded")
+				<< "Added item: "
+				<< path;
 			filePaths.push_back(path);
 			ofNotifyEvent(onItemAdded, path, this);
 		}
diff --git a/src/MediaServer/MediaServer.cpp b/src/MediaServer/MediaServer.cpp
index ef7304f..29c6fb9 100644
--- a/src/MediaServer/MediaServer.cpp
+++ b/src/MediaServer/MediaServer.cpp
@@ -447,6 +447,9 @@ void MediaServer::unloadFboSource(string & fboSourceName){
 }   // unloadFboSource
 
 void MediaServer::handleImageAdded(string & path){
+	ofLogNotice("MediaServer::handleImageAdded")
+		<< "Image path: "
+		<< path;
 	ofNotifyEvent(onImageAdded, path, this);
 }
 void MediaServer::handleImageRemoved(string & path){