2014-06-27 17:25:39 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
2016-02-12 17:19:30 +01:00
|
|
|
* Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
|
2014-06-27 17:25:39 +02:00
|
|
|
*
|
|
|
|
* 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 "PartitionPage.h"
|
|
|
|
|
|
|
|
// Local
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "core/BootLoaderModel.h"
|
|
|
|
#include "core/DeviceModel.h"
|
|
|
|
#include "core/PartitionCoreModule.h"
|
|
|
|
#include "core/PartitionModel.h"
|
2015-09-18 15:41:07 +02:00
|
|
|
#include "core/KPMHelpers.h"
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "gui/CreatePartitionDialog.h"
|
|
|
|
#include "gui/EditExistingPartitionDialog.h"
|
2015-12-30 16:37:57 +01:00
|
|
|
#include "gui/ScanningDialog.h"
|
2014-08-08 13:25:56 +02:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "ui_PartitionPage.h"
|
|
|
|
#include "ui_CreatePartitionTableDialog.h"
|
2014-07-10 19:55:16 +02:00
|
|
|
|
2014-11-11 17:09:33 +01:00
|
|
|
#include "utils/Retranslator.h"
|
2015-12-31 13:56:19 +01:00
|
|
|
#include "Branding.h"
|
2016-02-10 14:59:37 +01:00
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "GlobalStorage.h"
|
2014-11-11 17:09:33 +01:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
// KPMcore
|
|
|
|
#include <kpmcore/core/device.h>
|
|
|
|
#include <kpmcore/core/partition.h>
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
// Qt
|
|
|
|
#include <QDebug>
|
2014-07-02 17:55:53 +02:00
|
|
|
#include <QHeaderView>
|
2014-06-27 17:42:13 +02:00
|
|
|
#include <QItemSelectionModel>
|
2014-07-10 19:55:16 +02:00
|
|
|
#include <QMessageBox>
|
2014-07-01 16:46:33 +02:00
|
|
|
#include <QPointer>
|
2015-03-29 16:55:25 +02:00
|
|
|
#include <QDir>
|
2015-12-30 16:37:57 +01:00
|
|
|
#include <QFutureWatcher>
|
|
|
|
#include <QtConcurrent/QtConcurrent>
|
2014-06-27 17:25:39 +02:00
|
|
|
|
2014-07-02 16:06:54 +02:00
|
|
|
PartitionPage::PartitionPage( PartitionCoreModule* core, QWidget* parent )
|
2014-07-03 18:00:40 +02:00
|
|
|
: QWidget( parent )
|
2014-06-27 17:25:39 +02:00
|
|
|
, m_ui( new Ui_PartitionPage )
|
2014-07-02 16:06:54 +02:00
|
|
|
, m_core( core )
|
2014-06-27 17:25:39 +02:00
|
|
|
{
|
|
|
|
m_ui->setupUi( this );
|
2014-07-21 16:15:53 +02:00
|
|
|
m_ui->deviceComboBox->setModel( m_core->deviceModel() );
|
2014-07-22 10:42:50 +02:00
|
|
|
m_ui->bootLoaderComboBox->setModel( m_core->bootLoaderModel() );
|
2016-02-10 14:59:37 +01:00
|
|
|
PartitionBarsView::NestedPartitionsMode mode = Calamares::JobQueue::instance()->globalStorage()->
|
|
|
|
value( "drawNestedPartitions" ).toBool() ?
|
|
|
|
PartitionBarsView::DrawNestedPartitions :
|
|
|
|
PartitionBarsView::NoNestedPartitions;
|
|
|
|
m_ui->partitionBarsView->setNestedPartitionsMode( mode );
|
2014-06-30 15:04:10 +02:00
|
|
|
updateButtons();
|
2014-07-23 18:15:46 +02:00
|
|
|
updateBootLoaderInstallPath();
|
2014-06-27 17:25:39 +02:00
|
|
|
|
2014-07-24 19:26:19 +02:00
|
|
|
updateFromCurrentDevice();
|
|
|
|
|
2014-07-21 16:15:53 +02:00
|
|
|
connect( m_ui->deviceComboBox, &QComboBox::currentTextChanged,
|
|
|
|
[ this ]( const QString& /* text */ )
|
2014-06-27 18:42:15 +02:00
|
|
|
{
|
2014-07-24 19:26:19 +02:00
|
|
|
updateFromCurrentDevice();
|
2014-06-27 18:42:15 +02:00
|
|
|
} );
|
2014-06-30 16:17:28 +02:00
|
|
|
|
2014-07-23 18:15:46 +02:00
|
|
|
connect( m_ui->bootLoaderComboBox, &QComboBox::currentTextChanged,
|
|
|
|
[ this ]( const QString& /* text */ )
|
|
|
|
{
|
|
|
|
updateBootLoaderInstallPath();
|
|
|
|
} );
|
|
|
|
|
2014-07-24 19:28:53 +02:00
|
|
|
connect( m_core, &PartitionCoreModule::isDirtyChanged, m_ui->revertButton, &QWidget::setEnabled );
|
2014-07-23 18:15:46 +02:00
|
|
|
|
2014-07-25 13:07:22 +02:00
|
|
|
connect( m_ui->partitionTreeView, &QAbstractItemView::activated, this, &PartitionPage::onPartitionViewActivated );
|
2014-07-24 19:28:53 +02:00
|
|
|
connect( m_ui->revertButton, &QAbstractButton::clicked, this, &PartitionPage::onRevertClicked );
|
2014-07-10 19:55:16 +02:00
|
|
|
connect( m_ui->newPartitionTableButton, &QAbstractButton::clicked, this, &PartitionPage::onNewPartitionTableClicked );
|
2014-06-30 16:17:28 +02:00
|
|
|
connect( m_ui->createButton, &QAbstractButton::clicked, this, &PartitionPage::onCreateClicked );
|
2014-07-15 17:37:04 +02:00
|
|
|
connect( m_ui->editButton, &QAbstractButton::clicked, this, &PartitionPage::onEditClicked );
|
2014-07-02 15:49:35 +02:00
|
|
|
connect( m_ui->deleteButton, &QAbstractButton::clicked, this, &PartitionPage::onDeleteClicked );
|
2014-11-11 17:09:33 +01:00
|
|
|
|
2015-03-29 16:55:25 +02:00
|
|
|
if ( QDir( "/sys/firmware/efi/efivars" ).exists() ) {
|
|
|
|
m_ui->bootLoaderComboBox->setDisabled( true );
|
|
|
|
m_ui->label_3->setDisabled( true );
|
|
|
|
}
|
|
|
|
|
2014-11-11 17:09:33 +01:00
|
|
|
CALAMARES_RETRANSLATE( m_ui->retranslateUi( this ); )
|
2014-06-27 17:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PartitionPage::~PartitionPage()
|
|
|
|
{
|
|
|
|
}
|
2014-06-30 15:04:10 +02:00
|
|
|
|
2014-07-02 14:12:47 +02:00
|
|
|
void
|
|
|
|
PartitionPage::updateButtons()
|
2014-06-30 15:04:10 +02:00
|
|
|
{
|
|
|
|
bool create = false, edit = false, del = false;
|
|
|
|
|
2014-07-02 17:55:53 +02:00
|
|
|
QModelIndex index = m_ui->partitionTreeView->currentIndex();
|
2014-06-30 15:04:10 +02:00
|
|
|
if ( index.isValid() )
|
|
|
|
{
|
|
|
|
const PartitionModel* model = static_cast< const PartitionModel* >( index.model() );
|
|
|
|
Q_ASSERT( model );
|
|
|
|
Partition* partition = model->partitionForIndex( index );
|
|
|
|
Q_ASSERT( partition );
|
2015-09-18 15:41:07 +02:00
|
|
|
bool isFree = KPMHelpers::isPartitionFreeSpace( partition );
|
2014-07-16 10:59:24 +02:00
|
|
|
bool isExtended = partition->roles().has( PartitionRole::Extended );
|
|
|
|
|
2014-06-30 15:04:10 +02:00
|
|
|
create = isFree;
|
2014-07-16 10:59:24 +02:00
|
|
|
// Keep it simple for now: do not support editing extended partitions as
|
|
|
|
// it does not work with our current edit implementation which is
|
|
|
|
// actually remove + add. This would not work with extended partitions
|
|
|
|
// because they need to be created *before* creating logical partitions
|
|
|
|
// inside them, so an edit must be applied without altering the job
|
|
|
|
// order.
|
|
|
|
edit = !isFree && !isExtended;
|
|
|
|
del = !isFree;
|
2014-06-30 15:04:10 +02:00
|
|
|
}
|
|
|
|
m_ui->createButton->setEnabled( create );
|
|
|
|
m_ui->editButton->setEnabled( edit );
|
|
|
|
m_ui->deleteButton->setEnabled( del );
|
2014-07-10 19:55:16 +02:00
|
|
|
|
2014-07-21 16:15:53 +02:00
|
|
|
m_ui->newPartitionTableButton->setEnabled( m_ui->deviceComboBox->currentIndex() >= 0 );
|
2014-07-10 19:55:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionPage::onNewPartitionTableClicked()
|
|
|
|
{
|
2014-07-29 15:54:48 +02:00
|
|
|
QModelIndex index = m_core->deviceModel()->index( m_ui->deviceComboBox->currentIndex(), 0 );
|
2014-07-10 19:55:16 +02:00
|
|
|
Q_ASSERT( index.isValid() );
|
|
|
|
Device* device = m_core->deviceModel()->deviceForIndex( index );
|
|
|
|
|
2014-07-15 14:40:08 +02:00
|
|
|
QPointer<QDialog> dlg = new QDialog( this );
|
|
|
|
Ui_CreatePartitionTableDialog ui;
|
|
|
|
ui.setupUi( dlg.data() );
|
|
|
|
QString areYouSure = tr( "Are you sure you want to create a new partition table on %1?" ).arg( device->name() );
|
|
|
|
ui.areYouSureLabel->setText( areYouSure );
|
|
|
|
if ( dlg->exec() == QDialog::Accepted )
|
|
|
|
{
|
|
|
|
PartitionTable::TableType type = ui.mbrRadioButton->isChecked() ? PartitionTable::msdos : PartitionTable::gpt;
|
|
|
|
m_core->createPartitionTable( device, type );
|
|
|
|
}
|
|
|
|
delete dlg;
|
2014-06-30 15:04:10 +02:00
|
|
|
}
|
2014-06-30 16:17:28 +02:00
|
|
|
|
2014-07-02 14:12:47 +02:00
|
|
|
void
|
|
|
|
PartitionPage::onCreateClicked()
|
2014-06-30 16:17:28 +02:00
|
|
|
{
|
2014-07-02 17:55:53 +02:00
|
|
|
QModelIndex index = m_ui->partitionTreeView->currentIndex();
|
2014-06-30 16:17:28 +02:00
|
|
|
Q_ASSERT( index.isValid() );
|
|
|
|
|
|
|
|
const PartitionModel* model = static_cast< const PartitionModel* >( index.model() );
|
|
|
|
Partition* partition = model->partitionForIndex( index );
|
|
|
|
Q_ASSERT( partition );
|
|
|
|
|
2014-07-15 17:37:04 +02:00
|
|
|
QPointer<CreatePartitionDialog> dlg = new CreatePartitionDialog( model->device(), partition->parent(), this );
|
2014-07-16 10:15:57 +02:00
|
|
|
dlg->initFromFreeSpace( partition );
|
2014-07-01 16:46:33 +02:00
|
|
|
if ( dlg->exec() == QDialog::Accepted )
|
2016-03-08 12:44:49 +01:00
|
|
|
{
|
|
|
|
Partition* newPart = dlg->createPartition();
|
2016-03-08 16:26:20 +01:00
|
|
|
m_core->createPartition( model->device(), newPart, dlg->newFlags() );
|
2016-03-08 12:44:49 +01:00
|
|
|
}
|
2014-07-01 16:46:33 +02:00
|
|
|
delete dlg;
|
2014-06-30 16:17:28 +02:00
|
|
|
}
|
2014-07-02 15:49:35 +02:00
|
|
|
|
2014-07-15 17:37:04 +02:00
|
|
|
void
|
|
|
|
PartitionPage::onEditClicked()
|
|
|
|
{
|
|
|
|
QModelIndex index = m_ui->partitionTreeView->currentIndex();
|
|
|
|
Q_ASSERT( index.isValid() );
|
|
|
|
|
|
|
|
const PartitionModel* model = static_cast< const PartitionModel* >( index.model() );
|
|
|
|
Partition* partition = model->partitionForIndex( index );
|
|
|
|
Q_ASSERT( partition );
|
|
|
|
|
2015-09-18 15:41:07 +02:00
|
|
|
if ( KPMHelpers::isPartitionNew( partition ) )
|
2014-07-16 16:20:58 +02:00
|
|
|
updatePartitionToCreate( model->device(), partition );
|
2014-07-15 17:37:04 +02:00
|
|
|
else
|
2014-07-17 09:49:15 +02:00
|
|
|
editExistingPartition( model->device(), partition );
|
2014-07-15 17:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-02 15:49:35 +02:00
|
|
|
void
|
|
|
|
PartitionPage::onDeleteClicked()
|
|
|
|
{
|
2014-07-02 17:55:53 +02:00
|
|
|
QModelIndex index = m_ui->partitionTreeView->currentIndex();
|
2014-07-02 15:49:35 +02:00
|
|
|
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 );
|
|
|
|
}
|
2014-07-15 17:37:04 +02:00
|
|
|
|
2015-12-30 16:37:57 +01:00
|
|
|
|
2014-07-24 19:28:53 +02:00
|
|
|
void
|
|
|
|
PartitionPage::onRevertClicked()
|
|
|
|
{
|
2015-12-31 13:56:19 +01:00
|
|
|
ScanningDialog::run(
|
|
|
|
QtConcurrent::run( [ this ]
|
|
|
|
{
|
|
|
|
QMutexLocker locker( &m_revertMutex );
|
|
|
|
|
|
|
|
int oldIndex = m_ui->deviceComboBox->currentIndex();
|
|
|
|
m_core->revertAllDevices();
|
|
|
|
m_ui->deviceComboBox->setCurrentIndex( oldIndex );
|
|
|
|
updateFromCurrentDevice();
|
|
|
|
} ),
|
2015-12-31 15:39:32 +01:00
|
|
|
[]{},
|
2015-12-31 13:56:19 +01:00
|
|
|
this );
|
2014-07-24 19:28:53 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 13:07:22 +02:00
|
|
|
void
|
|
|
|
PartitionPage::onPartitionViewActivated()
|
|
|
|
{
|
|
|
|
QModelIndex index = m_ui->partitionTreeView->currentIndex();
|
|
|
|
if ( !index.isValid() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const PartitionModel* model = static_cast< const PartitionModel* >( index.model() );
|
|
|
|
Q_ASSERT( model );
|
|
|
|
Partition* partition = model->partitionForIndex( index );
|
|
|
|
Q_ASSERT( partition );
|
|
|
|
|
|
|
|
// Use the buttons to trigger the actions so that they do nothing if they
|
|
|
|
// are disabled. Alternatively, the code could use QAction to centralize,
|
|
|
|
// but I don't expect there will be other occurences of triggering the same
|
|
|
|
// action from multiple UI elements in this page, so it does not feel worth
|
|
|
|
// the price.
|
2015-09-18 15:41:07 +02:00
|
|
|
if ( KPMHelpers::isPartitionFreeSpace( partition ) )
|
2014-07-25 13:07:22 +02:00
|
|
|
m_ui->createButton->click();
|
|
|
|
else
|
|
|
|
m_ui->editButton->click();
|
|
|
|
}
|
|
|
|
|
2014-07-15 17:37:04 +02:00
|
|
|
void
|
2014-07-16 16:20:58 +02:00
|
|
|
PartitionPage::updatePartitionToCreate( Device* device, Partition* partition )
|
2014-07-15 17:37:04 +02:00
|
|
|
{
|
|
|
|
QPointer<CreatePartitionDialog> dlg = new CreatePartitionDialog( device, partition->parent(), this );
|
2014-07-16 16:20:58 +02:00
|
|
|
dlg->initFromPartitionToCreate( partition );
|
2014-07-15 17:37:04 +02:00
|
|
|
if ( dlg->exec() == QDialog::Accepted )
|
|
|
|
{
|
2014-08-04 19:23:39 +02:00
|
|
|
Partition* newPartition = dlg->createPartition();
|
2014-07-15 17:37:04 +02:00
|
|
|
m_core->deletePartition( device, partition );
|
2016-03-08 16:26:20 +01:00
|
|
|
m_core->createPartition( device, newPartition, dlg->newFlags() );
|
2014-07-15 17:37:04 +02:00
|
|
|
}
|
|
|
|
delete dlg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-17 09:49:15 +02:00
|
|
|
PartitionPage::editExistingPartition( Device* device, Partition* partition )
|
2014-07-15 17:37:04 +02:00
|
|
|
{
|
2014-07-17 09:49:15 +02:00
|
|
|
QPointer<EditExistingPartitionDialog> dlg = new EditExistingPartitionDialog( device, partition, this );
|
|
|
|
if ( dlg->exec() == QDialog::Accepted )
|
|
|
|
dlg->applyChanges( m_core );
|
|
|
|
delete dlg;
|
2014-07-15 17:37:04 +02:00
|
|
|
}
|
2014-07-23 18:15:46 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
PartitionPage::updateBootLoaderInstallPath()
|
|
|
|
{
|
|
|
|
QVariant var = m_ui->bootLoaderComboBox->currentData( BootLoaderModel::BootLoaderPathRole );
|
|
|
|
if ( !var.isValid() )
|
|
|
|
return;
|
|
|
|
m_core->setBootLoaderInstallPath( var.toString() );
|
|
|
|
}
|
2014-07-24 19:26:19 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
PartitionPage::updateFromCurrentDevice()
|
|
|
|
{
|
|
|
|
QModelIndex index = m_core->deviceModel()->index( m_ui->deviceComboBox->currentIndex(), 0 );
|
|
|
|
if ( !index.isValid() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Device* device = m_core->deviceModel()->deviceForIndex( index );
|
|
|
|
|
2014-07-29 10:57:10 +02:00
|
|
|
QAbstractItemModel* oldModel = m_ui->partitionTreeView->model();
|
|
|
|
if ( oldModel )
|
|
|
|
disconnect( oldModel, 0, this, 0 );
|
|
|
|
|
2014-07-24 19:26:19 +02:00
|
|
|
PartitionModel* model = m_core->partitionModelForDevice( device );
|
2015-12-03 17:19:02 +01:00
|
|
|
m_ui->partitionBarsView->setModel( model );
|
2014-07-24 19:26:19 +02:00
|
|
|
m_ui->partitionTreeView->setModel( model );
|
2014-07-29 10:57:10 +02:00
|
|
|
m_ui->partitionTreeView->expandAll();
|
2014-07-24 19:26:19 +02:00
|
|
|
|
2015-12-30 17:25:48 +01:00
|
|
|
// Make both views use the same selection model.
|
|
|
|
if ( m_ui->partitionBarsView->selectionModel() !=
|
|
|
|
m_ui->partitionTreeView->selectionModel() )
|
|
|
|
{
|
|
|
|
QItemSelectionModel* selectionModel = m_ui->partitionTreeView->selectionModel();
|
|
|
|
m_ui->partitionTreeView->setSelectionModel( m_ui->partitionBarsView->selectionModel() );
|
|
|
|
selectionModel->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is necessary because even with the same selection model it might happen that
|
|
|
|
// a !=0 column is selected in the tree view, which for some reason doesn't trigger a
|
|
|
|
// timely repaint in the bars view.
|
|
|
|
connect( m_ui->partitionBarsView->selectionModel(), &QItemSelectionModel::currentChanged,
|
|
|
|
this, [=]
|
|
|
|
{
|
|
|
|
QModelIndex selectedIndex = m_ui->partitionBarsView->selectionModel()->currentIndex();
|
|
|
|
selectedIndex = selectedIndex.sibling( selectedIndex.row(), 0 );
|
|
|
|
m_ui->partitionBarsView->setCurrentIndex( selectedIndex );
|
|
|
|
}, Qt::UniqueConnection );
|
|
|
|
|
2014-07-24 19:26:19 +02:00
|
|
|
// 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
|
|
|
|
connect( m_ui->partitionTreeView->selectionModel(), &QItemSelectionModel::currentChanged,
|
|
|
|
[ this ]( const QModelIndex& index, const QModelIndex& oldIndex )
|
|
|
|
{
|
|
|
|
updateButtons();
|
|
|
|
} );
|
2014-07-29 10:57:10 +02:00
|
|
|
connect( model, &QAbstractItemModel::modelReset, this, &PartitionPage::onPartitionModelReset );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionPage::onPartitionModelReset()
|
|
|
|
{
|
|
|
|
m_ui->partitionTreeView->expandAll();
|
|
|
|
updateButtons();
|
2014-07-24 19:26:19 +02:00
|
|
|
}
|