addressmap.cpp

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

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3