Refactor: Introduce PartitionInfoProvider interface, and make DeviceInfo implement it
This commit is contained in:
parent
61b17490eb
commit
f0dffb7400
@ -40,6 +40,48 @@ PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device )
|
|||||||
, partitionModel( new PartitionModel )
|
, 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::PartitionCoreModule( QObject* parent )
|
PartitionCoreModule::PartitionCoreModule( QObject* parent )
|
||||||
: QObject( parent )
|
: QObject( parent )
|
||||||
@ -58,7 +100,7 @@ PartitionCoreModule::PartitionCoreModule( QObject* parent )
|
|||||||
auto deviceInfo = new DeviceInfo( device );
|
auto deviceInfo = new DeviceInfo( device );
|
||||||
m_deviceInfos << deviceInfo;
|
m_deviceInfos << deviceInfo;
|
||||||
|
|
||||||
deviceInfo->partitionModel->init( device, &m_infoForPartitionHash );
|
deviceInfo->partitionModel->init( device, deviceInfo );
|
||||||
}
|
}
|
||||||
m_deviceModel->init( devices );
|
m_deviceModel->init( devices );
|
||||||
|
|
||||||
@ -66,7 +108,6 @@ PartitionCoreModule::PartitionCoreModule( QObject* parent )
|
|||||||
|
|
||||||
PartitionCoreModule::~PartitionCoreModule()
|
PartitionCoreModule::~PartitionCoreModule()
|
||||||
{
|
{
|
||||||
qDeleteAll( m_infoForPartitionHash );
|
|
||||||
qDeleteAll( m_deviceInfos );
|
qDeleteAll( m_deviceInfos );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,10 +143,13 @@ PartitionCoreModule::createPartition( Device* device, PartitionInfo* partitionIn
|
|||||||
{
|
{
|
||||||
auto deviceInfo = infoForDevice( device );
|
auto deviceInfo = infoForDevice( device );
|
||||||
Q_ASSERT( deviceInfo );
|
Q_ASSERT( deviceInfo );
|
||||||
auto partition = partitionInfo->partition;
|
if ( !deviceInfo->addInfoForPartition( partitionInfo ) )
|
||||||
Q_ASSERT( !m_infoForPartitionHash.contains( partition ) );
|
{
|
||||||
m_infoForPartitionHash[ partition ] = partitionInfo;
|
cDebug() << "Adding partition failed, there is already a PartitionInfo instance for it";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto partition = partitionInfo->partition;
|
||||||
CreatePartitionJob* job = new CreatePartitionJob( device, partition );
|
CreatePartitionJob* job = new CreatePartitionJob( device, partition );
|
||||||
job->updatePreview();
|
job->updatePreview();
|
||||||
|
|
||||||
@ -121,11 +165,7 @@ PartitionCoreModule::deletePartition( Device* device, Partition* partition )
|
|||||||
{
|
{
|
||||||
auto deviceInfo = infoForDevice( device );
|
auto deviceInfo = infoForDevice( device );
|
||||||
Q_ASSERT( deviceInfo );
|
Q_ASSERT( deviceInfo );
|
||||||
auto it = m_infoForPartitionHash.find( partition );
|
deviceInfo->removeInfoForPartition( partition );
|
||||||
if ( it != m_infoForPartitionHash.end() )
|
|
||||||
{
|
|
||||||
m_infoForPartitionHash.erase( it );
|
|
||||||
}
|
|
||||||
|
|
||||||
QList< Calamares::job_ptr >& jobs = deviceInfo->jobs;
|
QList< Calamares::job_ptr >& jobs = deviceInfo->jobs;
|
||||||
|
|
||||||
@ -203,11 +243,12 @@ void PartitionCoreModule::updateHasRootMountPoint()
|
|||||||
bool oldValue = m_hasRootMountPoint;
|
bool oldValue = m_hasRootMountPoint;
|
||||||
|
|
||||||
m_hasRootMountPoint = false;
|
m_hasRootMountPoint = false;
|
||||||
for ( auto it : m_infoForPartitionHash )
|
for ( auto deviceInfo : m_deviceInfos )
|
||||||
{
|
{
|
||||||
if ( it->mountPoint == "/" )
|
if ( deviceInfo->hasRootMountPoint() )
|
||||||
{
|
{
|
||||||
m_hasRootMountPoint = true;
|
m_hasRootMountPoint = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#define PARTITIONCOREMODULE_H
|
#define PARTITIONCOREMODULE_H
|
||||||
|
|
||||||
#include <PartitionInfo.h>
|
#include <PartitionInfo.h>
|
||||||
|
#include <PartitionModel.h>
|
||||||
#include <Typedefs.h>
|
#include <Typedefs.h>
|
||||||
|
|
||||||
// CalaPM
|
// CalaPM
|
||||||
@ -34,7 +35,6 @@ class Device;
|
|||||||
class DeviceModel;
|
class DeviceModel;
|
||||||
class FileSystem;
|
class FileSystem;
|
||||||
class Partition;
|
class Partition;
|
||||||
class PartitionModel;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Owns the Qt models and the PM devices
|
* Owns the Qt models and the PM devices
|
||||||
@ -52,6 +52,9 @@ public:
|
|||||||
|
|
||||||
void createPartitionTable( Device* device );
|
void createPartitionTable( Device* device );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes ownership of partitionInfo
|
||||||
|
*/
|
||||||
void createPartition( Device* device, PartitionInfo* partitionInfo );
|
void createPartition( Device* device, PartitionInfo* partitionInfo );
|
||||||
|
|
||||||
void deletePartition( Device* device, Partition* partition );
|
void deletePartition( Device* device, Partition* partition );
|
||||||
@ -67,20 +70,36 @@ Q_SIGNALS:
|
|||||||
void hasRootMountPointChanged( bool value );
|
void hasRootMountPointChanged( bool value );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct DeviceInfo
|
/**
|
||||||
|
* Owns the Device, PartitionModel and all attached PartitionInfo instances.
|
||||||
|
* Implements the PartitionInfoProvider interface.
|
||||||
|
*/
|
||||||
|
struct DeviceInfo : public PartitionInfoProvider
|
||||||
{
|
{
|
||||||
DeviceInfo( Device* );
|
DeviceInfo( Device* );
|
||||||
|
~DeviceInfo();
|
||||||
QScopedPointer< Device > device;
|
QScopedPointer< Device > device;
|
||||||
QScopedPointer< PartitionModel > partitionModel;
|
QScopedPointer< PartitionModel > partitionModel;
|
||||||
QList< Calamares::job_ptr > jobs;
|
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;
|
QList< DeviceInfo* > m_deviceInfos;
|
||||||
|
|
||||||
DeviceModel* m_deviceModel;
|
DeviceModel* m_deviceModel;
|
||||||
bool m_hasRootMountPoint = false;
|
bool m_hasRootMountPoint = false;
|
||||||
|
|
||||||
InfoForPartitionHash m_infoForPartitionHash;
|
|
||||||
|
|
||||||
void listDevices();
|
void listDevices();
|
||||||
void updateHasRootMountPoint();
|
void updateHasRootMountPoint();
|
||||||
void refreshPartitionModel( Device* device );
|
void refreshPartitionModel( Device* device );
|
||||||
|
@ -35,6 +35,4 @@ struct PartitionInfo
|
|||||||
bool format = false;
|
bool format = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef QHash< Partition*, PartitionInfo* > InfoForPartitionHash;
|
|
||||||
|
|
||||||
#endif /* PARTITIONINFO_H */
|
#endif /* PARTITIONINFO_H */
|
||||||
|
@ -30,16 +30,19 @@
|
|||||||
// KF5
|
// KF5
|
||||||
#include <KFormat>
|
#include <KFormat>
|
||||||
|
|
||||||
|
PartitionInfoProvider::~PartitionInfoProvider()
|
||||||
|
{}
|
||||||
|
|
||||||
PartitionModel::PartitionModel( QObject* parent )
|
PartitionModel::PartitionModel( QObject* parent )
|
||||||
: QAbstractListModel( parent )
|
: QAbstractListModel( parent )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PartitionModel::init( Device* device, InfoForPartitionHash* infoForPartitionHash )
|
PartitionModel::init( Device* device, PartitionInfoProvider* infoProvider )
|
||||||
{
|
{
|
||||||
m_device = device;
|
m_device = device;
|
||||||
m_infoForPartitionHash = infoForPartitionHash;
|
m_infoProvider = infoProvider;
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +109,7 @@ PartitionModel::data( const QModelIndex& index, int role ) const
|
|||||||
}
|
}
|
||||||
if ( col == MountPointColumn )
|
if ( col == MountPointColumn )
|
||||||
{
|
{
|
||||||
PartitionInfo* info = m_infoForPartitionHash->value( partition );
|
PartitionInfo* info = m_infoProvider->infoForPartition( partition );
|
||||||
return info ? info->mountPoint : QString();
|
return info ? info->mountPoint : QString();
|
||||||
}
|
}
|
||||||
if ( col == SizeColumn )
|
if ( col == SizeColumn )
|
||||||
|
@ -27,6 +27,13 @@ class Device;
|
|||||||
class Partition;
|
class Partition;
|
||||||
class PartitionNode;
|
class PartitionNode;
|
||||||
|
|
||||||
|
class PartitionInfoProvider
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~PartitionInfoProvider();
|
||||||
|
virtual PartitionInfo* infoForPartition( Partition* partition ) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
class PartitionModel : public QAbstractListModel
|
class PartitionModel : public QAbstractListModel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -44,7 +51,7 @@ public:
|
|||||||
* device and infoForPartitions must remain alive for the life of
|
* device and infoForPartitions must remain alive for the life of
|
||||||
* PartitionModel
|
* PartitionModel
|
||||||
*/
|
*/
|
||||||
void init( Device* device, InfoForPartitionHash* infoForPartitionHash );
|
void init( Device* device, PartitionInfoProvider* infoProvider );
|
||||||
|
|
||||||
int columnCount( const QModelIndex& parent = QModelIndex() ) const override;
|
int columnCount( const QModelIndex& parent = QModelIndex() ) const override;
|
||||||
int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
|
int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
|
||||||
@ -64,7 +71,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Device* m_device;
|
Device* m_device;
|
||||||
InfoForPartitionHash* m_infoForPartitionHash;
|
PartitionInfoProvider* m_infoProvider;
|
||||||
QList< Partition* > m_partitionList;
|
QList< Partition* > m_partitionList;
|
||||||
|
|
||||||
void fillPartitionList( PartitionNode* parent );
|
void fillPartitionList( PartitionNode* parent );
|
||||||
|
Loading…
Reference in New Issue
Block a user