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 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Associates an IP with a geographic location
00026  */
00027 
00028 #include <QStringList>
00029 
00030 #include "geoip.h"
00031 
00032 
00033 /** Constructor. */
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 /** Parses the GeoIp information from a comma-delimited string. The format of
00046  * the string is as in the following example:
00047  *
00048  *      128.213.48.13,Troy,NY,US,42.7495,-73.5951,1138402852
00049  */
00050 GeoIp
00051 GeoIp::fromString(QString geoip)
00052 {
00053   /* Split comma-delimited data fields */
00054   QStringList data = geoip.split(",");
00055   if (data.size() < 6) {
00056     return GeoIp();
00057   }
00058   
00059   /* Parse the data from the string */
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   /* Create a new GeoIp object with the parsed data. */
00068   return GeoIp(ip, latitude, longitude, city, state, country);
00069 }
00070 
00071 /** Formats the GeoIp information as a comma-delimited string. */
00072 QString
00073 GeoIp::toString() const
00074 {
00075   QString s;
00076   /* Assemble and comma-delimit the data fields */
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 /** Returns true if the GeoIp object is invalid. */
00087 bool
00088 GeoIp::isEmpty() const
00089 {
00090   return (_ip.isNull() && !_latitude && !_longitude);
00091 }
00092 
00093 /** Returns a human-readable string of GeoIp location information. */
00094 QString
00095 GeoIp::toLocation() const
00096 {
00097   QStringList location;
00098   
00099   /* Add the city name (if present) */
00100   if (!_city.isEmpty()) {
00101     location << _city;
00102   }
00103   /* Add the state or region name (if present) */
00104   if (!_state.isEmpty()) {
00105     /* Only display non-numeric region codes. */
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   /* Add the country code (if present) */
00118   if (!_country.isEmpty()) {
00119     location << _country;
00120   }
00121   return location.join(", ");
00122 }
00123 

Generated on Mon Oct 23 20:08:15 2006 for Vidalia by  doxygen 1.5.0