Libkleo

validation.cpp
1/* -*- mode: c++; c-basic-offset:4 -*-
2 This file is part of libkleopatra
3 SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
4 SPDX-FileCopyrightText: 2022 g10 Code GmbH
5 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include "validation.h"
11
12#include "multivalidator_p.h"
13#include <libkleo_debug.h>
14
15#include <QValidator>
16
17#include <KEmailAddress>
18
19#include <QRegularExpression>
20
21using namespace Kleo;
22
23namespace
24{
25
26template<class Validator>
27class TrimmingValidator : public Validator
28{
29public:
30 using Validator::Validator;
31
32 QValidator::State validate(QString &str, int &pos) const override
33 {
34 auto trimmed = str.trimmed();
35 auto posCopy = pos;
36 return Validator::validate(trimmed, posCopy);
37 }
38};
39
40template<class Validator>
41class EmptyIsAcceptableValidator : public Validator
42{
43public:
44 using Validator::Validator;
45
46 QValidator::State validate(QString &str, int &pos) const override
47 {
48 if (str.isEmpty()) {
50 }
51 return Validator::validate(str, pos);
52 }
53};
54
55class EMailValidator : public QValidator
56{
57public:
58 EMailValidator()
59 : QValidator{}
60 {
61 }
62
63 State validate(QString &str, int &pos) const override
64 {
65 Q_UNUSED(pos)
67 return Acceptable;
68 }
69 return Intermediate;
70 }
71};
72
73std::shared_ptr<QValidator> regularExpressionValidator(Validation::Flags flags, const QString &regexp)
74{
75 if (flags & Validation::Required) {
76 return std::make_shared<TrimmingValidator<QRegularExpressionValidator>>(QRegularExpression{regexp});
77 } else {
78 return std::make_shared<TrimmingValidator<EmptyIsAcceptableValidator<QRegularExpressionValidator>>>(QRegularExpression{regexp});
79 }
80}
81
82}
83
84std::shared_ptr<QValidator> Validation::email(Flags flags)
85{
86 if (flags & Required) {
87 return std::make_shared<TrimmingValidator<EMailValidator>>();
88 } else {
89 return std::make_shared<TrimmingValidator<EmptyIsAcceptableValidator<EMailValidator>>>();
90 }
91}
92
93std::shared_ptr<QValidator> Validation::email(const QString &addRX, Flags flags)
94{
95 return MultiValidator::create({email(flags), regularExpressionValidator(flags, addRX)});
96}
97
98std::shared_ptr<QValidator> Validation::pgpName(Flags flags)
99{
100 // this regular expression is modeled after gnupg/g10/keygen.c:ask_user_id:
101 static const QString name_rx{QLatin1StringView{"[^0-9<>][^<>@]{4,}"}};
102 return regularExpressionValidator(flags, name_rx);
103}
104
105std::shared_ptr<QValidator> Validation::pgpName(const QString &addRX, Flags flags)
106{
107 return MultiValidator::create({pgpName(flags), regularExpressionValidator(flags, addRX)});
108}
109
110std::shared_ptr<QValidator> Validation::simpleName(Flags flags)
111{
112 static const QString name_rx{QLatin1StringView{"[^<>@]*"}};
113 return std::shared_ptr<QValidator>{regularExpressionValidator(flags, name_rx)};
114}
115
116std::shared_ptr<QValidator> Validation::simpleName(const QString &additionalRegExp, Flags flags)
117{
118 return MultiValidator::create({simpleName(flags), regularExpressionValidator(flags, additionalRegExp)});
119}
KCODECS_EXPORT bool isValidSimpleAddress(const QString &aStr)
bool isEmpty() const const
QString trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:00:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.