Commit d0eb5aa6 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

AlbumTrack: Expose the discNumber property

parent 526653bc
......@@ -45,6 +45,10 @@ class IAlbumTrack
virtual const std::string& genre() = 0;
virtual unsigned int trackNumber() = 0;
virtual std::shared_ptr<IAlbum> album() = 0;
/**
* @return Which disc this tracks appears on (or 0 if unspecified)
*/
virtual unsigned int discNumber() const = 0;
/**
* @brief releaseYear Represent the track release year. It doesn't
* imply anything regarding the album's release year.
......
......@@ -156,10 +156,10 @@ std::vector<MediaPtr> Album::tracks() const
return m_tracks;
}
std::shared_ptr<AlbumTrack> Album::addTrack(std::shared_ptr<Media> media, unsigned int trackNb )
std::shared_ptr<AlbumTrack> Album::addTrack(std::shared_ptr<Media> media, unsigned int trackNb, unsigned int discNumber )
{
//FIXME: This MUST be executed as a transaction
auto track = AlbumTrack::create( m_dbConnection, m_id, media.get(), trackNb );
auto track = AlbumTrack::create( m_dbConnection, m_id, media.get(), trackNb, discNumber );
if ( track == nullptr )
return nullptr;
if ( media->setAlbumTrack( track ) == false )
......
......@@ -75,7 +75,7 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
bool setArtworkUrl( const std::string& artworkUrl );
virtual time_t lastSyncDate() const override;
virtual std::vector<MediaPtr> tracks() const override;
std::shared_ptr<AlbumTrack> addTrack( std::shared_ptr<Media> media, unsigned int trackNb );
std::shared_ptr<AlbumTrack> addTrack(std::shared_ptr<Media> media, unsigned int trackNb , unsigned int discNumber);
unsigned int nbTracks() const override;
virtual ArtistPtr albumArtist() const override;
......
......@@ -40,16 +40,18 @@ AlbumTrack::AlbumTrack(DBConnection dbConnection, sqlite::Row& row )
>> m_genre
>> m_trackNumber
>> m_albumId
>> m_releaseYear;
>> m_releaseYear
>> m_discNumber;
}
//FIXME: constify media
AlbumTrack::AlbumTrack( Media* media, unsigned int trackNumber, unsigned int albumId )
AlbumTrack::AlbumTrack( Media* media, unsigned int trackNumber, unsigned int albumId, unsigned int discNumber )
: m_id( 0 )
, m_mediaId( media->id() )
, m_trackNumber( trackNumber )
, m_albumId( albumId )
, m_releaseYear( 0 )
, m_discNumber( discNumber )
, m_album( nullptr )
{
}
......@@ -86,6 +88,7 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
"track_number UNSIGNED INTEGER,"
"album_id UNSIGNED INTEGER NOT NULL,"
"release_year UNSIGNED INTEGER,"
"disc_number UNSIGNED INTEGER,"
"FOREIGN KEY (media_id) REFERENCES " + policy::MediaTable::Name + "(id_media)"
" ON DELETE CASCADE, "
"FOREIGN KEY (album_id) REFERENCES Album(id_album) "
......@@ -94,12 +97,12 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
return sqlite::Tools::executeRequest( dbConnection, req );
}
std::shared_ptr<AlbumTrack> AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, Media* media, unsigned int trackNb)
std::shared_ptr<AlbumTrack> AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, Media* media, unsigned int trackNb, unsigned int discNumber )
{
auto self = std::make_shared<AlbumTrack>( media, trackNb, albumId );
auto self = std::make_shared<AlbumTrack>( media, trackNb, albumId, discNumber );
static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
+ "(media_id, track_number, album_id) VALUES(?, ?, ?)";
if ( _Cache::insert( dbConnection, self, req, media->id(), trackNb, albumId ) == false )
+ "(media_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?)";
if ( _Cache::insert( dbConnection, self, req, media->id(), trackNb, albumId, discNumber ) == false )
return nullptr;
self->m_dbConnection = dbConnection;
return self;
......@@ -142,6 +145,11 @@ bool AlbumTrack::setReleaseYear(unsigned int year)
return true;
}
unsigned int AlbumTrack::discNumber() const
{
return m_discNumber;
}
std::shared_ptr<IAlbum> AlbumTrack::album()
{
if ( m_album == nullptr && m_albumId != 0 )
......
......@@ -50,7 +50,7 @@ class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, pol
typedef Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable> _Cache;
public:
AlbumTrack( DBConnection dbConnection, sqlite::Row& row );
AlbumTrack(Media* media, unsigned int trackNumber, unsigned int albumId );
AlbumTrack(Media* media, unsigned int trackNumber, unsigned int albumId , unsigned int discNumber);
virtual unsigned int id() const override;
virtual const std::string& artist() const override;
......@@ -60,11 +60,12 @@ class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, pol
virtual unsigned int trackNumber() override;
virtual unsigned int releaseYear() const override;
bool setReleaseYear( unsigned int year );
virtual unsigned int discNumber() const override;
virtual std::shared_ptr<IAlbum> album() override;
static bool createTable( DBConnection dbConnection );
static std::shared_ptr<AlbumTrack> create(DBConnection dbConnection, unsigned int albumId,
Media* media, unsigned int trackNb );
Media* media, unsigned int trackNb , unsigned int discNumber);
private:
DBConnection m_dbConnection;
......@@ -75,6 +76,7 @@ class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, pol
unsigned int m_trackNumber;
unsigned int m_albumId;
unsigned int m_releaseYear;
unsigned int m_discNumber;
std::shared_ptr<Album> m_album;
......
......@@ -358,7 +358,13 @@ std::shared_ptr<AlbumTrack> VLCMetadataService::handleTrack(std::shared_ptr<Albu
trackNb = atoi( trackNbStr.c_str() );
else
trackNb = 0;
auto track = std::static_pointer_cast<AlbumTrack>( album->addTrack( media, trackNb ) );
auto discNumberStr = vlcMedia.meta( libvlc_meta_DiscNumber );
auto discNumber = 0;
if ( discNumberStr.empty() == false )
discNumber = atoi( discNumberStr.c_str() );
auto track = std::static_pointer_cast<AlbumTrack>( album->addTrack( media, trackNb, discNumber ) );
if ( track == nullptr )
{
LOG_ERROR( "Failed to create album track" );
......
......@@ -60,7 +60,7 @@ TEST_F( Albums, AddTrack )
{
auto a = ml->createAlbum( "albumtag" );
auto f = ml->addFile( "track.mp3", nullptr );
auto track = a->addTrack( f, 10 );
auto track = a->addTrack( f, 10, 0 );
ASSERT_NE( track, nullptr );
auto tracks = a->tracks();
......@@ -80,7 +80,7 @@ TEST_F( Albums, NbTracks )
for ( auto i = 1u; i <= 10; ++i )
{
auto f = ml->addFile( "track" + std::to_string(i) + ".mp3", nullptr );
auto track = a->addTrack( f, i );
auto track = a->addTrack( f, i, i );
ASSERT_NE( track, nullptr );
}
auto tracks = a->tracks();
......@@ -97,7 +97,7 @@ TEST_F( Albums, SetGenre )
{
auto a = ml->createAlbum( "album" );
auto f = ml->addFile( "track.mp3", nullptr );
auto t = a->addTrack( f, 1 );
auto t = a->addTrack( f, 1, 0 );
t->setGenre( "happy underground post progressive death metal" );
ASSERT_EQ( t->genre(), "happy underground post progressive death metal" );
......@@ -169,7 +169,7 @@ TEST_F( Albums, FetchAlbumFromTrack )
{
auto a = ml->createAlbum( "album" );
auto f = ml->addFile( "file.mp3", nullptr );
auto t = a->addTrack( f, 1 );
auto t = a->addTrack( f, 1, 0 );
f->setAlbumTrack( t );
}
Reload();
......
......@@ -36,7 +36,7 @@ TEST_F( AlbumTracks, Artist )
{
auto album = ml->createAlbum( "album" );
auto f = ml->addFile( "track1.mp3", nullptr );
auto track = album->addTrack( f, 1 );
auto track = album->addTrack( f, 1, 0 );
ASSERT_EQ( track->artist(), "" );
track->setArtist( "artist" );
......@@ -54,7 +54,7 @@ TEST_F( AlbumTracks, SetReleaseYear )
{
auto a = ml->createAlbum( "album" );
auto m = ml->addFile( "test.mp3", nullptr );
auto t = a->addTrack( m, 1 );
auto t = a->addTrack( m, 1, 0 );
ASSERT_EQ( 0u, t->releaseYear() );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment