Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit bdf111ba authored by Ivan Čukić's avatar Ivan Čukić 👁

Showing error messages for badly chosen paths

parent 42ad78f2
......@@ -87,6 +87,7 @@ bool DialogModule::isValid() const
void DialogModule::init(const PlasmaVault::Vault::Payload &payload)
{
Q_UNUSED(payload);
}
......
......@@ -35,35 +35,58 @@ public:
class DirectoryValidator {
public:
bool requireNew;
bool requireEmptyDirectory;
bool requireExisting;
bool valid = false;
QString defaultPath;
std::function<void()> update;
DirectoryValidator(bool requireNew, bool requireExisting,
KMessageWidget* widget = nullptr;
DirectoryValidator(bool requireEmptyDirectory, bool requireExisting,
QString defaultPath,
std::function<void()> update)
: requireNew(requireNew)
: requireEmptyDirectory(requireEmptyDirectory)
, requireExisting(requireExisting)
, valid(!requireNew && !requireExisting)
, valid(!requireEmptyDirectory && !requireExisting)
, defaultPath(defaultPath)
, update(update)
{
}
bool isValid(const QUrl &url) const
{
if (url.isEmpty()) return false;
if (url.isEmpty()) {
widget->hide();
return false;
}
const bool directoryExists
= PlasmaVault::Backend::directoryExists(url.toLocalFile());
if (url.toLocalFile() == defaultPath) {
widget->hide();
return true;
}
QDir dir(url.toLocalFile());
if (requireNew && directoryExists) {
if (!dir.exists()) {
widget->setText(i18n("The specified path does not exist"));
widget->show();
return false;
}
if (requireExisting && !directoryExists) {
if (requireEmptyDirectory && !dir.isEmpty()) {
widget->setText(i18n("The specified directory is not empty"));
widget->show();
return false;
}
if (requireExisting && dir.isEmpty()) {
widget->setText(i18n("The specified directory is empty"));
widget->show();
return false;
}
widget->hide();
return true;
}
......@@ -97,13 +120,15 @@ public:
: flags(flags)
, q(parent)
, deviceValidator(
flags & RequireNewDevice,
flags & RequireEmptyDevice,
flags & RequireExistingDevice,
nullptr,
[&] { updateValidity(); }
)
, mountPointValidator(
flags & RequireNewMountPoint,
flags & RequireEmptyMountPoint,
flags & RequireExistingMountPoint,
nullptr,
[&] { updateValidity(); }
)
, allValid(deviceValidator.valid && mountPointValidator.valid)
......@@ -117,6 +142,9 @@ DirectoryPairChooserWidget::DirectoryPairChooserWidget(
{
d->ui.setupUi(this);
d->deviceValidator.widget = d->ui.messageDevice;
d->mountPointValidator.widget = d->ui.messageMountPoint;
if (!(flags & DirectoryPairChooserWidget::ShowDevicePicker)) {
d->ui.editDevice->setVisible(false);
d->ui.labelDevice->setVisible(false);
......@@ -127,6 +155,9 @@ DirectoryPairChooserWidget::DirectoryPairChooserWidget(
d->ui.labelMountPoint->setVisible(false);
}
d->ui.messageDevice->hide();
d->ui.messageMountPoint->hide();
connect(d->ui.editDevice, &KUrlRequester::textEdited,
this, [&] () {
d->deviceValidator.updateFor(d->ui.editDevice->url());
......@@ -173,6 +204,9 @@ void DirectoryPairChooserWidget::init(
d->ui.editDevice->setText(path);
d->ui.editMountPoint->setText(QDir::homePath() + QStringLiteral("/Vaults/") + name);
d->deviceValidator.defaultPath = path;
d->mountPointValidator.defaultPath = QDir::homePath() + QStringLiteral("/Vaults/") + name;
}
d->deviceValidator.updateFor(d->ui.editDevice->url());
......
......@@ -31,9 +31,9 @@ public:
enum Flag {
ShowDevicePicker = 1,
ShowMountPointPicker = 2,
RequireNewDevice = 4,
RequireEmptyDevice = 4,
RequireExistingDevice = 8,
RequireNewMountPoint = 16,
RequireEmptyMountPoint = 16,
RequireExistingMountPoint = 32,
AutoFillPaths = 64
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>653</width>
<height>82</height>
<height>166</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
......@@ -25,20 +25,65 @@
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="labelMountPoint">
<property name="text">
<string>Mount point</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="KUrlRequester" name="editMountPoint">
<property name="mode">
<set>KFile::Directory|KFile::LocalOnly</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="KMessageWidget" name="messageDevice">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="closeButtonVisible">
<bool>false</bool>
</property>
<property name="messageType">
<enum>KMessageWidget::Error</enum>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="KMessageWidget" name="messageMountPoint">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="closeButtonVisible">
<bool>false</bool>
</property>
<property name="messageType">
<enum>KMessageWidget::Error</enum>
</property>
</widget>
</item>
<item row="4" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
......@@ -47,6 +92,11 @@
<extends>QWidget</extends>
<header>kurlrequester.h</header>
</customwidget>
<customwidget>
<class>KMessageWidget</class>
<extends>QFrame</extends>
<header>kmessagewidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
......
......@@ -43,6 +43,7 @@ NameChooserWidget::NameChooserWidget()
connect(d->ui.editVaultName, &QLineEdit::textChanged,
this, [this] (const QString &text) {
Q_UNUSED(text);
setIsValid(!d->ui.editVaultName->text().isEmpty());
});
}
......
......@@ -84,7 +84,7 @@ public:
step { directoryPairChooser(
DirectoryPairChooserWidget::AutoFillPaths |
DirectoryPairChooserWidget::ShowMountPointPicker |
DirectoryPairChooserWidget::RequireNewMountPoint
DirectoryPairChooserWidget::RequireEmptyMountPoint
) },
step {
activitiesChooser(),
......@@ -115,8 +115,8 @@ public:
DirectoryPairChooserWidget::AutoFillPaths |
DirectoryPairChooserWidget::ShowDevicePicker |
DirectoryPairChooserWidget::ShowMountPointPicker |
DirectoryPairChooserWidget::RequireNewDevice |
DirectoryPairChooserWidget::RequireNewMountPoint
DirectoryPairChooserWidget::RequireEmptyDevice |
DirectoryPairChooserWidget::RequireEmptyMountPoint
) },
step {
cryfsCypherChooser(),
......
......@@ -67,7 +67,7 @@ public:
DirectoryPairChooserWidget::ShowDevicePicker |
DirectoryPairChooserWidget::ShowMountPointPicker |
DirectoryPairChooserWidget::RequireExistingDevice |
DirectoryPairChooserWidget::RequireNewMountPoint
DirectoryPairChooserWidget::RequireEmptyMountPoint
) },
step { passwordChooser() },
step {
......@@ -83,7 +83,7 @@ public:
DirectoryPairChooserWidget::ShowDevicePicker |
DirectoryPairChooserWidget::ShowMountPointPicker |
DirectoryPairChooserWidget::RequireExistingDevice |
DirectoryPairChooserWidget::RequireNewMountPoint
DirectoryPairChooserWidget::RequireEmptyMountPoint
) },
step { passwordChooser() },
step {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment