Vidalia 0.2.10

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.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 GeoIpRecord.cpp
00013 ** \version $Id: GeoIpRecord.cpp 4378 2010-08-05 20:28:54Z edmanm $
00014 ** \brief Associates an IP with a geographic location
00015 */
00016 
00017 #include "GeoIpRecord.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 GeoIpRecord::GeoIpRecord()
00028 {
00029   _latitude  = 0.0;
00030   _longitude = 0.0;
00031 }
00032 
00033 GeoIpRecord::GeoIpRecord(const QHostAddress &ip, float latitude, float longitude,
00034                          const QString &country, const QString &countryCode)
00035 {
00036   _ip = ip;
00037   _latitude = latitude;
00038   _longitude = longitude;
00039   _country  = country;
00040   _countryCode = countryCode;
00041 }
00042 
00043 GeoIpRecord::GeoIpRecord(const QHostAddress &ip, float latitude, float longitude,
00044                          const QString &city, const QString &region,
00045                          const QString &country, const QString &countryCode) 
00046 {
00047   _ip = ip;
00048   _latitude = latitude;
00049   _longitude = longitude;
00050   _city = city;
00051   _region = region;
00052   _country = country;
00053   _countryCode = countryCode;
00054 }
00055 
00056 bool
00057 GeoIpRecord::isValid() const
00058 {
00059   return (! _ip.isNull()
00060             && IS_VALID_LATITUDE(_latitude)
00061             && IS_VALID_LONGITUDE(_longitude));
00062 }
00063 
00064 QString
00065 GeoIpRecord::toString() const
00066 {
00067   QStringList location;
00068 
00069   /* Add the city name (if present) */
00070   if (!_city.isEmpty())
00071     location << _city;
00072 
00073   /* Add the full state or region name (if present) */
00074   if (!_region.isEmpty() && _region != _city)
00075       location << _region;
00076 
00077   /* Add the country name or the country code (if present) */
00078   if (!_country.isEmpty())
00079     location << _country;
00080   else if (!_countryCode.isEmpty())
00081     location << _countryCode;
00082 
00083   return location.join(", ");
00084 }
00085