From 61b17490ebc78bed20e59520187b1a70a162c38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Fri, 11 Jul 2014 15:58:12 +0200 Subject: [PATCH] Refactor: group jobs and partitionModel for a device in a DeviceInfo struct Makes it easier to clear all jobs for a specific device, which is necessary when replacing a device partition table --- src/modules/partition/PartitionCoreModule.cpp | 78 +++++++++++++++---- src/modules/partition/PartitionCoreModule.h | 20 +++-- 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/modules/partition/PartitionCoreModule.cpp b/src/modules/partition/PartitionCoreModule.cpp index 9df844297..3cbdf3f50 100644 --- a/src/modules/partition/PartitionCoreModule.cpp +++ b/src/modules/partition/PartitionCoreModule.cpp @@ -34,6 +34,13 @@ #include +//- DeviceInfo --------------------------------------------- +PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device ) + : device( _device ) + , partitionModel( new PartitionModel ) +{} + +//- PartitionCoreModule ------------------------------------ PartitionCoreModule::PartitionCoreModule( QObject* parent ) : QObject( parent ) , m_deviceModel( new DeviceModel( this ) ) @@ -45,21 +52,22 @@ PartitionCoreModule::PartitionCoreModule( QObject* parent ) } CoreBackend* backend = CoreBackendManager::self()->backend(); - m_devices = backend->scanDevices(); - for ( auto device : m_devices ) + auto devices = backend->scanDevices(); + for ( auto device : devices ) { - PartitionModel* model = new PartitionModel; - model->init( device, &m_infoForPartitionHash ); - m_partitionModelForDeviceHash[ device ] = model; + auto deviceInfo = new DeviceInfo( device ); + m_deviceInfos << deviceInfo; + + deviceInfo->partitionModel->init( device, &m_infoForPartitionHash ); } - m_deviceModel->init( m_devices ); + m_deviceModel->init( devices ); } PartitionCoreModule::~PartitionCoreModule() { qDeleteAll( m_infoForPartitionHash ); - qDeleteAll( m_devices ); + qDeleteAll( m_deviceInfos ); } DeviceModel* @@ -71,7 +79,9 @@ PartitionCoreModule::deviceModel() const PartitionModel* PartitionCoreModule::partitionModelForDevice( Device* device ) const { - return m_partitionModelForDeviceHash[ device ]; + DeviceInfo* info = infoForDevice( device ); + Q_ASSERT( info ); + return info->partitionModel.data(); } void @@ -83,13 +93,15 @@ PartitionCoreModule::createPartitionTable( Device* device ) // FIXME: Remove all jobs queued for this device, as well as all partition // info - m_jobs << Calamares::job_ptr( job ); + infoForDevice( device )->jobs << Calamares::job_ptr( job ); updateHasRootMountPoint(); } void PartitionCoreModule::createPartition( Device* device, PartitionInfo* partitionInfo ) { + auto deviceInfo = infoForDevice( device ); + Q_ASSERT( deviceInfo ); auto partition = partitionInfo->partition; Q_ASSERT( !m_infoForPartitionHash.contains( partition ) ); m_infoForPartitionHash[ partition ] = partitionInfo; @@ -99,7 +111,7 @@ PartitionCoreModule::createPartition( Device* device, PartitionInfo* partitionIn refreshPartitionModel( device ); - m_jobs << Calamares::job_ptr( job ); + deviceInfo->jobs << Calamares::job_ptr( job ); updateHasRootMountPoint(); } @@ -107,21 +119,25 @@ PartitionCoreModule::createPartition( Device* device, PartitionInfo* partitionIn void 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 ); } + QList< Calamares::job_ptr >& jobs = deviceInfo->jobs; + if ( partition->state() == Partition::StateNew ) { // Find matching CreatePartitionJob - auto it = std::find_if( m_jobs.begin(), m_jobs.end(), [ partition ]( Calamares::job_ptr job ) + auto it = std::find_if( jobs.begin(), jobs.end(), [ partition ]( Calamares::job_ptr job ) { CreatePartitionJob* createJob = qobject_cast< CreatePartitionJob* >( job.data() ); return createJob && createJob->partition() == partition; } ); - if ( it == m_jobs.end() ) + if ( it == jobs.end() ) { cDebug() << "Failed to find a CreatePartitionJob matching the partition to remove"; return; @@ -133,7 +149,7 @@ PartitionCoreModule::deletePartition( Device* device, Partition* partition ) return; } device->partitionTable()->updateUnallocated( *device ); - m_jobs.erase( it ); + jobs.erase( it ); // The partition is no longer referenced by either a job or the device // partition list, so we have to delete it delete partition; @@ -142,27 +158,42 @@ PartitionCoreModule::deletePartition( Device* device, Partition* partition ) { DeletePartitionJob* job = new DeletePartitionJob( device, partition ); job->updatePreview(); - m_jobs << Calamares::job_ptr( job ); + jobs << Calamares::job_ptr( job ); } refreshPartitionModel( device ); updateHasRootMountPoint(); } +QList< Calamares::job_ptr > +PartitionCoreModule::jobs() const +{ + QList< Calamares::job_ptr > lst; + for ( auto info : m_deviceInfos ) + { + lst << info->jobs; + } + return lst; +} + void PartitionCoreModule::dumpQueue() const { cDebug() << "Queue:"; - for ( auto job : m_jobs ) + for ( auto info : m_deviceInfos ) { - cDebug() << job->prettyName(); + cDebug() << "Device:" << info->device->name(); + for ( auto job : info->jobs ) + { + cDebug() << job->prettyName(); + } } } void PartitionCoreModule::refreshPartitionModel( Device* device ) { - auto model = m_partitionModelForDeviceHash.value( device ); + auto model = partitionModelForDevice( device ); Q_ASSERT( model ); model->reload(); } @@ -185,3 +216,16 @@ void PartitionCoreModule::updateHasRootMountPoint() hasRootMountPointChanged( m_hasRootMountPoint ); } } + +PartitionCoreModule::DeviceInfo* +PartitionCoreModule::infoForDevice( Device* device ) const +{ + for ( auto deviceInfo : m_deviceInfos ) + { + if ( deviceInfo->device.data() == device ) + { + return deviceInfo; + } + } + return nullptr; +} diff --git a/src/modules/partition/PartitionCoreModule.h b/src/modules/partition/PartitionCoreModule.h index 42cd68735..dde240366 100644 --- a/src/modules/partition/PartitionCoreModule.h +++ b/src/modules/partition/PartitionCoreModule.h @@ -56,10 +56,7 @@ public: void deletePartition( Device* device, Partition* partition ); - QList< Calamares::job_ptr > jobs() const - { - return m_jobs; - } + QList< Calamares::job_ptr > jobs() const; bool hasRootMountPoint() const { @@ -70,20 +67,27 @@ Q_SIGNALS: void hasRootMountPointChanged( bool value ); private: - QList< Device* > m_devices; - QHash< Device*, PartitionModel* > m_partitionModelForDeviceHash; + struct DeviceInfo + { + DeviceInfo( Device* ); + QScopedPointer< Device > device; + QScopedPointer< PartitionModel > partitionModel; + QList< Calamares::job_ptr > jobs; + }; + QList< DeviceInfo* > m_deviceInfos; + DeviceModel* m_deviceModel; bool m_hasRootMountPoint = false; InfoForPartitionHash m_infoForPartitionHash; - QList< Calamares::job_ptr > m_jobs; - void listDevices(); void updateHasRootMountPoint(); void refreshPartitionModel( Device* device ); void dumpQueue() const; + + DeviceInfo* infoForDevice( Device* ) const; }; #endif /* PARTITIONCOREMODULE_H */