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

Album: Improve album's date handling

parent f3a9f056
...@@ -31,7 +31,12 @@ class IAlbum ...@@ -31,7 +31,12 @@ class IAlbum
virtual ~IAlbum() {} virtual ~IAlbum() {}
virtual unsigned int id() const = 0; virtual unsigned int id() const = 0;
virtual const std::string& title() const = 0; virtual const std::string& title() const = 0;
virtual time_t releaseYear() const = 0; /**
* @brief releaseYear returns the release year, or 0 if unknown.
* The release date of an album is considered unknown if multiple tracks
* of the same album have different release dates
*/
virtual unsigned int releaseYear() const = 0;
virtual const std::string& shortSummary() const = 0; virtual const std::string& shortSummary() const = 0;
virtual const std::string& artworkUrl() const = 0; virtual const std::string& artworkUrl() const = 0;
virtual time_t lastSyncDate() const = 0; virtual time_t lastSyncDate() const = 0;
......
...@@ -49,7 +49,7 @@ Album::Album(const std::string& title ) ...@@ -49,7 +49,7 @@ Album::Album(const std::string& title )
: m_id( 0 ) : m_id( 0 )
, m_title( title ) , m_title( title )
, m_artistId( 0 ) , m_artistId( 0 )
, m_releaseYear( 0 ) , m_releaseYear( ~0u )
, m_lastSyncDate( 0 ) , m_lastSyncDate( 0 )
, m_nbTracks( 0 ) , m_nbTracks( 0 )
, m_tracksCached( false ) , m_tracksCached( false )
...@@ -66,13 +66,27 @@ const std::string& Album::title() const ...@@ -66,13 +66,27 @@ const std::string& Album::title() const
return m_title; return m_title;
} }
time_t Album::releaseYear() const unsigned int Album::releaseYear() const
{ {
if ( m_releaseYear == ~0U )
return 0;
return m_releaseYear; return m_releaseYear;
} }
bool Album::setReleaseYear( time_t date ) bool Album::setReleaseYear( unsigned int date, bool force )
{ {
if ( date == m_releaseYear )
return true;
if ( force == false )
{
if ( m_releaseYear != ~0u && date != m_releaseYear )
{
// If we already have set the date back to 0, don't do it again.
if ( m_releaseYear == 0 )
return true;
date = 0;
}
}
static const std::string req = "UPDATE " + policy::AlbumTable::Name static const std::string req = "UPDATE " + policy::AlbumTable::Name
+ " SET release_year = ? WHERE id_album = ?"; + " SET release_year = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, date, m_id ) == false ) if ( sqlite::Tools::executeUpdate( m_dbConnection, req, date, m_id ) == false )
......
...@@ -57,8 +57,17 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable> ...@@ -57,8 +57,17 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
virtual unsigned int id() const override; virtual unsigned int id() const override;
virtual const std::string& title() const override; virtual const std::string& title() const override;
virtual time_t releaseYear() const override; virtual unsigned int releaseYear() const override;
bool setReleaseYear( time_t date ); /**
* @brief setReleaseYear Updates the release year
* @param date The desired date.
* @param force If force is true, the date will be applied no matter what.
* If force is false, the date will be applied if it never was
* applied before. Otherwise, setReleaseYear() will restore the release
* date to 0.
* @return
*/
bool setReleaseYear( unsigned int date, bool force );
virtual const std::string& shortSummary() const override; virtual const std::string& shortSummary() const override;
bool setShortSummary( const std::string& summary ); bool setShortSummary( const std::string& summary );
virtual const std::string& artworkUrl() const override; virtual const std::string& artworkUrl() const override;
......
...@@ -369,12 +369,10 @@ std::shared_ptr<AlbumTrack> VLCMetadataService::handleTrack(std::shared_ptr<Albu ...@@ -369,12 +369,10 @@ std::shared_ptr<AlbumTrack> VLCMetadataService::handleTrack(std::shared_ptr<Albu
{ {
auto releaseYear = atoi( releaseYearStr.c_str() ); auto releaseYear = atoi( releaseYearStr.c_str() );
track->setReleaseYear( releaseYear ); track->setReleaseYear( releaseYear );
// If the album release year is unset, set it based on track // Let the album handle multiple dates. In order to do this properly, we need
if ( album->releaseYear() == 0 ) // to know if the date has been changed before, which can be known only by
album->setReleaseYear( releaseYear ); // using Album class internals.
// However, if it's now different, unset it album->setReleaseYear( releaseYear, false );
else if ( album->releaseYear() != releaseYear )
album->setReleaseYear( 0 );
} }
return track; return track;
} }
......
...@@ -115,8 +115,22 @@ TEST_F( Albums, SetReleaseDate ) ...@@ -115,8 +115,22 @@ TEST_F( Albums, SetReleaseDate )
{ {
auto a = ml->createAlbum( "album" ); auto a = ml->createAlbum( "album" );
a->setReleaseYear( 1234 ); ASSERT_EQ( 0u, a->releaseYear() );
ASSERT_EQ( a->releaseYear(), 1234 );
a->setReleaseYear( 1234, false );
ASSERT_EQ( a->releaseYear(), 1234u );
a->setReleaseYear( 4321, false );
// We now have conflicting dates, it should be restored to 0.
ASSERT_EQ( 0u, a->releaseYear() );
// Check that this is not considered initial state anymore, and that pretty much any other date will be ignored.
a->setReleaseYear( 666, false );
ASSERT_EQ( 0u, a->releaseYear() );
// Now check that forcing a date actually forces it
a->setReleaseYear( 9876, true );
ASSERT_EQ( 9876u, a->releaseYear() );
Reload(); Reload();
......
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