4 changed files with 28 additions and 245 deletions
@ -1,46 +1,41 @@ |
|||||
//
|
|
||||
// DirectoryWatcher.cpp
|
|
||||
// example
|
|
||||
//
|
|
||||
// Created by felix on 23.09.14.
|
|
||||
//
|
|
||||
//
|
|
||||
|
|
||||
#include "DirectoryWatcher.h" |
#include "DirectoryWatcher.h" |
||||
|
|
||||
namespace ofx { |
namespace ofx { |
||||
namespace piMapper { |
namespace piMapper { |
||||
|
|
||||
DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){ |
DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){ |
||||
mediaType = watcherMediaType; |
_mediaType = watcherMediaType; |
||||
// Decide what filter we need depending on media type
|
|
||||
if(mediaType == SourceType::SOURCE_TYPE_VIDEO){ |
if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){ |
||||
filter = new VideoPathFilter(); |
_directory.allowExt("mp4"); |
||||
}else if(mediaType == SourceType::SOURCE_TYPE_IMAGE){ |
_directory.allowExt("h264"); |
||||
filter = new ImagePathFilter(); |
_directory.allowExt("mov"); |
||||
|
_directory.allowExt("avi"); |
||||
|
_directory.allowExt("ogv"); |
||||
|
_directory.allowExt("mpeg"); |
||||
|
}else if(_mediaType == SourceType::SOURCE_TYPE_IMAGE){ |
||||
|
_directory.allowExt("png"); |
||||
|
_directory.allowExt("jpg"); |
||||
|
_directory.allowExt("jpeg"); |
||||
}else{ |
}else{ |
||||
ofLogFatalError("DirectoryWatcher::DirectoryWatcher", "Unkonwn media type"); |
ofLogFatalError("DirectoryWatcher::DirectoryWatcher", "Unkonwn media type"); |
||||
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
||||
} |
} |
||||
dirWatcher.registerAllEvents(this); |
|
||||
// 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(){ |
_directory.listDir(path); |
||||
delete filter; |
_directory.sort(); |
||||
filter = 0; |
|
||||
|
for(int i = 0; i < _directory.size(); ++i){ |
||||
|
_filePaths.push_back(path + _directory.getName(i)); |
||||
|
} |
||||
} |
} |
||||
|
|
||||
vector <string> & DirectoryWatcher::getFilePaths(){ |
vector <string> & DirectoryWatcher::getFilePaths(){ |
||||
return filePaths; |
return _filePaths; |
||||
} |
} |
||||
|
|
||||
int DirectoryWatcher::getMediaType(){ |
int DirectoryWatcher::getMediaType(){ |
||||
return mediaType; |
return _mediaType; |
||||
} |
} |
||||
|
|
||||
} // namespace piMapper
|
} // namespace piMapper
|
||||
|
@ -1,146 +1,21 @@ |
|||||
//
|
|
||||
// DirectoryWatcher.h
|
|
||||
// example
|
|
||||
//
|
|
||||
// Created by felix on 23.09.14.
|
|
||||
//
|
|
||||
//
|
|
||||
|
|
||||
#pragma once |
#pragma once |
||||
|
|
||||
#include "ofMain.h" |
#include "ofMain.h" |
||||
#include "ofxIO.h" |
|
||||
#include "SourceType.h" |
#include "SourceType.h" |
||||
|
|
||||
namespace ofx { |
namespace ofx { |
||||
namespace piMapper { |
namespace piMapper { |
||||
|
|
||||
class BasePathFilter : public ofx::IO::AbstractPathFilter { |
|
||||
public: |
|
||||
BasePathFilter(){} |
|
||||
virtual ~BasePathFilter(){} |
|
||||
virtual bool accept(const Poco::Path & path) const { return false; } |
|
||||
}; |
|
||||
|
|
||||
class VideoPathFilter : public BasePathFilter { |
|
||||
public: |
|
||||
VideoPathFilter(){} |
|
||||
virtual ~VideoPathFilter(){} |
|
||||
|
|
||||
// TODO: Make Raspi load only mp4s (and mov and h264?)
|
|
||||
// How to detect codec? Read file headers?
|
|
||||
|
|
||||
bool accept(const Poco::Path & path) const { |
|
||||
return !Poco::File(path).isHidden() && |
|
||||
(ofIsStringInString(path.toString(), ".mp4") || |
|
||||
ofIsStringInString(path.toString(), ".h264") || |
|
||||
ofIsStringInString(path.toString(), ".mov") || |
|
||||
ofIsStringInString(path.toString(), ".avi") || |
|
||||
ofIsStringInString(path.toString(), ".ogv") || |
|
||||
ofIsStringInString(path.toString(), ".mpeg")); |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
class ImagePathFilter : public BasePathFilter { |
|
||||
public: |
|
||||
ImagePathFilter(){} |
|
||||
virtual ~ImagePathFilter(){} |
|
||||
|
|
||||
bool accept(const Poco::Path & path) const { |
|
||||
return !Poco::File(path).isHidden() && |
|
||||
(ofIsStringInString(path.toString(), ".png") || |
|
||||
ofIsStringInString(path.toString(), ".jpg") || |
|
||||
ofIsStringInString(path.toString(), ".jpeg")); |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
class DirectoryWatcher { |
class DirectoryWatcher { |
||||
public: |
public: |
||||
DirectoryWatcher(string path, int watcherMediaType); |
DirectoryWatcher(string path, int watcherMediaType); |
||||
~DirectoryWatcher(); |
|
||||
|
|
||||
// TODO make useful stuff with onDirectoryWatcher*
|
|
||||
void onDirectoryWatcherItemAdded(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){ |
|
||||
string path = evt.item.path(); |
|
||||
Poco::Path pocoPath = Poco::Path(path); |
|
||||
if(!filter->accept(pocoPath)){ |
|
||||
return; |
|
||||
} |
|
||||
ofLogNotice("DirectoryWatcher::onDirectoryWatcherItemAdded") |
|
||||
<< "Added item: " |
|
||||
<< path; |
|
||||
filePaths.push_back(path); |
|
||||
ofNotifyEvent(onItemAdded, path, this); |
|
||||
} |
|
||||
|
|
||||
void onDirectoryWatcherItemRemoved(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){ |
|
||||
string path = evt.item.path(); |
|
||||
Poco::Path pocoPath = Poco::Path(path); |
|
||||
if(!filter->accept(pocoPath)){ |
|
||||
return; |
|
||||
} |
|
||||
// Remove path from vector
|
|
||||
for(int i = 0; i < filePaths.size(); i++){ |
|
||||
if(path == filePaths[i]){ |
|
||||
filePaths.erase(filePaths.begin() + i); |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
ofNotifyEvent(onItemRemoved, path, this); |
|
||||
} |
|
||||
|
|
||||
void onDirectoryWatcherItemModified(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){ |
|
||||
string path = evt.item.path(); |
|
||||
Poco::Path pocoPath = Poco::Path(path); |
|
||||
if(!filter->accept(pocoPath)){ |
|
||||
return; |
|
||||
} |
|
||||
ofNotifyEvent(onItemModified, path, this); |
|
||||
} |
|
||||
|
|
||||
void onDirectoryWatcherItemMovedFrom(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){ |
|
||||
string path = evt.item.path(); |
|
||||
Poco::Path pocoPath = Poco::Path(path); |
|
||||
if(!filter->accept(pocoPath)){ |
|
||||
return; |
|
||||
} |
|
||||
ofLogNotice("ofApp::onDirectoryWatcherItemMovedFrom") |
|
||||
<< "Moved From: " << path; |
|
||||
ofNotifyEvent(onItemMovedFrom, path, this); |
|
||||
} |
|
||||
|
|
||||
void onDirectoryWatcherItemMovedTo(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){ |
|
||||
string path = evt.item.path(); |
|
||||
Poco::Path pocoPath = Poco::Path(path); |
|
||||
if(!filter->accept(pocoPath)){ |
|
||||
return; |
|
||||
} |
|
||||
ofLogNotice("ofApp::onDirectoryWatcherItemMovedTo") |
|
||||
<< "Moved To: " << path; |
|
||||
ofNotifyEvent(onItemMovedTo, path, this); |
|
||||
} |
|
||||
|
|
||||
void onDirectoryWatcherError(const Poco::Exception & exc){ |
|
||||
ofLogError("ofApp::onDirectoryWatcherError") |
|
||||
<< "Error: " << exc.displayText(); |
|
||||
} |
|
||||
|
|
||||
// Getters
|
|
||||
vector<string> & getFilePaths(); |
vector<string> & getFilePaths(); |
||||
int getMediaType(); |
int getMediaType(); |
||||
|
|
||||
// Custom events
|
|
||||
ofEvent <string> onItemAdded; |
|
||||
ofEvent <string> onItemRemoved; |
|
||||
ofEvent <string> onItemModified; |
|
||||
ofEvent <string> onItemMovedFrom; |
|
||||
ofEvent <string> onItemMovedTo; |
|
||||
|
|
||||
private: |
private: |
||||
ofx::IO::DirectoryWatcherManager dirWatcher; |
ofDirectory _directory; |
||||
BasePathFilter * filter; |
vector<string> _filePaths; |
||||
vector <string> filePaths; |
int _mediaType; |
||||
int mediaType; |
|
||||
}; |
}; |
||||
|
|
||||
} // namespace piMapper
|
} // namespace piMapper
|
||||
|
Loading…
Reference in new issue