geoipcacheitem.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 geoipcacheitem.cpp
00013 ** \version $Id: geoipcacheitem.cpp 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief Cached result of a single IP-to-geolocation result
00015 */
00016 
00017 #include <QStringList>
00018 
00019 #include "geoipcacheitem.h"
00020 
00021 
00022 /** Constructor */
00023 GeoIpCacheItem::GeoIpCacheItem(GeoIp geoip, QDateTime timestamp)
00024 {
00025   _geoip = geoip;
00026   _timestamp = timestamp;
00027 }
00028 
00029 /** Returns true if this cache item is empty and invalid. A valid cache item
00030  * must have a valid GeoIp object and timestamp. */
00031 bool
00032 GeoIpCacheItem::isEmpty() const
00033 {
00034   return (_geoip.isEmpty() || _timestamp.isNull());
00035 }
00036 
00037 /** Returns a string representing the contents of this cache item, suitable
00038  * for writing to disk. The format is as in the following example:
00039  *                     <Geo IP Data>:<Timestamp>
00040  */
00041 QString
00042 GeoIpCacheItem::toString() const
00043 {
00044   return _geoip.toString() + ":" + QString::number(_timestamp.toTime_t());
00045 }
00046 
00047 /** Returns a GeoIpCacheItem from a string as read from the cache that was
00048  * written to disk. The format is:
00049  *                     <Geo IP Data>[:<Timestamp>]
00050  *
00051  * If no value for Timestamp is given, the current date and time will be used.
00052  * If the string cannot be parsed for valid cached GeoIP data, then an empty
00053  * GeoIpCacheItem object is returned. The calling method should call isEmpty()
00054  * on the returned GeoIpCacheItem object to ensure it got a valid object.
00055  */
00056 GeoIpCacheItem
00057 GeoIpCacheItem::fromString(QString cacheString)
00058 {
00059   QDateTime timestamp;
00060   QStringList cacheData = cacheString.split(":");
00061 
00062   if (cacheData.size() >= 1) {
00063     GeoIp geoip = GeoIp::fromString(cacheData.at(0));
00064     if (cacheData.size() >= 2)
00065       timestamp.setTime_t(cacheData.at(1).toUInt());
00066     else
00067       timestamp = QDateTime::currentDateTime();
00068     return GeoIpCacheItem(geoip, timestamp);
00069   }
00070   return GeoIpCacheItem();
00071 }
00072 
00073 /** Returns true if the cache item is too old to be considered valid. Normal
00074  * cached responses are valid for one month. Cached UNKNOWN responses are
00075  * considered valid for one week. */
00076 bool
00077 GeoIpCacheItem::isExpired() const
00078 {
00079   if (_geoip.isUnknown()) {
00080     return (_timestamp.addDays(7) < QDateTime::currentDateTime());
00081   }
00082   return (_timestamp.addMonths(1) < QDateTime::currentDateTime()); 
00083 }
00084 

Generated on Sat Aug 16 17:31:48 2008 for Vidalia by  doxygen 1.5.6