From b060f664562c8dc423614433eda0de3395256191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Mon, 30 Jun 2014 18:08:13 +0200 Subject: [PATCH] Introduce CreatePartitionDialog and CreatePartitionJob --- src/modules/partition/CMakeLists.txt | 10 +- .../partition/CreatePartitionDialog.cpp | 68 +++++++++++ src/modules/partition/CreatePartitionDialog.h | 44 +++++++ .../partition/CreatePartitionDialog.ui | 110 ++++++++++++++++++ src/modules/partition/CreatePartitionJob.cpp | 60 ++++++++++ src/modules/partition/CreatePartitionJob.h | 47 ++++++++ src/modules/partition/PartitionCoreModule.cpp | 37 ++---- src/modules/partition/PartitionCoreModule.h | 5 +- src/modules/partition/PartitionModel.h | 5 + src/modules/partition/PartitionPage.cpp | 20 ++-- 10 files changed, 362 insertions(+), 44 deletions(-) create mode 100644 src/modules/partition/CreatePartitionDialog.cpp create mode 100644 src/modules/partition/CreatePartitionDialog.h create mode 100644 src/modules/partition/CreatePartitionDialog.ui create mode 100644 src/modules/partition/CreatePartitionJob.cpp create mode 100644 src/modules/partition/CreatePartitionJob.h diff --git a/src/modules/partition/CMakeLists.txt b/src/modules/partition/CMakeLists.txt index 7919804e1..bcace6982 100644 --- a/src/modules/partition/CMakeLists.txt +++ b/src/modules/partition/CMakeLists.txt @@ -8,12 +8,15 @@ calamares_add_plugin( partition EXPORT_MACRO PLUGINDLLEXPORT_PRO CONFIG_FILE module.conf SOURCES + CreatePartitionDialog.cpp + CreatePartitionJob.cpp DeviceModel.cpp PartitionModel.cpp PartitionPage.cpp PartitionViewStep.cpp PMUtils.cpp UI + CreatePartitionDialog.ui PartitionPage.ui LINK_LIBRARIES calapm @@ -24,6 +27,8 @@ calamares_add_plugin( partition # Temporary, until views are integrated set( partview_SRCS + CreatePartitionDialog.cpp + CreatePartitionJob.cpp DeviceModel.cpp PartitionCoreModule.cpp PartitionModel.cpp @@ -32,7 +37,10 @@ set( partview_SRCS ${calamares_SOURCE_DIR}/viewpages/AbstractPage.cpp main.cpp ) -qt5_wrap_ui( partview_SRCS PartitionPage.ui ) +qt5_wrap_ui( partview_SRCS + CreatePartitionDialog.ui + PartitionPage.ui +) include_directories( ${CMAKE_CURRENT_BINARY_DIR} ) diff --git a/src/modules/partition/CreatePartitionDialog.cpp b/src/modules/partition/CreatePartitionDialog.cpp new file mode 100644 index 000000000..9e9696e32 --- /dev/null +++ b/src/modules/partition/CreatePartitionDialog.cpp @@ -0,0 +1,68 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Aurélien Gâteau + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include + +#include +#include + +// CalaPM +#include +#include +#include +#include + +CreatePartitionDialog::CreatePartitionDialog( Device* device, Partition* freePartition, QWidget* parent ) + : QDialog( parent ) + , m_ui( new Ui_CreatePartitionDialog ) + , m_device( device ) + , m_freePartition( freePartition ) +{ + m_ui->setupUi( this ); + + FileSystemFactory::init(); + QStringList fsNames; + for ( auto fs : FileSystemFactory::map() ) + { + if ( fs->supportCreate() != FileSystem::cmdSupportNone && fs->type() != FileSystem::Extended ) + { + fsNames << fs->name(); + } + } + m_ui->fsComboBox->addItems( fsNames ); + + qint64 maxSize = ( freePartition->lastSector() - freePartition->firstSector() + 1 ) * device->logicalSectorSize(); + + m_ui->sizeSpinBox->setMaximum( maxSize / 1024 / 1024 ); + m_ui->sizeSpinBox->setValue( m_ui->sizeSpinBox->maximum() ); +} + +CreatePartitionDialog::~CreatePartitionDialog() +{} + +CreatePartitionJob* +CreatePartitionDialog::createJob() +{ + qint64 first = m_freePartition->firstSector(); + // FIXME: Check rounding errors here + qint64 last = first + qint64( m_ui->sizeSpinBox->value() ) * 1024 * 1024 / m_device->logicalSectorSize(); + + FileSystem::Type type = FileSystem::typeForName( m_ui->fsComboBox->currentText() ); + FileSystem* fs = FileSystemFactory::create( type, first, last ); + return new CreatePartitionJob( m_device, m_freePartition, fs ); +} diff --git a/src/modules/partition/CreatePartitionDialog.h b/src/modules/partition/CreatePartitionDialog.h new file mode 100644 index 000000000..f51167d6f --- /dev/null +++ b/src/modules/partition/CreatePartitionDialog.h @@ -0,0 +1,44 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Aurélien Gâteau + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef CREATEPARTITIONDIALOG_H +#define CREATEPARTITIONDIALOG_H + +#include +#include + +class CreatePartitionJob; +class Device; +class Partition; +class Ui_CreatePartitionDialog; + +class CreatePartitionDialog : public QDialog +{ +public: + CreatePartitionDialog( Device* device, Partition* freePartition, QWidget* parent = nullptr ); + ~CreatePartitionDialog(); + + CreatePartitionJob* createJob(); + +private: + QScopedPointer< Ui_CreatePartitionDialog > m_ui; + Device* m_device; + Partition* m_freePartition; +}; + +#endif /* CREATEPARTITIONDIALOG_H */ diff --git a/src/modules/partition/CreatePartitionDialog.ui b/src/modules/partition/CreatePartitionDialog.ui new file mode 100644 index 000000000..cd03e0743 --- /dev/null +++ b/src/modules/partition/CreatePartitionDialog.ui @@ -0,0 +1,110 @@ + + + CreatePartitionDialog + + + + 0 + 0 + 186 + 170 + + + + Create a Partition + + + + + + + + File System: + + + + + + + + + + Size: + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + MB + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + fsComboBox + sizeSpinBox + buttonBox + + + + + buttonBox + accepted() + CreatePartitionDialog + accept() + + + 181 + 165 + + + 157 + 274 + + + + + buttonBox + rejected() + CreatePartitionDialog + reject() + + + 181 + 165 + + + 286 + 274 + + + + + diff --git a/src/modules/partition/CreatePartitionJob.cpp b/src/modules/partition/CreatePartitionJob.cpp new file mode 100644 index 000000000..e329ab25b --- /dev/null +++ b/src/modules/partition/CreatePartitionJob.cpp @@ -0,0 +1,60 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Aurélien Gâteau + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include + +// CalaPM +#include +#include +#include +#include + +CreatePartitionJob::CreatePartitionJob( Device* device, Partition* freePartition, FileSystem* fs ) + : m_device( device ) + , m_freePartition( freePartition ) + , m_fs( fs ) +{ +} + +QString +CreatePartitionJob::prettyName() +{ + return tr( "Create partition" ); // FIXME +} + +void +CreatePartitionJob::exec() +{ +} + +void +CreatePartitionJob::createPreview() +{ + PartitionNode* parent = m_freePartition->parent(); + Partition* partition = new Partition( + parent, + *m_device, + PartitionRole( PartitionRole::Primary ), // FIXME: Support extended partitions + m_fs, m_fs->firstSector(), m_fs->lastSector(), + QString() /* path */ + ); + + m_device->partitionTable()->removeUnallocated(); + parent->insert( partition ); + m_device->partitionTable()->updateUnallocated( *m_device ); +} diff --git a/src/modules/partition/CreatePartitionJob.h b/src/modules/partition/CreatePartitionJob.h new file mode 100644 index 000000000..c49bf34e0 --- /dev/null +++ b/src/modules/partition/CreatePartitionJob.h @@ -0,0 +1,47 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Aurélien Gâteau + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef CREATEPARTITIONJOB_H +#define CREATEPARTITIONJOB_H + +#include + +class Device; +class Partition; +class FileSystem; + +class CreatePartitionJob : public Calamares::Job +{ +public: + CreatePartitionJob( Device* device, Partition* freePartition, FileSystem* fs ); + QString prettyName() override; + void exec() override; + + void createPreview(); + Device* device() const + { + return m_device; + } + +private: + Device* m_device; + Partition* m_freePartition; + FileSystem* m_fs; +}; + +#endif /* CREATEPARTITIONJOB_H */ diff --git a/src/modules/partition/PartitionCoreModule.cpp b/src/modules/partition/PartitionCoreModule.cpp index 189f5ac49..b4cac2d3b 100644 --- a/src/modules/partition/PartitionCoreModule.cpp +++ b/src/modules/partition/PartitionCoreModule.cpp @@ -18,16 +18,16 @@ #include +#include #include +#include #include +#include // CalaPM #include #include #include -#include -#include -#include //- DeviceInfo -------------------------------------------- @@ -89,40 +89,21 @@ PartitionCoreModule::partitionModelForDevice( Device* device ) const } void -PartitionCoreModule::createPartition( Partition* freeSpacePartition, FileSystem* fs, const QString& mountPoint, PartitionTable::Flags flags ) +PartitionCoreModule::createPartition( CreatePartitionJob* job ) { - DeviceInfo* info = deviceInfoForPath( freeSpacePartition->devicePath() ); + DeviceInfo* info = deviceInfoForDevice( job->device() ); Q_ASSERT( info ); - - PartitionNode* parent = freeSpacePartition->parent(); - - // Create a Partition object - Partition* partition = new Partition( - parent, - *info->device, - PartitionRole( PartitionRole::Primary ), // FIXME: Support extended partitions - fs, fs->firstSector(), fs->lastSector(), - QString() /* path */ - ); - - // Add Partition object - info->device->partitionTable()->removeUnallocated(); - parent->insert( partition ); - info->device->partitionTable()->updateUnallocated( *info->device ); - - // Update model + job->createPreview(); info->partitionModel->reload(); - - // Create a CreatePartitionJob - // Enqueue job + Calamares::JobQueue::instance()->enqueue( Calamares::job_ptr( job ) ); } PartitionCoreModule::DeviceInfo* -PartitionCoreModule::deviceInfoForPath( const QString& path ) const +PartitionCoreModule::deviceInfoForDevice( Device* device ) const { for ( auto info : m_devices ) { - if ( info->device->deviceNode() == path ) + if ( info->device == device ) { return info; } diff --git a/src/modules/partition/PartitionCoreModule.h b/src/modules/partition/PartitionCoreModule.h index f363b9056..fa23aa751 100644 --- a/src/modules/partition/PartitionCoreModule.h +++ b/src/modules/partition/PartitionCoreModule.h @@ -25,6 +25,7 @@ // CalaPM #include +class CreatePartitionJob; class Device; class DeviceModel; class FileSystem; @@ -44,7 +45,7 @@ public: PartitionModel* partitionModelForDevice( Device* device ) const; - void createPartition( Partition* freeSpacePartition, FileSystem* fs, const QString& mountPoint, PartitionTable::Flags flags ); + void createPartition( CreatePartitionJob* job ); private: struct DeviceInfo @@ -59,7 +60,7 @@ private: void listDevices(); - DeviceInfo* deviceInfoForPath( const QString& path ) const; + DeviceInfo* deviceInfoForDevice( Device* device ) const; }; #endif /* PARTITIONCOREMODULE_H */ diff --git a/src/modules/partition/PartitionModel.h b/src/modules/partition/PartitionModel.h index 98cc61a1f..64fb018f8 100644 --- a/src/modules/partition/PartitionModel.h +++ b/src/modules/partition/PartitionModel.h @@ -35,6 +35,11 @@ public: Partition* partitionForIndex( const QModelIndex& index ) const; + Device* device() const + { + return m_device; + } + /** * Reload model from m_device new content */ diff --git a/src/modules/partition/PartitionPage.cpp b/src/modules/partition/PartitionPage.cpp index c1282d4a8..a71e265ac 100644 --- a/src/modules/partition/PartitionPage.cpp +++ b/src/modules/partition/PartitionPage.cpp @@ -19,17 +19,13 @@ #include "PartitionPage.h" // Local +#include #include #include #include #include #include -// CalaPM -#include -#include -#include - // Qt #include #include @@ -97,12 +93,10 @@ void PartitionPage::onCreateClicked() Partition* partition = model->partitionForIndex( index ); Q_ASSERT( partition ); - // FIXME: Ask user partition details here - qint64 start = partition->firstSector(); - qint64 end = partition->lastSector(); - FileSystem* fs = new FS::ext4( start, end, 0, "Calamares" ); - PartitionTable::Flags flags = PartitionTable::FlagNone; - QString mountPoint; - - m_core->createPartition( partition, fs, mountPoint, flags ); + CreatePartitionDialog dlg( model->device(), partition, this ); + if ( !dlg.exec() ) + { + return; + } + m_core->createPartition( dlg.createJob() ); }