Changeset View
Changeset View
Standalone View
Standalone View
libs/editor/simpleipv4addressvalidator.cpp
Show All 17 Lines | |||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "simpleipv4addressvalidator.h" | 21 | #include "simpleipv4addressvalidator.h" | ||
22 | 22 | | |||
23 | #include <QStringList> | 23 | #include <QStringList> | ||
24 | #include <QVector> | 24 | #include <QVector> | ||
25 | 25 | | |||
26 | SimpleIpV4AddressValidator::SimpleIpV4AddressValidator(QObject *parent) | 26 | SimpleIpV4AddressValidator::SimpleIpV4AddressValidator(QObject *parent, AddressStyle style) | ||
27 | : QValidator(parent) | 27 | : QValidator(parent) | ||
28 | , m_addressStyle(style) | ||||
28 | { | 29 | { | ||
30 | switch (style) { | ||||
31 | case Base: | ||||
32 | m_validator.setRegularExpression(QRegularExpression(QLatin1String("[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}"))); | ||||
33 | break; | ||||
34 | case WithCidr: | ||||
35 | m_validator.setRegularExpression(QRegularExpression(QLatin1String("([0-9]{1,3}\\.){3,3}[0-9]{1,3}/[0-9]{1,2}"))); | ||||
36 | break; | ||||
37 | case WithPort: | ||||
38 | m_validator.setRegularExpression(QRegularExpression(QLatin1String("([0-9]{1,3}\\.){3,3}[0-9]{1,3}:[0-9]{1,5}"))); | ||||
39 | break; | ||||
40 | } | ||||
29 | } | 41 | } | ||
30 | 42 | | |||
31 | SimpleIpV4AddressValidator::~SimpleIpV4AddressValidator() | 43 | SimpleIpV4AddressValidator::~SimpleIpV4AddressValidator() | ||
32 | { | 44 | { | ||
33 | } | 45 | } | ||
34 | 46 | | |||
35 | QValidator::State SimpleIpV4AddressValidator::validate(QString &address, int &pos) const | 47 | QValidator::State SimpleIpV4AddressValidator::validate(QString &address, int &pos) const | ||
36 | { | 48 | { | ||
37 | if (QValidator::Invalid == checkWithInputMask(address, pos)) { | 49 | QValidator::State maskResult = checkWithInputMask(address, pos); | ||
50 | if (QValidator::Invalid == maskResult) { | ||||
38 | return QValidator::Invalid; | 51 | return QValidator::Invalid; | ||
39 | } | 52 | } | ||
40 | 53 | | |||
41 | // this list will be filled with tetrad values. It can be used to make | 54 | // this list will be filled with tetrad values. It can be used to make | ||
42 | // some additional correctness checks on the last validation step. | 55 | // some additional correctness checks on the last validation step. | ||
43 | QList<int> tetrads; | 56 | QList<int> tetrads; | ||
44 | 57 | | |||
45 | return checkTetradsRanges(address, tetrads); | 58 | QValidator::State tetradResult = checkTetradsRanges(address, tetrads); | ||
59 | if (QValidator::Invalid == tetradResult) | ||||
60 | return QValidator::Invalid; | ||||
61 | else if (QValidator::Intermediate == tetradResult || QValidator::Intermediate == maskResult) | ||||
62 | return QValidator::Intermediate; | ||||
63 | else | ||||
64 | return QValidator::Acceptable; | ||||
46 | } | 65 | } | ||
47 | 66 | | |||
48 | QValidator::State SimpleIpV4AddressValidator::checkWithInputMask(QString &value, int &pos) const | 67 | QValidator::State SimpleIpV4AddressValidator::checkWithInputMask(QString &value, int &pos) const | ||
49 | { | 68 | { | ||
50 | QRegExpValidator v(QRegExp(QLatin1String("[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}")), nullptr); | 69 | return m_validator.validate(value, pos); | ||
51 | | ||||
52 | return v.validate(value, pos); | | |||
53 | } | 70 | } | ||
54 | 71 | | |||
55 | QValidator::State SimpleIpV4AddressValidator::checkTetradsRanges(QString &value, QList<int> &tetrads) const | 72 | QValidator::State SimpleIpV4AddressValidator::checkTetradsRanges(QString &value, QList<int> &tetrads) const | ||
56 | { | 73 | { | ||
57 | QStringList temp; | 74 | QStringList temp; | ||
58 | const QVector<QStringRef> addrParts = value.splitRef(QLatin1Char('.')); | 75 | QVector<QStringRef> addrParts; | ||
76 | QStringList cidrParts; | ||||
77 | QStringList portParts; | ||||
78 | | ||||
79 | switch (m_addressStyle) { | ||||
80 | case Base: | ||||
81 | addrParts = value.splitRef(QLatin1Char('.')); | ||||
82 | break; | ||||
83 | | ||||
84 | case WithCidr: | ||||
85 | cidrParts = value.split(QLatin1Char('/')); | ||||
86 | addrParts = cidrParts[0].splitRef(QLatin1Char('.')); | ||||
87 | break; | ||||
88 | | ||||
89 | case WithPort: | ||||
90 | portParts = value.split(QLatin1Char(':')); | ||||
91 | addrParts = portParts[0].splitRef(QLatin1Char('.')); | ||||
92 | break; | ||||
93 | } | ||||
94 | | ||||
59 | int i = 0; | 95 | int i = 0; | ||
60 | // fill in the list with invalid values | 96 | // fill in the list with invalid values | ||
61 | tetrads << -1 << -1 << -1 << -1; | 97 | tetrads << -1 << -1 << -1 << -1; | ||
62 | 98 | | |||
63 | // lets check address parts | 99 | // lets check address parts | ||
64 | Q_FOREACH (const QStringRef &part, addrParts) { | 100 | for (const QStringRef &part : addrParts) { | ||
pino: while this change is OK, please do not mix it together with this patch | |||||
65 | if (part.isEmpty()) { | 101 | if (part.isEmpty()) { | ||
66 | if (i != (addrParts.size() - 1)) { | 102 | if (i != (addrParts.size() - 1)) { | ||
67 | // qCDebug(PLASMA_NM) << "part.isEmpty()"; | 103 | // qCDebug(PLASMA_NM) << "part.isEmpty()"; | ||
68 | return QValidator::Invalid; | 104 | return QValidator::Invalid; | ||
69 | } | 105 | } | ||
70 | // the last tetrad can be empty, continue... | 106 | // the last tetrad can be empty, continue... | ||
71 | return QValidator::Intermediate; | 107 | return QValidator::Intermediate; | ||
72 | } | 108 | } | ||
Show All 15 Lines | |||||
88 | value = temp.join(QLatin1String(".")); | 124 | value = temp.join(QLatin1String(".")); | ||
89 | 125 | | |||
90 | if (i < 4) { | 126 | if (i < 4) { | ||
91 | // not all tetrads are filled... continue | 127 | // not all tetrads are filled... continue | ||
92 | // qCDebug(PLASMA_NM) << "QValidator::Intermediate"; | 128 | // qCDebug(PLASMA_NM) << "QValidator::Intermediate"; | ||
93 | return QValidator::Intermediate; | 129 | return QValidator::Intermediate; | ||
94 | } | 130 | } | ||
95 | else { | 131 | else { | ||
132 | if (m_addressStyle == WithCidr) { | ||||
133 | if (cidrParts.size() > 1) { | ||||
134 | value += QLatin1String("/"); | ||||
135 | if (!cidrParts[1].isEmpty()) { | ||||
136 | int cidrValue = cidrParts[1].toInt(); | ||||
137 | if (cidrValue > 32) { | ||||
138 | return QValidator::Invalid; | ||||
139 | } else { | ||||
140 | value += cidrParts[1]; | ||||
141 | return QValidator::Acceptable; | ||||
142 | } | ||||
143 | } | ||||
144 | else { | ||||
145 | return QValidator::Intermediate; | ||||
146 | } | ||||
147 | } | ||||
148 | } else if (m_addressStyle == WithPort) { | ||||
149 | if (portParts.size() > 1) { | ||||
150 | value += QLatin1String(":"); | ||||
151 | if (!portParts[1].isEmpty()) { | ||||
152 | int portValue = portParts[1].toInt(); | ||||
153 | if (portValue > 65535) { | ||||
154 | return QValidator::Invalid; | ||||
155 | } else { | ||||
156 | value += portParts[1]; | ||||
157 | return QValidator::Acceptable; | ||||
158 | } | ||||
159 | } | ||||
160 | else { | ||||
161 | return QValidator::Intermediate; | ||||
162 | } | ||||
163 | } | ||||
164 | } | ||||
165 | | ||||
96 | // qCDebug(PLASMA_NM) << "QValidator::Acceptable"; | 166 | // qCDebug(PLASMA_NM) << "QValidator::Acceptable"; | ||
97 | return QValidator::Acceptable; | 167 | return QValidator::Acceptable; | ||
98 | } | 168 | } | ||
99 | } | 169 | } |
while this change is OK, please do not mix it together with this patch