From f0dffb74002ae1f65ce36e16f3c66cd1af3e4856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Fri, 11 Jul 2014 16:55:21 +0200 Subject: [PATCH] Refactor: Introduce PartitionInfoProvider interface, and make DeviceInfo implement it --- src/modules/partition/PartitionCoreModule.cpp | 65 +++++++++++++++---- src/modules/partition/PartitionCoreModule.h | 27 ++++++-- src/modules/partition/PartitionInfo.h | 2 - src/modules/partition/PartitionModel.cpp | 9 ++- src/modules/partition/PartitionModel.h | 11 +++- 5 files changed, 91 insertions(+), 23 deletions(-) diff --git a/src/modules/partition/PartitionCoreModule.cpp b/src/modules/partition/PartitionCoreModule.cpp index 3cbdf3f50..e2863e804 100644 --- a/src/modules/partition/PartitionCoreModule.cpp +++ b/src/modules/partition/PartitionCoreModule.cpp @@ -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; } } diff --git a/src/modules/partition/PartitionCoreModule.h b/src/modules/partition/PartitionCoreModule.h index dde240366..8ee079310 100644 --- a/src/modules/partition/PartitionCoreModule.h +++ b/src/modules/partition/PartitionCoreModule.h @@ -20,6 +20,7 @@ #define PARTITIONCOREMODULE_H #include +#include #include // 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 ); diff --git a/src/modules/partition/PartitionInfo.h b/src/modules/partition/PartitionInfo.h index de33f2267..67c8fcd94 100644 --- a/src/modules/partition/PartitionInfo.h +++ b/src/modules/partition/PartitionInfo.h @@ -35,6 +35,4 @@ struct PartitionInfo bool format = false; }; -typedef QHash< Partition*, PartitionInfo* > InfoForPartitionHash; - #endif /* PARTITIONINFO_H */ diff --git a/src/modules/partition/PartitionModel.cpp b/src/modules/partition/PartitionModel.cpp index 7841c467b..4b15b9afa 100644 --- a/src/modules/partition/PartitionModel.cpp +++ b/src/modules/partition/PartitionModel.cpp @@ -30,16 +30,19 @@ // KF5 #include +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 ) diff --git a/src/modules/partition/PartitionModel.h b/src/modules/partition/PartitionModel.h index 50e9646db..36f4be037 100644 --- a/src/modules/partition/PartitionModel.h +++ b/src/modules/partition/PartitionModel.h @@ -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 );