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

IDevice: Remove setPresent and deduce presence based on the available mountpoints

parent 1e216662
......@@ -49,8 +49,9 @@ public:
/**
* @brief onDeviceUnplugged Shall be invoked when a known device gets unplugged
* @param uuid The device UUID
* @param mountpoint The mountpoint the device was mounted on
*/
virtual void onDeviceUnplugged( const std::string& uuid ) = 0;
virtual void onDeviceUnplugged( const std::string& uuid, const std::string& mountpoint ) = 0;
/**
* @brief isDeviceKnown Returns true is the provided device is already known to the media library
*
......
......@@ -37,7 +37,6 @@ public:
virtual const std::string& uuid() const = 0;
virtual bool isRemovable() const = 0;
virtual bool isPresent() const = 0;
virtual void setPresent( bool present ) = 0;
/**
* @brief mountpoint Returns a mountpoint of this device.
*
......@@ -46,6 +45,7 @@ public:
virtual const std::string& mountpoint() const = 0;
virtual void addMountpoint( std::string mountpoint ) = 0;
virtual void removeMountpoint( const std::string& mountpoint ) = 0;
/**
* @brief matchesMountpoint checks if the provided mrl matches this device
* @param mrl The mrl to probe
......
......@@ -1564,11 +1564,14 @@ bool MediaLibrary::DeviceListerCb::onDevicePlugged( const std::string& uuid, con
auto deviceFs = fsFactory->createDevice( uuid );
if ( deviceFs != nullptr )
{
LOG_INFO( "Device ", uuid, " changed presence state: 0 -> 1" );
assert( deviceFs->isPresent() == false );
deviceFs->setPresent( true );
if ( currentDevice != nullptr )
currentDevice->setPresent( true );
auto previousPresence = deviceFs->isPresent();
deviceFs->addMountpoint( mountpoint );
if ( previousPresence == false )
{
LOG_INFO( "Device ", uuid, " changed presence state: 0 -> 1" );
if ( currentDevice != nullptr )
currentDevice->setPresent( true );
}
}
break;
}
......@@ -1576,7 +1579,8 @@ bool MediaLibrary::DeviceListerCb::onDevicePlugged( const std::string& uuid, con
return currentDevice == nullptr;
}
void MediaLibrary::DeviceListerCb::onDeviceUnplugged( const std::string& uuid )
void MediaLibrary::DeviceListerCb::onDeviceUnplugged( const std::string& uuid,
const std::string& mountpoint )
{
auto device = Device::fromUuid( m_ml, uuid );
assert( device->isRemovable() == true );
......@@ -1594,9 +1598,12 @@ void MediaLibrary::DeviceListerCb::onDeviceUnplugged( const std::string& uuid )
if ( deviceFs != nullptr )
{
assert( deviceFs->isPresent() == true );
LOG_INFO( "Device ", uuid, " changed presence state: 1 -> 0" );
deviceFs->setPresent( false );
device->setPresent( false );
deviceFs->removeMountpoint( mountpoint );
if ( deviceFs->isPresent() == false )
{
LOG_INFO( "Device ", uuid, " changed presence state: 1 -> 0" );
device->setPresent( false );
}
}
}
}
......
......@@ -231,7 +231,7 @@ class MediaLibrary : public IMediaLibrary
DeviceListerCb( MediaLibrary* ml );
private:
virtual bool onDevicePlugged( const std::string& uuid, const std::string& mountpoint ) override;
virtual void onDeviceUnplugged(const std::string& uuid) override;
virtual void onDeviceUnplugged(const std::string& uuid, const std::string& mountpoint) override;
virtual bool isDeviceKnown( const std::string& uuid ) const override;
private:
......
......@@ -116,16 +116,8 @@ void FileSystemFactory::refreshDevices()
[&devicePair]( decltype(devices)::value_type& deviceTuple ) {
return std::get<0>( deviceTuple ) == devicePair.first;
});
// If the device isn't present anymore, mark it as such, but don't remove it
// That way, the directories that cached the device still have an up to date information
if ( it == end( devices ) )
devicePair.second->setPresent( false );
else
{
// If we already know the device, ensure it's marked as present
devicePair.second->setPresent( true );
if ( it != end( devices ) )
devices.erase( it );
}
}
// And now insert all new devices, if any
for ( const auto& d : devices )
......
......@@ -27,6 +27,7 @@
#include "CommonDevice.h"
#include "utils/Filename.h"
#include <algorithm>
#include <cassert>
namespace medialibrary
......@@ -37,7 +38,6 @@ namespace fs
CommonDevice::CommonDevice( const std::string& uuid, const std::string& mountpoint, bool isRemovable )
: m_uuid( uuid )
, m_mountpoints( { utils::file::toFolderPath( mountpoint ) } )
, m_present( true )
, m_removable( isRemovable )
{
}
......@@ -54,12 +54,7 @@ bool CommonDevice::isRemovable() const
bool CommonDevice::isPresent() const
{
return m_present;
}
void CommonDevice::setPresent( bool present )
{
m_present = present;
return m_mountpoints.empty() == false;
}
const std::string& CommonDevice::mountpoint() const
......@@ -73,6 +68,13 @@ void CommonDevice::addMountpoint( std::string mountpoint )
m_mountpoints.push_back( std::move( mountpoint ) );
}
void CommonDevice::removeMountpoint( const std::string& mountpoint )
{
auto it = std::find( begin( m_mountpoints ), end( m_mountpoints ), mountpoint );
if ( it != end( m_mountpoints ) )
m_mountpoints.erase( it );
}
std::tuple<bool, std::string>
CommonDevice::matchesMountpoint( const std::string& mrl ) const
{
......
......@@ -37,9 +37,9 @@ public:
virtual const std::string& uuid() const override;
virtual bool isRemovable() const override;
virtual bool isPresent() const override;
virtual void setPresent( bool present ) override;
virtual const std::string& mountpoint() const override;
virtual void addMountpoint( std::string mountpoint ) override;
virtual void removeMountpoint( const std::string& mountpoint ) override;
virtual std::tuple<bool, std::string>
matchesMountpoint( const std::string& mrl ) const override;
virtual std::string relativeMrl( const std::string& absoluteMrl ) const override;
......@@ -48,7 +48,6 @@ public:
private:
std::string m_uuid;
std::vector<std::string> m_mountpoints;
bool m_present;
bool m_removable;
};
......
......@@ -316,10 +316,6 @@ public:
return true;
}
virtual void setPresent( bool ) override
{
}
virtual const std::string& mountpoint() const override
{
return m_mountpoint;
......@@ -330,6 +326,11 @@ public:
assert( false );
}
virtual void removeMountpoint( const std::string& ) override
{
assert( false );
}
virtual std::string relativeMrl( const std::string& mrl ) const override
{
return mrl;
......
......@@ -67,6 +67,11 @@ void Device::addMountpoint( std::string )
assert( false );
}
void Device::removeMountpoint( const std::string& )
{
assert( false );
}
std::tuple<bool, std::string> Device::matchesMountpoint( const std::string& mrl ) const
{
if ( mrl.find( m_mountpoint ) != 0 )
......
......@@ -52,10 +52,11 @@ public:
virtual bool isPresent() const override;
virtual const std::string& mountpoint() const override;
virtual void addMountpoint( std::string mountpoint ) override;
virtual void removeMountpoint( const std::string& mountpoint ) override;
virtual std::tuple<bool, std::string> matchesMountpoint( const std::string& mrl ) const override;
void setRemovable( bool value );
virtual void setPresent( bool value ) override;
void setPresent( bool value );
virtual std::string relativeMrl( const std::string& mrl ) const override;
virtual std::string absoluteMrl( const std::string& mrl ) const 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