addressmap.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 addressmap.cpp
00013 ** \version $Id: bandwidthevent.h 1563 2006-12-26 06:06:04Z edmanm $
00014 ** \brief Stores a list of address mappings and their expiration times
00015 */
00016 
00017 #include <QStringList>
00018 #include "tcglobal.h"
00019 #include "addressmap.h"
00020 
00021 /** Format of expiry times in address map events. */
00022 #define DATE_FMT "\"yyyy-MM-dd HH:mm:ss\""
00023 
00024 
00025 /** Adds a new address mapping from the address <b>from</b> to the address
00026  * <b>to</b>, that expires at <b>expires</b>. */
00027 void
00028 AddressMap::add(QString from, QString to, QDateTime expires)
00029 {
00030   tc::debug("New address mapping: %1 -> %2 (expires %3)").arg(from)
00031                                                           .arg(to)
00032                           .arg(expires.isValid() ? expires.toString(DATE_FMT)
00033                                                  : "never");
00034   insert(from, addr_map_entry_t(to, expires));
00035 }
00036 
00037 /** Adds a new address mapping by parsing the fields in <b>mapping</b>, which
00038  * should be formatted as follows: 
00039  *
00040  *   Address SP Address SP Expiry
00041  *   Expiry = DQUOTE ISOTime DQUOTE / "NEVER"
00042  */
00043 void
00044 AddressMap::add(QString mapping)
00045 {
00046   QStringList parts = mapping.split(" ");
00047   if (parts.size() >= 2) {
00048     QDateTime expires;
00049     if (parts.size() >= 4 && parts.at(2) != "NEVER")
00050       expires = QDateTime::fromString(parts.at(2) + " " + parts.at(3),
00051                                       DATE_FMT);
00052     add(parts.at(0), parts.at(1), expires);
00053   }
00054 }
00055 
00056 /** Returns true if <b>entry</b> is expired; false otherwise. */
00057 bool
00058 AddressMap::isExpired(addr_map_entry_t entry) const
00059 {
00060   if (entry.second.isValid())
00061     return (entry.second < QDateTime::currentDateTime());
00062   return false; /* No expiry time == valid forever */
00063 }
00064 
00065 /** Returns true if there exists a mapping for <b>addr</b> and that mapping is
00066  * not expired. */
00067 bool
00068 AddressMap::isMapped(QString addr) const
00069 {
00070   return (contains(addr) && !isExpired(value(addr)));
00071 }
00072 
00073 /** Returns the address to which <b>addr</b> is currently mapped. If there is
00074  * no mapping for <b>addr</b> (or the mapping is expired), then an empty
00075  * string is returned. */
00076 QString
00077 AddressMap::mappedTo(QString addr) const
00078 {
00079   addr_map_entry_t entry = value(addr);
00080   return (isExpired(entry) ? QString() : entry.first);
00081 }
00082 
00083 /** Returns the reverse of this address map by swapping each address in the
00084  * address map with its mapped address. The expiration times are unaltered. */
00085 AddressMap
00086 AddressMap::reverse() const
00087 {
00088   AddressMap reverseMap;
00089   foreach (QString from, keys()) {
00090     /* Flip the "from" and the "to" addresses and retain the expiry time. */
00091     addr_map_entry_t entry = value(from);
00092     reverseMap.add(entry.first, from, entry.second);
00093   }
00094   return reverseMap;
00095 }
00096 

Generated on Wed Nov 26 21:02:38 2008 for Vidalia by  doxygen 1.5.6