Commit ccac9c15 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

IMediaLibrary: Add a removeEntryPoint method

Fix #15
parent 6778d18d
......@@ -222,6 +222,7 @@ class IMediaLibrary
virtual void discover( const std::string& entryPoint ) = 0;
virtual void setDiscoverNetworkEnabled( bool enable ) = 0;
virtual std::vector<FolderPtr> entryPoints() const = 0;
virtual bool removeEntryPoint( const std::string& entryPoint ) = 0;
/**
* @brief banFolder will blacklist a folder for discovery
*/
......
......@@ -261,6 +261,11 @@ bool Folder::isPresent() const
return m_isPresent;
}
bool Folder::isRootFolder() const
{
return m_parent == 0;
}
std::vector<std::shared_ptr<Folder>> Folder::fetchRootFolders( MediaLibraryPtr ml )
{
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
......
......@@ -75,6 +75,7 @@ public:
std::shared_ptr<Folder> parent();
int64_t deviceId() const;
virtual bool isPresent() const override;
bool isRootFolder() const;
private:
enum class BannedType
......
......@@ -705,6 +705,30 @@ std::vector<FolderPtr> MediaLibrary::entryPoints() const
return Folder::fetchAll<IFolder>( this, req );
}
bool MediaLibrary::removeEntryPoint( const std::string& entryPoint )
{
auto folder = Folder::fromPath( this, entryPoint );
if ( folder == nullptr )
{
LOG_WARN( "Can't remove unknown entrypoint: ", entryPoint );
return false;
}
// The easy case is that this folder was directly discovered. In which case, we just
// have to delete it and it won't be discovered again.
// If it isn't, we also have to ban it to prevent it from reappearing. The Folder::banFolder
// method already handles the prior deletion
bool res;
if ( folder->isRootFolder() == false )
res = banFolder( entryPoint );
else
res = deleteFolder( *folder );
if ( res == false )
return false;
// Force a cache cleanup to avoid stalled media
Media::clear();
return true;
}
bool MediaLibrary::banFolder( const std::string& path )
{
return Folder::blacklist( this, path );
......
......@@ -116,6 +116,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual void discover( const std::string& entryPoint ) override;
virtual void setDiscoverNetworkEnabled( bool enabled ) override;
virtual std::vector<FolderPtr> entryPoints() const override;
virtual bool removeEntryPoint( const std::string& entryPoint ) override;
virtual bool banFolder( const std::string& path ) override;
virtual bool unbanFolder( const std::string& path ) override;
......
......@@ -433,3 +433,48 @@ TEST_F( Folders, FetchEntryPoints )
eps = ml->entryPoints();
ASSERT_EQ( 1u, eps.size() );
}
TEST_F( Folders, RemoveRootEntryPoint )
{
auto media = ml->files();
ASSERT_NE( 0u, media.size() );
auto res = ml->removeEntryPoint( mock::FileSystemFactory::Root );
ASSERT_TRUE( res );
media = ml->files();
ASSERT_EQ( 0u, media.size() );
auto eps = ml->entryPoints();
ASSERT_EQ( 0u, eps.size() );
}
TEST_F( Folders, RemoveEntryPoint )
{
auto media = ml->files();
ASSERT_NE( 0u, media.size() );
auto res = ml->removeEntryPoint( mock::FileSystemFactory::SubFolder );
ASSERT_TRUE( res );
media = ml->files();
ASSERT_NE( 0u, media.size() );
auto eps = ml->entryPoints();
ASSERT_EQ( 1u, eps.size() );
cbMock->prepareForReload();
ml->reload();
auto reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
// Ensure it wasn't re-discovered, ie. that it was properly blacklisted
auto media2 = ml->files();
ASSERT_EQ( media.size(), media2.size() );
}
TEST_F( Folders, RemoveNonExistantEntryPoint )
{
auto res = ml->removeEntryPoint( "/sea/otter" );
ASSERT_FALSE( res );
}
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