Changeset View
Changeset View
Standalone View
Standalone View
libs/editor/simpleipv6addressvalidator.cpp
Show All 15 Lines | |||||
16 | 16 | | |||
17 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License | ||
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 "simpleipv6addressvalidator.h" | 21 | #include "simpleipv6addressvalidator.h" | ||
22 | 22 | | |||
23 | #include <QStringList> | 23 | #include <QStringList> | ||
24 | #include <QVector> | ||||
24 | 25 | | |||
25 | SimpleIpV6AddressValidator::SimpleIpV6AddressValidator(QObject *parent) | 26 | SimpleIpV6AddressValidator::SimpleIpV6AddressValidator(QObject *parent, enum AddressStyle style) | ||
pino: no need for "enum" | |||||
26 | : QValidator(parent) | 27 | : QValidator(parent) | ||
27 | { | 28 | { | ||
29 | addressStyle = style; | ||||
pino: in constructor initialization list | |||||
28 | } | 30 | } | ||
29 | 31 | | |||
30 | SimpleIpV6AddressValidator::~SimpleIpV6AddressValidator() | 32 | SimpleIpV6AddressValidator::~SimpleIpV6AddressValidator() | ||
31 | { | 33 | { | ||
32 | } | 34 | } | ||
33 | 35 | | |||
34 | QValidator::State SimpleIpV6AddressValidator::validate(QString &address, int &pos) const | 36 | QValidator::State SimpleIpV6AddressValidator::validate(QString &address, int &pos) const | ||
35 | { | 37 | { | ||
36 | if (QValidator::Invalid == checkWithInputMask(address, pos)) { | 38 | if (QValidator::Invalid == checkWithInputMask(address, pos)) { | ||
37 | return QValidator::Invalid; | 39 | return QValidator::Invalid; | ||
38 | } | 40 | } | ||
39 | 41 | | |||
40 | return checkTetradsRanges(address); | 42 | return checkTetradsRanges(address); | ||
41 | } | 43 | } | ||
42 | 44 | | |||
43 | QValidator::State SimpleIpV6AddressValidator::checkWithInputMask(QString &value, int &pos) const | 45 | QValidator::State SimpleIpV6AddressValidator::checkWithInputMask(QString &value, int &pos) const | ||
44 | { | 46 | { | ||
45 | QRegExpValidator v(QRegExp(QLatin1String("([0-9a-fA-F]{1,4}|:)+")), nullptr); | 47 | QRegExpValidator *v; | ||
46 | 48 | | |||
47 | return v.validate(value, pos); | 49 | switch (addressStyle) { | ||
50 | case Base: | ||||
51 | v = new QRegExpValidator(QRegExp(QLatin1String("([0-9a-fA-F]{1,4}|:)+")), nullptr); | ||||
52 | break; | ||||
53 | | ||||
54 | case WithCidr: | ||||
55 | v = new QRegExpValidator(QRegExp(QLatin1String("([0-9a-fA-F]{1,4}|:)+/[0-9]{1,3}")), nullptr); | ||||
56 | break; | ||||
57 | } | ||||
pino: as above, created every time, and leaked | |||||
58 | | ||||
59 | return v->validate(value, pos); | ||||
48 | } | 60 | } | ||
49 | 61 | | |||
50 | QValidator::State SimpleIpV6AddressValidator::checkTetradsRanges(QString &value) const | 62 | QValidator::State SimpleIpV6AddressValidator::checkTetradsRanges(QString &value) const | ||
51 | { | 63 | { | ||
52 | const QStringList addrParts = value.split(QLatin1Char(':')); | 64 | QStringList addrParts; | ||
65 | QStringList cidrParts; | ||||
66 | | ||||
67 | switch (addressStyle) { | ||||
68 | case Base: | ||||
69 | addrParts = value.split(QLatin1Char(':')); | ||||
70 | break; | ||||
71 | | ||||
72 | case WithCidr: | ||||
73 | cidrParts = value.split(QLatin1Char('/')); | ||||
74 | addrParts = cidrParts[0].split(QLatin1Char(':')); | ||||
75 | break; | ||||
76 | } | ||||
77 | | ||||
53 | int number = addrParts.size(); | 78 | int number = addrParts.size(); | ||
54 | if (number > 8) { | 79 | if (number > 8) { | ||
55 | return QValidator::Invalid; | 80 | return QValidator::Invalid; | ||
56 | } | 81 | } | ||
57 | 82 | | |||
58 | // lets check address parts | 83 | // lets check address parts | ||
59 | bool emptypresent = false; | 84 | bool emptypresent = false; | ||
60 | int i = 1; | 85 | int i = 1; | ||
61 | Q_FOREACH (QString part, addrParts) { // krazy:exclude=Q_FOREACH | 86 | Q_FOREACH (QString part, addrParts) { // krazy:exclude=Q_FOREACH | ||
62 | if (part.isEmpty() && i < number) { | 87 | if (part.isEmpty() && i < number) { | ||
63 | if (emptypresent) { | 88 | if (emptypresent && i != 2) { | ||
64 | // qCDebug(PLASMA_NM) << "part.isEmpty()"; | 89 | // qCDebug(PLASMA_NM) << "part.isEmpty()"; | ||
65 | return QValidator::Invalid; | 90 | return QValidator::Invalid; | ||
66 | } | 91 | } | ||
67 | else if (!emptypresent) | 92 | else if (!emptypresent) | ||
68 | { | 93 | { | ||
69 | part.setNum(0,16); | 94 | part.setNum(0,16); | ||
70 | emptypresent = true; | 95 | emptypresent = true; | ||
71 | } | 96 | } | ||
72 | } | 97 | } | ||
73 | i++; | 98 | i++; | ||
74 | 99 | | |||
75 | bool ok; | 100 | bool ok; | ||
76 | if (part.toInt(&ok, 16) > 65535) { | 101 | if (part.toInt(&ok, 16) > 65535) { | ||
77 | return QValidator::Invalid; | 102 | return QValidator::Invalid; | ||
78 | } | 103 | } | ||
79 | } | 104 | } | ||
80 | 105 | | |||
81 | if (number < 8 && !emptypresent) | 106 | if (number < 8 && !emptypresent) { | ||
82 | return QValidator::Intermediate; | 107 | return QValidator::Intermediate; | ||
108 | } | ||||
109 | else if (addressStyle == WithCidr && cidrParts.size() > 1) { | ||||
110 | if (cidrParts[1].isEmpty()) | ||||
111 | return QValidator::Intermediate; | ||||
112 | | ||||
113 | int cidrValue = cidrParts[1].toInt(); | ||||
114 | if (cidrValue > 128) | ||||
115 | return QValidator::Invalid; | ||||
116 | } | ||||
83 | 117 | | |||
84 | return QValidator::Acceptable; | 118 | return QValidator::Acceptable; | ||
85 | } | 119 | } |
no need for "enum"