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

parser: Pass the file type to tasks

refs #29
parent 87958a3f
......@@ -29,6 +29,7 @@
#include "medialibrary/filesystem/IFile.h"
#include "medialibrary/filesystem/IDirectory.h"
#include "medialibrary/Types.h"
#include "medialibrary/IFile.h"
namespace medialibrary
{
......@@ -119,6 +120,11 @@ public:
*/
virtual const std::string& mrl() const = 0;
/**
* @brief fileType Returns the analyzed file type
*/
virtual IFile::Type fileType() const = 0;
/**
* @return The number of subitems for this item.
*/
......
......@@ -463,6 +463,7 @@ void MediaLibrary::onDiscoveredFile( std::shared_ptr<fs::IFile> fileFs,
}
task = parser::Task::create( this, std::move( fileFs ), std::move( parentFolder ),
std::move( parentFolderFs ),
IFile::Type::Main,
std::move( parentPlaylist ) );
if ( task != nullptr && m_parser != nullptr )
m_parser->parse( task );
......@@ -1150,7 +1151,10 @@ void MediaLibrary::migrateModel13to14( uint32_t originalPreviousVersion )
#include "database/tables/Task_v14.sql"
"INSERT INTO " + parser::Task::Table::Name + " SELECT "
"id_task, step, retry_count, mrl, file_id, parent_folder_id, parent_playlist_id,"
"id_task, step, retry_count, mrl, " +
std::to_string( static_cast<std::underlying_type<IFile::Type>::type>(
IFile::Type::Main ) ) + ","
"file_id, parent_folder_id, parent_playlist_id,"
"parent_playlist_index, 0 FROM " + parser::Task::Table::Name + "_backup",
"DROP TABLE " + parser::Task::Table::Name + "_backup",
......
......@@ -41,8 +41,10 @@
"artist_id = " + Artist::Table::Name + ".id_artist"
")",
"INSERT INTO " + parser::Task::Table::Name + " (step, retry_count, file_id, parent_folder_id) "
"SELECT parser_step, parser_retries, id_file, folder_id FROM " + File::Table::Name,
"INSERT INTO " + parser::Task::Table::Name + " (file_type, step, retry_count, file_id, parent_folder_id) "
"SELECT " + std::to_string( static_cast<std::underlying_type<IFile::Type>::type>(
IFile::Type::Main ) ) + ","
"parser_step, parser_retries, id_file, folder_id FROM " + File::Table::Name,
"CREATE TEMPORARY TABLE " + File::Table::Name + "_backup("
"id_file INTEGER PRIMARY KEY AUTOINCREMENT,"
......
......@@ -4,6 +4,7 @@
"step INTEGER NOT NULL DEFAULT 0,"
"retry_count INTEGER NOT NULL DEFAULT 0,"
"mrl TEXT,"
"file_type INTEGER NOT NULL,"
"file_id UNSIGNED INTEGER,"
"parent_folder_id UNSIGNED INTEGER,"
"parent_playlist_id INTEGER,"
......
......@@ -56,22 +56,25 @@ Task::Task( MediaLibraryPtr ml, sqlite::Row& row )
{
std::string mrl;
unsigned int parentPlaylistIndex;
IFile::Type fileType;
bool isRefresh;
row >> m_id
>> m_step
>> m_retryCount
>> mrl
>> fileType
>> m_fileId
>> m_parentFolderId
>> m_parentPlaylistId
>> parentPlaylistIndex
>> isRefresh;
m_item = Item{ this, std::move( mrl ), parentPlaylistIndex, isRefresh };
m_item = Item{ this, std::move( mrl ), fileType, parentPlaylistIndex, isRefresh };
}
Task::Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
std::shared_ptr<fs::IDirectory> parentFolderFs,
IFile::Type fileType,
std::shared_ptr<Playlist> parentPlaylist,
unsigned int parentPlaylistIndex )
: currentService( 0 )
......@@ -79,8 +82,8 @@ Task::Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
, m_step( Step::None )
, m_fileId( 0 )
, m_item( this, std::move( fileFs ), std::move( parentFolder ),
std::move( parentFolderFs ), std::move( parentPlaylist ),
parentPlaylistIndex, false )
std::move( parentFolderFs ), fileType,
std::move( parentPlaylist ), parentPlaylistIndex, false )
{
}
......@@ -162,23 +165,25 @@ Task::Item& Task::item()
return m_item;
}
Task::Item::Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemPosition,
bool isRefresh )
Task::Item::Item( ITaskCb* taskCb, std::string mrl, IFile::Type fileType,
unsigned int subitemPosition, bool isRefresh )
: m_taskCb( taskCb )
, m_mrl( std::move( mrl ) )
, m_fileType( fileType )
, m_duration( 0 )
, m_parentPlaylistIndex( subitemPosition )
, m_isRefresh( isRefresh )
{
}
Task::Item::Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
Task::Item::Item(ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
std::shared_ptr<fs::IDirectory> parentFolderFs,
std::shared_ptr<fs::IDirectory> parentFolderFs, IFile::Type fileType,
std::shared_ptr<Playlist> parentPlaylist, unsigned int parentPlaylistIndex,
bool isRefresh )
: m_taskCb( taskCb )
, m_mrl( fileFs->mrl() )
, m_fileType( fileType )
, m_duration( 0 )
, m_fileFs( std::move( fileFs ) )
, m_parentFolder( std::move( parentFolder ) )
......@@ -189,9 +194,11 @@ Task::Item::Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
{
}
Task::Item::Item( ITaskCb* taskCb, std::shared_ptr<File> file, std::shared_ptr<fs::IFile> fileFs )
Task::Item::Item( ITaskCb* taskCb, std::shared_ptr<File> file,
std::shared_ptr<fs::IFile> fileFs )
: m_taskCb( taskCb )
, m_mrl( fileFs->mrl() )
, m_fileType( file->type() )
, m_duration( 0 )
, m_file( std::move( file ) )
, m_fileFs( std::move( fileFs ) )
......@@ -222,6 +229,11 @@ void Task::Item::setMrl( std::string mrl )
m_mrl = std::move( mrl );
}
IFile::Type Task::Item::fileType() const
{
return m_fileType;
}
size_t Task::Item::nbSubItems() const
{
return m_subItems.size();
......@@ -234,7 +246,8 @@ const IItem& Task::Item::subItem( unsigned int index ) const
IItem& Task::Item::createSubItem( std::string mrl, unsigned int playlistIndex )
{
m_subItems.emplace_back( nullptr, std::move( mrl ), playlistIndex, false );
m_subItems.emplace_back( nullptr, std::move( mrl ), IFile::Type::Main,
playlistIndex, false );
return m_subItems.back();
}
......@@ -426,8 +439,9 @@ bool Task::restoreLinkedEntities()
}
m_item = Item{ this, std::move( fileFs ), std::move( parentFolder ),
std::move( parentFolderFs ), std::move( parentPlaylist ),
m_item.parentPlaylistIndex(), m_item.isRefresh() };
std::move( parentFolderFs ), m_item.fileType(),
std::move( parentPlaylist ), m_item.parentPlaylistIndex(),
m_item.isRefresh() };
if ( file != nullptr )
{
m_item.setMedia( file->media() );
......@@ -486,6 +500,7 @@ std::vector<std::shared_ptr<Task>> Task::fetchUncompleted( MediaLibraryPtr ml )
std::shared_ptr<Task>
Task::create( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder, std::shared_ptr<fs::IDirectory> parentFolderFs,
IFile::Type fileType,
std::pair<std::shared_ptr<Playlist>, unsigned int> parentPlaylist )
{
auto parentFolderId = parentFolder->id();
......@@ -493,12 +508,13 @@ Task::create( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
auto parentPlaylistIndex = parentPlaylist.second;
std::shared_ptr<Task> self = std::make_shared<Task>( ml, std::move( fileFs ),
std::move( parentFolder ), std::move( parentFolderFs ),
std::move( parentFolder ), std::move( parentFolderFs ), fileType,
std::move( parentPlaylist.first ), parentPlaylist.second );
const std::string req = "INSERT INTO " + Task::Table::Name +
"(mrl, parent_folder_id, parent_playlist_id, parent_playlist_index, is_refresh) "
"VALUES(?, ?, ?, ?, ?)";
if ( insert( ml, self, req, self->m_item.mrl(), parentFolderId,
"(mrl, file_type, parent_folder_id, parent_playlist_id, "
"parent_playlist_index, is_refresh) "
"VALUES(?, ?, ?, ?, ?, ?)";
if ( insert( ml, self, req, self->m_item.mrl(), fileType, parentFolderId,
sqlite::ForeignKey( parentPlaylistId ),
parentPlaylistIndex, false ) == false )
return nullptr;
......
......@@ -90,9 +90,11 @@ public:
*
* The position is used to keep subitems ordering for playlists
*/
Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemIndex, bool isRefresh );
Item( ITaskCb* taskCb, std::string mrl, IFile::Type fileType,
unsigned int subitemIndex, bool isRefresh );
Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> folder, std::shared_ptr<fs::IDirectory> folderFs,
IFile::Type fileType,
std::shared_ptr<Playlist> parentPlaylist, unsigned int parentPlaylistIndex,
bool isRefresh );
Item( ITaskCb* taskCb, std::shared_ptr<File> file, std::shared_ptr<fs::IFile> fileFs );
......@@ -104,6 +106,8 @@ public:
virtual const std::string& mrl() const override;
void setMrl( std::string mrl );
virtual IFile::Type fileType() const override;
virtual size_t nbSubItems() const override;
virtual const IItem& subItem( unsigned int index ) const override;
virtual IItem& createSubItem( std::string mrl, unsigned int playlistIndex ) override;
......@@ -136,6 +140,7 @@ public:
ITaskCb* m_taskCb;
std::string m_mrl;
IFile::Type m_fileType;
std::unordered_map<Metadata, std::string, MetadataHash> m_metadata;
std::vector<Item> m_subItems;
std::vector<Track> m_tracks;
......@@ -153,17 +158,29 @@ public:
static_assert( std::is_move_assignable<Item>::value, "Item must be move assignable" );
/*
* Constructs a task to be resumed.
* The Media is provided as a parameter to avoid this to implicitely query
* the database for the media associated to the provided file
*/
Task( MediaLibraryPtr ml, sqlite::Row& row );
/**
* @brief Task Construct a task for a newly detected file
* @param ml A pointer to the medialibrary instance
* @param fileFs The file, as seen on the filesystem
* @param parentFolder The parent folder, in DB
* @param parentFolderFs The parent folder, as seen on the filesystem
* @param fileType The newly detected file type
* @param parentPlaylist A parent playlist, if any
* @param parentPlaylistIndex The index in the playlist
*/
Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
std::shared_ptr<fs::IDirectory> parentFolderFs,
IFile::Type fileType,
std::shared_ptr<Playlist> parentPlaylist,
unsigned int parentPlaylistIndex );
/**
* @brief Task Constructor for refresh tasks
* @param ml A medialibrary instance pointer
* @param file The known file, to be refreshed
* @param fileFs The updated file, on the filesystem
*/
Task( MediaLibraryPtr ml, std::shared_ptr<File> file,
std::shared_ptr<fs::IFile> fileFs );
......@@ -206,6 +223,7 @@ public:
static std::shared_ptr<Task> create( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
std::shared_ptr<fs::IDirectory> parentFolderFs,
IFile::Type fileType,
std::pair<std::shared_ptr<Playlist>,
unsigned int> parentPlaylist );
static std::shared_ptr<Task> createRefreshTask( MediaLibraryPtr ml, std::shared_ptr<File> file,
......
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