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

Thumbnail: Add an is_generated flag

Which stores whether the thumbnail was generated by the medialibrary
itself or not.
parent 6df72e32
......@@ -167,7 +167,8 @@ std::shared_ptr<Thumbnail> Album::thumbnail()
return m_thumbnail.get();
}
bool Album::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin )
bool Album::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin,
bool isGenerated )
{
if ( m_thumbnailId != 0 )
return Thumbnail::setMrlFromPrimaryKey( m_ml, m_thumbnail, m_thumbnailId,
......@@ -177,7 +178,7 @@ bool Album::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin orig
if ( sqlite::Transaction::transactionInProgress() == false )
t = m_ml->getConn()->newTransaction();
auto lock = m_thumbnail.lock();
m_thumbnail = Thumbnail::create( m_ml, artworkMrl, Thumbnail::Origin::Album );
m_thumbnail = Thumbnail::create( m_ml, artworkMrl, Thumbnail::Origin::Album, isGenerated );
if ( m_thumbnail.get() == nullptr )
return false;
static const std::string req = "UPDATE " + Album::Table::Name
......
......@@ -71,7 +71,8 @@ class Album : public IAlbum, public DatabaseHelpers<Album>
bool setShortSummary( const std::string& summary );
virtual const std::string& artworkMrl() const override;
std::shared_ptr<Thumbnail> thumbnail();
bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin );
bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin,
bool isGenerated);
virtual Query<IMedia> tracks( const QueryParameters* params ) const override;
virtual Query<IMedia> tracks( GenrePtr genre, const QueryParameters* params ) const override;
///
......
......@@ -204,7 +204,8 @@ std::shared_ptr<Thumbnail> Artist::thumbnail()
return m_thumbnail.get();
}
bool Artist::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin )
bool Artist::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin,
bool isGenerated )
{
if ( m_thumbnailId != 0 )
return Thumbnail::setMrlFromPrimaryKey( m_ml, m_thumbnail, m_thumbnailId,
......@@ -214,7 +215,7 @@ bool Artist::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin ori
if ( sqlite::Transaction::transactionInProgress() == false )
t = m_ml->getConn()->newTransaction();
auto lock = m_thumbnail.lock();
m_thumbnail = Thumbnail::create( m_ml, artworkMrl, Thumbnail::Origin::Artist );
m_thumbnail = Thumbnail::create( m_ml, artworkMrl, Thumbnail::Origin::Artist, isGenerated );
if ( m_thumbnail.get() == nullptr )
return false;
static const std::string req = "UPDATE " + Artist::Table::Name +
......
......@@ -60,7 +60,8 @@ public:
bool addMedia( Media& tracks );
virtual const std::string& artworkMrl() const override;
std::shared_ptr<Thumbnail> thumbnail();
bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin );
bool setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin,
bool isGenerated );
bool updateNbAlbum( int increment );
bool updateNbTrack( int increment );
std::shared_ptr<Album> unknownAlbum();
......
......@@ -378,7 +378,8 @@ void Media::setReleaseDate( unsigned int date )
m_changed = true;
}
bool Media::setThumbnail( const std::string& thumbnailMrl, Thumbnail::Origin origin )
bool Media::setThumbnail( const std::string& thumbnailMrl, Thumbnail::Origin origin,
bool isGenerated )
{
if ( m_thumbnailId != 0 )
return Thumbnail::setMrlFromPrimaryKey( m_ml, m_thumbnail, m_thumbnailId,
......@@ -388,7 +389,7 @@ bool Media::setThumbnail( const std::string& thumbnailMrl, Thumbnail::Origin ori
if ( sqlite::Transaction::transactionInProgress() == false )
t = m_ml->getConn()->newTransaction();
auto lock = m_thumbnail.lock();
auto thumbnail = Thumbnail::create( m_ml, thumbnailMrl, origin );
auto thumbnail = Thumbnail::create( m_ml, thumbnailMrl, origin, isGenerated );
if ( thumbnail == nullptr )
return false;
......@@ -406,7 +407,7 @@ bool Media::setThumbnail( const std::string& thumbnailMrl, Thumbnail::Origin ori
bool Media::setThumbnail( const std::string& thumbnailMrl )
{
return setThumbnail( thumbnailMrl, Thumbnail::Origin::UserProvided );
return setThumbnail( thumbnailMrl, Thumbnail::Origin::UserProvided, false );
}
bool Media::save()
......
......@@ -114,7 +114,8 @@ class Media : public IMedia, public DatabaseHelpers<Media>
virtual const std::string& thumbnail() const override;
virtual bool isThumbnailGenerated() const override;
virtual bool setThumbnail( const std::string &thumbnail ) override;
bool setThumbnail( const std::string& thumbnail, Thumbnail::Origin origin );
bool setThumbnail( const std::string& thumbnail, Thumbnail::Origin origin,
bool isGenerated);
virtual unsigned int insertionDate() const override;
virtual unsigned int releaseDate() const override;
uint32_t nbPlaylists() const;
......
......@@ -41,14 +41,17 @@ Thumbnail::Thumbnail( MediaLibraryPtr ml, sqlite::Row& row )
, m_id( row.extract<decltype(m_id)>() )
, m_mrl( row.extract<decltype(m_mrl)>() )
, m_origin( row.extract<decltype(m_origin)>() )
, m_isGenerated( row.extract<decltype(m_isGenerated)>() )
{
}
Thumbnail::Thumbnail( MediaLibraryPtr ml, std::string mrl, Thumbnail::Origin origin )
Thumbnail::Thumbnail( MediaLibraryPtr ml, std::string mrl,
Thumbnail::Origin origin, bool isGenerated )
: m_ml( ml )
, m_id( 0 )
, m_mrl( std::move( mrl ) )
, m_origin( origin )
, m_isGenerated( isGenerated )
{
}
......@@ -104,18 +107,19 @@ void Thumbnail::createTable( sqlite::Connection* dbConnection )
"("
"id_thumbnail INTEGER PRIMARY KEY AUTOINCREMENT,"
"mrl TEXT NOT NULL,"
"origin INTEGER NOT NULL"
"origin INTEGER NOT NULL,"
"is_generated BOOLEAN NOT NULL"
")";
sqlite::Tools::executeRequest( dbConnection, req );
}
std::shared_ptr<Thumbnail> Thumbnail::create( MediaLibraryPtr ml, std::string mrl,
Thumbnail::Origin origin )
Thumbnail::Origin origin, bool isGenerated )
{
static const std::string req = "INSERT INTO " + Thumbnail::Table::Name +
"(mrl, origin) VALUES(?,?)";
auto self = std::make_shared<Thumbnail>( ml, std::move( mrl ), origin );
if ( insert( ml, self, req, self->mrl(), origin ) == false )
"(mrl, origin, is_generated) VALUES(?,?,?)";
auto self = std::make_shared<Thumbnail>( ml, std::move( mrl ), origin, isGenerated );
if ( insert( ml, self, req, self->mrl(), origin, isGenerated ) == false )
return nullptr;
return self;
}
......
......@@ -52,7 +52,7 @@ public:
};
Thumbnail( MediaLibraryPtr ml, sqlite::Row& row );
Thumbnail( MediaLibraryPtr ml, std::string mrl, Origin origin );
Thumbnail( MediaLibraryPtr ml, std::string mrl, Origin origin, bool isGenerated );
int64_t id() const;
const std::string& mrl() const;
......@@ -73,7 +73,7 @@ public:
static void createTable( sqlite::Connection* dbConnection );
static std::shared_ptr<Thumbnail> create( MediaLibraryPtr ml, std::string mrl,
Origin origin );
Origin origin, bool isGenerated );
static const std::string EmptyMrl;
......@@ -82,6 +82,7 @@ private:
int64_t m_id;
std::string m_mrl;
Origin m_origin;
bool m_isGenerated;
friend Thumbnail::Table;
};
......
......@@ -17,9 +17,9 @@
"INSERT INTO " + Media::Table::Name + "_backup SELECT * FROM " + Media::Table::Name,
"INSERT INTO " + Thumbnail::Table::Name + "(id_thumbnail, mrl, origin) "
"INSERT INTO " + Thumbnail::Table::Name + "(id_thumbnail, mrl, origin, is_generated) "
"SELECT id_media, thumbnail, " +
std::to_string( static_cast<ThumbnailType>( Thumbnail::Origin::UserProvided ) ) +
std::to_string( static_cast<ThumbnailType>( Thumbnail::Origin::UserProvided ) ) + ", 1"
" FROM " + Media::Table::Name + " WHERE thumbnail IS NOT NULL AND thumbnail != ''",
"DROP TABLE " + Media::Table::Name,
......
......@@ -338,7 +338,7 @@ bool MetadataAnalyzer::parseVideoFile( IItem& item ) const
media->setTitleBuffered( title );
if ( artworkMrl.empty() == false )
media->setThumbnail( artworkMrl, Thumbnail::Origin::Media );
media->setThumbnail( artworkMrl, Thumbnail::Origin::Media, false );
if ( showName.length() != 0 )
{
......@@ -586,7 +586,7 @@ bool MetadataAnalyzer::parseAudioFile( IItem& item )
auto artworkMrl = item.meta( IItem::Metadata::ArtworkUrl );
if ( artworkMrl.empty() == false )
{
media->setThumbnail( artworkMrl, Thumbnail::Origin::Media );
media->setThumbnail( artworkMrl, Thumbnail::Origin::Media, false );
// Don't use an attachment as default artwork for album/artists
if ( utils::file::schemeIs( "attachment", artworkMrl ) )
artworkMrl.clear();
......@@ -608,7 +608,7 @@ bool MetadataAnalyzer::parseAudioFile( IItem& item )
if ( artworkMrl.empty() == false )
{
auto thumbnail = Thumbnail::create( m_ml, artworkMrl,
Thumbnail::Origin::Album );
Thumbnail::Origin::Album, false );
if ( thumbnail != nullptr )
thumbnailId = thumbnail->id();
}
......@@ -943,7 +943,8 @@ bool MetadataAnalyzer::link( Media& media, Album& album,
// If the album artist has no thumbnail, let's assign it
if ( albumArtistThumbnail == nullptr )
{
albumArtist->setArtworkMrl( albumThumbnail->mrl(), Thumbnail::Origin::AlbumArtist );
albumArtist->setArtworkMrl( albumThumbnail->mrl(),
Thumbnail::Origin::AlbumArtist, false );
}
else if ( albumArtistThumbnail->origin() == Thumbnail::Origin::Artist )
{
......@@ -958,7 +959,7 @@ bool MetadataAnalyzer::link( Media& media, Album& album,
artist->id() != VariousArtistID &&
albumThumbnail != nullptr && artist->thumbnail() == nullptr )
{
artist->setArtworkMrl( album.artworkMrl(), Thumbnail::Origin::Artist );
artist->setArtworkMrl( album.artworkMrl(), Thumbnail::Origin::Artist, false );
}
albumArtist->addMedia( media );
......
......@@ -167,7 +167,7 @@ TEST_F( Albums, SetArtworkMrl )
{
auto a = ml->createAlbum( "album" );
a->setArtworkMrl( "artwork", Thumbnail::Origin::UserProvided );
a->setArtworkMrl( "artwork", Thumbnail::Origin::UserProvided, false);
ASSERT_EQ( a->artworkMrl(), "artwork" );
Reload();
......@@ -183,7 +183,7 @@ TEST_F( Albums, Thumbnail )
ASSERT_EQ( nullptr, t );
std::string mrl = "/path/to/sea/otter/artwork.png";
auto res = a->setArtworkMrl( mrl, Thumbnail::Origin::UserProvided );
auto res = a->setArtworkMrl( mrl, Thumbnail::Origin::UserProvided, false );
ASSERT_TRUE( res );
t = a->thumbnail();
......
......@@ -82,7 +82,7 @@ TEST_F( Artists, ArtworkMrl )
ASSERT_EQ( a->artworkMrl(), "" );
std::string artwork("/tmp/otter.png");
a->setArtworkMrl( artwork, Thumbnail::Origin::UserProvided );
a->setArtworkMrl( artwork, Thumbnail::Origin::UserProvided, false );
ASSERT_EQ( a->artworkMrl(), artwork );
Reload();
......@@ -99,7 +99,7 @@ TEST_F( Artists, Thumbnail )
ASSERT_EQ( nullptr, t );
std::string mrl = "/path/to/sea/otter/artwork.png";
auto res = a->setArtworkMrl( mrl, Thumbnail::Origin::UserProvided );
auto res = a->setArtworkMrl( mrl, Thumbnail::Origin::UserProvided, false );
ASSERT_TRUE( res );
t = a->thumbnail();
......
......@@ -36,7 +36,7 @@ class Thumbnails : public Tests
TEST_F( Thumbnails, Create )
{
std::string mrl = "/path/to/thumbnail.png";
auto t = Thumbnail::create( ml.get(), mrl, Thumbnail::Origin::UserProvided );
auto t = Thumbnail::create( ml.get(), mrl, Thumbnail::Origin::UserProvided, false );
ASSERT_NE( t, nullptr );
ASSERT_EQ( t->mrl(), mrl );
ASSERT_EQ( t->origin(), Thumbnail::Origin::UserProvided );
......@@ -62,7 +62,7 @@ TEST_F( Thumbnails, MediaSetThumbnail )
TEST_F( Thumbnails, Update )
{
std::string mrl = "/path/to/thumbnail.png";
auto t = Thumbnail::create( ml.get(), mrl, Thumbnail::Origin::UserProvided );
auto t = Thumbnail::create( ml.get(), mrl, Thumbnail::Origin::UserProvided, false );
ASSERT_EQ( t->mrl(), mrl );
ASSERT_EQ( t->origin(), Thumbnail::Origin::UserProvided );
......
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