Commit addbfad3 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Optionally make the validators accept empty values

This makes it possible to use the validators if name, email or comment
are not required to have a non-empty value.

GnuPG-bug-id: 5916
parent 1a2b258b
......@@ -25,6 +25,21 @@ using namespace Kleo;
namespace
{
template<class Validator>
class EmptyIsAcceptableValidator : public Validator
{
public:
using Validator::Validator;
QValidator::State validate(QString &str, int &pos) const override
{
if (str.trimmed().isEmpty()) {
return QValidator::Acceptable;
}
return Validator::validate(str, pos);
}
};
class EMailValidator : public QValidator
{
public:
......@@ -40,38 +55,59 @@ public:
}
};
QValidator *regularExpressionValidator(Validation::Flags flags, const QString &regexp, QObject *parent)
{
if (flags & Validation::Required) {
return new QRegularExpressionValidator{QRegularExpression{regexp}, parent};
} else {
return new EmptyIsAcceptableValidator<QRegularExpressionValidator>{QRegularExpression{regexp}, parent};
}
}
}
QValidator *Validation::email(QObject *parent)
QValidator *Validation::email(Flags flags, QObject *parent)
{
return new EMailValidator(parent);
if (flags & Required) {
return new EMailValidator{parent};
} else {
return new EmptyIsAcceptableValidator<EMailValidator>{parent};
}
}
QValidator *Validation::email(const QString &addRX, QObject *parent)
QValidator *Validation::email(const QString &addRX, Flags flags, QObject *parent)
{
return new MultiValidator{email(), new QRegularExpressionValidator{QRegularExpression{addRX}, nullptr}, parent};
return new MultiValidator{email(flags), regularExpressionValidator(flags, addRX, nullptr), parent};
}
QValidator *Validation::pgpName(QObject *parent)
QValidator *Validation::pgpName(Flags flags, QObject *parent)
{
// this regular expression is modeled after gnupg/g10/keygen.c:ask_user_id:
static const auto name_rx = QRegularExpression{QStringLiteral("[^0-9<>][^<>@]{3,}")};
return new QRegularExpressionValidator{name_rx, parent};
if (flags & Required) {
return new QRegularExpressionValidator{name_rx, parent};
} else {
return new EmptyIsAcceptableValidator<QRegularExpressionValidator>{name_rx, parent};
}
}
QValidator *Validation::pgpName(const QString &addRX, QObject *parent)
QValidator *Validation::pgpName(const QString &addRX, Flags flags, QObject *parent)
{
return new MultiValidator{pgpName(), new QRegularExpressionValidator{QRegularExpression{addRX}, nullptr}, parent};
return new MultiValidator{pgpName(flags), regularExpressionValidator(flags, addRX, nullptr), parent};
}
QValidator *Validation::pgpComment(QObject *parent)
QValidator *Validation::pgpComment(Flags flags, QObject *parent)
{
// this regular expression is modeled after gnupg/g10/keygen.c:ask_user_id:
static const auto comment_rx = QRegularExpression{QStringLiteral("[^()]*")};
return new QRegularExpressionValidator{comment_rx, parent};
if (flags & Required) {
return new QRegularExpressionValidator{comment_rx, parent};
} else {
return new EmptyIsAcceptableValidator<QRegularExpressionValidator>{comment_rx, parent};
}
}
QValidator *Validation::pgpComment(const QString &addRX, QObject *parent)
QValidator *Validation::pgpComment(const QString &addRX, Flags flags, QObject *parent)
{
return new MultiValidator{pgpComment(), new QRegularExpressionValidator{QRegularExpression{addRX}, nullptr}, parent};
return new MultiValidator{pgpComment(flags), regularExpressionValidator(flags, addRX, nullptr), parent};
}
......@@ -18,14 +18,18 @@ namespace Kleo
namespace Validation
{
QValidator *email(QObject *parent = nullptr);
QValidator *pgpName(QObject *parent = nullptr);
QValidator *pgpComment(QObject *parent = nullptr);
enum Flags {
Optional,
Required
};
QValidator *email(const QString &additionalRegExp, QObject *parent = nullptr);
QValidator *pgpName(const QString &additionalRegExp, QObject *parent = nullptr);
QValidator *pgpComment(const QString &additionalRegExp, QObject *parent = nullptr);
QValidator *email(Flags flags = Required, QObject *parent = nullptr);
QValidator *pgpName(Flags flags = Required, QObject *parent = nullptr);
QValidator *pgpComment(Flags flags = Required, QObject *parent = nullptr);
QValidator *email(const QString &additionalRegExp, Flags flags = Required, QObject *parent = nullptr);
QValidator *pgpName(const QString &additionalRegExp, Flags flags = Required, QObject *parent = nullptr);
QValidator *pgpComment(const QString &additionalRegExp, Flags flags = Required, QObject *parent = nullptr);
}
}
Supports Markdown
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