calamares/src/modules/partition/gui/CreatePartitionDialog.cpp

211 lines
6.6 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "gui/CreatePartitionDialog.h"
#include "core/ColorUtils.h"
#include "core/PartitionInfo.h"
#include "core/PMUtils.h"
#include "gui/PartitionSizeController.h"
#include "ui_CreatePartitionDialog.h"
#include "utils/Logger.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
// KPMcore
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
#include <kpmcore/fs/filesystem.h>
#include <kpmcore/fs/filesystemfactory.h>
2014-07-04 16:14:06 +02:00
// Qt
#include <QComboBox>
#include <QDir>
#include <QSet>
2014-07-04 16:14:06 +02:00
static QSet< FileSystem::Type > s_unmountableFS(
{
FileSystem::Unformatted,
FileSystem::LinuxSwap,
FileSystem::Extended,
FileSystem::Unknown,
FileSystem::Lvm2_PV
} );
CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget )
: QDialog( parentWidget )
, m_ui( new Ui_CreatePartitionDialog )
, m_partitionSizeController( new PartitionSizeController( this ) )
, m_device( device )
, m_parent( parentPartition )
{
m_ui->setupUi( this );
QStringList mountPoints = { "/", "/boot", "/home", "/opt", "/usr", "/var" };
if ( QDir( "/sys/firmware/efi/efivars" ).exists() )
mountPoints << Calamares::JobQueue::instance()->globalStorage()->value( "efiSystemPartition" ).toString();
mountPoints.removeDuplicates();
mountPoints.sort();
m_ui->mountPointComboBox->addItems( mountPoints );
if ( device->partitionTable()->type() == PartitionTable::msdos ||
device->partitionTable()->type() == PartitionTable::msdos_sectorbased )
initMbrPartitionTypeUi();
else
initGptPartitionTypeUi();
// File system
QStringList fsNames;
for ( auto fs : FileSystemFactory::map() )
{
if ( fs->supportCreate() != FileSystem::cmdSupportNone && fs->type() != FileSystem::Extended )
fsNames << fs->name();
}
m_ui->fsComboBox->addItems( fsNames );
// Connections
connect( m_ui->fsComboBox, SIGNAL( activated( int ) ), SLOT( updateMountPointUi() ) );
connect( m_ui->extendedRadioButton, SIGNAL( toggled( bool ) ), SLOT( updateMountPointUi() ) );
}
CreatePartitionDialog::~CreatePartitionDialog()
{}
void
CreatePartitionDialog::initMbrPartitionTypeUi()
{
QString fixedPartitionString;
bool parentIsPartitionTable = m_parent->isRoot();
if ( !parentIsPartitionTable )
{
m_role = PartitionRole( PartitionRole::Logical );
fixedPartitionString = tr( "Logical" );
}
else if ( m_device->partitionTable()->hasExtended() )
{
m_role = PartitionRole( PartitionRole::Primary );
fixedPartitionString = tr( "Primary" );
}
if ( fixedPartitionString.isEmpty() )
m_ui->fixedPartitionLabel->hide();
else
{
m_ui->fixedPartitionLabel->setText( fixedPartitionString );
m_ui->primaryRadioButton->hide();
m_ui->extendedRadioButton->hide();
}
}
void
CreatePartitionDialog::initGptPartitionTypeUi()
{
m_role = PartitionRole( PartitionRole::Primary );
m_ui->fixedPartitionLabel->setText( tr( "GPT" ) );
m_ui->primaryRadioButton->hide();
m_ui->extendedRadioButton->hide();
}
Partition*
CreatePartitionDialog::createPartition()
{
if ( m_role.roles() == PartitionRole::None )
{
m_role = PartitionRole(
2014-07-02 14:12:47 +02:00
m_ui->extendedRadioButton->isChecked()
? PartitionRole::Extended
: PartitionRole::Primary
);
}
qint64 first = m_partitionSizeController->firstSector();
qint64 last = m_partitionSizeController->lastSector();
FileSystem::Type fsType = m_role.has( PartitionRole::Extended )
? FileSystem::Extended
: FileSystem::typeForName( m_ui->fsComboBox->currentText() );
Partition* partition = PMUtils::createNewPartition(
2014-08-05 09:54:30 +02:00
m_parent,
*m_device,
m_role,
fsType, first, last );
PartitionInfo::setMountPoint( partition, m_ui->mountPointComboBox->currentText() );
PartitionInfo::setFormat( partition, true );
return partition;
}
void
CreatePartitionDialog::updateMountPointUi()
{
bool enabled = m_ui->primaryRadioButton->isChecked();
if ( enabled )
{
FileSystem::Type type = FileSystem::typeForName( m_ui->fsComboBox->currentText() );
enabled = !s_unmountableFS.contains( type );
}
m_ui->mountPointLabel->setEnabled( enabled );
m_ui->mountPointComboBox->setEnabled( enabled );
if ( !enabled )
m_ui->mountPointComboBox->setCurrentText( QString() );
}
void
CreatePartitionDialog::initPartResizerWidget( Partition* partition )
{
2014-08-07 17:26:26 +02:00
QColor color = PMUtils::isPartitionFreeSpace( partition )
? ColorUtils::colorForPartitionInFreeSpace( partition )
: ColorUtils::colorForPartition( partition );
m_partitionSizeController->init( m_device, partition, color );
m_partitionSizeController->setPartResizerWidget( m_ui->partResizerWidget );
m_partitionSizeController->setSpinBox( m_ui->sizeSpinBox );
}
void
CreatePartitionDialog::initFromFreeSpace( Partition* freeSpacePartition )
{
initPartResizerWidget( freeSpacePartition );
}
void
CreatePartitionDialog::initFromPartitionToCreate( Partition* partition )
{
Q_ASSERT( partition );
bool isExtended = partition->roles().has( PartitionRole::Extended );
Q_ASSERT( !isExtended );
if ( isExtended )
{
cDebug() << "Editing extended partitions is not supported for now";
return;
}
initPartResizerWidget( partition );
// File System
FileSystem::Type fsType = partition->fileSystem().type();
m_ui->fsComboBox->setCurrentText( FileSystem::nameForType( fsType ) );
// Mount point
m_ui->mountPointComboBox->setCurrentText( PartitionInfo::mountPoint( partition ) );
updateMountPointUi();
}