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