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

Task: Introduce an Item subclass

parent 1cc9bf33
......@@ -69,9 +69,9 @@ bool MetadataParser::initialize( MediaLibrary* ml)
return cacheUnknownArtist();
}
int MetadataParser::toInt( parser::Task& task, parser::Task::Metadata meta )
int MetadataParser::toInt( parser::Task& task, parser::Task::Item::Metadata meta )
{
auto str = task.meta( meta );
auto str = task.item().meta( meta );
if ( str.empty() == false )
{
try
......@@ -81,7 +81,7 @@ int MetadataParser::toInt( parser::Task& task, parser::Task::Metadata meta )
catch( std::logic_error& ex)
{
LOG_WARN( "Invalid meta #",
static_cast<typename std::underlying_type<parser::Task::Metadata>::type>( meta ),
static_cast<typename std::underlying_type<parser::Task::Item::Metadata>::type>( meta ),
" provided (", str, "): ", ex.what() );
}
}
......@@ -230,7 +230,7 @@ bool MetadataParser::addPlaylistMedias( parser::Task& task, int nbSubitem ) cons
{
auto t = m_ml->getConn()->newTransaction();
LOG_INFO( "Try to import ", task.mrl, " as a playlist" );
auto playlistName = task.meta( parser::Task::Metadata::Title );
auto playlistName = task.item().meta( parser::Task::Item::Metadata::Title );
if ( playlistName.empty() == true )
playlistName = utils::url::decode( utils::file::fileName( task.mrl ) );
auto playlistPtr = Playlist::create( m_ml, playlistName );
......@@ -341,12 +341,12 @@ bool MetadataParser::parseVideoFile( parser::Task& task ) const
{
auto media = task.media.get();
media->setType( IMedia::Type::Video );
const auto& title = task.meta( parser::Task::Metadata::Title );
const auto& title = task.item().meta( parser::Task::Item::Metadata::Title );
if ( title.length() == 0 )
return true;
const auto& showName = task.meta( parser::Task::Metadata::ShowName );
const auto& artworkMrl = task.meta( parser::Task::Metadata::ArtworkUrl );
const auto& showName = task.item().meta( parser::Task::Item::Metadata::ShowName );
const auto& artworkMrl = task.item().meta( parser::Task::Item::Metadata::ArtworkUrl );
return sqlite::Tools::withRetries( 3, [this, &showName, &title, &task, &artworkMrl]() {
auto t = m_ml->getConn()->newTransaction();
......@@ -364,7 +364,7 @@ bool MetadataParser::parseVideoFile( parser::Task& task ) const
if ( show == nullptr )
return false;
}
auto episode = toInt( task, parser::Task::Metadata::Episode );
auto episode = toInt( task, parser::Task::Item::Metadata::Episode );
if ( episode != 0 )
{
std::shared_ptr<Show> s = std::static_pointer_cast<Show>( show );
......@@ -388,7 +388,7 @@ bool MetadataParser::parseAudioFile( parser::Task& task )
{
task.media->setType( IMedia::Type::Audio );
auto artworkMrl = task.meta( parser::Task::Metadata::ArtworkUrl );
auto artworkMrl = task.item().meta( parser::Task::Item::Metadata::ArtworkUrl );
if ( artworkMrl.empty() == false )
{
task.media->setThumbnail( artworkMrl, Thumbnail::Origin::Media );
......@@ -408,7 +408,7 @@ bool MetadataParser::parseAudioFile( parser::Task& task )
auto t = m_ml->getConn()->newTransaction();
if ( album == nullptr )
{
const auto& albumName = task.meta( parser::Task::Metadata::Album );
const auto& albumName = task.item().meta( parser::Task::Item::Metadata::Album );
int64_t thumbnailId = 0;
if ( artworkMrl.empty() == false )
{
......@@ -435,7 +435,7 @@ bool MetadataParser::parseAudioFile( parser::Task& task )
std::shared_ptr<Genre> MetadataParser::handleGenre( parser::Task& task ) const
{
const auto& genreStr = task.meta( parser::Task::Metadata::Genre );
const auto& genreStr = task.item().meta( parser::Task::Item::Metadata::Genre );
if ( genreStr.length() == 0 )
return nullptr;
auto genre = Genre::fromName( m_ml, genreStr );
......@@ -453,7 +453,7 @@ std::shared_ptr<Genre> MetadataParser::handleGenre( parser::Task& task ) const
std::shared_ptr<Album> MetadataParser::findAlbum( parser::Task& task, std::shared_ptr<Artist> albumArtist,
std::shared_ptr<Artist> trackArtist )
{
const auto& albumName = task.meta( parser::Task::Metadata::Album );
const auto& albumName = task.item().meta( parser::Task::Item::Metadata::Album );
if ( albumName.empty() == true )
{
if ( albumArtist != nullptr )
......@@ -478,8 +478,8 @@ std::shared_ptr<Album> MetadataParser::findAlbum( parser::Task& task, std::share
if ( albums.size() == 0 )
return nullptr;
const auto discTotal = toInt( task, parser::Task::Metadata::DiscTotal );
const auto discNumber = toInt( task, parser::Task::Metadata::DiscNumber );
const auto discTotal = toInt( task, parser::Task::Item::Metadata::DiscTotal );
const auto discNumber = toInt( task, parser::Task::Item::Metadata::DiscNumber );
/*
* Even if we get only 1 album, we need to filter out invalid matches.
* For instance, if we have already inserted an album "A" by an artist "john"
......@@ -576,7 +576,7 @@ std::shared_ptr<Album> MetadataParser::findAlbum( parser::Task& task, std::share
// tagged with a year.
if ( multipleArtists == false )
{
auto candidateDate = task.meta( parser::Task::Metadata::Date );
auto candidateDate = task.item().meta( parser::Task::Item::Metadata::Date );
if ( candidateDate.empty() == false )
{
try
......@@ -628,8 +628,8 @@ std::pair<std::shared_ptr<Artist>, std::shared_ptr<Artist>> MetadataParser::find
std::shared_ptr<Artist> artist;
static const std::string req = "SELECT * FROM " + policy::ArtistTable::Name + " WHERE name = ?";
const auto& albumArtistStr = task.meta( parser::Task::Metadata::AlbumArtist );
const auto& artistStr = task.meta( parser::Task::Metadata::Artist );
const auto& albumArtistStr = task.item().meta( parser::Task::Item::Metadata::AlbumArtist );
const auto& artistStr = task.item().meta( parser::Task::Item::Metadata::Artist );
if ( albumArtistStr.empty() == true && artistStr.empty() == true )
{
return {m_unknownArtist, m_unknownArtist};
......@@ -673,9 +673,9 @@ std::shared_ptr<AlbumTrack> MetadataParser::handleTrack( std::shared_ptr<Album>
{
assert( sqlite::Transaction::transactionInProgress() == true );
auto title = task.meta( parser::Task::Metadata::Title );
const auto trackNumber = toInt( task, parser::Task::Metadata::TrackNumber );
const auto discNumber = toInt( task, parser::Task::Metadata::DiscNumber );
auto title = task.item().meta( parser::Task::Item::Metadata::Title );
const auto trackNumber = toInt( task, parser::Task::Item::Metadata::TrackNumber );
const auto discNumber = toInt( task, parser::Task::Item::Metadata::DiscNumber );
if ( title.empty() == true )
{
LOG_WARN( "Failed to get track title" );
......@@ -697,7 +697,7 @@ std::shared_ptr<AlbumTrack> MetadataParser::handleTrack( std::shared_ptr<Album>
return nullptr;
}
const auto& releaseDate = task.meta( parser::Task::Metadata::Date );
const auto& releaseDate = task.item().meta( parser::Task::Item::Metadata::Date );
if ( releaseDate.empty() == false )
{
auto releaseYear = atoi( releaseDate.c_str() );
......
......@@ -59,7 +59,7 @@ protected:
std::shared_ptr<Genre> handleGenre( parser::Task& task ) const;
private:
static int toInt( parser::Task& task, parser::Task::Metadata meta );
static int toInt( parser::Task& task, parser::Task::Item::Metadata meta );
private:
MediaLibrary* m_ml;
......
......@@ -91,29 +91,29 @@ parser::Task::Status VLCMetadataService::run( parser::Task& task )
if ( res == false )
return parser::Task::Status::Fatal;
}
task.setMeta( parser::Task::Metadata::Title,
task.item().setMeta( parser::Task::Item::Metadata::Title,
task.vlcMedia.meta( libvlc_meta_Title ) );
task.setMeta( parser::Task::Metadata::ArtworkUrl,
task.item().setMeta( parser::Task::Item::Metadata::ArtworkUrl,
task.vlcMedia.meta( libvlc_meta_ArtworkURL ) );
task.setMeta( parser::Task::Metadata::ShowName,
task.item().setMeta( parser::Task::Item::Metadata::ShowName,
task.vlcMedia.meta( libvlc_meta_ShowName ) );
task.setMeta( parser::Task::Metadata::Episode,
task.item().setMeta( parser::Task::Item::Metadata::Episode,
task.vlcMedia.meta( libvlc_meta_Episode ) );
task.setMeta( parser::Task::Metadata::Album,
task.item().setMeta( parser::Task::Item::Metadata::Album,
task.vlcMedia.meta( libvlc_meta_Album ) );
task.setMeta( parser::Task::Metadata::Genre,
task.item().setMeta( parser::Task::Item::Metadata::Genre,
task.vlcMedia.meta( libvlc_meta_Genre ) );
task.setMeta( parser::Task::Metadata::Date,
task.item().setMeta( parser::Task::Item::Metadata::Date,
task.vlcMedia.meta( libvlc_meta_Date ) );
task.setMeta( parser::Task::Metadata::AlbumArtist,
task.item().setMeta( parser::Task::Item::Metadata::AlbumArtist,
task.vlcMedia.meta( libvlc_meta_AlbumArtist ) );
task.setMeta( parser::Task::Metadata::Artist,
task.item().setMeta( parser::Task::Item::Metadata::Artist,
task.vlcMedia.meta( libvlc_meta_Artist ) );
task.setMeta( parser::Task::Metadata::TrackNumber,
task.item().setMeta( parser::Task::Item::Metadata::TrackNumber,
task.vlcMedia.meta( libvlc_meta_TrackNumber ) );
task.setMeta( parser::Task::Metadata::DiscNumber,
task.item().setMeta( parser::Task::Item::Metadata::DiscNumber,
task.vlcMedia.meta( libvlc_meta_DiscNumber ) );
task.setMeta( parser::Task::Metadata::DiscTotal,
task.item().setMeta( parser::Task::Item::Metadata::DiscTotal,
task.vlcMedia.meta( libvlc_meta_DiscTotal ) );
// Don't save the file parsing step yet, since all data are just in memory. Just mark
// the extraction as done.
......
......@@ -130,7 +130,12 @@ int64_t Task::id() const
return m_id;
}
std::string Task::meta( Task::Metadata type ) const
Task::Item& Task::item()
{
return m_item;
}
std::string Task::Item::meta( Task::Item::Metadata type ) const
{
auto it = m_metadata.find( type );
if ( it == end( m_metadata ) )
......@@ -138,7 +143,7 @@ std::string Task::meta( Task::Metadata type ) const
return it->second;
}
void Task::setMeta( Task::Metadata type, std::string value )
void Task::Item::setMeta( Task::Item::Metadata type, std::string value )
{
m_metadata[type] = std::move( value );
}
......
......@@ -89,22 +89,32 @@ public:
Completed = 1 | 2,
};
enum class Metadata : uint8_t
class Item
{
Title,
ArtworkUrl,
ShowName,
Episode,
Album,
Genre,
Date,
AlbumArtist,
Artist,
TrackNumber,
DiscNumber,
DiscTotal,
public:
enum class Metadata : uint8_t
{
Title,
ArtworkUrl,
ShowName,
Episode,
Album,
Genre,
Date,
AlbumArtist,
Artist,
TrackNumber,
DiscNumber,
DiscTotal,
};
std::string meta( Metadata type ) const;
void setMeta( Metadata type, std::string value );
private:
std::unordered_map<Metadata, std::string> m_metadata;
};
/*
* Constructs a task to be resumed.
* The Media is provided as a parameter to avoid this to implicitely query
......@@ -136,8 +146,7 @@ public:
bool updateFileId();
int64_t id() const;
std::string meta( Metadata type ) const;
void setMeta( Metadata type, std::string value );
Item& item();
// Restore attached entities such as media/files
bool restoreLinkedEntities();
......@@ -172,8 +181,7 @@ private:
int64_t m_fileId;
int64_t m_parentFolderId;
int64_t m_parentPlaylistId;
std::unordered_map<Metadata, std::string> m_metadata;
Item m_item;
friend policy::TaskTable;
};
......
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