Implement partition removal

This commit is contained in:
Aurélien Gâteau 2014-07-02 15:49:35 +02:00
parent f9ca45face
commit d013e663b8
6 changed files with 93 additions and 4 deletions

View File

@ -10,6 +10,7 @@ calamares_add_plugin( partition
SOURCES
CreatePartitionDialog.cpp
CreatePartitionJob.cpp
DeletePartitionJob.cpp
DeviceModel.cpp
PartitionCoreModule.cpp
PartitionModel.cpp
@ -30,6 +31,7 @@ calamares_add_plugin( partition
set( partview_SRCS
CreatePartitionDialog.cpp
CreatePartitionJob.cpp
DeletePartitionJob.cpp
DeviceModel.cpp
PartitionCoreModule.cpp
PartitionModel.cpp

View File

@ -27,6 +27,7 @@ class FileSystem;
class CreatePartitionJob : public Calamares::Job
{
Q_OBJECT
public:
CreatePartitionJob( Device* device, Partition* partition );
QString prettyName() override;
@ -38,6 +39,11 @@ public:
return m_device;
}
Partition* partition() const
{
return m_partition;
}
private:
Device* m_device;
Partition* m_partition;

View File

@ -19,13 +19,17 @@
#include <PartitionCoreModule.h>
#include <CreatePartitionJob.h>
#include <DeletePartitionJob.h>
#include <DeviceModel.h>
#include <JobQueue.h>
#include <PartitionModel.h>
#include <Typedefs.h>
#include <utils/Logger.h>
// CalaPM
#include <CalaPM.h>
#include <core/device.h>
#include <core/partition.h>
#include <backend/corebackend.h>
#include <backend/corebackendmanager.h>
@ -95,7 +99,9 @@ PartitionCoreModule::createPartition( CreatePartitionJob* job )
Q_ASSERT( info );
job->updatePreview();
info->partitionModel->reload();
Calamares::JobQueue::instance()->enqueue( Calamares::job_ptr( job ) );
m_jobs << Calamares::job_ptr( job );
dumpQueue();
}
PartitionCoreModule::DeviceInfo*
@ -110,3 +116,55 @@ PartitionCoreModule::deviceInfoForDevice( Device* device ) const
}
return nullptr;
}
void
PartitionCoreModule::deletePartition( Device* device, Partition* partition )
{
if ( partition->state() == Partition::StateNew )
{
// Find matching CreatePartitionJob
auto it = std::find_if( m_jobs.begin(), m_jobs.end(), [ partition ]( Calamares::job_ptr job )
{
CreatePartitionJob* createJob = qobject_cast< CreatePartitionJob* >( job.data() );
return createJob && createJob->partition() == partition;
} );
if ( it == m_jobs.end() )
{
cDebug() << "Failed to find a CreatePartitionJob matching the partition to remove";
return;
}
// Remove it
if ( ! partition->parent()->remove( partition ) )
{
cDebug() << "Failed to remove partition from preview";
return;
}
device->partitionTable()->updateUnallocated( *device );
m_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;
}
else
{
DeletePartitionJob* job = new DeletePartitionJob( device, partition );
job->updatePreview();
Calamares::JobQueue::instance()->enqueue( Calamares::job_ptr( job ) );
m_jobs << Calamares::job_ptr( job );
}
DeviceInfo* info = deviceInfoForDevice( device );
info->partitionModel->reload();
dumpQueue();
}
void
PartitionCoreModule::dumpQueue() const
{
cDebug() << "Queue:";
for ( auto job : m_jobs )
{
cDebug() << job->prettyName();
}
}

View File

@ -19,12 +19,15 @@
#ifndef PARTITIONCOREMODULE_H
#define PARTITIONCOREMODULE_H
#include <QList>
#include <QObject>
#include <Typedefs.h>
// CalaPM
#include <core/partitiontable.h>
// Qt
#include <QList>
#include <QObject>
class CreatePartitionJob;
class Device;
class DeviceModel;
@ -47,6 +50,8 @@ public:
void createPartition( CreatePartitionJob* job );
void deletePartition( Device* device, Partition* partition );
private:
struct DeviceInfo
{
@ -58,9 +63,13 @@ private:
QList< DeviceInfo* > m_devices;
DeviceModel* m_deviceModel;
QList< Calamares::job_ptr > m_jobs;
void listDevices();
DeviceInfo* deviceInfoForDevice( Device* device ) const;
void dumpQueue() const;
};
#endif /* PARTITIONCOREMODULE_H */

View File

@ -58,6 +58,7 @@ PartitionPage::PartitionPage( QWidget* parent )
} );
connect( m_ui->createButton, &QAbstractButton::clicked, this, &PartitionPage::onCreateClicked );
connect( m_ui->deleteButton, &QAbstractButton::clicked, this, &PartitionPage::onDeleteClicked );
}
PartitionPage::~PartitionPage()
@ -92,7 +93,6 @@ PartitionPage::onCreateClicked()
Q_ASSERT( index.isValid() );
const PartitionModel* model = static_cast< const PartitionModel* >( index.model() );
Q_ASSERT( model );
Partition* partition = model->partitionForIndex( index );
Q_ASSERT( partition );
@ -103,3 +103,16 @@ PartitionPage::onCreateClicked()
}
delete dlg;
}
void
PartitionPage::onDeleteClicked()
{
QModelIndex index = m_ui->partitionListView->currentIndex();
Q_ASSERT( index.isValid() );
const PartitionModel* model = static_cast< const PartitionModel* >( index.model() );
Partition* partition = model->partitionForIndex( index );
Q_ASSERT( partition );
m_core->deletePartition( model->device(), partition );
}

View File

@ -44,6 +44,7 @@ private:
PartitionCoreModule* m_core;
void updateButtons();
void onCreateClicked();
void onDeleteClicked();
};
#endif // PARTITIONPAGE_H