| 1 | #include "IpValidator.h" |
| 2 | #include "HerculesStudio.h" |
| 3 | |
| 4 | #include <QLineEdit> |
| 5 | #include <QMessageBox> |
| 6 | |
| 7 | IpValidator::IpValidator(QWidget * parent) : |
| 8 | QDialog(parent) |
| 9 | { |
| 10 | } |
| 11 | |
| 12 | bool IpValidator::validateIp(QLineEdit * ipLineEdit, bool allowNull, bool quite) |
| 13 | { |
| 14 | const std::string ip = ipLineEdit->text().toStdString(); |
| 15 | bool ret = true; |
| 16 | if (allowNull && ip.compare("...") == 0) |
| 17 | return ret; |
| 18 | QString qip = ip.c_str(); |
| 19 | int pos=0; |
| 20 | for (int i=0; i<4 && ret; i++) |
| 21 | { |
| 22 | hOutDebug(5, "IP:" << ip << " pos:" << pos << " i=" << i << std::endl); |
| 23 | if (i>0) |
| 24 | { |
| 25 | if (qip.toLatin1().data()[pos] != '.') |
| 26 | { |
| 27 | ret = false; |
| 28 | break; |
| 29 | } |
| 30 | pos++; |
| 31 | } |
| 32 | int num = 0; |
| 33 | int ppos=pos; |
| 34 | while(ip[pos] >= '0' && ip[pos] <= '9') |
| 35 | { |
| 36 | num = num*10 + ip[pos] - '0'; |
| 37 | pos++; |
| 38 | } |
| 39 | if (pos == ppos || num > 255) |
| 40 | { |
| 41 | ret = false; |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | if (!ret) |
| 46 | { |
| 47 | if (!quite) |
| 48 | { |
| 49 | QString msg = ip.c_str(); |
| 50 | msg += " is an illegel IP address"; |
| 51 | QMessageBox::warning(this, "Illegal IP address", msg , QMessageBox::Ok); |
| 52 | ipLineEdit->setFocus(); |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | return true; |
| 57 | } |