00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.vidalia-project.net/. No part of Vidalia, including this file, 00007 ** may be copied, modified, propagated, or distributed except according to the 00008 ** terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file GeoIp.cpp 00013 ** \version $Id: GeoIp.cpp 3768 2009-05-13 19:07:26Z edmanm $ 00014 ** \brief Associates an IP with a geographic location 00015 */ 00016 00017 #include "GeoIp.h" 00018 00019 #include <QStringList> 00020 00021 /** Verifies a latitude is between -90.0 and 90.0 degrees. */ 00022 #define IS_VALID_LATITUDE(x) (((x) >= -90.0) && ((x) <= 90.0)) 00023 /** Verifies a longitude is between -180.0 and 180.0 degrees. */ 00024 #define IS_VALID_LONGITUDE(x) (((x) >= -180.0) && ((x) <= 180.0)) 00025 00026 00027 GeoIp::GeoIp() 00028 { 00029 _latitude = 0.0; 00030 _longitude = 0.0; 00031 } 00032 00033 GeoIp::GeoIp(const QHostAddress &ip, float latitude, float longitude, 00034 const QString &city, const QString ®ion, 00035 const QString &country, const QString &countryCode) 00036 { 00037 _ip = ip; 00038 _latitude = latitude; 00039 _longitude = longitude; 00040 _city = city; 00041 _region = region; 00042 _country = country; 00043 _countryCode = countryCode; 00044 } 00045 00046 bool 00047 GeoIp::isValid() const 00048 { 00049 return (! _ip.isNull() 00050 && IS_VALID_LATITUDE(_latitude) 00051 && IS_VALID_LONGITUDE(_longitude)); 00052 } 00053 00054 QString 00055 GeoIp::toString() const 00056 { 00057 QStringList location; 00058 00059 /* Add the city name (if present) */ 00060 if (!_city.isEmpty()) 00061 location << _city; 00062 00063 /* Add the full state or region name (if present) */ 00064 if (!_region.isEmpty() && _region != _city) 00065 location << _region; 00066 00067 /* Add the country name or the country code (if present) */ 00068 if (!_country.isEmpty()) 00069 location << _country; 00070 else if (!_countryCode.isEmpty()) 00071 location << _countryCode; 00072 00073 return location.join(", "); 00074 } 00075