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

MediaLibrary: Move device refresh to FsHolder class

refs #362
parent 83350480
......@@ -1111,7 +1111,7 @@ void MediaLibrary::startDiscovererLocked()
{
if ( m_discovererWorker != nullptr )
return;
auto discoverer = std::make_unique<FsDiscoverer>( this, m_callback );
auto discoverer = std::make_unique<FsDiscoverer>( this, m_fsHolder, m_callback );
m_discovererWorker.reset( new DiscovererWorker( this, &m_fsHolder,
std::move( discoverer ) ) );
}
......@@ -2589,40 +2589,12 @@ void MediaLibrary::setLogger( ILogger* logger )
Log::SetLogger( logger );
}
void MediaLibrary::refreshDevice( Device& device, fs::IFileSystemFactory* fsFactory )
{
auto deviceFs = fsFactory != nullptr ?
fsFactory->createDevice( device.uuid() ) : nullptr;
auto fsDevicePresent = deviceFs != nullptr && deviceFs->isPresent();
if ( device.isPresent() != fsDevicePresent )
{
LOG_INFO( "Device ", device.uuid(), " changed presence state: ",
device.isPresent(), " -> ", fsDevicePresent );
device.setPresent( fsDevicePresent );
}
else
LOG_INFO( "Device ", device.uuid(), " presence is unchanged" );
if ( device.isRemovable() == true && device.isPresent() == true )
device.updateLastSeen();
}
DiscovererWorker* MediaLibrary::getDiscovererWorker()
{
std::lock_guard<compat::Mutex> lock{ m_mutex };
return m_discovererWorker.get();
}
void MediaLibrary::refreshDevices( fs::IFileSystemFactory& fsFactory )
{
auto devices = Device::fetchByScheme( this, fsFactory.scheme() );
for ( auto& d : devices )
{
refreshDevice( *d, &fsFactory );
}
LOG_DEBUG( "Done refreshing devices in database." );
}
void MediaLibrary::startFsFactory( fs::IFileSystemFactory &fsFactory ) const
{
m_fsHolder.startFsFactory( fsFactory );
......
......@@ -229,15 +229,6 @@ public:
std::shared_ptr<fs::IFileSystemFactory> fsFactoryForMrl( const std::string& mrl ) const;
/**
* @brief refreshDevices Refreshes the devices from a specific FS factory
* @param fsFactory The file system factory for which devices must be refreshed
*
* This is expected to be used when a specific factory signals that a device
* was plugged/unplugged.
*/
void refreshDevices(fs::IFileSystemFactory& fsFactory);
void startFsFactory( fs::IFileSystemFactory& fsFactory ) const;
virtual bool forceRescan() override;
......@@ -341,7 +332,6 @@ private:
/* Temporary public accessors during refactoring */
public:
void refreshDevice( Device& device, fs::IFileSystemFactory* fsFactory );
DiscovererWorker* getDiscovererWorker();
protected:
......
......@@ -46,11 +46,12 @@
namespace medialibrary
{
FsDiscoverer::FsDiscoverer( MediaLibrary* ml, IMediaLibraryCb* cb )
FsDiscoverer::FsDiscoverer( MediaLibrary* ml, FsHolder& fsHolder, IMediaLibraryCb* cb )
: m_ml( ml )
, m_cb( cb )
, m_isInterrupted( false )
, m_paused( false )
, m_fsHolder( fsHolder )
{
}
......@@ -117,7 +118,7 @@ void FsDiscoverer::checkRemovedDevices( fs::IDirectory& fsFolder,
". Refreshing device cache..." );
fsFactory.refreshDevices();
m_ml->refreshDevices( fsFactory );
m_fsHolder.refreshDevices( fsFactory );
// If the device was missing, refresh our list of devices in case
// the device was plugged back and/or we missed a notification for it
if ( device == nullptr )
......
......@@ -35,11 +35,12 @@ namespace medialibrary
class MediaLibrary;
class IMediaLibraryCb;
class Folder;
class FsHolder;
class FsDiscoverer
{
public:
FsDiscoverer(MediaLibrary* ml , IMediaLibraryCb* cb);
FsDiscoverer( MediaLibrary* ml, FsHolder& fsHolder, IMediaLibraryCb* cb );
bool reload();
bool reload( const std::string& entryPoint );
bool addEntryPoint(const std::string& entryPoint );
......@@ -74,6 +75,7 @@ private:
mutable compat::Mutex m_mutex;
mutable compat::ConditionVariable m_cond;
bool m_paused;
FsHolder& m_fsHolder;
};
}
......@@ -146,6 +146,16 @@ bool FsHolder::isNetworkEnabled() const
return m_networkDiscoveryEnabled.load( std::memory_order_acquire );
}
void FsHolder::refreshDevices( fs::IFileSystemFactory& fsFactory )
{
auto devices = Device::fetchByScheme( m_ml, fsFactory.scheme() );
for ( auto& d : devices )
{
refreshDevice( *d, &fsFactory );
}
LOG_DEBUG( "Done refreshing devices in database." );
}
void FsHolder::startFsFactoriesAndRefresh()
{
std::lock_guard<compat::Mutex> lock( m_mutex );
......@@ -167,7 +177,7 @@ void FsHolder::startFsFactoriesAndRefresh()
for ( const auto& d : devices )
{
auto fsFactory = fsFactoryForMrlLocked( d->scheme() );
m_ml->refreshDevice( *d, fsFactory.get() );
refreshDevice( *d, fsFactory.get() );
}
}
......@@ -207,6 +217,24 @@ std::shared_ptr<fs::IFileSystemFactory> FsHolder::fsFactoryForMrlLocked(const st
return nullptr;
}
void FsHolder::refreshDevice( Device& device, fs::IFileSystemFactory* fsFactory )
{
auto deviceFs = fsFactory != nullptr ?
fsFactory->createDevice( device.uuid() ) : nullptr;
auto fsDevicePresent = deviceFs != nullptr && deviceFs->isPresent();
if ( device.isPresent() != fsDevicePresent )
{
LOG_INFO( "Device ", device.uuid(), " changed presence state: ",
device.isPresent(), " -> ", fsDevicePresent );
device.setPresent( fsDevicePresent );
}
else
LOG_INFO( "Device ", device.uuid(), " presence is unchanged" );
if ( device.isRemovable() == true && device.isPresent() == true )
device.updateLastSeen();
}
void FsHolder::startFsFactory( fs::IFileSystemFactory& fsFactory ) const
{
fsFactory.start( &m_fsFactoryCb );
......
......@@ -36,6 +36,8 @@
namespace medialibrary
{
class Device;
class FsHolder
{
public:
......@@ -47,6 +49,15 @@ public:
bool setNetworkEnabled( bool enabled );
bool isNetworkEnabled() const;
/**
* @brief refreshDevices Refreshes the devices from a specific FS factory
* @param fsFactory The file system factory for which devices must be refreshed
*
* This is expected to be used when a specific factory signals that a device
* was plugged/unplugged.
*/
void refreshDevices( fs::IFileSystemFactory& fsFactory );
/**
* @brief startFsFactoriesAndRefresh Starts fs factories & refreshes all known devices
*
......@@ -83,6 +94,8 @@ private:
std::shared_ptr<fs::IFileSystemFactory>
fsFactoryForMrlLocked( const std::string& mrl ) const;
void refreshDevice( Device& device, fs::IFileSystemFactory* fsFactory );
private:
MediaLibrary* m_ml;
mutable compat::Mutex m_mutex;
......
Supports Markdown
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