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 GeoIpCacheItem.h 00013 ** \version $Id: GeoIpCacheItem.h 3768 2009-05-13 19:07:26Z edmanm $ 00014 ** \brief Cached result of a single IP-to-geolocation result 00015 */ 00016 00017 #ifndef _GEOIPCACHEITEM_H 00018 #define _GEOIPCACHEITEM_H 00019 00020 #include <QHash> 00021 #include <QString> 00022 #include <QVariant> 00023 #include <QDateTime> 00024 00025 class GeoIp; 00026 class QHostAddress; 00027 00028 00029 class GeoIpCacheItem 00030 { 00031 public: 00032 /** Default constructor 00033 */ 00034 GeoIpCacheItem(); 00035 00036 /** Constructor. 00037 */ 00038 GeoIpCacheItem(const QHostAddress &from, const QHostAddress &to, 00039 const GeoIp &geoIp, const QDateTime &expires); 00040 00041 /** Returns the first IP address in the range of IP addresses associated 00042 * with this GeoIpCacheItem. 00043 */ 00044 QHostAddress ipRangeStart() const; 00045 00046 /** Returns the last IP address in the range of IP addresses associated 00047 * with this GeoIpCacheItem. 00048 */ 00049 QHostAddress ipRangeEnd() const; 00050 00051 /** Returns true if <b>ip</b> is within the range of IP addresses associated 00052 * with this GeoIpCacheItem. 00053 * \sa ipRangeStart() 00054 * \sa ipRangeEnd() 00055 */ 00056 bool contains(const QHostAddress &ip) const; 00057 00058 /** Returns true if this GeoIpCacheItem object contains valid values for 00059 * all required fields. 00060 */ 00061 bool isValid() const; 00062 00063 /** Returns true if the cache item is too old to be considered accurate. 00064 * Cached GeoIp responses are considered valid for thirty days after they 00065 * are first added to the cache. 00066 */ 00067 bool isExpired() const; 00068 00069 /** Returns a GeoIp object for <b>ip</b>, populated with the cached 00070 * geographic information stored by this GeoIpCacheObject. If <b>ip</b> 00071 * is not within the range of IP addresses associated with this object, 00072 * an empty GeoIp object is returned. 00073 * \sa contains 00074 */ 00075 GeoIp toGeoIp(const QHostAddress &ip) const; 00076 00077 /** Formats the fields contained in this GeoIpCacheItem as a string 00078 * suitable for writing to a cache file. 00079 */ 00080 QString toCacheString() const; 00081 00082 /** Parses <b>cacheLine</b> and constructs a new GeoIpCacheItem object 00083 * with the parsed values. The format of <b>cacheLine</b> must follow the 00084 * format as produced by toCacheString(). 00085 * \sa toCacheString() 00086 */ 00087 static GeoIpCacheItem fromCacheString(const QString &cacheLine); 00088 00089 private: 00090 quint32 _fromIp; 00091 quint32 _toIp; 00092 QDateTime _expires; /**< Time this item was cached. */ 00093 QHash<QString,QVariant> _fields; 00094 }; 00095 00096 #endif 00097