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 replyline.cpp 00024 * \version $Id: replyline.cpp 1735 2007-05-13 07:23:57Z edmanm $ 00025 * \brief Reply from a previous control command sent to Tor 00026 */ 00027 00028 #include "replyline.h" 00029 00030 /** Default constructor */ 00031 ReplyLine::ReplyLine() 00032 { 00033 } 00034 00035 /** Constructor */ 00036 ReplyLine::ReplyLine(QString status, QString msg) 00037 { 00038 _status = status; 00039 setMessage(msg); 00040 } 00041 00042 /** Constructor */ 00043 ReplyLine::ReplyLine(QString status, QString msg, QString data) 00044 { 00045 _status = status; 00046 setMessage(msg); 00047 appendData(data); 00048 } 00049 00050 /** Set the status code for this reply line. See Tor Control Protocol V1 00051 * specification for a description of status codes. */ 00052 void 00053 ReplyLine::setStatus(QString status) 00054 { 00055 _status = status; 00056 } 00057 00058 /** Returns the status code for this reply line. */ 00059 QString 00060 ReplyLine::getStatus() const 00061 { 00062 return _status; 00063 } 00064 00065 /** Sets the ReplyText message this reply line to <b>msg</b>. */ 00066 void 00067 ReplyLine::setMessage(QString msg) 00068 { 00069 _message = unescape(msg); 00070 } 00071 00072 /** Returns the ReplyText portion of this reply line. */ 00073 QString 00074 ReplyLine::getMessage() const 00075 { 00076 return _message; 00077 } 00078 00079 /** Appends <b>data</b> to this reply line. */ 00080 void 00081 ReplyLine::appendData(QString data) 00082 { 00083 _data << unescape(data); 00084 } 00085 00086 /** Returns a QStringList of all data lines for this reply line */ 00087 QStringList 00088 ReplyLine::getData() const 00089 { 00090 return _data; 00091 } 00092 00093 /** Unescapes special characters in <b>str</b> and returns the unescaped 00094 * result. */ 00095 QString 00096 ReplyLine::unescape(QString str) 00097 { 00098 /* If the line starts with a "." and was escaped, then unescape it */ 00099 if (str.startsWith("..")) { 00100 str.remove(0, 1); 00101 } 00102 00103 /* Trim off trailing whitespace (including \r\n) */ 00104 return str.trimmed(); 00105 } 00106 00107 QString 00108 ReplyLine::toString() const 00109 { 00110 QString str = _status + " " + _message; 00111 if (!_data.isEmpty()) { 00112 str.append("\n"); 00113 str.append(_data.join("\n")); 00114 } 00115 return str; 00116 } 00117