Vidalia  0.3.1
ReplyLine.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file ReplyLine.cpp
13 ** \brief Reply from a previous control command sent to Tor
14 */
15 
16 #include "ReplyLine.h"
17 
18 
19 /** Default constructor */
21 {
22 }
23 
24 /** Constructor */
25 ReplyLine::ReplyLine(const QString &status, const QString &msg)
26 {
27  _status = status;
28  setMessage(msg);
29 }
30 
31 /** Constructor */
32 ReplyLine::ReplyLine(const QString &status, const QString &msg,
33  const QString &data)
34 {
35  _status = status;
36  setMessage(msg);
37  appendData(data);
38 }
39 
40 /** Set the status code for this reply line. See Tor Control Protocol V1
41  * specification for a description of status codes. */
42 void
43 ReplyLine::setStatus(const QString &status)
44 {
45  _status = status;
46 }
47 
48 /** Returns the status code for this reply line. */
49 QString
51 {
52  return _status;
53 }
54 
55 /** Sets the ReplyText message this reply line to <b>msg</b>. */
56 void
57 ReplyLine::setMessage(const QString &msg)
58 {
59  _message = unescape(msg);
60 }
61 
62 /** Returns the ReplyText portion of this reply line. */
63 QString
65 {
66  return _message;
67 }
68 
69 /** Appends <b>data</b> to this reply line. */
70 void
71 ReplyLine::appendData(const QString &data)
72 {
73  _data << unescape(data);
74 }
75 
76 /** Returns a QStringList of all data lines for this reply line */
77 QStringList
79 {
80  return _data;
81 }
82 
83 /** Unescapes special characters in <b>str</b> and returns the unescaped
84  * result. */
85 QString
86 ReplyLine::unescape(const QString &escaped)
87 {
88  QString str = escaped;
89  /* If the line starts with a "." and was escaped, then unescape it */
90  if (str.startsWith("..")) {
91  str.remove(0, 1);
92  }
93 
94  /* Trim off trailing whitespace (including \r\n) */
95  return str.trimmed();
96 }
97 
98 QString
100 {
101  QString str = _status + " " + _message;
102  if (!_data.isEmpty()) {
103  str.append("\n");
104  str.append(_data.join("\n"));
105  }
106  return str;
107 }
108 
QString _status
Definition: ReplyLine.h:55
QStringList getData() const
Definition: ReplyLine.cpp:78
QString getMessage() const
Definition: ReplyLine.cpp:64
QString getStatus() const
Definition: ReplyLine.cpp:50
void setMessage(const QString &msg)
Definition: ReplyLine.cpp:57
QStringList _data
Definition: ReplyLine.h:57
QString _message
Definition: ReplyLine.h:56
static QString unescape(const QString &escaped)
Definition: ReplyLine.cpp:86
void setStatus(const QString &status)
Definition: ReplyLine.cpp:43
void appendData(const QString &data)
Definition: ReplyLine.cpp:71
QString toString() const
Definition: ReplyLine.cpp:99