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" |
|||
|
|||
namespace ofx { |
|||
namespace piMapper { |
|||
|
|||
DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){ |
|||
mediaType = watcherMediaType; |
|||
// Decide what filter we need depending on media type
|
|||
if(mediaType == SourceType::SOURCE_TYPE_VIDEO){ |
|||
filter = new VideoPathFilter(); |
|||
}else if(mediaType == SourceType::SOURCE_TYPE_IMAGE){ |
|||
filter = new ImagePathFilter(); |
|||
_mediaType = watcherMediaType; |
|||
|
|||
if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){ |
|||
_directory.allowExt("mp4"); |
|||
_directory.allowExt("h264"); |
|||
_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{ |
|||
ofLogFatalError("DirectoryWatcher::DirectoryWatcher", "Unkonwn media type"); |
|||
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(){ |
|||
delete filter; |
|||
filter = 0; |
|||
|
|||
_directory.listDir(path); |
|||
_directory.sort(); |
|||
|
|||
for(int i = 0; i < _directory.size(); ++i){ |
|||
_filePaths.push_back(path + _directory.getName(i)); |
|||
} |
|||
} |
|||
|
|||
vector <string> & DirectoryWatcher::getFilePaths(){ |
|||
return filePaths; |
|||
return _filePaths; |
|||
} |
|||
|
|||
int DirectoryWatcher::getMediaType(){ |
|||
return mediaType; |
|||
return _mediaType; |
|||
} |
|||
|
|||
} // namespace piMapper
|
|||
|
@ -1,146 +1,21 @@ |
|||
//
|
|||
// DirectoryWatcher.h
|
|||
// example
|
|||
//
|
|||
// Created by felix on 23.09.14.
|
|||
//
|
|||
//
|
|||
|
|||
#pragma once |
|||
|
|||
#include "ofMain.h" |
|||
#include "ofxIO.h" |
|||
#include "SourceType.h" |
|||
|
|||
namespace ofx { |
|||
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 { |
|||
public: |
|||
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(); |
|||
|
|||
// Custom events
|
|||
ofEvent <string> onItemAdded; |
|||
ofEvent <string> onItemRemoved; |
|||
ofEvent <string> onItemModified; |
|||
ofEvent <string> onItemMovedFrom; |
|||
ofEvent <string> onItemMovedTo; |
|||
|
|||
private: |
|||
ofx::IO::DirectoryWatcherManager dirWatcher; |
|||
BasePathFilter * filter; |
|||
vector <string> filePaths; |
|||
int mediaType; |
|||
ofDirectory _directory; |
|||
vector<string> _filePaths; |
|||
int _mediaType; |
|||
}; |
|||
|
|||
} // namespace piMapper
|
|||
|
Loading…
Reference in new issue