Vidalia  0.2.15
GeoIpRecord.cpp
Go to the documentation of this file.
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.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file GeoIpRecord.cpp
00013 ** \brief Associates an IP with a geographic location
00014 */
00015 
00016 #include "GeoIpRecord.h"
00017 
00018 #include <QStringList>
00019 
00020 /** Verifies a latitude is between -90.0 and 90.0 degrees. */
00021 #define IS_VALID_LATITUDE(x)    (((x) >= -90.0) && ((x) <= 90.0))
00022 /** Verifies a longitude is between -180.0 and 180.0 degrees. */
00023 #define IS_VALID_LONGITUDE(x)   (((x) >= -180.0) && ((x) <= 180.0))
00024 
00025 
00026 GeoIpRecord::GeoIpRecord()
00027 {
00028   _latitude  = 0.0;
00029   _longitude = 0.0;
00030 }
00031 
00032 GeoIpRecord::GeoIpRecord(const QHostAddress &ip, float latitude, float longitude,
00033                          const QString &country, const QString &countryCode)
00034 {
00035   _ip = ip;
00036   _latitude = latitude;
00037   _longitude = longitude;
00038   _country  = country;
00039   _countryCode = countryCode;
00040 }
00041 
00042 GeoIpRecord::GeoIpRecord(const QHostAddress &ip, float latitude, float longitude,
00043                          const QString &city, const QString &region,
00044                          const QString &country, const QString &countryCode) 
00045 {
00046   _ip = ip;
00047   _latitude = latitude;
00048   _longitude = longitude;
00049   _city = city;
00050   _region = region;
00051   _country = country;
00052   _countryCode = countryCode;
00053 }
00054 
00055 bool
00056 GeoIpRecord::isValid() const
00057 {
00058   return (! _ip.isNull()
00059             && IS_VALID_LATITUDE(_latitude)
00060             && IS_VALID_LONGITUDE(_longitude));
00061 }
00062 
00063 QString
00064 GeoIpRecord::toString() const
00065 {
00066   QStringList location;
00067 
00068   /* Add the city name (if present) */
00069   if (!_city.isEmpty())
00070     location << _city;
00071 
00072   /* Add the full state or region name (if present) */
00073   if (!_region.isEmpty() && _region != _city)
00074       location << _region;
00075 
00076   /* Add the country name or the country code (if present) */
00077   if (!_country.isEmpty())
00078     location << _country;
00079   else if (!_countryCode.isEmpty())
00080     location << _countryCode;
00081 
00082   return location.join(", ");
00083 }
00084