geoip.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <QStringList>
00018
00019 #include "geoip.h"
00020
00021
00022 #define IS_VALID_LATITUDE(x) (((x) >= -90.0) && ((x) <= 90.0))
00023
00024 #define IS_VALID_LONGITUDE(x) (((x) >= -180.0) && ((x) <= 180.0))
00025
00026
00027
00028 GeoIp::GeoIp(QHostAddress ip)
00029 {
00030 _ip = ip;
00031 _latitude = _longitude = 0xFFFF;
00032 }
00033
00034
00035 GeoIp::GeoIp(QHostAddress ip, float latitude, float longitude,
00036 QString city, QString state, QString country)
00037 {
00038 _ip = ip;
00039 _latitude = latitude;
00040 _longitude = longitude;
00041 _city = city;
00042 _state = state;
00043 _country = country;
00044 }
00045
00046
00047
00048
00049
00050
00051 GeoIp
00052 GeoIp::fromString(QString geoip)
00053 {
00054
00055 QStringList data = geoip.split(",");
00056
00057 if (data.size() == 2 && data.at(1).toLower() == "unknown") {
00058 return GeoIp(QHostAddress(data.at(0)));
00059 } else if (data.size() < 6) {
00060 return GeoIp();
00061 }
00062
00063
00064 QHostAddress ip(data.at(0));
00065 QString city = data.at(1);
00066 QString state = data.at(2);
00067 QString country = data.at(3);
00068 float latitude = data.at(4).toFloat();
00069 float longitude = data.at(5).toFloat();
00070
00071
00072 return GeoIp(ip, latitude, longitude, city, state, country);
00073 }
00074
00075
00076 QString
00077 GeoIp::toString() const
00078 {
00079 QString s;
00080
00081 s.append(_ip.toString());
00082 s.append("," + _city);
00083 s.append("," + _state);
00084 s.append("," + _country);
00085 s.append("," + QString::number(_latitude, 'f', 4));
00086 s.append("," + QString::number(_longitude, 'f', 4));
00087 return s;
00088 }
00089
00090
00091 bool
00092 GeoIp::isEmpty() const
00093 {
00094 return (_ip.isNull() &&
00095 !IS_VALID_LATITUDE(_latitude) &&
00096 !IS_VALID_LONGITUDE(_longitude));
00097 }
00098
00099
00100
00101 bool
00102 GeoIp::isUnknown() const
00103 {
00104 return (!_ip.isNull() &&
00105 !IS_VALID_LATITUDE(_latitude) &&
00106 !IS_VALID_LONGITUDE(_longitude));
00107 }
00108
00109
00110 QString
00111 GeoIp::toLocation() const
00112 {
00113 QStringList location;
00114
00115
00116 if (!_city.isEmpty()) {
00117 location << _city;
00118 }
00119
00120 if (!_state.isEmpty()) {
00121
00122 bool valid = true;
00123 for (int i = 0; i < _state.length(); i++) {
00124 if (_state[i].isDigit()) {
00125 valid = false;
00126 break;
00127 }
00128 }
00129 if (valid) {
00130 location << _state;
00131 }
00132 }
00133
00134 if (!_country.isEmpty()) {
00135 location << _country;
00136 }
00137 return location.join(", ");
00138 }
00139