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
00034 GeoIp::GeoIp(QHostAddress ip, float latitude, float longitude,
00035 QString city, QString state, QString country)
00036 {
00037 _ip = ip;
00038 _latitude = latitude;
00039 _longitude = longitude;
00040 _city = city;
00041 _state = state;
00042 _country = country;
00043 }
00044
00045
00046
00047
00048
00049
00050 GeoIp
00051 GeoIp::fromString(QString geoip)
00052 {
00053
00054 QStringList data = geoip.split(",");
00055 if (data.size() < 6) {
00056 return GeoIp();
00057 }
00058
00059
00060 QHostAddress ip(data.at(0));
00061 QString city = data.at(1);
00062 QString state = data.at(2);
00063 QString country = data.at(3);
00064 float latitude = data.at(4).toFloat();
00065 float longitude = data.at(5).toFloat();
00066
00067
00068 return GeoIp(ip, latitude, longitude, city, state, country);
00069 }
00070
00071
00072 QString
00073 GeoIp::toString() const
00074 {
00075 QString s;
00076
00077 s.append(_ip.toString());
00078 s.append("," + _city);
00079 s.append("," + _state);
00080 s.append("," + _country);
00081 s.append("," + QString::number(_latitude, 'f', 4));
00082 s.append("," + QString::number(_longitude, 'f', 4));
00083 return s;
00084 }
00085
00086
00087 bool
00088 GeoIp::isEmpty() const
00089 {
00090 return (_ip.isNull() && !_latitude && !_longitude);
00091 }
00092
00093
00094 QString
00095 GeoIp::toLocation() const
00096 {
00097 QStringList location;
00098
00099
00100 if (!_city.isEmpty()) {
00101 location << _city;
00102 }
00103
00104 if (!_state.isEmpty()) {
00105
00106 bool valid = true;
00107 for (int i = 0; i < _state.length(); i++) {
00108 if (_state[i].isDigit()) {
00109 valid = false;
00110 break;
00111 }
00112 }
00113 if (valid) {
00114 location << _state;
00115 }
00116 }
00117
00118 if (!_country.isEmpty()) {
00119 location << _country;
00120 }
00121 return location.join(", ");
00122 }
00123