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 1238 2006-09-25 17:50:57Z 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 a string representing the contents of this cache item, suitable 00041 * for writing to disk. The format is as in the following example: 00042 * <Geo IP Data>:<Timestamp> 00043 */ 00044 QString 00045 GeoIpCacheItem::toString() const 00046 { 00047 return _geoip.toString() + ":" + QString::number(_timestamp.toTime_t()); 00048 } 00049 00050 /** Returns a GeoIpCacheItem from a string as read from the cache that was 00051 * written to disk. The format is: 00052 * <Geo IP Data>:<Timestamp> 00053 */ 00054 GeoIpCacheItem 00055 GeoIpCacheItem::fromString(QString cacheString) 00056 { 00057 QDateTime timestamp; 00058 QStringList cacheData = cacheString.split(":"); 00059 00060 GeoIp geoip = GeoIp::fromString(cacheData.at(0)); 00061 timestamp.setTime_t(cacheData.at(1).toUInt()); 00062 return GeoIpCacheItem(geoip, timestamp); 00063 } 00064 00065 /** Returns true if the cache item is too old to be considered valid. */ 00066 bool 00067 GeoIpCacheItem::isExpired() const 00068 { 00069 /* Consider cache entries to be valid for one month. */ 00070 return (_timestamp.addMonths(1) < QDateTime::currentDateTime()); 00071 } 00072