Initial import from SVN

This commit is contained in:
emeric
2014-03-09 10:58:52 +01:00
commit b6abce0c2f
440 changed files with 30862 additions and 0 deletions
Binary file not shown.
+31
View File
@@ -0,0 +1,31 @@
#include "AudioTypes.hpp"
Artist::Artist(const std::string& name)
: _name(name)
{
}
// Accesoors
Artist::pointer
Artist::getByName(Wt::Dbo::Session& session, const std::string& name)
{
return session.find<Artist>().where("name = ?").bind( name );
}
// Create
Artist::pointer
Artist::create(Wt::Dbo::Session& session, const std::string& name)
{
return session.add( new Artist( name ) );
}
Artist::pointer
Artist::getNone(Wt::Dbo::Session& session)
{
pointer res = getByName(session, "<None>");
if (!res)
res = create(session, "<None>");
return res;
}
+195
View File
@@ -0,0 +1,195 @@
#ifndef _AUDIO_TYPES_HPP_
#define _AUDIO_TYPES_HPP_
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/WtSqlTraits>
#include <Wt/WDateTime>
class Track;
class Release;
class Artist;
class Artist
{
public:
typedef Wt::Dbo::ptr<Artist> pointer;
Artist() {}
Artist(const std::string& p_name);
// Accessors
static pointer getByName(Wt::Dbo::Session& session, const std::string& name);
static pointer getNone(Wt::Dbo::Session& session);
// Create
static pointer create(Wt::Dbo::Session& session, const std::string& name);
bool isNone(void) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToOne, "artist");
}
private:
std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks; // Tracks of this artist
};
// Album release
class Release
{
public:
typedef Wt::Dbo::ptr<Release> pointer;
Release() {}
Release(const std::string& name);
// Accessors
static pointer getByName(Wt::Dbo::Session& session, const std::string& name);
static pointer getNone(Wt::Dbo::Session& session);
// Create
static pointer create(Wt::Dbo::Session& session, const std::string& name);
std::string getName() const;
bool isNone(void) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToOne, "release");
}
private:
std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks; // Tracks in the release
};
class Genre
{
public:
typedef Wt::Dbo::ptr<Genre> pointer;
Genre();
Genre(const std::string& name);
// Find utility
static pointer getByName(Wt::Dbo::Session& session, const std::string& name);
static pointer getNone(Wt::Dbo::Session& session);
// Create utility
static pointer create(Wt::Dbo::Session& session, const std::string& name);
// Accessors
const std::string& getName(void) const { return _name; }
bool isNone(void) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_genre", "", Wt::Dbo::OnDeleteCascade);
}
private:
std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
};
class Track
{
public:
typedef Wt::Dbo::ptr<Track> pointer;
Track() {}
Track(const boost::filesystem::path& p, Artist::pointer artist, Release::pointer release);
// Find utilities
static pointer getByPath(Wt::Dbo::Session& session, const boost::filesystem::path& p);
static Wt::Dbo::collection< pointer > getAll(Wt::Dbo::Session& session);
// Create utility
static pointer create(Wt::Dbo::Session& session, const boost::filesystem::path& p, Artist::pointer artist, Release::pointer release);
// Accessors
void setTrackNumber(int num) { _trackNumber = num; }
void setDiscNumber(int num) { _discNumber = num; }
void setName(const std::string& name) { _name = name; }
void setDuration(boost::posix_time::time_duration duration) { _duration = duration; }
void setLastWriteTime(boost::posix_time::ptime time) { _fileLastWrite = time; }
void setChecksum(const std::vector<unsigned char>& checksum) { _fileChecksum = checksum; }
void setCreationTime(const boost::posix_time::ptime& time) { _creationTime = time; }
void setGenres(const std::string& genreList) { _genreList = genreList; }
void setGenres(std::vector<Genre::pointer> genres);
void setArtist(Artist::pointer artist) { _artist = artist; }
void setRelease(Release::pointer release) { _release = release; }
std::string getName(void) const { return _name; }
const std::string& getPath(void) const { return _filePath; }
boost::posix_time::time_duration getDuration(void) const { return _duration; }
boost::posix_time::ptime getLastWriteTime(void) const { return _fileLastWrite; }
const std::vector<unsigned char>& getChecksum(void) const { return _fileChecksum; }
Artist::pointer getArtist(void) const { return _artist; }
Release::pointer getRelease(void) const { return _release; }
bool hasGenre(Genre::pointer genre) const { return _genres.count(genre); }
std::vector< Genre::pointer > getGenres(void) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _trackNumber, "track_number");
Wt::Dbo::field(a, _discNumber, "disc_number");
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _duration, "duration");
Wt::Dbo::field(a, _creationTime, "creation_time");
Wt::Dbo::field(a, _genreList, "genre_list");
Wt::Dbo::field(a, _filePath, "path");
Wt::Dbo::field(a, _fileLastWrite, "last_write");
Wt::Dbo::field(a, _fileChecksum, "checksum");
Wt::Dbo::hasMany(a, _genres, Wt::Dbo::ManyToMany, "track_genre", "", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _release, "release", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _artist, "artist", Wt::Dbo::OnDeleteCascade);
}
private:
int _trackNumber;
int _discNumber;
std::string _name;
boost::posix_time::time_duration _duration;
boost::posix_time::ptime _creationTime;
std::string _genreList;
std::string _filePath;
std::vector<unsigned char> _fileChecksum;
boost::posix_time::ptime _fileLastWrite;
Artist::pointer _artist; // Associated Artist
Release::pointer _release; // Associated Release
Wt::Dbo::collection< Genre::pointer > _genres; // Tracks in the release
};
#endif
+44
View File
@@ -0,0 +1,44 @@
#include <fstream>
#include <stdexcept>
#include <boost/crc.hpp> // for boost::crc_32_type
#include "Checksum.hpp"
typedef boost::crc_32_type crc_type;
void computeCrc(const boost::filesystem::path& p, std::vector<unsigned char>& crc)
{
crc_type result;
std::ifstream ifs( p.string().c_str(), std::ios_base::binary );
if (ifs)
{
do
{
std::array<char,1024> buffer;
ifs.read( buffer.data(), buffer.size() );
result.process_bytes( buffer.data(), ifs.gcount() );
}
while ( ifs );
}
else
{
std::cerr << "Failed to open file '" << p << "'" << std::endl;
throw std::runtime_error("Failed to open file '" + p.string() + "'" );
}
// Copy back result into the vector
// Copy the result into a vector of unsigned char
const crc_type::value_type checksum = result.checksum();
for (std::size_t i = 0; (i+1)*8 <= crc_type::bit_count; i++)
{
const unsigned char* data = reinterpret_cast<const unsigned char*>( &checksum );
crc.push_back(data[i]);
}
}
+6
View File
@@ -0,0 +1,6 @@
#include <vector>
#include <boost/filesystem.hpp>
void computeCrc(const boost::filesystem::path& p, std::vector<unsigned char>& checksum);
+422
View File
@@ -0,0 +1,422 @@
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include "Database.hpp"
#include "Checksum.hpp"
#include "AudioTypes.hpp"
#include "transcode/InputMediaFile.hpp"
Database::Database(boost::filesystem::path dbPath, MetaData::Parser& parser)
: _db(dbPath),
_metadataParser(parser)
{
}
void
Database::watchDirectory(WatchedDirectory directory)
{
_directories.push_back(directory);
}
void
Database::unwatchDirectory(WatchedDirectory directory)
{
std::list<WatchedDirectory>::iterator it = std::find(_directories.begin(), _directories.end(), directory);
if (it != _directories.end())
_directories.erase(it);
}
void
Database::refresh(void)
{
removeMissingAudioFiles();
BOOST_FOREACH( const WatchedDirectory& directory, _directories) {
switch (directory.getType()) {
case WatchedDirectory::Audio:
refreshAudioDirectory(directory.getPath());
break;
case WatchedDirectory::Video:
refreshVideoDirectory(directory.getPath());
break;
default:
assert(0);
}
}
}
void
Database::processAudioFile( const boost::filesystem::path& file)
{
try {
// Check last update time
boost::posix_time::ptime lastWriteTime (boost::posix_time::from_time_t( boost::filesystem::last_write_time( file ) ) );
Wt::Dbo::Transaction transaction(_db.getSession());
// Skip file if last write is the same
Wt::Dbo::ptr<Track> track = Track::getByPath(_db.getSession(), file);
if (track && track->getLastWriteTime() == lastWriteTime)
{
std::cerr << "Skipped '" << file << "' (last write time match)" << std::endl;
return;
}
std::vector<unsigned char> checksum;
/* Compute CRC */
computeCrc( file, checksum );
// Skip file if its checksum is still the same
if (track && track->getChecksum() == checksum) {
std::cerr << "Skipped '" << file << "' (checksum match)" << std::endl;
return;
}
std::cout << "parsing file " << file << std::endl;
MetaData::Items items;
_metadataParser.parse(file, items);
// We estimate this is a audio file if:
// - we found a least one audio
// - there is no video stream
// - the duration is not null
Transcode::InputMediaFile mediaFile(file);
/* if (!mediaFile.getStreams( Transcode::Stream::Video ).empty())
{
std::cerr << "Skipped '" << file << "' (estimated video)" << std::endl;
return;
}
else */if (mediaFile.getStreams( Transcode::Stream::Audio ).empty())
{
std::cerr << "Skipped '" << file << "' (no audio stream found)" << std::endl;
return;
}
else if (mediaFile.getDuration().total_seconds() == 0)
{
std::cerr << "Skipped '" << file << "' (duration null!)" << std::endl;
// If Track exists here, delete it!
if (track)
track.remove();
}
std::string title;
if (items.find(MetaData::Title) != items.end()) {
title = boost::any_cast<std::string>(items[MetaData::Title]);
}
else
{
// TODO parse file name guess track etc.
// For now juste use file name as title
title = file.filename().string();
}
// ***** Artist
Wt::Dbo::ptr<Artist> artist;
if (items.find(MetaData::Artist) != items.end())
{
const std::string artistName (boost::any_cast<std::string>(items[MetaData::Artist]));
artist = Artist::getByName(_db.getSession(), artistName );
if (!artist)
artist = Artist::create( _db.getSession(), artistName );
}
else
artist = Artist::getNone(_db.getSession());
assert(artist);
// ***** Release
Wt::Dbo::ptr<Release> release;
if (items.find(MetaData::Album) != items.end())
{
const std::string albumName (boost::any_cast<std::string>(items[MetaData::Album]));
release = Release::getByName(_db.getSession(), albumName);
if (!release)
release = Release::create( _db.getSession(), albumName );
}
else
release = Release::getNone( _db.getSession() );
assert(release);
// ***** Genres
typedef std::list<std::string> GenreList;
GenreList genreList;
std::vector< Genre::pointer > genres;
if (items.find(MetaData::Genre) != items.end())
{
genreList = (boost::any_cast<GenreList>(items[MetaData::Genre]));
BOOST_FOREACH(const std::string& genre, genreList) {
Genre::pointer dbGenre ( Genre::getByName(_db.getSession(), genre) );
if (!dbGenre)
dbGenre = Genre::create(_db.getSession(), genre);
genres.push_back( dbGenre );
}
}
if (genres.empty())
genres.push_back( Genre::getNone( _db.getSession() ));
assert( !genres.empty() );
// If file already exist, update data
// Otherwise, create it
if (!track)
{
// Create a new song
track = Track::create(_db.getSession(), file, artist, release);
std::cout << "Adding '" << file << "'" << std::endl;
}
else
{
std::cout << "Updating '" << file << "'" << std::endl;
}
assert(track);
track.modify()->setChecksum(checksum);
track.modify()->setLastWriteTime(lastWriteTime);
track.modify()->setName(title);
{
std::string trackGenreList;
// Product genre list
BOOST_FOREACH(const std::string& genre, genreList) {
if (!trackGenreList.empty())
trackGenreList += ", ";
trackGenreList += genre;
}
std::cout << "Genre list = " << trackGenreList << std::endl;
track.modify()->setGenres( trackGenreList );
}
track.modify()->setGenres( genres );
track.modify()->setArtist( artist );
track.modify()->setRelease( release );
if (items.find(MetaData::TrackNumber) != items.end())
track.modify()->setTrackNumber( boost::any_cast<std::size_t>(items[MetaData::TrackNumber]) );
if (items.find(MetaData::DiscNumber) != items.end())
track.modify()->setDiscNumber( boost::any_cast<std::size_t>(items[MetaData::DiscNumber]) );
if (items.find(MetaData::Duration) != items.end())
track.modify()->setDuration( boost::any_cast<boost::posix_time::time_duration>(items[MetaData::Duration]) );
if (items.find(MetaData::CreationTime) != items.end())
track.modify()->setCreationTime( boost::any_cast<boost::posix_time::ptime>(items[MetaData::CreationTime]) );
transaction.commit();
}
catch( std::exception& e ) {
std::cerr << "Exception while parsing audio file : '" << file << "': '" << e.what() << "' => skipping!" << std::endl;
}
}
void
Database::refreshAudioDirectory( const boost::filesystem::path& p)
{
std::cout << "Refreshing audio directory " << p << std::endl;
if (boost::filesystem::exists(p) && boost::filesystem::is_directory(p)) {
typedef std::vector<boost::filesystem::path> Paths; // store paths,
Paths files;
std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), std::back_inserter(files));
BOOST_FOREACH(const boost::filesystem::path& file, files) {
if (boost::filesystem::is_directory(file)) {
refreshAudioDirectory( file );
}
else if (boost::filesystem::is_regular(file)) {
processAudioFile( file );
}
else {
std::cout << "Skipped '" << file << "' (not regular)" << std::endl;
}
}
}
std::cout << "Refreshing audio directory " << p << ": DONE" << std::endl;
}
void
Database::removeMissingAudioFiles( void )
{
std::cerr << "Removing missing files..." << std::endl;
Wt::Dbo::Transaction transaction(_db.getSession());
typedef Wt::Dbo::collection< Wt::Dbo::ptr<Track> > Tracks;
Tracks tracks = Track::getAll(_db.getSession());
for (Tracks::iterator i = tracks.begin(); i != tracks.end(); ++i)
{
const boost::filesystem::path p ((*i)->getPath() );
if (!boost::filesystem::exists( p )
|| !boost::filesystem::is_regular( p ) )
{
(*i).remove();
std::cerr << "Removing file '" << p << "'" << std::endl;
}
}
transaction.commit();
std::cerr << "Refrshing missing files done!" << std::endl;
}
Path::pointer
Database::getAddPath(const boost::filesystem::path& path)
{
Path::pointer res;
Path::pointer parentDirectory;
if (path.has_parent_path())
parentDirectory = Path::getByPath(_db.getSession(), path.parent_path());
res = Path::getByPath(_db.getSession(), path);
if (!res)
res = Path::create(_db.getSession(), path, parentDirectory);
else
{
// Make sure the parent directory owns the child
if (parentDirectory && !res->getParent())
parentDirectory.modify()->addChild( res );
}
return res;
}
void
Database::refreshVideoDirectory( const boost::filesystem::path& path)
{
std::cout << "Refreshing video directory " << path << std::endl;
if (boost::filesystem::exists(path) && boost::filesystem::is_directory(path))
{
// Add this directory in the database
{
Wt::Dbo::Transaction transaction(_db.getSession());
Path::pointer pathDirectory = getAddPath(path);
assert( pathDirectory->isDirectory() );
transaction.commit();
}
// Now process all files/dirs in directory
typedef std::vector<boost::filesystem::path> Paths; // store paths,
Paths pathChildren;
std::copy(boost::filesystem::directory_iterator(path), boost::filesystem::directory_iterator(), std::back_inserter(pathChildren));
BOOST_FOREACH(const boost::filesystem::path& pathChild, pathChildren) {
if (boost::filesystem::is_directory(pathChild)) {
refreshVideoDirectory( pathChild );
}
else if (boost::filesystem::is_regular(pathChild)) {
processVideoFile( pathChild );
}
else {
std::cout << "Skipped '" << pathChild << "' (not regular)" << std::endl;
}
}
}
std::cout << "Refreshing video directory " << path << ": DONE" << std::endl;
}
void
Database::processVideoFile( const boost::filesystem::path& file)
{
try {
// Check last update time
boost::posix_time::ptime lastWriteTime (boost::posix_time::from_time_t( boost::filesystem::last_write_time( file ) ) );
Wt::Dbo::Transaction transaction(_db.getSession());
// Skip file if last write is the same
Path::pointer dbPath = Path::getByPath(_db.getSession(), file);
if (dbPath && dbPath->getLastWriteTime() == lastWriteTime)
{
std::cerr << "Skipped '" << file << "' (last write time match)" << std::endl;
return;
}
std::cout << "Video, parsing file " << file << std::endl;
// TODO try to open the video file and get info on it
Transcode::InputMediaFile mediaFile(file);
// We estimate this is a video if:
// - we found a least one video stream
// - the duration is not null
std::vector<Transcode::Stream> videoStreams(mediaFile.getStreams( Transcode::Stream::Video ));
if (videoStreams.empty())
{
std::cerr << "Skipped '" << file << "' (no video stream found!)" << std::endl;
// If Path exist, delete it!
if (dbPath)
dbPath.remove();
}
else if (mediaFile.getDuration().total_seconds() == 0)
{
std::cerr << "Skipped '" << file << "' (duration null!)" << std::endl;
// If Path exist, delete it!
if (dbPath)
dbPath.remove();
}
else
{
// add Path if needed
if (!dbPath)
dbPath = getAddPath( file );
dbPath.modify()->setLastWriteTime( lastWriteTime );
assert(dbPath);
// Valid video here
// Today we are very aggressive, but we could also guess names from path, etc.
Video::pointer video = dbPath.modify()->getVideo();
if (!video) {
video = Video::create(_db.getSession(), dbPath);
std::cout << "Adding '" << file << "'" << std::endl;
}
else
std::cout << "Updating '" << file << "'" << std::endl;
assert(video);
video.modify()->setName( file.filename().string() );
video.modify()->setDuration( mediaFile.getDuration() );
}
transaction.commit();
}
catch( std::exception& e ) {
std::cerr << "Exception while parsing video file : '" << file << "': '" << e.what() << "' => skipping!" << std::endl;
}
}
+67
View File
@@ -0,0 +1,67 @@
#include "metadata/MetaData.hpp"
#include "DatabaseHandler.hpp"
#include "FileTypes.hpp"
class WatchedDirectory {
public:
enum Type {
Audio,
Video,
};
WatchedDirectory( boost::filesystem::path path, Type type) : _path(path), _type(type) {
if (!boost::filesystem::is_directory(path))
throw std::runtime_error( "path '" + path.string() + "' is not a directory!");
}
bool operator==(WatchedDirectory const& other) { return _path == other._path && _type == other._type; }
const boost::filesystem::path& getPath() const { return _path; }
Type getType() const { return _type; }
private:
boost::filesystem::path _path;
Type _type;
};
class Database
{
public:
Database(boost::filesystem::path db, MetaData::Parser& parser);
void watchDirectory(WatchedDirectory dir);
void unwatchDirectory(WatchedDirectory dir);
// Rescan media folders
void refresh();
private:
void refresh(const WatchedDirectory& directory);
// Video
void refreshVideoDirectory( const boost::filesystem::path& directory );
void processVideoFile( const boost::filesystem::path& file);
// Audio
void removeMissingAudioFiles( void );
void refreshAudioDirectory( const boost::filesystem::path& directory);
void processAudioFile( const boost::filesystem::path& file);
Path::pointer getAddPath(const boost::filesystem::path& path);
DatabaseHandler _db;
std::list<WatchedDirectory> _directories;
MetaData::Parser& _metadataParser;
};
+28
View File
@@ -0,0 +1,28 @@
#include "DatabaseHandler.hpp"
#include "AudioTypes.hpp"
#include "FileTypes.hpp"
DatabaseHandler::DatabaseHandler(boost::filesystem::path db)
: _dbBackend( db.string() )
{
_session.setConnection(_dbBackend);
_session.mapClass<Genre>("genre");
_session.mapClass<Track>("track");
_session.mapClass<Artist>("artist");
_session.mapClass<Release>("release");
_session.mapClass<Release>("release");
_session.mapClass<Path>("path");
_session.mapClass<Video>("video");
try {
_session.createTables();
}
catch(std::exception& e) {
std::cerr << "Cannot create tables: " << e.what() << std::endl;
}
_dbBackend.executeSql("pragma journal_mode=WAL");
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef DATABASE_HANDLER_HPP
#define DATABASE_HANDLER_HPP
#include <boost/filesystem.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/backend/Sqlite3>
// Long living class handling the database
class DatabaseHandler
{
public:
DatabaseHandler(boost::filesystem::path db);
Wt::Dbo::Session& getSession() { return _session; }
private:
Wt::Dbo::backend::Sqlite3 _dbBackend;
Wt::Dbo::Session _session;
};
#endif
+76
View File
@@ -0,0 +1,76 @@
#ifndef DATABASE_FILE_TYPES_HPP
#define DATABASE_FILE_TYPES_HPP
#include <string>
#include <boost/filesystem.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/WtSqlTraits>
#include <Wt/WDateTime>
#include "VideoTypes.hpp"
class Path
{
public:
typedef Wt::Dbo::ptr<Path> pointer;
Path();
Path(boost::filesystem::path p);
// utility
static pointer create(Wt::Dbo::Session& session, boost::filesystem::path p);
static pointer create(Wt::Dbo::Session& session, boost::filesystem::path p, Path::pointer parent);
static pointer getByPath(Wt::Dbo::Session& session, boost::filesystem::path p);
static std::vector< pointer > getRoots(Wt::Dbo::Session& session); // get root pathes (no parent!)
// Modifiers
void addChild( pointer child );
void setLastWriteTime(boost::posix_time::ptime time) { _fileLastWrite = time; }
void setChecksum(const std::vector<unsigned char>& checksum) { _fileChecksum = checksum; }
void setCreationTime(const boost::posix_time::ptime& time) { _creationTime = time; }
// Accessors
std::string getFileName(void) const;
boost::filesystem::path getPath(void) const {return boost::filesystem::path(_filePath);}
bool isDirectory() const {return _isDirectory;}
std::vector< pointer > getChilds() const;
pointer getParent() const;
boost::posix_time::ptime getLastWriteTime(void) const { return _fileLastWrite; }
const std::vector<unsigned char>& getChecksum(void) const { return _fileChecksum; }
Wt::Dbo::ptr<Video> getVideo();
const Wt::Dbo::ptr<Video> getVideo() const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _filePath, "path");
Wt::Dbo::field(a, _isDirectory, "directory");
Wt::Dbo::field(a, _creationTime, "creation_time");
Wt::Dbo::field(a, _fileLastWrite, "last_write");
Wt::Dbo::field(a, _fileChecksum, "checksum");
Wt::Dbo::hasMany(a, _childPathes, Wt::Dbo::ManyToMany, "path_path", "child_path_id");
Wt::Dbo::hasMany(a, _parentPathes, Wt::Dbo::ManyToMany, "path_path", "parent_path_id");
Wt::Dbo::hasMany(a, _video, Wt::Dbo::ManyToOne, "path");
}
private:
std::string _filePath;
bool _isDirectory;
boost::posix_time::ptime _creationTime;
std::vector<unsigned char> _fileChecksum;
boost::posix_time::ptime _fileLastWrite;
Wt::Dbo::collection< Wt::Dbo::ptr<Video> > _video;
Wt::Dbo::collection< Wt::Dbo::ptr<Path> > _childPathes; // Child pathes
Wt::Dbo::collection< Wt::Dbo::ptr<Path> > _parentPathes; // Parent pathes
};
#endif
+41
View File
@@ -0,0 +1,41 @@
#include "AudioTypes.hpp"
Genre::Genre()
{
}
Genre::Genre(const std::string& name)
: _name( name )
{
}
Genre::pointer
Genre::getByName(Wt::Dbo::Session& session, const std::string& name)
{
// TODO use like search
return session.find<Genre>().where("name = ?").bind( name );
}
Genre::pointer
Genre::getNone(Wt::Dbo::Session& session)
{
pointer res = getByName(session, "<None>");
if (!res)
res = create(session, "<None>");
return res;
}
bool
Genre::isNone(void) const
{
return (_name == "<None>");
}
Genre::pointer
Genre::create(Wt::Dbo::Session& session, const std::string& name)
{
return session.add(new Genre(name));
}
+102
View File
@@ -0,0 +1,102 @@
#include "FileTypes.hpp"
Path::Path()
{}
Path::Path(boost::filesystem::path p)
: _filePath (p.string()),
_isDirectory( boost::filesystem::is_directory( _filePath ) )
{}
Path::pointer
Path::create(Wt::Dbo::Session& session, boost::filesystem::path p)
{
return session.add(new Path(p));
}
Path::pointer
Path::create(Wt::Dbo::Session& session, boost::filesystem::path p, Path::pointer parent)
{
Path::pointer res = session.add(new Path(p));
if (parent)
parent.modify()->addChild( res );
return res;
}
void
Path::addChild( pointer child)
{
_childPathes.insert(child);
}
Path::pointer
Path::getByPath(Wt::Dbo::Session& session, boost::filesystem::path p)
{
return session.find<Path>().where("path = ?").bind( p.string() );
}
std::vector<Path::pointer>
Path::getChilds(void) const
{
std::vector< pointer > childs;
// Get childs in another way: order by type then name
Wt::Dbo::Query< Path::pointer> query = _childPathes.find().orderBy("path.directory DESC, path.path");
Wt::Dbo::collection< Path::pointer > res = query.resultList();
std::copy( res.begin(), res.end(), std::back_inserter( childs ));
return childs;
}
Path::pointer
Path::getParent(void) const
{
Wt::Dbo::collection< Wt::Dbo::ptr<Path> >::const_iterator it = _parentPathes.begin();
return it != _parentPathes.end() ? *it : Path::pointer();
}
std::vector<Path::pointer>
Path::getRoots(Wt::Dbo::Session& session)
{
std::vector<pointer> roots;
typedef Wt::Dbo::collection< pointer > Pathes;
Pathes pathes = session.find<Path>();
for (Pathes::const_iterator it = pathes.begin(); it != pathes.end(); ++it)
{
if (!(*it)->getParent())
{
std::cout << "path '" << (*it)->getPath() << "' has no parent!" << std::endl;
roots.push_back( *it );
}
}
return roots;
}
Wt::Dbo::ptr<Video>
Path::getVideo()
{
Wt::Dbo::collection< Wt::Dbo::ptr<Video> >::const_iterator it = _video.begin();
return it != _video.end() ? *it : Wt::Dbo::ptr<Video>();
}
const Wt::Dbo::ptr<Video>
Path::getVideo() const
{
Wt::Dbo::collection< Wt::Dbo::ptr<Video> >::const_iterator it = _video.begin();
return it != _video.end() ? *it : Wt::Dbo::ptr<Video>();
}
std::string
Path::getFileName(void) const
{
if (isDirectory())
return boost::filesystem::path(_filePath).string();
else
return boost::filesystem::path(_filePath).filename().string();
}
+29
View File
@@ -0,0 +1,29 @@
#include "AudioTypes.hpp"
Release::Release(const std::string& name)
: _name(name)
{
}
Release::pointer
Release::getByName(Wt::Dbo::Session& session, const std::string& name)
{
return session.find<Release>().where("name = ?").bind( name );
}
Release::pointer
Release::getNone(Wt::Dbo::Session& session)
{
pointer res = getByName(session, "<None>");
if (!res)
res = create(session, "<None>");
return res;
}
Release::pointer
Release::create(Wt::Dbo::Session& session, const std::string& name)
{
return session.add(new Release(name));
}
+144
View File
@@ -0,0 +1,144 @@
#include <algorithm>
#include <boost/foreach.hpp>
#include <sstream>
#include <stdexcept>
#include "SqlQuery.hpp"
WhereClause&
WhereClause::And(const WhereClause& otherClause)
{
if (!otherClause._clause.empty()) {
if (!_clause.empty())
_clause += " AND ";
_clause += "(" + otherClause._clause + ")";
// Add associated bind args
BOOST_FOREACH(const std::string& otherBindArg, otherClause._bindArgs) {
_bindArgs.push_back(otherBindArg);
}
}
return *this;
}
WhereClause&
WhereClause::Or(const WhereClause& otherClause)
{
if (!otherClause._clause.empty()) {
if (!_clause.empty())
_clause += " OR ";
_clause += "(" + otherClause._clause + ")";
// Add associated bind args
BOOST_FOREACH(const std::string& otherBindArg, otherClause._bindArgs) {
_bindArgs.push_back(otherBindArg);
}
}
return *this;
}
std::string
WhereClause::get(void) const
{
if (!_clause.empty())
return "WHERE " + _clause;
else
return "";
}
WhereClause&
WhereClause::bind(const std::string& bindArg)
{
if (_bindArgs.size() >= static_cast<std::size_t>( std::count(_clause.begin(), _clause.end(), '?') ))
throw std::runtime_error("Too many bind args!");
_bindArgs.push_back(bindArg);
return *this;
}
SelectStatement&
SelectStatement::And(const SelectStatement& statement)
{
if( _statement.empty() && !statement._statement.empty())
_statement = "SELECT ";
else if (!_statement.empty() && !statement._statement.empty())
_statement += ",";
_statement += statement._statement;
return *this;
}
FromClause::FromClause(const std::string& clause)
{
_clause.push_back(clause);
}
GroupByStatement&
GroupByStatement::And(const GroupByStatement& statement)
{
if( _statement.empty() && !statement._statement.empty())
_statement = "GROUP BY ";
else if (!_statement.empty() && !statement._statement.empty())
_statement += ",";
_statement += statement._statement;
return *this;
}
FromClause&
FromClause::And(const FromClause& clause)
{
BOOST_FOREACH(const std::string fromClause, clause._clause) {
_clause.push_back(fromClause);
}
_clause.sort();
_clause.unique();
return *this;
}
std::string
FromClause::get() const
{
std::ostringstream oss;
if (!_clause.empty())
{
oss << "FROM ";
for (std::list<std::string>::const_iterator it = _clause.begin(); it != _clause.end(); ++it) {
if (it != _clause.begin())
oss << ",";
oss << *it;
}
}
return oss.str();
}
std::string
SqlQuery::get(void) const
{
std::ostringstream oss;
oss << _selectStatement.get();
if (!_fromClause.get().empty())
oss << " " << _fromClause.get();
if (!_whereClause.get().empty())
oss << " " << _whereClause.get();
if (!_groupByStatement.get().empty())
oss << " " << _groupByStatement.get();
return oss.str();
}
+100
View File
@@ -0,0 +1,100 @@
#ifndef SQL_QUERY_HPP___
#define SQL_QUERY_HPP___
#include <list>
#include <string>
class WhereClause
{
public:
WhereClause() {}
WhereClause(const std::string& clause) { _clause = clause; }
WhereClause& And(const WhereClause& clause);
WhereClause& Or(const WhereClause& clause);
// Arguments binding (for each '?' in where clause)
WhereClause& bind(const std::string& arg);
std::string get() const;
const std::list<std::string>& getBindArgs(void) const {return _bindArgs;}
private:
std::string _clause; // WHERE clause
std::list<std::string> _bindArgs;
};
class GroupByStatement
{
public:
GroupByStatement() {}
GroupByStatement(const std::string& statement) { _statement = statement; }
GroupByStatement& And(const GroupByStatement& statement);
std::string get() const {return _statement;}
private:
std::string _statement; // SELECT statement
};
class SelectStatement
{
public:
SelectStatement() {}
SelectStatement(const std::string& statement) { _statement = statement; }
SelectStatement& And(const SelectStatement& statement);
std::string get() const {return _statement;}
private:
std::string _statement; // SELECT statement
};
class FromClause
{
public:
FromClause() {}
FromClause(const std::string& clause);
FromClause& And(const FromClause& clause);
std::string get() const;
private:
std::list<std::string> _clause;
};
class SqlQuery
{
public:
SelectStatement& select(void) { return _selectStatement;}
FromClause& from(void) { return _fromClause; }
WhereClause& where(void) { return _whereClause; }
const WhereClause& where(void) const { return _whereClause; }
GroupByStatement& groupBy(void) { return _groupByStatement; }
const GroupByStatement& groupBy(void) const { return _groupByStatement; }
std::string get(void) const;
private:
SelectStatement _selectStatement; // SELECT statement
FromClause _fromClause; // FROM tables
WhereClause _whereClause; // WHERE clause
GroupByStatement _groupByStatement; // GROUP BY statement
};
#endif
+55
View File
@@ -0,0 +1,55 @@
#include <boost/foreach.hpp>
#include "AudioTypes.hpp"
Track::Track(const boost::filesystem::path& p, Artist::pointer artist, Release::pointer release)
:
_trackNumber(0),
_discNumber(0),
_filePath( p.string() ),
_artist(artist),
_release(release)
{
}
void
Track::setGenres(std::vector<Genre::pointer> genres)
{
if (_genres.size())
_genres.clear();
BOOST_FOREACH(Genre::pointer genre, genres) {
_genres.insert( genre );
}
}
Track::pointer
Track::getByPath(Wt::Dbo::Session& session, const boost::filesystem::path& p)
{
Wt::Dbo::Transaction transaction(session);
return session.find<Track>().where("path = ?").bind(p.string());
}
Track::pointer
Track::create(Wt::Dbo::Session& session, const boost::filesystem::path& p, Artist::pointer artist, Release::pointer release)
{
return session.add(new Track(p, artist, release) );
}
Wt::Dbo::collection< Track::pointer >
Track::getAll(Wt::Dbo::Session& session)
{
return session.find<Track>();
}
std::vector< Genre::pointer >
Track::getGenres(void) const
{
std::vector< Genre::pointer > genres;
std::copy(_genres.begin(), _genres.end(), std::back_inserter(genres));
return genres;
}
+18
View File
@@ -0,0 +1,18 @@
#include "VideoTypes.hpp"
#include "FileTypes.hpp"
Video::Video()
{
}
Video::Video(Wt::Dbo::ptr<Path> path)
: _path(path)
{
}
Video::pointer
Video::create(Wt::Dbo::Session& session, Wt::Dbo::ptr<Path> path)
{
return session.add(new Video(path));
}
+59
View File
@@ -0,0 +1,59 @@
#ifndef _VIDEO_TYPES_HPP
#define _VIDEO_TYPES_HPP
#include <string>
#include <boost/filesystem.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/WtSqlTraits>
#include <Wt/WDateTime>
class Path;
class Video
{
public:
typedef Wt::Dbo::ptr<Video> pointer;
Video();
Video( Wt::Dbo::ptr<Path> path);
// Find utilities
static pointer getByPath(Wt::Dbo::Session& session, Wt::Dbo::ptr<Path> path);
static Wt::Dbo::collection< pointer > getAll(Wt::Dbo::Session& session);
// Create utility
static pointer create(Wt::Dbo::Session& session, Wt::Dbo::ptr<Path> path);
// Modifiers
void setName(const std::string& name) { _name = name; }
void setDuration(boost::posix_time::time_duration duration) { _duration = duration; }
// Accessors
std::string getName(void) const { return _name; }
Wt::Dbo::ptr<Path> getPath(void) { return _path; }
boost::posix_time::time_duration getDuration(void) const { return _duration; }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _duration, "duration");
Wt::Dbo::belongsTo(a, _path, "path");
}
private:
Wt::Dbo::ptr<Path> _path;
std::string _name;
boost::posix_time::time_duration _duration;
}; // Video
#endif