Avoid creating a new partition with a used mountpoint.

We get the mountpoints already used by other partitions, and
disable the Ok button in the "Create new partition" dialog if
the user selects/writes a mountpoint which is already used.

We are going to do the same in the Edit partition dialog
after testing.
This commit is contained in:
shainer 2016-11-20 22:05:55 +00:00
parent e5f5bb99d7
commit c8dbeb5341
5 changed files with 63 additions and 12 deletions

View File

@ -40,8 +40,9 @@
// Qt
#include <QComboBox>
#include <QDir>
#include <QSet>
#include <QListWidgetItem>
#include <QPushButton>
#include <QSet>
static QSet< FileSystem::Type > s_unmountableFS(
{
@ -52,12 +53,13 @@ static QSet< FileSystem::Type > s_unmountableFS(
FileSystem::Lvm2_PV
} );
CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget )
CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, const QStringList& usedMountPoints, QWidget* parentWidget )
: QDialog( parentWidget )
, m_ui( new Ui_CreatePartitionDialog )
, m_partitionSizeController( new PartitionSizeController( this ) )
, m_device( device )
, m_parent( parentPartition )
, m_usedMountPoints( usedMountPoints )
{
m_ui->setupUi( this );
m_ui->encryptWidget->setText( tr( "En&crypt" ) );
@ -101,6 +103,8 @@ CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* par
connect( m_ui->fsComboBox, SIGNAL( activated( int ) ), SLOT( updateMountPointUi() ) );
connect( m_ui->extendedRadioButton, SIGNAL( toggled( bool ) ), SLOT( updateMountPointUi() ) );
connect( m_ui->mountPointComboBox, &QComboBox::currentTextChanged, this, &CreatePartitionDialog::checkMountPointSelection );
// Select a default
m_ui->fsComboBox->setCurrentIndex( defaultFsIndex );
updateMountPointUi();
@ -252,6 +256,18 @@ CreatePartitionDialog::updateMountPointUi()
m_ui->mountPointComboBox->setCurrentText( QString() );
}
void
CreatePartitionDialog::checkMountPointSelection(const QString& selection)
{
if (m_usedMountPoints.contains(selection)) {
m_ui->labelMountPoint->setText("Mountpoint already used. Please select another one.");
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
} else {
m_ui->labelMountPoint->setText( QString() );
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
}
}
void
CreatePartitionDialog::initPartResizerWidget( Partition* partition )
{

View File

@ -42,7 +42,7 @@ class CreatePartitionDialog : public QDialog
{
Q_OBJECT
public:
CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget = nullptr );
CreatePartitionDialog( Device* device, PartitionNode* parentPartition, const QStringList& usedMountPoints, QWidget* parentWidget = nullptr );
~CreatePartitionDialog();
/**
@ -61,6 +61,7 @@ public:
private Q_SLOTS:
void updateMountPointUi();
void checkMountPointSelection(const QString &);
private:
void setupFlagsList();
@ -69,6 +70,7 @@ private:
Device* m_device;
PartitionNode* m_parent;
PartitionRole m_role = PartitionRole( PartitionRole::None );
QStringList m_usedMountPoints;
void initGptPartitionTypeUi();
void initMbrPartitionTypeUi();

View File

@ -108,7 +108,7 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>12</height>
<height>13</height>
</size>
</property>
</spacer>
@ -126,6 +126,9 @@
<item row="3" column="1">
<widget class="QComboBox" name="fsComboBox"/>
</item>
<item row="4" column="1">
<widget class="EncryptWidget" name="encryptWidget" native="true"/>
</item>
<item row="5" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
@ -162,14 +165,26 @@
</property>
</widget>
</item>
<item row="7" column="0">
<item row="7" column="1">
<widget class="QLabel" name="labelMountPoint">
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Flags:</string>
</property>
</widget>
</item>
<item row="7" column="1">
<item row="8" column="1">
<widget class="QListWidget" name="m_listFlags">
<property name="alternatingRowColors">
<bool>true</bool>
@ -182,7 +197,7 @@
</property>
</widget>
</item>
<item row="8" column="0">
<item row="9" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -195,9 +210,6 @@
</property>
</spacer>
</item>
<item row="4" column="1">
<widget class="EncryptWidget" name="encryptWidget" native="true"/>
</item>
</layout>
</item>
<item>

View File

@ -176,7 +176,7 @@ PartitionPage::onCreateClicked()
Partition* partition = model->partitionForIndex( index );
Q_ASSERT( partition );
QPointer<CreatePartitionDialog> dlg = new CreatePartitionDialog( model->device(), partition->parent(), this );
QPointer<CreatePartitionDialog> dlg = new CreatePartitionDialog( model->device(), partition->parent(), getCurrentUsedMountpoints(), this );
dlg->initFromFreeSpace( partition );
if ( dlg->exec() == QDialog::Accepted )
{
@ -265,7 +265,7 @@ PartitionPage::onPartitionViewActivated()
void
PartitionPage::updatePartitionToCreate( Device* device, Partition* partition )
{
QPointer<CreatePartitionDialog> dlg = new CreatePartitionDialog( device, partition->parent(), this );
QPointer<CreatePartitionDialog> dlg = new CreatePartitionDialog( device, partition->parent(), getCurrentUsedMountpoints(), this );
dlg->initFromPartitionToCreate( partition );
if ( dlg->exec() == QDialog::Accepted )
{
@ -375,3 +375,22 @@ PartitionPage::updateBootLoaderIndex()
m_ui->bootLoaderComboBox->setCurrentIndex( m_lastSelectedBootLoaderIndex );
}
}
QStringList
PartitionPage::getCurrentUsedMountpoints()
{
QModelIndex index = m_core->deviceModel()->index( m_ui->deviceComboBox->currentIndex(), 0 );
if ( !index.isValid() )
return QStringList();
Device* device = m_core->deviceModel()->deviceForIndex( index );
QStringList mountPoints;
for (Partition* partition : device->partitionTable()->children()) {
if (!partition->mountPoint().isEmpty()) {
mountPoints << partition->mountPoint();
}
}
return mountPoints;
}

View File

@ -62,6 +62,8 @@ private:
void updateFromCurrentDevice();
void updateBootLoaderIndex();
QStringList getCurrentUsedMountpoints();
QMutex m_revertMutex;
int m_lastSelectedBootLoaderIndex;
};