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 | if (QValidator::Invalid == checkWithInputMask(address, pos)) { | ||
38 | return QValidator::Invalid; | 50 | return QValidator::Invalid; | ||
39 | } | 51 | } | ||
40 | 52 | | |||
41 | // this list will be filled with tetrad values. It can be used to make | 53 | // this list will be filled with tetrad values. It can be used to make | ||
42 | // some additional correctness checks on the last validation step. | 54 | // some additional correctness checks on the last validation step. | ||
43 | QList<int> tetrads; | 55 | QList<int> tetrads; | ||
44 | 56 | | |||
45 | return checkTetradsRanges(address, tetrads); | 57 | return checkTetradsRanges(address, tetrads); | ||
46 | } | 58 | } | ||
47 | 59 | | |||
48 | QValidator::State SimpleIpV4AddressValidator::checkWithInputMask(QString &value, int &pos) const | 60 | QValidator::State SimpleIpV4AddressValidator::checkWithInputMask(QString &value, int &pos) const | ||
49 | { | 61 | { | ||
50 | QRegExpValidator v(QRegExp(QLatin1String("[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}\\.[0-9, ]{1,3}")), nullptr); | 62 | return m_validator.validate(value, pos); | ||
51 | | ||||
52 | return v.validate(value, pos); | | |||
53 | } | 63 | } | ||
54 | 64 | | |||
55 | QValidator::State SimpleIpV4AddressValidator::checkTetradsRanges(QString &value, QList<int> &tetrads) const | 65 | QValidator::State SimpleIpV4AddressValidator::checkTetradsRanges(QString &value, QList<int> &tetrads) const | ||
56 | { | 66 | { | ||
57 | QStringList temp; | 67 | QStringList temp; | ||
58 | const QVector<QStringRef> addrParts = value.splitRef(QLatin1Char('.')); | 68 | QVector<QStringRef> addrParts; | ||
69 | QStringList cidrParts; | ||||
70 | QStringList portParts; | ||||
71 | | ||||
72 | switch (m_addressStyle) { | ||||
73 | case Base: | ||||
74 | addrParts = value.splitRef(QLatin1Char('.')); | ||||
75 | break; | ||||
76 | | ||||
77 | case WithCidr: | ||||
78 | cidrParts = value.split(QLatin1Char('/')); | ||||
79 | addrParts = cidrParts[0].splitRef(QLatin1Char('.')); | ||||
80 | break; | ||||
81 | | ||||
82 | case WithPort: | ||||
83 | portParts = value.split(QLatin1Char(':')); | ||||
84 | addrParts = portParts[0].splitRef(QLatin1Char('.')); | ||||
85 | break; | ||||
86 | } | ||||
87 | | ||||
59 | int i = 0; | 88 | int i = 0; | ||
60 | // fill in the list with invalid values | 89 | // fill in the list with invalid values | ||
61 | tetrads << -1 << -1 << -1 << -1; | 90 | tetrads << -1 << -1 << -1 << -1; | ||
62 | 91 | | |||
63 | // lets check address parts | 92 | // lets check address parts | ||
64 | Q_FOREACH (const QStringRef &part, addrParts) { | 93 | for (const QStringRef &part : addrParts) { | ||
pino: while this change is OK, please do not mix it together with this patch | |||||
65 | if (part.isEmpty()) { | 94 | if (part.isEmpty()) { | ||
66 | if (i != (addrParts.size() - 1)) { | 95 | if (i != (addrParts.size() - 1)) { | ||
67 | // qCDebug(PLASMA_NM) << "part.isEmpty()"; | 96 | // qCDebug(PLASMA_NM) << "part.isEmpty()"; | ||
68 | return QValidator::Invalid; | 97 | return QValidator::Invalid; | ||
69 | } | 98 | } | ||
70 | // the last tetrad can be empty, continue... | 99 | // the last tetrad can be empty, continue... | ||
71 | return QValidator::Intermediate; | 100 | return QValidator::Intermediate; | ||
72 | } | 101 | } | ||
Show All 15 Lines | |||||
88 | value = temp.join(QLatin1String(".")); | 117 | value = temp.join(QLatin1String(".")); | ||
89 | 118 | | |||
90 | if (i < 4) { | 119 | if (i < 4) { | ||
91 | // not all tetrads are filled... continue | 120 | // not all tetrads are filled... continue | ||
92 | // qCDebug(PLASMA_NM) << "QValidator::Intermediate"; | 121 | // qCDebug(PLASMA_NM) << "QValidator::Intermediate"; | ||
93 | return QValidator::Intermediate; | 122 | return QValidator::Intermediate; | ||
94 | } | 123 | } | ||
95 | else { | 124 | else { | ||
125 | if (m_addressStyle == WithCidr) { | ||||
126 | if (cidrParts.size() > 1) { | ||||
127 | value += QLatin1String("/"); | ||||
128 | if (!cidrParts[1].isEmpty()) { | ||||
129 | int cidrValue = cidrParts[1].toInt(); | ||||
130 | if (cidrValue > 32) { | ||||
131 | return QValidator::Invalid; | ||||
132 | } else { | ||||
133 | value += cidrParts[1]; | ||||
134 | return QValidator::Acceptable; | ||||
135 | } | ||||
136 | } | ||||
137 | else { | ||||
138 | return QValidator::Intermediate; | ||||
139 | } | ||||
140 | } | ||||
141 | } else if (m_addressStyle == WithPort) { | ||||
142 | if (portParts.size() > 1) { | ||||
143 | value += QLatin1String(":"); | ||||
144 | if (!portParts[1].isEmpty()) { | ||||
145 | int portValue = portParts[1].toInt(); | ||||
146 | if (portValue > 65535) { | ||||
147 | return QValidator::Invalid; | ||||
148 | } else { | ||||
149 | value += portParts[1]; | ||||
150 | return QValidator::Acceptable; | ||||
151 | } | ||||
152 | } | ||||
153 | else { | ||||
154 | return QValidator::Intermediate; | ||||
155 | } | ||||
156 | } | ||||
157 | } | ||||
158 | | ||||
96 | // qCDebug(PLASMA_NM) << "QValidator::Acceptable"; | 159 | // qCDebug(PLASMA_NM) << "QValidator::Acceptable"; | ||
97 | return QValidator::Acceptable; | 160 | return QValidator::Acceptable; | ||
98 | } | 161 | } | ||
99 | } | 162 | } |
while this change is OK, please do not mix it together with this patch