Vidalia  0.3.1
RouterDescriptorView.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 you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** 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 RouterDescriptorView.cpp
13 ** \brief Formats and displays a router descriptor as HTML
14 */
15 
16 #include "RouterDescriptorView.h"
17 #include "Vidalia.h"
18 
19 #include "html.h"
20 #include "stringutil.h"
21 
22 #include <QMenu>
23 #include <QIcon>
24 #include <QTextCursor>
25 #include <QClipboard>
26 #include <QShortcut>
27 #include <QTextDocumentFragment>
28 
29 #define IMG_COPY ":/images/22x22/edit-copy.png"
30 
31 
32 /** Default constructor. */
34 : QTextEdit(parent)
35 {
36  /* Steal QTextEdit's default "Copy" shortcut, since we want to do some
37  * tweaking of the selected text before putting it on the clipboard. */
38  QShortcut *shortcut = new QShortcut(QKeySequence::Copy, this,
39  SLOT(copySelectedText()));
40  Q_UNUSED(shortcut);
41 }
42 
43 /** Displays a context menu for the user when they right-click on the
44  * widget. */
45 void
46 RouterDescriptorView::contextMenuEvent(QContextMenuEvent *event)
47 {
48  QMenu *menu = new QMenu();
49 
50  QAction *copyAction = new QAction(QIcon(IMG_COPY), tr("Copy"), menu);
51  copyAction->setShortcut(QKeySequence::Copy);
52  connect(copyAction, SIGNAL(triggered()), this, SLOT(copySelectedText()));
53 
54  if (textCursor().selectedText().isEmpty())
55  copyAction->setEnabled(false);
56 
57  menu->addAction(copyAction);
58  menu->exec(event->globalPos());
59  delete menu;
60 }
61 
62 /** Copies any selected text to the clipboard. */
63 void
65 {
66  QString selectedText = textCursor().selection().toPlainText();
67  selectedText.replace(":\n", ": ");
68  vApp->clipboard()->setText(selectedText);
69 }
70 
71 /** Adjusts the displayed uptime to include time since the router's descriptor
72  * was last published. */
73 quint64
74 RouterDescriptorView::adjustUptime(quint64 uptime, QDateTime published)
75 {
76  QDateTime now = QDateTime::currentDateTime().toUTC();
77 
78  if (now < published) {
79  return uptime;
80  }
81  return (uptime + (now.toTime_t() - published.toTime_t()));
82 }
83 
84 /** Displays all router descriptors in the given list. */
85 void
86 RouterDescriptorView::display(QList<RouterDescriptor> rdlist)
87 {
89  QString html = "<html><body>";
90 
91  for (int r = 0; r < rdlist.size(); r++) {
92  rd = rdlist.at(r);
93  if (rd.isEmpty())
94  continue;
95 
96  /* Router name and status */
97  html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")"));
98 
99  /* IP and platform */
100  html.append("<table>");
101 
102  /* If we have location information, show that first. */
103  if (!rd.location().isEmpty()) {
104  html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location())));
105  }
106 
107  /* Add the IP address and router platform information */
108  html.append(trow(tcol(b(tr("IP Address:"))) + tcol(rd.ip().toString())));
109  html.append(trow(tcol(b(tr("Platform:"))) + tcol(rd.platform())));
110 
111  /* If the router is online, then show the uptime and bandwidth stats. */
112  if (!rd.offline()) {
113  qint64 minBandwidth = (qint64)qMin(rd.observedBandwidth(),
114  qMin(rd.averageBandwidth(),
115  rd.burstBandwidth()));
116  html.append(trow(tcol(b(tr("Bandwidth:"))) +
117  tcol(string_format_bandwidth(minBandwidth))));
118  html.append(trow(tcol(b(tr("Uptime:"))) +
120  adjustUptime(rd.uptime(), rd.published())))));
121  }
122 
123  /* Date the router was published */
124  html.append(trow(tcol(b(tr("Last Updated:"))) +
125  tcol(string_format_datetime(rd.published()) + " GMT")));
126 
127  html.append("</table>");
128 
129  /* If there are multiple descriptors, and this isn't is the last one
130  * then separate them with a short horizontal line. */
131  if (r+1 != rdlist.size()) {
132  html.append("<center><hr width=\"50%\"/></center>");
133  }
134  }
135  html.append("</body></html>");
136  setHtml(html);
137 }
138 
139 /** Displays the given router descriptor. */
140 void
142 {
143  display(QList<RouterDescriptor>() << rd);
144 }
145 
QString trow(QString str)
Definition: html.cpp:46
QString p(QString str)
Definition: html.cpp:22
quint64 uptime() const
QString string_format_datetime(const QDateTime &date)
Definition: stringutil.cpp:411
QString name() const
RouterDescriptorView(QWidget *parent=0)
quint64 observedBandwidth() const
void display(RouterDescriptor rd)
QString string_format_bandwidth(quint64 bytes)
Definition: stringutil.cpp:419
quint64 adjustUptime(quint64 uptime, QDateTime published)
QString tcol(QString str)
Definition: html.cpp:53
QString location() const
QString string_format_uptime(quint64 seconds)
Definition: stringutil.cpp:388
bool offline() const
quint64 burstBandwidth() const
#define IMG_COPY
QString i(QString str)
Definition: html.cpp:32
stop errmsg connect(const QHostAddress &address, quint16 port)
virtual void contextMenuEvent(QContextMenuEvent *event)
QString platform() const
quint64 averageBandwidth() const
#define vApp
Definition: Vidalia.h:37
QHostAddress ip() const
QString b(QString str)
Definition: html.cpp:39
QDateTime published() const