Browse Source

Workaround path filters not working with DirectoryWatcherManager.addPath

master
Krisjanis Rijnieks 11 years ago
parent
commit
08822add89
  1. 19
      src/MediaServer/DirectoryWatcher.cpp
  2. 62
      src/MediaServer/DirectoryWatcher.h

19
src/MediaServer/DirectoryWatcher.cpp

@ -12,12 +12,23 @@ namespace ofx {
namespace piMapper { namespace piMapper {
DirectoryWatcher::DirectoryWatcher(std::string path, bool video) { DirectoryWatcher::DirectoryWatcher(std::string path, bool video) {
if (video) { if (video) {
filter = CustomVideoPathFilter(); filter = new VideoPathFilter();
} else { } else {
filter = CustomImagePathFilter(); filter = new ImagePathFilter();
} }
dirWatcher.registerAllEvents(this); dirWatcher.registerAllEvents(this);
dirWatcher.addPath(path, true, &filter);
// For some reason the filters are not working,
// we leave just the path here and do the filter logic in the listeners
dirWatcher.addPath(path);
// Initial directory listing. Fill the file paths vector.
IO::DirectoryUtils::list(path, filePaths, true, filter);
}
DirectoryWatcher::~DirectoryWatcher() {
delete filter;
filter = NULL;
} }
std::vector<std::string>& DirectoryWatcher::getFilePaths() { std::vector<std::string>& DirectoryWatcher::getFilePaths() {
@ -25,4 +36,4 @@ namespace ofx {
} }
} // namespace piMapper } // namespace piMapper
} // namespace ifx } // namespace ofx

62
src/MediaServer/DirectoryWatcher.h

@ -14,17 +14,18 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class CustomPathFilter : public ofx::IO::AbstractPathFilter { class BasePathFilter : public ofx::IO::AbstractPathFilter {
public: public:
CustomPathFilter() {}; BasePathFilter() {};
virtual ~CustomPathFilter() {}; virtual ~BasePathFilter() {};
virtual bool accept(const Poco::Path& path) const {}; virtual bool accept(const Poco::Path& path) const {};
}; };
class CustomVideoPathFilter : public CustomPathFilter { class VideoPathFilter : public BasePathFilter {
public: public:
CustomVideoPathFilter() {}; VideoPathFilter() {};
virtual ~CustomVideoPathFilter() {}; virtual ~VideoPathFilter() {};
// TODO: Find useful filters e.g. *.mp4, etc // TODO: Find useful filters e.g. *.mp4, etc
bool accept(const Poco::Path& path) const { bool accept(const Poco::Path& path) const {
return !Poco::File(path).isHidden() && return !Poco::File(path).isHidden() &&
@ -32,10 +33,11 @@ class CustomVideoPathFilter : public CustomPathFilter {
} }
}; };
class CustomImagePathFilter : public CustomPathFilter { class ImagePathFilter : public BasePathFilter {
public: public:
CustomImagePathFilter() {}; ImagePathFilter() {};
virtual ~CustomImagePathFilter() {}; virtual ~ImagePathFilter() {};
// TODO: Find useful filters e.g. *.png,*.jpeg, etc. // TODO: Find useful filters e.g. *.png,*.jpeg, etc.
bool accept(const Poco::Path& path) const { bool accept(const Poco::Path& path) const {
return !Poco::File(path).isHidden() && return !Poco::File(path).isHidden() &&
@ -46,11 +48,18 @@ class CustomImagePathFilter : public CustomPathFilter {
class DirectoryWatcher { class DirectoryWatcher {
public: public:
DirectoryWatcher(std::string path, bool video); DirectoryWatcher(std::string path, bool video);
~DirectoryWatcher();
// TODO make useful stuff with onDirectoryWatcher* // TODO make useful stuff with onDirectoryWatcher*
void onDirectoryWatcherItemAdded( void onDirectoryWatcherItemAdded(
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) {
string path = evt.item.path(); string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if (!filter->accept(pocoPath)) {
return;
}
filePaths.push_back(path); filePaths.push_back(path);
ofNotifyEvent(onItemAdded, path, this); ofNotifyEvent(onItemAdded, path, this);
} }
@ -58,10 +67,14 @@ class DirectoryWatcher {
void onDirectoryWatcherItemRemoved( void onDirectoryWatcherItemRemoved(
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) {
string path = evt.item.path(); string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if (!filter->accept(pocoPath)) {
return;
}
// Remove path from vector // Remove path from vector
int i; for (int i = 0; i < filePaths.size(); i++) {
for (i = 0; i < filePaths.size(); i++) {
if (path == filePaths[i]) { if (path == filePaths[i]) {
filePaths.erase(filePaths.begin() + i); filePaths.erase(filePaths.begin() + i);
break; break;
@ -74,6 +87,11 @@ class DirectoryWatcher {
void onDirectoryWatcherItemModified( void onDirectoryWatcherItemModified(
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) {
string path = evt.item.path(); string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if (!filter->accept(pocoPath)) {
return;
}
ofNotifyEvent(onItemModified, path, this); ofNotifyEvent(onItemModified, path, this);
} }
@ -81,6 +99,12 @@ class DirectoryWatcher {
void onDirectoryWatcherItemMovedFrom( void onDirectoryWatcherItemMovedFrom(
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) {
string path = evt.item.path(); string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if (!filter->accept(pocoPath)) {
return;
}
ofLogNotice("ofApp::onDirectoryWatcherItemMovedFrom") ofLogNotice("ofApp::onDirectoryWatcherItemMovedFrom")
<< "Moved From: " << path; << "Moved From: " << path;
ofNotifyEvent(onItemMovedFrom, path, this); ofNotifyEvent(onItemMovedFrom, path, this);
@ -89,6 +113,12 @@ class DirectoryWatcher {
void onDirectoryWatcherItemMovedTo( void onDirectoryWatcherItemMovedTo(
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) {
string path = evt.item.path(); string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
if (!filter->accept(pocoPath)) {
return;
}
ofLogNotice("ofApp::onDirectoryWatcherItemMovedTo") ofLogNotice("ofApp::onDirectoryWatcherItemMovedTo")
<< "Moved To: " << path; << "Moved To: " << path;
ofNotifyEvent(onItemMovedTo, path, this); ofNotifyEvent(onItemMovedTo, path, this);
@ -111,7 +141,7 @@ class DirectoryWatcher {
private: private:
ofx::IO::DirectoryWatcherManager dirWatcher; ofx::IO::DirectoryWatcherManager dirWatcher;
CustomPathFilter filter; BasePathFilter* filter;
std::vector<std::string> filePaths; std::vector<std::string> filePaths;
}; };
} }

Loading…
Cancel
Save