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

Add an fs::IFileSystemFactoryCb class

For external IFileSystemFactory instances to signal devices being
plugged/unplugged, but without the IDeviceListerCb overhead of
refreshing the internals FS factories
parent cbc0b49d
Pipeline #2645 passed with stages
in 12 minutes and 8 seconds
......@@ -34,6 +34,31 @@ namespace fs
class IFile;
class IDevice;
/**
* @brief IFileSystemFactoryCb is for external file system factories to signal device changes
*/
class IFileSystemFactoryCb
{
public:
virtual ~IFileSystemFactoryCb() = default;
/**
* @brief onDevicePlugged Shall be invoked when a device gets plugged
* @param uuid The device UUID
*
* When this callback returns, the medialibrary will assume that the
* corresponding fs::IDevice::isPresent method will return true.
*/
virtual void onDevicePlugged( const std::string& uuid ) = 0;
/**
* @brief onDeviceUnplugged Shall be invoked when a device gets unplugged
* @param uuid The device UUID
*
* When this callback returns, the medialibrary will assume that the
* corresponding fs::IDevice::isPresent method will return false.
*/
virtual void onDeviceUnplugged( const std::string& uuid ) = 0;
};
class IFileSystemFactory
{
public:
......@@ -79,8 +104,11 @@ namespace fs
///
/// \brief start Starts any potential background operation that would be
/// required by this factory.
/// \param cb An instance of IFileSystemFactoryCb to be used to signal device changes
/// This instance is owned by the medialibrary, and must not
/// be released
///
virtual void start() = 0;
virtual void start( IFileSystemFactoryCb* cb ) = 0;
///
/// \brief stop stops any potential background operation that would be
/// required by this factory.
......
......@@ -114,6 +114,7 @@ const size_t MediaLibrary::NbSupportedExtensions = sizeof(supportedExtensions) /
MediaLibrary::MediaLibrary()
: m_callback( nullptr )
, m_fsFactoryCb( this )
, m_deviceListerCbImpl( this )
, m_verbosity( LogLevel::Error )
, m_settings( this )
......@@ -824,8 +825,8 @@ void MediaLibrary::startThumbnailer()
void MediaLibrary::populateNetworkFsFactories()
{
#ifdef HAVE_LIBVLC
m_externalNetworkFsFactories.emplace_back( std::make_shared<factory::NetworkFileSystemFactory>(
&m_deviceListerCbImpl, "smb://", "dsm-sd" ) );
m_externalNetworkFsFactories.emplace_back(
std::make_shared<factory::NetworkFileSystemFactory>( "smb://", "dsm-sd" ) );
#endif
}
......@@ -1378,7 +1379,7 @@ bool MediaLibrary::setDiscoverNetworkEnabled( bool enabled )
auto previousSize = m_fsFactories.size();
for ( auto fsFactory : m_externalNetworkFsFactories )
{
fsFactory->start();
fsFactory->start( &m_fsFactoryCb );
m_fsFactories.push_back( std::move( fsFactory ) );
}
std::copy( begin( m_externalNetworkFsFactories ), end( m_externalNetworkFsFactories ),
......@@ -1606,4 +1607,28 @@ MediaLibrary::DeviceListerCb::DeviceListerCb( MediaLibrary* ml )
{
}
MediaLibrary::FsFactoryCb::FsFactoryCb(MediaLibrary* ml)
: m_ml( ml )
{
}
void MediaLibrary::FsFactoryCb::onDevicePlugged( const std::string& uuid )
{
onDeviceChanged( uuid, true );
}
void MediaLibrary::FsFactoryCb::onDeviceUnplugged( const std::string& uuid )
{
onDeviceChanged( uuid, false );
}
void MediaLibrary::FsFactoryCb::onDeviceChanged( const std::string& uuid, bool isPresent )
{
auto device = Device::fromUuid( m_ml, uuid );
assert( device->isRemovable() == true );
if ( device->isPresent() == isPresent )
return;
device->setPresent( isPresent );
}
}
......@@ -27,6 +27,7 @@
#include "Settings.h"
#include "medialibrary/IDeviceLister.h"
#include "medialibrary/filesystem/IFileSystemFactory.h"
#include "medialibrary/IMedia.h"
#include <atomic>
......@@ -54,7 +55,6 @@ namespace fs
{
class IFile;
class IDirectory;
class IFileSystemFactory;
}
namespace parser
......@@ -237,12 +237,25 @@ class MediaLibrary : public IMediaLibrary
MediaLibrary* m_ml;
};
class FsFactoryCb : public fs::IFileSystemFactoryCb
{
public:
FsFactoryCb( MediaLibrary* ml );
private:
virtual void onDevicePlugged( const std::string& uuid ) override;
virtual void onDeviceUnplugged( const std::string& uuid ) override;
void onDeviceChanged( const std::string& uuid, bool isPresent );
private:
MediaLibrary* m_ml;
};
protected:
std::shared_ptr<sqlite::Connection> m_dbConnection;
std::vector<std::shared_ptr<fs::IFileSystemFactory>> m_fsFactories;
std::vector<std::shared_ptr<fs::IFileSystemFactory>> m_externalNetworkFsFactories;
std::string m_thumbnailPath;
IMediaLibraryCb* m_callback;
FsFactoryCb m_fsFactoryCb;
// Private IDeviceListerCb implementation
DeviceListerCb m_deviceListerCbImpl;
// External device lister
......
......@@ -148,7 +148,7 @@ const std::string&FileSystemFactory::scheme() const
return s;
}
void FileSystemFactory::start()
void FileSystemFactory::start( fs::IFileSystemFactoryCb* )
{
}
......
......@@ -48,7 +48,7 @@ namespace factory
virtual bool isMrlSupported( const std::string& path ) const override;
virtual bool isNetworkFileSystem() const override;
virtual const std::string& scheme() const override;
virtual void start() override;
virtual void start( fs::IFileSystemFactoryCb* cb ) override;
virtual void stop() override;
private:
......
......@@ -43,13 +43,12 @@ namespace medialibrary
namespace factory
{
NetworkFileSystemFactory::NetworkFileSystemFactory( IDeviceListerCb* cb,
const std::string& protocol,
NetworkFileSystemFactory::NetworkFileSystemFactory(const std::string& protocol,
const std::string& name )
: m_protocol( protocol )
, m_discoverer( VLCInstance::get(), name )
, m_mediaList( m_discoverer.mediaList() )
, m_cb( cb )
, m_cb( nullptr )
{
auto& em = m_mediaList->eventManager();
em.onItemAdded( [this]( VLC::MediaPtr m, int ) { onDeviceAdded( std::move( m ) ); } );
......@@ -113,8 +112,9 @@ const std::string& NetworkFileSystemFactory::scheme() const
return m_protocol;
}
void NetworkFileSystemFactory::start()
void NetworkFileSystemFactory::start( fs::IFileSystemFactoryCb* cb )
{
m_cb = cb;
m_discoverer.start();
}
......@@ -144,7 +144,7 @@ void NetworkFileSystemFactory::onDeviceAdded( VLC::MediaPtr media )
}
m_deviceCond.notify_one();
LOG_INFO( "Adding new network device: name: ", name, " - mrl: ", mrl );
m_cb->onDevicePlugged( name, mrl );
m_cb->onDevicePlugged( name );
}
void NetworkFileSystemFactory::onDeviceRemoved( VLC::MediaPtr media )
......
......@@ -42,8 +42,7 @@ public:
* @brief NetworkFileSystemFactory Constructs a network protocol specific filesystem factory
* @param protocol The protocol name
*/
NetworkFileSystemFactory( IDeviceListerCb* cb,
const std::string& protocol, const std::string& name );
NetworkFileSystemFactory( const std::string& protocol, const std::string& name );
virtual std::shared_ptr<fs::IDirectory> createDirectory( const std::string& path ) override;
virtual std::shared_ptr<fs::IDevice> createDevice( const std::string& uuid ) override;
virtual std::shared_ptr<fs::IDevice> createDeviceFromMrl( const std::string& path ) override;
......@@ -51,7 +50,7 @@ public:
virtual bool isMrlSupported( const std::string& path ) const override;
virtual bool isNetworkFileSystem() const override;
virtual const std::string& scheme() const override;
virtual void start() override;
virtual void start( fs::IFileSystemFactoryCb* cb ) override;
virtual void stop() override;
private:
......@@ -79,7 +78,7 @@ private:
std::vector<Device> m_devices;
VLC::MediaDiscoverer m_discoverer;
std::shared_ptr<VLC::MediaList> m_mediaList;
IDeviceListerCb* m_cb;
fs::IFileSystemFactoryCb* m_cb;
};
}
......
......@@ -219,7 +219,7 @@ struct FileSystemFactory : public fs::IFileSystemFactory
return s;
}
virtual void start() override {}
virtual void start( fs::IFileSystemFactoryCb* ) override {}
virtual void stop() override {}
std::vector<std::shared_ptr<Device>> devices;
......@@ -393,7 +393,7 @@ public:
{
return false;
}
virtual void start() override {}
virtual void start( fs::IFileSystemFactoryCb* ) override {}
virtual void stop() override {}
};
......
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