00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file geoipcacheitem.cpp 00024 * \version $Id: geoipcacheitem.cpp 1622 2007-01-30 02:18:24Z edmanm $ 00025 * \brief Cached result of a single IP-to-geolocation result 00026 */ 00027 00028 #include <QStringList> 00029 00030 #include "geoipcacheitem.h" 00031 00032 00033 /** Constructor */ 00034 GeoIpCacheItem::GeoIpCacheItem(GeoIp geoip, QDateTime timestamp) 00035 { 00036 _geoip = geoip; 00037 _timestamp = timestamp; 00038 } 00039 00040 /** Returns true if this cache item is empty and invalid. A valid cache item 00041 * must have a valid GeoIp object and timestamp. */ 00042 bool 00043 GeoIpCacheItem::isEmpty() const 00044 { 00045 return (_geoip.isEmpty() || _timestamp.isNull()); 00046 } 00047 00048 /** Returns a string representing the contents of this cache item, suitable 00049 * for writing to disk. The format is as in the following example: 00050 * <Geo IP Data>:<Timestamp> 00051 */ 00052 QString 00053 GeoIpCacheItem::toString() const 00054 { 00055 return _geoip.toString() + ":" + QString::number(_timestamp.toTime_t()); 00056 } 00057 00058 /** Returns a GeoIpCacheItem from a string as read from the cache that was 00059 * written to disk. The format is: 00060 * <Geo IP Data>[:<Timestamp>] 00061 * 00062 * If no value for Timestamp is given, the current date and time will be used. 00063 * If the string cannot be parsed for valid cached GeoIP data, then an empty 00064 * GeoIpCacheItem object is returned. The calling method should call isEmpty() 00065 * on the returned GeoIpCacheItem object to ensure it got a valid object. 00066 */ 00067 GeoIpCacheItem 00068 GeoIpCacheItem::fromString(QString cacheString) 00069 { 00070 QDateTime timestamp; 00071 QStringList cacheData = cacheString.split(":"); 00072 00073 if (cacheData.size() >= 1) { 00074 GeoIp geoip = GeoIp::fromString(cacheData.at(0)); 00075 if (cacheData.size() >= 2) 00076 timestamp.setTime_t(cacheData.at(1).toUInt()); 00077 else 00078 timestamp = QDateTime::currentDateTime(); 00079 return GeoIpCacheItem(geoip, timestamp); 00080 } 00081 return GeoIpCacheItem(); 00082 } 00083 00084 /** Returns true if the cache item is too old to be considered valid. Normal 00085 * cached responses are valid for one month. Cached UNKNOWN responses are 00086 * considered valid for one week. */ 00087 bool 00088 GeoIpCacheItem::isExpired() const 00089 { 00090 if (_geoip.isUnknown()) { 00091 return (_timestamp.addDays(7) < QDateTime::currentDateTime()); 00092 } 00093 return (_timestamp.addMonths(1) < QDateTime::currentDateTime()); 00094 } 00095