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 circuit.cpp 00024 * \version $Id: circuit.cpp 1563 2006-12-26 06:06:04Z edmanm $ 00025 * \brief Object representing a Tor circuit 00026 */ 00027 00028 #include <QStringList> 00029 00030 #include "circuit.h" 00031 00032 00033 /** Default constructor. */ 00034 Circuit::Circuit() 00035 { 00036 _circId = 0; 00037 _status = Unknown; 00038 _path = QString(); 00039 } 00040 00041 /** Constructor */ 00042 Circuit::Circuit(quint64 circId, Status status, QString path) 00043 { 00044 _circId = circId; 00045 _status = status; 00046 _path = path; 00047 } 00048 00049 /** Constructor */ 00050 Circuit::Circuit(quint64 circId, Status status, QStringList hops) 00051 { 00052 _circId = circId; 00053 _status = status; 00054 _path = hops.join(","); 00055 } 00056 00057 /** Parses the string given in Tor control protocol format for a circuit. The 00058 * format is: 00059 * 00060 * CircuitID SP CircStatus [SP Path] 00061 * 00062 * If the status is "LAUNCHED", the Path is empty. 00063 */ 00064 Circuit 00065 Circuit::fromString(QString circuit) 00066 { 00067 QStringList parts = circuit.split(" "); 00068 if (parts.size() >= 2) { 00069 /* Get the circuit ID */ 00070 quint64 circId = (quint64)parts.at(0).toULongLong(); 00071 /* Get the circuit status value */ 00072 Circuit::Status status = Circuit::toStatus(parts.at(1)); 00073 /* Get the circuit path (list of routers) */ 00074 QString path = (parts.size() > 2 ? parts.at(2) : ""); 00075 00076 return Circuit(circId, status, path); 00077 } 00078 return Circuit(); 00079 } 00080 00081 /** Converts the circuit status string to its proper enum value */ 00082 Circuit::Status 00083 Circuit::toStatus(QString strStatus) 00084 { 00085 Status status; 00086 strStatus = strStatus.toUpper(); 00087 if (strStatus == "LAUNCHED") { 00088 status = Launched; 00089 } else if (strStatus == "BUILT") { 00090 status = Built; 00091 } else if (strStatus == "EXTENDED") { 00092 status = Extended; 00093 } else if (strStatus == "FAILED") { 00094 status = Failed; 00095 } else if (strStatus == "CLOSED") { 00096 status = Closed; 00097 } else { 00098 status = Unknown; 00099 } 00100 return status; 00101 } 00102 00103 /** Returns a string representation of the circuit's status. */ 00104 QString 00105 Circuit::statusString() 00106 { 00107 QString status; 00108 switch (_status) { 00109 case Launched: status = tr("New"); break; 00110 case Built: status = tr("Open"); break; 00111 case Extended: status = tr("Building"); break; 00112 case Failed: status = tr("Failed"); break; 00113 case Closed: status = tr("Closed"); break; 00114 default: status = tr("Unknown"); break; 00115 } 00116 return status; 00117 } 00118 00119 /** Returns true if all fields in this Circuit object are empty. */ 00120 bool 00121 Circuit::isEmpty() 00122 { 00123 return (!_circId && (_status == Unknown)); 00124 } 00125