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 GeoIpCache.h 00013 ** \version $Id: GeoIpCache.h 3768 2009-05-13 19:07:26Z edmanm $ 00014 ** \brief Caches the results of previous GeoIP requests 00015 */ 00016 00017 #ifndef _GEOIPCACHE_H 00018 #define _GEOIPCACHE_H 00019 00020 #include "GeoIpCacheItem.h" 00021 00022 #include <QObject> 00023 #include <QMap> 00024 00025 class GeoIp; 00026 class QString; 00027 class QHostAddress; 00028 00029 typedef QMap<quint32, GeoIpCacheItem> GeoIpCacheMap; 00030 00031 00032 class GeoIpCache : public QObject 00033 { 00034 Q_OBJECT 00035 00036 public: 00037 /** Default constructor. */ 00038 GeoIpCache(QObject *parent = 0); 00039 00040 /** Writes the current cache to disk. Returns true if the cache file was 00041 * successfully saved to disk. Otherwise, returns false and sets 00042 * <b>errmsg</b> to a string describing the error encountered, if 00043 * <b>errmsg</b> is not null. 00044 */ 00045 bool saveToDisk(QString *errmsg = 0); 00046 00047 /** Reads the cache in from disk. Returns true if the cache file was 00048 * successfully read. Otherwise, returns false and sets <b>errmsg</b> to 00049 * a string describing the error encountered, if <b>errmsg</b> is not null. 00050 */ 00051 bool loadFromDisk(QString *errmsg = 0); 00052 00053 /** Returns the location currently used for the cache file. 00054 */ 00055 QString cacheFileName() const; 00056 00057 /** Caches the geographic information in <b>geoip</b> associated with a 00058 * single IP address. 00059 */ 00060 void addToCache(const GeoIp &geoip); 00061 00062 /** Caches the geographic information in <b>geoip</b> associated with a 00063 * range of IP addresses, from <b>from</b> to <b>to</b> (inclusive). 00064 */ 00065 void addToCache(const QHostAddress &from, const QHostAddress &to, 00066 const GeoIp &geoip); 00067 00068 /** Returns a GeoIp object for the given <b>ip</b> from cache. If no cached 00069 * information is known for <b>ip</b>, an empty GeoIp object is returned. 00070 */ 00071 GeoIp geoIpForAddress(const QHostAddress &ip); 00072 00073 /** Returns true if the cache contains geographic location information for 00074 * <b>ip</b>. Otherwise, returns false. 00075 */ 00076 bool contains(const QHostAddress &ip); 00077 00078 private: 00079 /** Adds the GeoIpCacheItem <b>ci</b> to the cache. */ 00080 void addToCache(const GeoIpCacheItem &ci); 00081 00082 /**< List of cached GeoIp objects. */ 00083 GeoIpCacheMap _cache; 00084 }; 00085 00086 #endif 00087