Move LVM LV setup code to the base class.

The same checks (e.g. hide LVM LV name edit line) are also needed
in the other children of SizeDialogBase.
This commit is contained in:
Andrius Štikonas 2017-10-28 18:35:53 +01:00
parent 0b1780397e
commit f62edf052c
4 changed files with 54 additions and 46 deletions

View File

@ -23,7 +23,6 @@
#include <core/partition.h>
#include <core/device.h>
#include <core/lvmdevice.h>
#include <fs/filesystemfactory.h>
#include <fs/luks.h>
@ -49,8 +48,7 @@
NewDialog::NewDialog(QWidget* parent, Device& device, Partition& unallocatedPartition, PartitionRole::Roles r) :
SizeDialogBase(parent, device, unallocatedPartition, unallocatedPartition.firstSector(), unallocatedPartition.lastSector()),
m_PartitionRoles(r),
m_IsValidPassword(true),
m_IsValidLVName(true)
m_IsValidPassword(true)
{
setWindowTitle(xi18nc("@title:window", "Create a new partition"));
@ -103,27 +101,6 @@ void NewDialog::setupDialog()
dialogWidget().checkBoxEncrypt().hide();
dialogWidget().editPassphrase().hide();
if (device().type() == Device::Disk_Device) {
dialogWidget().lvName().hide();
dialogWidget().textLVName().hide();
}
if (device().type() == Device::LVM_Device) {
dialogWidget().hideBeforeAndAfter();
detailsWidget().checkAlign().setChecked(false);
detailsWidget().checkAlign().setEnabled(false);
detailsButton->hide();
dialogWidget().comboFileSystem().removeItem(dialogWidget().comboFileSystem().findText(QStringLiteral("lvm2 pv")));
m_IsValidLVName = false;
/* LVM logical volume name can consist of: letters numbers _ . - +
* It cannot start with underscore _ and must not be equal to . or .. or any entry in /dev/
* QLineEdit accepts QValidator::Intermediate, so we just disable . at the beginning */
QRegularExpression re(QStringLiteral(R"(^(?!_|\.)[\w\-.+]+)"));
QRegularExpressionValidator *validator = new QRegularExpressionValidator(re, this);
dialogWidget().lvName().setValidator(validator);
}
dialogWidget().editPassphrase().setMinimumPasswordLength(1);
dialogWidget().editPassphrase().setMaximumPasswordLength(512); // cryptsetup does not support longer passwords
@ -148,7 +125,6 @@ void NewDialog::setupConnections()
connect(&dialogWidget().label(), &QLineEdit::textChanged, this, &NewDialog::onLabelChanged);
// listen to password status updates
connect(&dialogWidget().editPassphrase(), &KNewPasswordWidget::passwordStatusChanged, this, &NewDialog::slotPasswordStatusChanged);
connect(&dialogWidget().lvName(), &QLineEdit::textChanged, this, &NewDialog::onLVNameChanged);
SizeDialogBase::setupConnections();
}
@ -265,20 +241,6 @@ void NewDialog::slotPasswordStatusChanged()
updateOkButtonStatus();
}
void NewDialog::onLVNameChanged(const QString& newName)
{
partition().setPartitionPath(device().deviceNode() + QStringLiteral("/") + newName.trimmed());
if ((dialogWidget().lvName().isVisible() &&
dialogWidget().lvName().text().isEmpty()) ||
(device().type() == Device::LVM_Device &&
dynamic_cast<LvmDevice&>(device()).partitionNodes().contains(partition().partitionPath())) ) {
m_IsValidLVName = false;
} else {
m_IsValidLVName = true;
}
updateOkButtonStatus();
}
void NewDialog::updateHideAndShow()
{
// this is mostly copy'n'pasted from PartPropsDialog::updateHideAndShow()

View File

@ -44,13 +44,12 @@ protected:
void onRoleChanged(bool);
void onFilesystemChanged(int);
void onLabelChanged(const QString& newLabel);
void onLVNameChanged(const QString& newName);
void setupConnections() override;
void setupDialog() override;
void slotPasswordStatusChanged();
void updateHideAndShow();
void updateOkButtonStatus();
void updateOkButtonStatus() override;
void updateFileSystem(FileSystem::Type t);
PartitionRole::Roles partitionRoles() const {
return m_PartitionRoles;
@ -66,14 +65,10 @@ protected:
bool isValidPassword() const {
return m_IsValidPassword;
}
bool isValidLVName() const {
return m_IsValidLVName;
}
private:
PartitionRole::Roles m_PartitionRoles;
bool m_IsValidPassword;
bool m_IsValidLVName;
};
#endif

View File

@ -25,6 +25,7 @@
#include <core/partitiontable.h>
#include <core/device.h>
#include <core/lvmdevice.h>
#include <core/partition.h>
#include <core/partitionalignment.h>
@ -56,7 +57,8 @@ SizeDialogBase::SizeDialogBase(QWidget* parent, Device& d, Partition& part, qint
m_MinimumFirstSector(minFirst),
m_MaximumLastSector(maxLast),
m_MinimumLength(-1),
m_MaximumLength(-1)
m_MaximumLength(-1),
m_IsValidLVName(true)
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
setLayout(mainLayout);
@ -102,6 +104,26 @@ void SizeDialogBase::setupDialog()
else
dialogWidget().partResizerWidget().init(device(), partition(), minimumFirstSector(), maximumLastSector(), true, canMove());
dialogWidget().partResizerWidget().setAlign(Config::alignDefault());
if (device().type() == Device::Disk_Device) {
dialogWidget().lvName().hide();
dialogWidget().textLVName().hide();
}
if (device().type() == Device::LVM_Device) {
dialogWidget().hideBeforeAndAfter();
detailsWidget().checkAlign().setChecked(false);
detailsWidget().checkAlign().setEnabled(false);
detailsButton->hide();
dialogWidget().comboFileSystem().removeItem(dialogWidget().comboFileSystem().findText(QStringLiteral("lvm2 pv")));
m_IsValidLVName = false;
/* LVM logical volume name can consist of: letters numbers _ . - +
* It cannot start with underscore _ and must not be equal to . or .. or any entry in /dev/
* QLineEdit accepts QValidator::Intermediate, so we just disable . at the beginning */
QRegularExpression re(QStringLiteral(R"(^(?!_|\.)[\w\-.+]+)"));
QRegularExpressionValidator *validator = new QRegularExpressionValidator(re, this);
dialogWidget().lvName().setValidator(validator);
}
}
void SizeDialogBase::setupConstraints()
@ -158,6 +180,8 @@ void SizeDialogBase::setupConnections()
connect(&detailsWidget().spinFirstSector(), qOverload<double>(&QDoubleSpinBox::valueChanged), this, &SizeDialogBase::onSpinFirstSectorChanged);
connect(&detailsWidget().spinLastSector(), qOverload<double>(&QDoubleSpinBox::valueChanged), this, &SizeDialogBase::onSpinLastSectorChanged);
connect(&detailsWidget().checkAlign(), &QCheckBox::toggled, this, &SizeDialogBase::onAlignToggled);
connect(&dialogWidget().lvName(), &QLineEdit::textChanged, this, &SizeDialogBase::onLVNameChanged);
}
void SizeDialogBase::toggleDetails()
@ -352,6 +376,25 @@ void SizeDialogBase::onAlignToggled(bool align)
}
}
void SizeDialogBase::onLVNameChanged(const QString& newName)
{
partition().setPartitionPath(device().deviceNode() + QStringLiteral("/") + newName.trimmed());
if ((dialogWidget().lvName().isVisible() &&
dialogWidget().lvName().text().isEmpty()) ||
(device().type() == Device::LVM_Device &&
dynamic_cast<LvmDevice&>(device()).partitionNodes().contains(partition().partitionPath())) ) {
m_IsValidLVName = false;
} else {
m_IsValidLVName = true;
}
updateOkButtonStatus();
}
void SizeDialogBase::updateOkButtonStatus()
{
okButton->setEnabled(isValidLVName());
}
void SizeDialogBase::updateSpinFreeBefore(qint64 sectorsFreeBefore)
{
const bool signalState = dialogWidget().spinFreeBefore().blockSignals(true);

View File

@ -114,6 +114,10 @@ protected:
m_MaximumLength = s;
}
bool isValidLVName() const {
return m_IsValidLVName;
}
protected:
void onResizerWidgetFirstSectorChanged(qint64 newFirst);
void onResizerWidgetLastSectorChanged(qint64 newLast);
@ -126,6 +130,9 @@ protected:
void onSpinLastSectorChanged(qint64 newLast);
void onAlignToggled(bool);
void onLVNameChanged(const QString& newName);
virtual void updateOkButtonStatus();
void toggleDetails();
SizeDialogWidget* m_SizeDialogWidget;
@ -136,6 +143,7 @@ protected:
qint64 m_MaximumLastSector;
qint64 m_MinimumLength;
qint64 m_MaximumLength;
bool m_IsValidLVName;
public:
QPushButton* okButton;