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

392 lines
14 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://calamares.io> ===
2014-12-16 16:24:05 +01:00
*
2020-08-22 01:19:58 +02:00
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
* SPDX-FileCopyrightText: 2019-2020 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
2014-12-16 16:24:05 +01:00
*
* Calamares is Free Software: see the License-Identifier above.
2014-12-16 16:24:05 +01:00
*
*/
#include "ReplaceWidget.h"
#include "ui_ReplaceWidget.h"
2014-12-16 16:24:05 +01:00
#include "core/DeviceModel.h"
#include "core/PartitionActions.h"
2020-08-22 01:19:58 +02:00
#include "core/PartitionCoreModule.h"
#include "core/PartitionInfo.h"
2014-12-16 16:24:05 +01:00
#include "Branding.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "partition/FileSystem.h"
#include "utils/CalamaresUtilsGui.h"
#include "utils/Retranslator.h"
#include <kpmcore/core/partition.h>
#include <kpmcore/fs/filesystem.h>
2014-12-16 16:24:05 +01:00
#include <QComboBox>
using CalamaresUtils::Partition::untranslatedFS;
using CalamaresUtils::Partition::userVisibleFS;
2020-08-22 01:19:58 +02:00
ReplaceWidget::ReplaceWidget( PartitionCoreModule* core, QComboBox* devicesComboBox, QWidget* parent )
2014-12-16 16:24:05 +01:00
: QWidget( parent )
, m_ui( new Ui_ReplaceWidget )
2014-12-16 16:24:05 +01:00
, m_core( core )
, m_isEfi( false )
2014-12-16 16:24:05 +01:00
{
m_ui->setupUi( this );
m_ui->bootComboBox->hide();
m_ui->bootComboBox->clear();
m_ui->bootStatusLabel->hide();
m_ui->bootStatusLabel->clear();
updateFromCurrentDevice( devicesComboBox );
2020-10-12 14:27:01 +02:00
connect( devicesComboBox, &QComboBox::currentTextChanged, this, [=]( const QString& /* text */ ) {
updateFromCurrentDevice( devicesComboBox );
2014-12-16 16:24:05 +01:00
} );
2021-03-16 16:11:02 +01:00
CALAMARES_RETRANSLATE( onPartitionSelected(); );
2014-12-16 16:24:05 +01:00
}
2020-08-22 01:19:58 +02:00
ReplaceWidget::~ReplaceWidget() {}
2014-12-16 16:24:05 +01:00
bool
ReplaceWidget::isNextEnabled() const
2014-12-16 16:24:05 +01:00
{
return m_nextEnabled;
}
2015-04-29 18:15:46 +02:00
void
ReplaceWidget::reset()
2015-04-29 18:15:46 +02:00
{
//moo;
2015-04-29 18:15:46 +02:00
}
2014-12-16 16:24:05 +01:00
void
ReplaceWidget::applyChanges()
2014-12-16 16:24:05 +01:00
{
auto gs = Calamares::JobQueue::instance()->globalStorage();
2014-12-16 16:24:05 +01:00
PartitionModel* model = qobject_cast< PartitionModel* >( m_ui->partitionTreeView->model() );
if ( model )
{
Partition* partition = model->partitionForIndex( m_ui->partitionTreeView->currentIndex() );
if ( partition )
{
Device* dev = model->device();
2020-10-12 14:27:01 +02:00
PartitionActions::doReplacePartition( m_core,
dev,
partition,
{ gs->value( "defaultPartitionTableType" ).toString(),
gs->value( "defaultFileSystemType" ).toString(),
QString() } );
2014-12-16 16:24:05 +01:00
if ( m_isEfi )
{
QList< Partition* > efiSystemPartitions = m_core->efiSystemPartitions();
if ( efiSystemPartitions.count() == 1 )
{
2020-08-22 01:19:58 +02:00
PartitionInfo::setMountPoint( efiSystemPartitions.first(),
gs->value( "efiSystemPartition" ).toString() );
}
else if ( efiSystemPartitions.count() > 1 )
{
2020-08-22 01:19:58 +02:00
PartitionInfo::setMountPoint( efiSystemPartitions.at( m_ui->bootComboBox->currentIndex() ),
gs->value( "efiSystemPartition" ).toString() );
}
}
2014-12-16 16:24:05 +01:00
m_core->dumpQueue();
}
}
}
void
ReplaceWidget::onPartitionSelected()
2014-12-16 16:24:05 +01:00
{
if ( Calamares::JobQueue::instance()->globalStorage()->value( "firmwareType" ) == "efi" )
2020-08-22 01:19:58 +02:00
{
m_isEfi = true;
2020-08-22 01:19:58 +02:00
}
2020-05-04 12:48:11 +02:00
const auto* branding = Calamares::Branding::instance();
2014-12-16 16:24:05 +01:00
if ( m_ui->partitionTreeView->currentIndex() == QModelIndex() )
{
2015-04-03 12:19:27 +02:00
updateStatus( CalamaresUtils::PartitionPartition,
2014-12-16 16:24:05 +01:00
tr( "Select where to install %1.<br/>"
"<font color=\"red\">Warning: </font>this will delete all files "
2014-12-16 16:24:05 +01:00
"on the selected partition." )
2020-05-04 12:48:11 +02:00
.arg( branding->versionedName() ) );
2014-12-16 16:24:05 +01:00
setNextEnabled( false );
return;
}
bool ok = false;
2020-08-22 01:19:58 +02:00
double requiredSpaceB
= Calamares::JobQueue::instance()->globalStorage()->value( "requiredStorageGiB" ).toDouble( &ok ) * 1024 * 1024
* 1024;
2014-12-16 16:24:05 +01:00
PartitionModel* model = qobject_cast< PartitionModel* >( m_ui->partitionTreeView->model() );
if ( model && ok )
{
2020-08-22 01:19:58 +02:00
const QStringList osproberLines
= Calamares::JobQueue::instance()->globalStorage()->value( "osproberLines" ).toStringList();
2014-12-16 16:24:05 +01:00
Partition* partition = model->partitionForIndex( m_ui->partitionTreeView->currentIndex() );
2020-08-22 01:19:58 +02:00
if ( !partition || partition->state() != KPM_PARTITION_STATE( None ) )
2014-12-16 16:24:05 +01:00
{
2020-08-22 01:19:58 +02:00
updateStatus( CalamaresUtils::Fail, tr( "The selected item does not appear to be a valid partition." ) );
2014-12-16 16:24:05 +01:00
setNextEnabled( false );
return;
}
if ( partition->roles().has( PartitionRole::Unallocated ) )
{
updateStatus( CalamaresUtils::Fail,
tr( "%1 cannot be installed on empty space. Please select an "
"existing partition." )
2020-08-22 01:19:58 +02:00
.arg( branding->versionedName() ) );
2014-12-16 16:24:05 +01:00
setNextEnabled( false );
return;
}
if ( partition->roles().has( PartitionRole::Extended ) )
{
updateStatus( CalamaresUtils::Fail,
tr( "%1 cannot be installed on an extended partition. Please select an "
"existing primary or logical partition." )
2020-08-22 01:19:58 +02:00
.arg( branding->versionedName() ) );
2014-12-16 16:24:05 +01:00
setNextEnabled( false );
return;
}
if ( partition->partitionPath().isEmpty() )
{
updateStatus( CalamaresUtils::Fail,
2020-08-22 01:19:58 +02:00
tr( "%1 cannot be installed on this partition." ).arg( branding->versionedName() ) );
2014-12-16 16:24:05 +01:00
setNextEnabled( false );
return;
}
QString fsNameForUser = userVisibleFS( partition->fileSystem() );
QString prettyName = tr( "Data partition (%1)" ).arg( fsNameForUser );
for ( const auto& line : osproberLines )
{
QStringList lineColumns = line.split( ':' );
QString path = lineColumns.value( 0 ).simplified();
if ( path == partition->partitionPath() )
{
QString osName;
if ( !lineColumns.value( 1 ).simplified().isEmpty() )
2020-08-22 01:19:58 +02:00
{
osName = lineColumns.value( 1 ).simplified();
2020-08-22 01:19:58 +02:00
}
else if ( !lineColumns.value( 2 ).simplified().isEmpty() )
2020-08-22 01:19:58 +02:00
{
osName = lineColumns.value( 2 ).simplified();
2020-08-22 01:19:58 +02:00
}
if ( osName.isEmpty() )
{
2020-08-22 01:19:58 +02:00
prettyName = tr( "Unknown system partition (%1)" ).arg( fsNameForUser );
}
else
{
2020-08-22 01:19:58 +02:00
prettyName = tr( "%1 system partition (%2)" )
.arg( osName.replace( 0, 1, osName.at( 0 ).toUpper() ) )
.arg( fsNameForUser );
}
break;
}
}
2014-12-16 16:24:05 +01:00
if ( partition->capacity() < requiredSpaceB )
{
updateStatus( CalamaresUtils::Fail,
tr( "<strong>%4</strong><br/><br/>"
"The partition %1 is too small for %2. Please select a partition "
2014-12-16 17:33:19 +01:00
"with capacity at least %3 GiB." )
2020-08-22 01:19:58 +02:00
.arg( partition->partitionPath() )
.arg( branding->versionedName() )
.arg( requiredSpaceB / ( 1024. * 1024. * 1024. ), 0, 'f', 1 )
.arg( prettyName ) );
2014-12-16 16:24:05 +01:00
setNextEnabled( false );
return;
}
m_ui->bootComboBox->hide();
m_ui->bootComboBox->clear();
m_ui->bootStatusLabel->hide();
m_ui->bootStatusLabel->clear();
if ( m_isEfi )
{
QList< Partition* > efiSystemPartitions = m_core->efiSystemPartitions();
if ( efiSystemPartitions.count() == 0 )
{
updateStatus( CalamaresUtils::Fail,
tr( "<strong>%2</strong><br/><br/>"
"An EFI system partition cannot be found anywhere "
"on this system. Please go back and use manual "
"partitioning to set up %1." )
2020-08-22 01:19:58 +02:00
.arg( branding->shortProductName() )
.arg( prettyName ) );
setNextEnabled( false );
}
else if ( efiSystemPartitions.count() == 1 )
{
updateStatus( CalamaresUtils::PartitionPartition,
tr( "<strong>%3</strong><br/><br/>"
"%1 will be installed on %2.<br/>"
"<font color=\"red\">Warning: </font>all data on partition "
2020-08-22 01:19:58 +02:00
"%2 will be lost." )
.arg( branding->versionedName() )
.arg( partition->partitionPath() )
.arg( prettyName ) );
m_ui->bootStatusLabel->show();
2020-08-22 01:19:58 +02:00
m_ui->bootStatusLabel->setText( tr( "The EFI system partition at %1 will be used for starting %2." )
.arg( efiSystemPartitions.first()->partitionPath() )
.arg( branding->shortProductName() ) );
setNextEnabled( true );
}
else
{
updateStatus( CalamaresUtils::PartitionPartition,
tr( "<strong>%3</strong><br/><br/>"
"%1 will be installed on %2.<br/>"
"<font color=\"red\">Warning: </font>all data on partition "
2020-08-22 01:19:58 +02:00
"%2 will be lost." )
.arg( branding->versionedName() )
.arg( partition->partitionPath() )
.arg( prettyName ) );
m_ui->bootStatusLabel->show();
m_ui->bootStatusLabel->setText( tr( "EFI system partition:" ) );
m_ui->bootComboBox->show();
for ( int i = 0; i < efiSystemPartitions.count(); ++i )
{
Partition* efiPartition = efiSystemPartitions.at( i );
m_ui->bootComboBox->addItem( efiPartition->partitionPath(), i );
2020-08-22 01:19:58 +02:00
if ( efiPartition->devicePath() == partition->devicePath() && efiPartition->number() == 1 )
{
m_ui->bootComboBox->setCurrentIndex( i );
2020-08-22 01:19:58 +02:00
}
}
setNextEnabled( true );
}
}
else
{
updateStatus( CalamaresUtils::PartitionPartition,
tr( "<strong>%3</strong><br/><br/>"
"%1 will be installed on %2.<br/>"
"<font color=\"red\">Warning: </font>all data on partition "
2020-08-22 01:19:58 +02:00
"%2 will be lost." )
.arg( branding->versionedName() )
.arg( partition->partitionPath() )
.arg( prettyName ) );
setNextEnabled( true );
}
2014-12-16 16:24:05 +01:00
}
}
void
ReplaceWidget::setNextEnabled( bool enabled )
2014-12-16 16:24:05 +01:00
{
if ( enabled == m_nextEnabled )
2020-08-22 01:19:58 +02:00
{
2014-12-16 16:24:05 +01:00
return;
2020-08-22 01:19:58 +02:00
}
2014-12-16 16:24:05 +01:00
m_nextEnabled = enabled;
emit nextStatusChanged( enabled );
}
void
ReplaceWidget::updateStatus( CalamaresUtils::ImageType imageType, const QString& text )
2014-12-16 16:24:05 +01:00
{
2015-11-20 15:12:22 +01:00
int iconSize = CalamaresUtils::defaultFontHeight() * 6;
2020-08-22 01:19:58 +02:00
m_ui->selectedIconLabel->setPixmap(
CalamaresUtils::defaultPixmap( imageType, CalamaresUtils::Original, QSize( iconSize, iconSize ) ) );
2014-12-16 16:24:05 +01:00
m_ui->selectedIconLabel->setFixedHeight( iconSize );
m_ui->selectedStatusLabel->setText( text );
}
void
ReplaceWidget::updateFromCurrentDevice( QComboBox* devicesComboBox )
2014-12-16 16:24:05 +01:00
{
QModelIndex index = m_core->deviceModel()->index( devicesComboBox->currentIndex(), 0 );
2014-12-16 16:24:05 +01:00
if ( !index.isValid() )
2020-08-22 01:19:58 +02:00
{
2014-12-16 16:24:05 +01:00
return;
2020-08-22 01:19:58 +02:00
}
2014-12-16 16:24:05 +01:00
Device* device = m_core->deviceModel()->deviceForIndex( index );
QAbstractItemModel* oldModel = m_ui->partitionTreeView->model();
if ( oldModel )
2020-08-22 01:19:58 +02:00
{
disconnect( oldModel, nullptr, this, nullptr );
2020-08-22 01:19:58 +02:00
}
2014-12-16 16:24:05 +01:00
PartitionModel* model = m_core->partitionModelForDevice( device );
m_ui->partitionTreeView->setModel( model );
m_ui->partitionTreeView->expandAll();
// Must be done here because we need to have a model set to define
// individual column resize mode
QHeaderView* header = m_ui->partitionTreeView->header();
header->setSectionResizeMode( QHeaderView::ResizeToContents );
header->setSectionResizeMode( 0, QHeaderView::Stretch );
//updateButtons();
// Establish connection here because selection model is destroyed when
// model changes
2020-08-22 01:19:58 +02:00
connect( m_ui->partitionTreeView->selectionModel(),
&QItemSelectionModel::currentRowChanged,
this,
&ReplaceWidget::onPartitionViewActivated );
connect( model, &QAbstractItemModel::modelReset, this, &ReplaceWidget::onPartitionModelReset );
2014-12-16 16:24:05 +01:00
}
void
ReplaceWidget::onPartitionViewActivated()
2014-12-16 16:24:05 +01:00
{
QModelIndex index = m_ui->partitionTreeView->currentIndex();
if ( !index.isValid() )
2020-08-22 01:19:58 +02:00
{
2014-12-16 16:24:05 +01:00
return;
2020-08-22 01:19:58 +02:00
}
2014-12-16 16:24:05 +01:00
const PartitionModel* model = static_cast< const PartitionModel* >( index.model() );
Q_ASSERT( model );
Partition* partition = model->partitionForIndex( index );
Q_ASSERT( partition );
onPartitionSelected();
}
void
ReplaceWidget::onPartitionModelReset()
2014-12-16 16:24:05 +01:00
{
m_ui->partitionTreeView->expandAll();
onPartitionSelected();
}