Refactor: Introduce PartitionInfoProvider interface, and make DeviceInfo implement it

main
Aurélien Gâteau 11 years ago
parent 61b17490eb
commit f0dffb7400

@ -40,6 +40,48 @@ PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device )
, partitionModel( new PartitionModel )
{}
PartitionCoreModule::DeviceInfo::~DeviceInfo()
{
qDeleteAll( m_partitionInfoHash );
}
PartitionInfo*
PartitionCoreModule::DeviceInfo::infoForPartition( Partition* partition ) const
{
return m_partitionInfoHash.value( partition );
}
bool
PartitionCoreModule::DeviceInfo::addInfoForPartition( PartitionInfo* partitionInfo )
{
Q_ASSERT( partitionInfo );
if ( infoForPartition( partitionInfo->partition ) )
{
return false;
}
m_partitionInfoHash.insert( partitionInfo->partition, partitionInfo );
return true;
}
void
PartitionCoreModule::DeviceInfo::removeInfoForPartition( Partition* partition )
{
m_partitionInfoHash.remove( partition );
}
bool
PartitionCoreModule::DeviceInfo::hasRootMountPoint() const
{
for ( auto info : m_partitionInfoHash )
{
if ( info->mountPoint == "/" )
{
return true;
}
}
return false;
}
//- PartitionCoreModule ------------------------------------
PartitionCoreModule::PartitionCoreModule( QObject* parent )
: QObject( parent )
@ -58,7 +100,7 @@ PartitionCoreModule::PartitionCoreModule( QObject* parent )
auto deviceInfo = new DeviceInfo( device );
m_deviceInfos << deviceInfo;
deviceInfo->partitionModel->init( device, &m_infoForPartitionHash );
deviceInfo->partitionModel->init( device, deviceInfo );
}
m_deviceModel->init( devices );
@ -66,7 +108,6 @@ PartitionCoreModule::PartitionCoreModule( QObject* parent )
PartitionCoreModule::~PartitionCoreModule()
{
qDeleteAll( m_infoForPartitionHash );
qDeleteAll( m_deviceInfos );
}
@ -102,10 +143,13 @@ PartitionCoreModule::createPartition( Device* device, PartitionInfo* partitionIn
{
auto deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
auto partition = partitionInfo->partition;
Q_ASSERT( !m_infoForPartitionHash.contains( partition ) );
m_infoForPartitionHash[ partition ] = partitionInfo;
if ( !deviceInfo->addInfoForPartition( partitionInfo ) )
{
cDebug() << "Adding partition failed, there is already a PartitionInfo instance for it";
return;
}
auto partition = partitionInfo->partition;
CreatePartitionJob* job = new CreatePartitionJob( device, partition );
job->updatePreview();
@ -121,11 +165,7 @@ PartitionCoreModule::deletePartition( Device* device, Partition* partition )
{
auto deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
auto it = m_infoForPartitionHash.find( partition );
if ( it != m_infoForPartitionHash.end() )
{
m_infoForPartitionHash.erase( it );
}
deviceInfo->removeInfoForPartition( partition );
QList< Calamares::job_ptr >& jobs = deviceInfo->jobs;
@ -203,11 +243,12 @@ void PartitionCoreModule::updateHasRootMountPoint()
bool oldValue = m_hasRootMountPoint;
m_hasRootMountPoint = false;
for ( auto it : m_infoForPartitionHash )
for ( auto deviceInfo : m_deviceInfos )
{
if ( it->mountPoint == "/" )
if ( deviceInfo->hasRootMountPoint() )
{
m_hasRootMountPoint = true;
break;
}
}

@ -20,6 +20,7 @@
#define PARTITIONCOREMODULE_H
#include <PartitionInfo.h>
#include <PartitionModel.h>
#include <Typedefs.h>
// CalaPM
@ -34,7 +35,6 @@ class Device;
class DeviceModel;
class FileSystem;
class Partition;
class PartitionModel;
/**
* Owns the Qt models and the PM devices
@ -52,6 +52,9 @@ public:
void createPartitionTable( Device* device );
/**
* Takes ownership of partitionInfo
*/
void createPartition( Device* device, PartitionInfo* partitionInfo );
void deletePartition( Device* device, Partition* partition );
@ -67,20 +70,36 @@ Q_SIGNALS:
void hasRootMountPointChanged( bool value );
private:
struct DeviceInfo
/**
* Owns the Device, PartitionModel and all attached PartitionInfo instances.
* Implements the PartitionInfoProvider interface.
*/
struct DeviceInfo : public PartitionInfoProvider
{
DeviceInfo( Device* );
~DeviceInfo();
QScopedPointer< Device > device;
QScopedPointer< PartitionModel > partitionModel;
QList< Calamares::job_ptr > jobs;
PartitionInfo* infoForPartition( Partition* partition ) const override;
/**
* Returns false if there was already a PartitionInfo for this partition
*/
bool addInfoForPartition( PartitionInfo* partitionInfo );
void removeInfoForPartition( Partition* partition );
bool hasRootMountPoint() const;
private:
QHash< Partition*, PartitionInfo* > m_partitionInfoHash;
};
QList< DeviceInfo* > m_deviceInfos;
DeviceModel* m_deviceModel;
bool m_hasRootMountPoint = false;
InfoForPartitionHash m_infoForPartitionHash;
void listDevices();
void updateHasRootMountPoint();
void refreshPartitionModel( Device* device );

@ -35,6 +35,4 @@ struct PartitionInfo
bool format = false;
};
typedef QHash< Partition*, PartitionInfo* > InfoForPartitionHash;
#endif /* PARTITIONINFO_H */

@ -30,16 +30,19 @@
// KF5
#include <KFormat>
PartitionInfoProvider::~PartitionInfoProvider()
{}
PartitionModel::PartitionModel( QObject* parent )
: QAbstractListModel( parent )
{
}
void
PartitionModel::init( Device* device, InfoForPartitionHash* infoForPartitionHash )
PartitionModel::init( Device* device, PartitionInfoProvider* infoProvider )
{
m_device = device;
m_infoForPartitionHash = infoForPartitionHash;
m_infoProvider = infoProvider;
reload();
}
@ -106,7 +109,7 @@ PartitionModel::data( const QModelIndex& index, int role ) const
}
if ( col == MountPointColumn )
{
PartitionInfo* info = m_infoForPartitionHash->value( partition );
PartitionInfo* info = m_infoProvider->infoForPartition( partition );
return info ? info->mountPoint : QString();
}
if ( col == SizeColumn )

@ -27,6 +27,13 @@ class Device;
class Partition;
class PartitionNode;
class PartitionInfoProvider
{
public:
virtual ~PartitionInfoProvider();
virtual PartitionInfo* infoForPartition( Partition* partition ) const = 0;
};
class PartitionModel : public QAbstractListModel
{
public:
@ -44,7 +51,7 @@ public:
* device and infoForPartitions must remain alive for the life of
* PartitionModel
*/
void init( Device* device, InfoForPartitionHash* infoForPartitionHash );
void init( Device* device, PartitionInfoProvider* infoProvider );
int columnCount( const QModelIndex& parent = QModelIndex() ) const override;
int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
@ -64,7 +71,7 @@ public:
private:
Device* m_device;
InfoForPartitionHash* m_infoForPartitionHash;
PartitionInfoProvider* m_infoProvider;
QList< Partition* > m_partitionList;
void fillPartitionList( PartitionNode* parent );

Loading…
Cancel
Save