Kill EraseDiskPage, add PartitionActions.

This commit is contained in:
Teo Mrnjavac 2015-10-29 17:33:15 +01:00
parent 6de8158bec
commit 9167a34f2e
3 changed files with 116 additions and 280 deletions

View File

@ -25,6 +25,7 @@ calamares_add_plugin( partition
core/ColorUtils.cpp
core/DeviceModel.cpp
core/KPMHelpers.cpp
core/PartitionActions.cpp
core/PartitionCoreModule.cpp
core/PartitionInfo.cpp
core/PartitionIterator.cpp
@ -34,7 +35,6 @@ calamares_add_plugin( partition
gui/CreatePartitionDialog.cpp
gui/EditExistingPartitionDialog.cpp
gui/AlongsidePage.cpp
gui/EraseDiskPage.cpp
gui/PartitionPage.cpp
gui/PartitionPreview.cpp
gui/PartitionSizeController.cpp

View File

@ -16,266 +16,27 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include "EraseDiskPage.h"
#include "PartitionActions.h"
#include "core/DeviceModel.h"
#include "core/PartitionCoreModule.h"
#include "core/KPMHelpers.h"
#include "core/PartitionInfo.h"
#include "gui/PartitionPreview.h"
#include "core/PartitionCoreModule.h"
#include "utils/CalamaresUtilsGui.h"
#include "utils/Logger.h"
#include "utils/Retranslator.h"
#include "utils/CalamaresUtilsSystem.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "utils/Logger.h"
#include "GlobalStorage.h"
// KPMcore
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
#include <kpmcore/fs/filesystem.h>
#include <QBoxLayout>
#include <QDir>
#include <QFormLayout>
#include <QListView>
#include <QLabel>
#include <QMutexLocker>
EraseDiskPage::EraseDiskPage( QWidget* parent )
: QWidget( parent )
, m_nextEnabled( false )
, m_core( nullptr )
namespace PartitionActions
{
QVBoxLayout* mainLayout = new QVBoxLayout;
setLayout( mainLayout );
QLabel* driveLabel = new QLabel( this );
mainLayout->addWidget( driveLabel );
CALAMARES_RETRANSLATE( driveLabel->setText( tr( "Select drive:" ) ); )
m_drivesView = new QListView;
mainLayout->addWidget( m_drivesView );
m_drivesView->setViewMode( QListView::IconMode );
m_drivesView->setWrapping( false );
m_drivesView->setFlow( QListView::LeftToRight );
m_drivesView->setSelectionRectVisible( false );
m_drivesView->setWordWrap( true );
m_drivesView->setUniformItemSizes( true );
m_drivesView->setSelectionMode( QAbstractItemView::SingleSelection );
m_drivesView->setIconSize( CalamaresUtils::defaultIconSize() * 3 );
m_drivesView->setGridSize( QSize( CalamaresUtils::defaultFontHeight() * 8,
m_drivesView->iconSize().height() +
CalamaresUtils::defaultFontHeight() * 4 ) );
m_drivesView->setMinimumHeight( m_drivesView->gridSize().height() +
CalamaresUtils::defaultFontHeight() / 2 );
m_drivesView->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
m_previewFrame = new QWidget;
m_previewFrame->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
mainLayout->addWidget( m_previewFrame );
setNextEnabled( false );
}
void
EraseDiskPage::init( PartitionCoreModule* core )
{
if ( m_core ) //this should probably never happen
{
m_core->revert();
return;
}
m_core = core;
m_drivesView->setModel( core->deviceModel() );
connect( m_drivesView->selectionModel(), &QItemSelectionModel::currentChanged,
this, [ this ]( const QModelIndex& index,
const QModelIndex& oldIndex )
{
setNextEnabled( m_drivesView->selectionModel()->hasSelection() );
if ( m_core->isDirty() )
m_core->clearJobs();
Device* dev = m_core->deviceModel()->deviceForIndex( index );
if ( dev )
doAutopartition( dev );
} );
connect( m_core, &PartitionCoreModule::isDirtyChanged,
this, &EraseDiskPage::updatePreviews );
}
bool
EraseDiskPage::isNextEnabled() const
{
return m_nextEnabled;
}
void
EraseDiskPage::setNextEnabled( bool enabled )
{
if ( enabled == m_nextEnabled )
return;
m_nextEnabled = enabled;
emit nextStatusChanged( enabled );
}
void
EraseDiskPage::doAutopartition( Device* dev )
{
bool isEfi = false;
if ( QDir( "/sys/firmware/efi/efivars" ).exists() )
isEfi = true;
#define MiB * static_cast< qint64 >( 1024 ) * 1024
#define GiB * static_cast< qint64 >( 1024 ) * 1024 * 1024
// Partition sizes are expressed in MiB, should be multiples of
// the logical sector size (usually 512B).
int uefisys_part_size = 0;
int empty_space_size = 0;
if ( isEfi )
{
uefisys_part_size = 300;
empty_space_size = 2;
}
else
{
// we start with a 1MiB offset before the first partition
empty_space_size = 1;
}
qint64 firstFreeSector = empty_space_size MiB / dev->logicalSectorSize() + 1;
if ( isEfi )
{
qint64 lastSector = firstFreeSector + ( uefisys_part_size MiB / dev->logicalSectorSize() );
m_core->createPartitionTable( dev, PartitionTable::gpt );
Partition* efiPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
*dev,
PartitionRole( PartitionRole::Primary ),
FileSystem::Fat32,
firstFreeSector,
lastSector
);
PartitionInfo::setFormat( efiPartition, true );
PartitionInfo::setMountPoint( efiPartition, Calamares::JobQueue::instance()
->globalStorage()
->value( "efiSystemPartition" )
.toString() );
m_core->createPartition( dev, efiPartition );
firstFreeSector = lastSector + 1;
}
else
{
m_core->createPartitionTable( dev, PartitionTable::msdos );
}
bool shouldCreateSwap = false;
qint64 availableSpaceB = ( dev->totalSectors() - firstFreeSector ) * dev->logicalSectorSize();
qint64 suggestedSwapSizeB = swapSuggestion( availableSpaceB );
qint64 requiredSpaceB =
( Calamares::JobQueue::instance()->
globalStorage()->
value( "requiredStorageGB" ).toDouble() + 0.1 + 2.0 ) GiB +
suggestedSwapSizeB;
// If there is enough room for ESP + root + swap, create swap, otherwise don't.
shouldCreateSwap = availableSpaceB > requiredSpaceB;
qint64 lastSectorForRoot = dev->totalSectors() - 1; //last sector of the device
if ( shouldCreateSwap )
{
lastSectorForRoot -= suggestedSwapSizeB / dev->logicalSectorSize() + 1;
}
Partition* rootPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
*dev,
PartitionRole( PartitionRole::Primary ),
FileSystem::Ext4,
firstFreeSector,
lastSectorForRoot
);
PartitionInfo::setFormat( rootPartition, true );
PartitionInfo::setMountPoint( rootPartition, "/" );
m_core->createPartition( dev, rootPartition );
if ( shouldCreateSwap )
{
Partition* swapPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
*dev,
PartitionRole( PartitionRole::Primary ),
FileSystem::LinuxSwap,
lastSectorForRoot + 1,
dev->totalSectors() - 1
);
PartitionInfo::setFormat( swapPartition, true );
m_core->createPartition( dev, swapPartition );
}
updatePreviews();
m_core->dumpQueue();
}
void
EraseDiskPage::updatePreviews()
{
QMutexLocker locker( &m_previewsMutex );
cDebug() << "Updating partitioning preview widgets.";
qDeleteAll( m_previewFrame->children() );
m_previewFrame->layout()->deleteLater();
if ( m_drivesView->selectionModel()->currentIndex() == QModelIndex() )
{
cDebug() << "No disk selected, bailing out.";
return;
}
QFormLayout* layout = new QFormLayout;
m_previewFrame->setLayout( layout );
layout->setMargin( 0 );
QList< PartitionCoreModule::SummaryInfo > list = m_core->createSummaryInfo();
for ( const auto& info : list )
{
PartitionPreview* preview;
layout->addRow( new QLabel( info.deviceName ) );
preview = new PartitionPreview;
preview->setLabelsVisible( true );
preview->setModel( info.partitionModelBefore );
info.partitionModelBefore->setParent( m_previewFrame );
layout->addRow( tr( "Before:" ), preview );
preview = new PartitionPreview;
preview->setLabelsVisible( true );
preview->setModel( info.partitionModelAfter );
info.partitionModelAfter->setParent( m_previewFrame );
layout->addRow( tr( "After:" ), preview );
}
}
qint64
EraseDiskPage::swapSuggestion( const qint64 availableSpaceB ) const {
swapSuggestion( const qint64 availableSpaceB )
{
#define MiB * static_cast< qint64 >( 1024 ) * 1024
#define GiB * static_cast< qint64 >( 1024 ) * 1024 * 1024
@ -333,3 +94,105 @@ EraseDiskPage::swapSuggestion( const qint64 availableSpaceB ) const {
return suggestedSwapSizeB;
}
void
doAutopartition( PartitionCoreModule* core, Device* dev )
{
bool isEfi = false;
if ( QDir( "/sys/firmware/efi/efivars" ).exists() )
isEfi = true;
#define MiB * static_cast< qint64 >( 1024 ) * 1024
#define GiB * static_cast< qint64 >( 1024 ) * 1024 * 1024
// Partition sizes are expressed in MiB, should be multiples of
// the logical sector size (usually 512B).
int uefisys_part_size = 0;
int empty_space_size = 0;
if ( isEfi )
{
uefisys_part_size = 300;
empty_space_size = 2;
}
else
{
// we start with a 1MiB offset before the first partition
empty_space_size = 1;
}
qint64 firstFreeSector = empty_space_size MiB / dev->logicalSectorSize() + 1;
if ( isEfi )
{
qint64 lastSector = firstFreeSector + ( uefisys_part_size MiB / dev->logicalSectorSize() );
core->createPartitionTable( dev, PartitionTable::gpt );
Partition* efiPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
*dev,
PartitionRole( PartitionRole::Primary ),
FileSystem::Fat32,
firstFreeSector,
lastSector
);
PartitionInfo::setFormat( efiPartition, true );
PartitionInfo::setMountPoint( efiPartition, Calamares::JobQueue::instance()
->globalStorage()
->value( "efiSystemPartition" )
.toString() );
core->createPartition( dev, efiPartition );
firstFreeSector = lastSector + 1;
}
else
{
core->createPartitionTable( dev, PartitionTable::msdos );
}
bool shouldCreateSwap = false;
qint64 availableSpaceB = ( dev->totalSectors() - firstFreeSector ) * dev->logicalSectorSize();
qint64 suggestedSwapSizeB = swapSuggestion( availableSpaceB );
qint64 requiredSpaceB =
( Calamares::JobQueue::instance()->
globalStorage()->
value( "requiredStorageGB" ).toDouble() + 0.1 + 2.0 ) GiB +
suggestedSwapSizeB;
// If there is enough room for ESP + root + swap, create swap, otherwise don't.
shouldCreateSwap = availableSpaceB > requiredSpaceB;
qint64 lastSectorForRoot = dev->totalSectors() - 1; //last sector of the device
if ( shouldCreateSwap )
{
lastSectorForRoot -= suggestedSwapSizeB / dev->logicalSectorSize() + 1;
}
Partition* rootPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
*dev,
PartitionRole( PartitionRole::Primary ),
FileSystem::Ext4,
firstFreeSector,
lastSectorForRoot
);
PartitionInfo::setFormat( rootPartition, true );
PartitionInfo::setMountPoint( rootPartition, "/" );
core->createPartition( dev, rootPartition );
if ( shouldCreateSwap )
{
Partition* swapPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
*dev,
PartitionRole( PartitionRole::Primary ),
FileSystem::LinuxSwap,
lastSectorForRoot + 1,
dev->totalSectors() - 1
);
PartitionInfo::setFormat( swapPartition, true );
core->createPartition( dev, swapPartition );
}
core->dumpQueue();
}
}

View File

@ -16,42 +16,15 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ERASEDISKPAGE_H
#define ERASEDISKPAGE_H
#include <QMutex>
#include <QWidget>
#ifndef PARTITIONACTIONS_H
#define PARTITIONACTIONS_H
class PartitionCoreModule;
class QListView;
class Device;
class EraseDiskPage : public QWidget
namespace PartitionActions
{
Q_OBJECT
public:
explicit EraseDiskPage( QWidget* parent = nullptr );
void doAutopartition( PartitionCoreModule* core, Device* dev );
}
void init( PartitionCoreModule* core );
bool isNextEnabled() const;
signals:
void nextStatusChanged( bool );
private:
void setNextEnabled( bool enabled );
void doAutopartition( Device* dev );
void updatePreviews();
qint64 swapSuggestion( const qint64 availableSpaceB ) const;
QListView* m_drivesView;
PartitionCoreModule* m_core;
QWidget* m_previewFrame;
QMutex m_previewsMutex;
bool m_nextEnabled;
};
#endif // ERASEDISKPAGE_H
#endif // PARTITIONACTIONS_H