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

Use a reference to media instead of a shared_ptr when possible

This is far from complete, but it's a start
parent afc732ff
......@@ -142,14 +142,14 @@ std::vector<MediaPtr> Album::tracks() const
return Media::fetchAll<IMedia>( m_dbConnection, req, m_id );
}
std::shared_ptr<AlbumTrack> Album::addTrack(std::shared_ptr<Media> media, unsigned int trackNb, unsigned int discNumber )
std::shared_ptr<AlbumTrack> Album::addTrack( Media& media, unsigned int trackNb, unsigned int discNumber )
{
auto t = m_dbConnection->newTransaction();
auto track = AlbumTrack::create( m_dbConnection, m_id, media.get(), trackNb, discNumber );
auto track = AlbumTrack::create( m_dbConnection, m_id, media.id(), trackNb, discNumber );
if ( track == nullptr )
return nullptr;
if ( media->setAlbumTrack( track ) == false )
if ( media.setAlbumTrack( track ) == false )
return nullptr;
static const std::string req = "UPDATE " + policy::AlbumTable::Name +
" SET nb_tracks = nb_tracks + 1 WHERE id_album = ?";
......
......@@ -72,7 +72,7 @@ class Album : public IAlbum, public DatabaseHelpers<Album, policy::AlbumTable>
virtual const std::string& artworkMrl() const override;
bool setArtworkMrl( const std::string& artworkMrl );
virtual std::vector<MediaPtr> tracks() const override;
std::shared_ptr<AlbumTrack> addTrack(std::shared_ptr<Media> media, unsigned int trackNb , unsigned int discNumber);
std::shared_ptr<AlbumTrack> addTrack(Media& media, unsigned int trackNb , unsigned int discNumber);
unsigned int nbTracks() const override;
virtual ArtistPtr albumArtist() const override;
......
......@@ -46,9 +46,9 @@ AlbumTrack::AlbumTrack(DBConnection dbConnection, sqlite::Row& row )
}
//FIXME: constify media
AlbumTrack::AlbumTrack( Media* media, unsigned int trackNumber, unsigned int albumId, unsigned int discNumber )
AlbumTrack::AlbumTrack( unsigned int mediaId, unsigned int trackNumber, unsigned int albumId, unsigned int discNumber )
: m_id( 0 )
, m_mediaId( media->id() )
, m_mediaId( mediaId )
, m_trackNumber( trackNumber )
, m_albumId( albumId )
, m_releaseYear( 0 )
......@@ -113,12 +113,12 @@ bool AlbumTrack::createTable( DBConnection dbConnection )
sqlite::Tools::executeRequest( dbConnection, triggerReq );
}
std::shared_ptr<AlbumTrack> AlbumTrack::create(DBConnection dbConnection, unsigned int albumId, Media* media, unsigned int trackNb, unsigned int discNumber )
std::shared_ptr<AlbumTrack> AlbumTrack::create( DBConnection dbConnection, unsigned int albumId, unsigned int mediaId, unsigned int trackNb, unsigned int discNumber )
{
auto self = std::make_shared<AlbumTrack>( media, trackNb, albumId, discNumber );
auto self = std::make_shared<AlbumTrack>( mediaId, trackNb, albumId, discNumber );
static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
+ "(media_id, track_number, album_id, disc_number) VALUES(?, ?, ?, ?)";
if ( insert( dbConnection, self, req, media->id(), trackNb, albumId, discNumber ) == false )
if ( insert( dbConnection, self, req, mediaId, trackNb, albumId, discNumber ) == false )
return nullptr;
self->m_dbConnection = dbConnection;
return self;
......
......@@ -50,7 +50,7 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
{
public:
AlbumTrack( DBConnection dbConnection, sqlite::Row& row );
AlbumTrack(Media* media, unsigned int trackNumber, unsigned int albumId , unsigned int discNumber);
AlbumTrack( unsigned int mediaId, unsigned int trackNumber, unsigned int albumId , unsigned int discNumber);
virtual unsigned int id() const override;
virtual ArtistPtr artist() const override;
......@@ -64,8 +64,8 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack, policy
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 , unsigned int discNumber);
static std::shared_ptr<AlbumTrack> create( DBConnection dbConnection, unsigned int albumId,
unsigned int mediaId, unsigned int trackNb , unsigned int discNumber );
private:
DBConnection m_dbConnection;
......
......@@ -106,12 +106,12 @@ std::vector<MediaPtr> Artist::media() const
}
}
bool Artist::addMedia(Media* media)
bool Artist::addMedia( Media& media )
{
static const std::string req = "INSERT INTO MediaArtistRelation VALUES(?, ?)";
// If track's ID is 0, the request will fail due to table constraints
sqlite::ForeignKey artistForeignKey( m_id );
return sqlite::Tools::insert( m_dbConnection, req, media->id(), artistForeignKey ) != 0;
return sqlite::Tools::insert( m_dbConnection, req, media.id(), artistForeignKey ) != 0;
}
const std::string& Artist::artworkMrl() const
......
......@@ -52,7 +52,7 @@ public:
bool setShortBio( const std::string& shortBio );
virtual std::vector<AlbumPtr> albums() const override;
virtual std::vector<MediaPtr> media() const override;
bool addMedia( Media* media );
bool addMedia( Media& media );
virtual const std::string& artworkMrl() const override;
bool setArtworkMrl( const std::string& artworkMrl );
bool updateNbAlbum( int increment );
......
......@@ -124,7 +124,7 @@ IMetadataService::Status VLCMetadataService::handleMediaMeta( std::shared_ptr<Me
t->commit();
if ( isAudio == true )
{
if ( parseAudioFile( media, vlcMedia ) == false )
if ( parseAudioFile( *media, vlcMedia ) == false )
return Status::Fatal;
}
else
......@@ -181,13 +181,13 @@ bool VLCMetadataService::parseVideoFile( std::shared_ptr<Media> file, VLC::Media
/* Audio files */
bool VLCMetadataService::parseAudioFile( std::shared_ptr<Media> media, VLC::Media& vlcMedia ) const
bool VLCMetadataService::parseAudioFile( Media& media, VLC::Media& vlcMedia ) const
{
media->setType( IMedia::Type::AudioType );
media.setType( IMedia::Type::AudioType );
auto cover = vlcMedia.meta( libvlc_meta_ArtworkURL );
if ( cover.empty() == false )
media->setThumbnail( cover );
media.setThumbnail( cover );
auto artists = handleArtists( vlcMedia );
auto album = handleAlbum( media, vlcMedia, artists.first, artists.second );
......@@ -203,7 +203,7 @@ bool VLCMetadataService::parseAudioFile( std::shared_ptr<Media> media, VLC::Medi
}
/* Album handling */
std::shared_ptr<Album> VLCMetadataService::findAlbum( Media* media, VLC::Media& vlcMedia, const std::string& title, Artist* albumArtist ) const
std::shared_ptr<Album> VLCMetadataService::findAlbum( Media& media, VLC::Media& vlcMedia, const std::string& title, Artist* albumArtist ) const
{
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" WHERE title = ?";
......@@ -276,7 +276,7 @@ std::shared_ptr<Album> VLCMetadataService::findAlbum( Media* media, VLC::Media&
// Assume album files will be in the same folder.
auto candidateFolder = utils::file::directory( tracks[0]->mrl() );
auto newFileFolder = utils::file::directory( media->mrl() );
auto newFileFolder = utils::file::directory( media.mrl() );
if ( candidateFolder != newFileFolder )
{
it = albums.erase( it );
......@@ -293,7 +293,7 @@ std::shared_ptr<Album> VLCMetadataService::findAlbum( Media* media, VLC::Media&
return std::static_pointer_cast<Album>( albums[0] );
}
std::shared_ptr<Album> VLCMetadataService::handleAlbum( std::shared_ptr<Media> media, VLC::Media& vlcMedia, std::shared_ptr<Artist> albumArtist, std::shared_ptr<Artist> trackArtist ) const
std::shared_ptr<Album> VLCMetadataService::handleAlbum( Media& media, VLC::Media& vlcMedia, std::shared_ptr<Artist> albumArtist, std::shared_ptr<Artist> trackArtist ) const
{
auto albumTitle = vlcMedia.meta( libvlc_meta_Album );
std::shared_ptr<Album> album;
......@@ -311,7 +311,7 @@ std::shared_ptr<Album> VLCMetadataService::handleAlbum( std::shared_ptr<Media> m
if ( albumTitle.length() > 0 )
{
album = findAlbum( media.get(), vlcMedia, albumTitle, albumArtist.get() );
album = findAlbum( media, vlcMedia, albumTitle, albumArtist.get() );
if ( album == nullptr )
{
......@@ -332,7 +332,7 @@ std::shared_ptr<Album> VLCMetadataService::handleAlbum( std::shared_ptr<Media> m
// If we know a track artist, specify it, otherwise, fallback to the album/unknown artist
auto track = handleTrack( album, media, vlcMedia, trackArtist ? trackArtist : artist );
if ( track != nullptr )
media->setAlbumTrack( track );
media.setAlbumTrack( track );
return album;
}
......@@ -388,7 +388,7 @@ std::pair<std::shared_ptr<Artist>, std::shared_ptr<Artist>> VLCMetadataService::
/* Tracks handling */
std::shared_ptr<AlbumTrack> VLCMetadataService::handleTrack( std::shared_ptr<Album> album, std::shared_ptr<Media> media, VLC::Media& vlcMedia, std::shared_ptr<Artist> artist ) const
std::shared_ptr<AlbumTrack> VLCMetadataService::handleTrack( std::shared_ptr<Album> album, Media& media, VLC::Media& vlcMedia, std::shared_ptr<Artist> artist ) const
{
auto trackNbStr = vlcMedia.meta( libvlc_meta_TrackNumber );
......@@ -403,7 +403,7 @@ std::shared_ptr<AlbumTrack> VLCMetadataService::handleTrack( std::shared_ptr<Alb
}
}
if ( title.empty() == false )
media->setTitle( title );
media.setTitle( title );
unsigned int trackNb;
if ( trackNbStr.empty() == false )
trackNb = atoi( trackNbStr.c_str() );
......@@ -443,7 +443,7 @@ std::shared_ptr<AlbumTrack> VLCMetadataService::handleTrack( std::shared_ptr<Alb
/* Misc */
bool VLCMetadataService::link( std::shared_ptr<Media> media, std::shared_ptr<Album> album,
bool VLCMetadataService::link( Media& media, std::shared_ptr<Album> album,
std::shared_ptr<Artist> albumArtist, std::shared_ptr<Artist> artist ) const
{
if ( albumArtist == nullptr && artist == nullptr )
......@@ -459,9 +459,9 @@ bool VLCMetadataService::link( std::shared_ptr<Media> media, std::shared_ptr<Alb
albumArtist->setArtworkMrl( album->artworkMrl() );
if ( albumArtist != nullptr )
albumArtist->addMedia( media.get() );
albumArtist->addMedia( media );
if ( artist != nullptr && ( albumArtist == nullptr || albumArtist->id() != artist->id() ) )
artist->addMedia( media.get() );
artist->addMedia( media );
auto currentAlbumArtist = album->albumArtist();
......
......@@ -52,13 +52,13 @@ class VLCMetadataService : public IMetadataService
private:
Status handleMediaMeta( std::shared_ptr<Media> media , VLC::Media &vlcMedia ) const;
std::shared_ptr<Album> findAlbum(Media* media, VLC::Media& vlcMedia, const std::string& title, Artist* albumArtist ) const;
bool parseAudioFile( std::shared_ptr<Media> media, VLC::Media &vlcMedia ) const;
std::shared_ptr<Album> findAlbum(Media& media, VLC::Media& vlcMedia, const std::string& title, Artist* albumArtist ) const;
bool parseAudioFile(Media& media, VLC::Media &vlcMedia ) const;
bool parseVideoFile( std::shared_ptr<Media> file, VLC::Media &media ) const;
std::pair<std::shared_ptr<Artist>, std::shared_ptr<Artist>> handleArtists(VLC::Media& vlcMedia ) const;
std::shared_ptr<AlbumTrack> handleTrack(std::shared_ptr<Album> album, std::shared_ptr<Media> media, VLC::Media& vlcMedia , std::shared_ptr<Artist> artist) const;
bool link(std::shared_ptr<Media> media, std::shared_ptr<Album> album, std::shared_ptr<Artist> albumArtist, std::shared_ptr<Artist> artist ) const;
std::shared_ptr<Album> handleAlbum(std::shared_ptr<Media> media, VLC::Media& vlcMedia, std::shared_ptr<Artist> albumArtist , std::shared_ptr<Artist> artist) const;
std::shared_ptr<AlbumTrack> handleTrack(std::shared_ptr<Album> album, Media& media, VLC::Media& vlcMedia , std::shared_ptr<Artist> artist) const;
bool link(Media& media, std::shared_ptr<Album> album, std::shared_ptr<Artist> albumArtist, std::shared_ptr<Artist> artist ) const;
std::shared_ptr<Album> handleAlbum( Media& media, VLC::Media& vlcMedia, std::shared_ptr<Artist> albumArtist, std::shared_ptr<Artist> artist ) const;
VLC::Instance m_instance;
IMetadataServiceCb* m_cb;
......
......@@ -60,7 +60,7 @@ TEST_F( Albums, AddTrack )
{
auto a = ml->createAlbum( "albumtag" );
auto f = ml->addFile( "track.mp3" );
auto track = a->addTrack( f, 10, 0 );
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" );
auto track = a->addTrack( f, i, 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" );
auto t = a->addTrack( f, 1, 0 );
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" );
......@@ -168,7 +168,7 @@ TEST_F( Albums, FetchAlbumFromTrack )
{
auto a = ml->createAlbum( "album" );
auto f = ml->addFile( "file.mp3" );
auto t = a->addTrack( f, 1, 0 );
auto t = a->addTrack( *f, 1, 0 );
f->setAlbumTrack( t );
Reload();
......
......@@ -35,7 +35,7 @@ TEST_F( AlbumTracks, Create )
{
auto album = ml->createAlbum( "album" );
auto f = ml->addFile( "track1.mp3" );
auto track = album->addTrack( f, 1, 10 );
auto track = album->addTrack( *f, 1, 10 );
ASSERT_NE( nullptr, track );
ASSERT_EQ( 10u, track->discNumber() );
......@@ -49,7 +49,7 @@ TEST_F( AlbumTracks, Artist )
{
auto album = ml->createAlbum( "album" );
auto f = ml->addFile( "track1.mp3" );
auto track = album->addTrack( f, 1, 0 );
auto track = album->addTrack( *f, 1, 0 );
auto artist = track->artist();
ASSERT_EQ( nullptr, artist );
......@@ -74,7 +74,7 @@ TEST_F( AlbumTracks, SetReleaseYear )
{
auto a = ml->createAlbum( "album" );
auto m = ml->addFile( "test.mp3" );
auto t = a->addTrack( m, 1, 0 );
auto t = a->addTrack( *m, 1, 0 );
ASSERT_EQ( 0u, t->releaseYear() );
......
......@@ -119,7 +119,7 @@ TEST_F( Artists, AllSongs )
for (auto i = 1; i <= 3; ++i)
{
auto f = ml->addFile( "song" + std::to_string(i) + ".mp3" );
auto res = artist->addMedia( f.get() );
auto res = artist->addMedia( *f );
ASSERT_TRUE( res );
}
......
......@@ -241,7 +241,7 @@ TEST_F( DeviceFs, RemoveAlbum )
{
auto album = std::static_pointer_cast<Album>( ml->createAlbum( "album" ) );
auto file = ml->media( mock::FileSystemFactory::Root + "audio.mp3" );
album->addTrack( std::static_pointer_cast<Media>( file ), 1, 1 );
album->addTrack( static_cast<Media&>( *file ), 1, 1 );
auto artist = ml->createArtist( "artist" );
album->setAlbumArtist( artist.get() );
}
......@@ -250,8 +250,8 @@ TEST_F( DeviceFs, RemoveAlbum )
auto album = std::static_pointer_cast<Album>( ml->createAlbum( "album 2" ) );
auto file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ml->media( RemovableDeviceMountpoint + "removablefile2.mp3" );
album->addTrack( std::static_pointer_cast<Media>( file ), 1, 1 );
album->addTrack( std::static_pointer_cast<Media>( file ), 2, 1 );
album->addTrack( static_cast<Media&>( *file ), 1, 1 );
album->addTrack( static_cast<Media&>( *file ), 2, 1 );
auto artist = ml->createArtist( "artist 2" );
album->setAlbumArtist( artist.get() );
}
......@@ -285,12 +285,12 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
auto album = std::static_pointer_cast<Album>( ml->createAlbum( "album" ) );
auto file = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto file2 = ml->media( RemovableDeviceMountpoint + "removablefile2.mp3" );
album->addTrack( std::static_pointer_cast<Media>( file ), 1, 1 );
album->addTrack( std::static_pointer_cast<Media>( file2 ), 2, 1 );
album->addTrack( static_cast<Media&>( *file ), 1, 1 );
album->addTrack( static_cast<Media&>( *file2 ), 2, 1 );
auto newArtist = ml->createArtist( "artist" );
album->setAlbumArtist( newArtist.get() );
newArtist->addMedia( static_cast<Media*>( file.get() ) );
newArtist->addMedia( static_cast<Media*>( file2.get() ) );
newArtist->addMedia( static_cast<Media&>( *file ) );
newArtist->addMedia( static_cast<Media&>( *file2 ) );
}
auto albums = ml->albums();
......
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