geoip.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /**
00023  * \file geoip.cpp
00024  * \version $Id: geoip.cpp 1563 2006-12-26 06:06:04Z edmanm $
00025  * \brief Associates an IP with a geographic location
00026  */
00027 
00028 #include <QStringList>
00029 
00030 #include "geoip.h"
00031 
00032 /** Verifies a latitude is between -90.0 and 90.0 degrees. */
00033 #define IS_VALID_LATITUDE(x)    (((x) >= -90.0) && ((x) <= 90.0))
00034 /** Verifies a longitude is between -180.0 and 180.0 degrees. */
00035 #define IS_VALID_LONGITUDE(x)   (((x) >= -180.0) && ((x) <= 180.0))
00036 
00037 
00038 /** Constructor */
00039 GeoIp::GeoIp(QHostAddress ip)
00040 {
00041   _ip = ip;
00042   _latitude = _longitude = 0xFFFF;
00043 }
00044 
00045 /** Constructor. */
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 /** Parses the GeoIp information from a comma-delimited string. The format of
00058  * the string is as in the following example:
00059  *
00060  *      128.213.48.13,Troy,NY,US,42.7495,-73.5951,1138402852
00061  */
00062 GeoIp
00063 GeoIp::fromString(QString geoip)
00064 {
00065   /* Split comma-delimited data fields */
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   /* Parse the data from the string */
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   /* Create a new GeoIp object with the parsed data. */
00083   return GeoIp(ip, latitude, longitude, city, state, country);
00084 }
00085 
00086 /** Formats the GeoIp information as a comma-delimited string. */
00087 QString
00088 GeoIp::toString() const
00089 {
00090   QString s;
00091   /* Assemble and comma-delimit the data fields */
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 /** Returns true if the GeoIp object is invalid. */
00102 bool
00103 GeoIp::isEmpty() const
00104 {
00105   return (_ip.isNull() && 
00106           !IS_VALID_LATITUDE(_latitude) && 
00107           !IS_VALID_LONGITUDE(_longitude));
00108 }
00109 
00110 /** Returns true if the GeoIp object is valid, but no location information
00111  * is known for the associated IP address. */
00112 bool
00113 GeoIp::isUnknown() const
00114 {
00115   return (!_ip.isNull() && 
00116           !IS_VALID_LATITUDE(_latitude) && 
00117           !IS_VALID_LONGITUDE(_longitude));
00118 }
00119 
00120 /** Returns a human-readable string of GeoIp location information. */
00121 QString
00122 GeoIp::toLocation() const
00123 {
00124   QStringList location;
00125   
00126   /* Add the city name (if present) */
00127   if (!_city.isEmpty()) {
00128     location << _city;
00129   }
00130   /* Add the state or region name (if present) */
00131   if (!_state.isEmpty()) {
00132     /* Only display non-numeric region codes. */
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   /* Add the country code (if present) */
00145   if (!_country.isEmpty()) {
00146     location << _country;
00147   }
00148   return location.join(", ");
00149 }
00150 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3