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

Add an unbanFolder method

parent 1a264240
......@@ -115,6 +115,7 @@ class IMediaLibrary
* @brief banFolder will blacklist a folder for discovery
*/
virtual bool banFolder( const std::string& path ) = 0;
virtual bool unbanFolder( const std::string& path ) = 0;
virtual const std::string& thumbnailPath() const = 0;
virtual void setLogger( ILogger* logger ) = 0;
/**
......
......@@ -142,6 +142,16 @@ void Folder::setFileSystemFactory( std::shared_ptr<factory::IFileSystem> fsFacto
}
std::shared_ptr<Folder> Folder::fromPath( DBConnection conn, const std::string& fullPath )
{
return fromPath( conn, fullPath, false );
}
std::shared_ptr<Folder> Folder::blacklistedFolder( DBConnection conn, const std::string& fullPath )
{
return fromPath( conn, fullPath, true );
}
std::shared_ptr<Folder> Folder::fromPath( DBConnection conn, const std::string& fullPath, bool blacklisted )
{
auto folderFs = FsFactory->createDirectory( fullPath );
if ( folderFs == nullptr )
......@@ -154,12 +164,18 @@ std::shared_ptr<Folder> Folder::fromPath( DBConnection conn, const std::string&
}
if ( deviceFs->isRemovable() == false )
{
const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE path = ? AND is_removable = 0"
" AND is_blacklisted IS NULL";
std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE path = ? AND is_removable = 0";
if ( blacklisted == false )
req += " AND is_blacklisted IS NULL";
else
req += " AND is_blacklisted = 1";
return fetch( conn, req, fullPath );
}
const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE path = ? AND device_id = ? "
"AND is_blacklisted IS NULL";
std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE path = ? AND device_id = ?";
if ( blacklisted == false )
req += " AND is_blacklisted IS NULL";
else
req += " AND is_blacklisted = 1";
auto device = Device::fromUuid( conn, deviceFs->uuid() );
// We are trying to find a folder. If we don't know the device it's on, we don't know the folder.
......
......@@ -69,6 +69,7 @@ public:
static void setFileSystemFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
static std::shared_ptr<Folder> fromPath( DBConnection conn, const std::string& fullPath );
static std::shared_ptr<Folder> blacklistedFolder( DBConnection conn, const std::string& fullPath );
unsigned int id() const;
const std::string& path() const;
......@@ -80,6 +81,7 @@ public:
private:
static std::shared_ptr<factory::IFileSystem> FsFactory;
static std::shared_ptr<Folder> fromPath( DBConnection conn, const std::string& fullPath, bool includeBlacklisted );
private:
DBConnection m_dbConection;
......
......@@ -467,6 +467,24 @@ bool MediaLibrary::banFolder( const std::string& path )
return Folder::blacklist( m_dbConnection.get(), path );
}
bool MediaLibrary::unbanFolder( const std::string& path )
{
auto folder = Folder::blacklistedFolder( m_dbConnection.get(), path );
if ( folder == nullptr )
return false;
deleteFolder( *folder );
// We are about to refresh the folder we blacklisted earlier, if we don't have a discoverer, stop early
if ( m_discoverer == nullptr )
return true;
auto parentPath = utils::file::parentDirectory( path );
// If the parent folder was never added to the media library, the discoverer will reject it.
// We could check it from here, but that would mean fetching the folder twice, which would be a waste.
m_discoverer->reload( parentPath );
return true;
}
const std::string& MediaLibrary::thumbnailPath() const
{
return m_thumbnailPath;
......
......@@ -84,6 +84,7 @@ class MediaLibrary : public IMediaLibrary
virtual void discover( const std::string& entryPoint ) override;
bool banFolder( const std::string& path ) override;
bool unbanFolder( const std::string& path ) override;
virtual const std::string& thumbnailPath() const override;
virtual void setLogger( ILogger* logger ) override;
......
......@@ -298,6 +298,31 @@ TEST_F( Folders, BlacklistAfterDiscovery )
ASSERT_EQ( nullptr, f2 );
}
TEST_F( FoldersNoDiscover, RemoveFromBlacklist )
{
cbMock->prepareForWait( 1 );
ml->banFolder( mock::FileSystemFactory::SubFolder );
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
auto files = ml->files();
ASSERT_EQ( 2u, files.size() );
auto f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( nullptr, f );
cbMock->prepareForReload();
auto res = ml->unbanFolder( mock::FileSystemFactory::SubFolder );
ASSERT_TRUE( res );
bool reloaded = cbMock->waitForReload();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_NE( nullptr, f );
}
TEST_F( FoldersNoDiscover, NoMediaBeforeDiscovery )
{
auto newFolder = mock::FileSystemFactory::Root + "newfolder/";
......
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