Vidalia  0.2.21
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  if (!rd.platform().isEmpty())
110  html.append(trow(tcol(b(tr("Platform:"))) + tcol(rd.platform())));
111 
112  /* If the router is online, then show the uptime and bandwidth stats. */
113  if (!rd.offline()) {
114  qint64 minBandwidth = (qint64)qMin(rd.observedBandwidth(),
115  qMin(rd.averageBandwidth(),
116  rd.burstBandwidth()));
117  html.append(trow(tcol(b(tr("Bandwidth:"))) +
118  tcol(string_format_bandwidth(minBandwidth))));
119  html.append(trow(tcol(b(tr("Uptime:"))) +
121  adjustUptime(rd.uptime(), rd.published())))));
122  }
123 
124  /* Date the router was published */
125  if (!rd.published().isNull())
126  html.append(trow(tcol(b(tr("Last Updated:"))) +
127  tcol(string_format_datetime(rd.published()) + " GMT")));
128 
129  html.append("</table>");
130 
131  /* If there are multiple descriptors, and this isn't is the last one
132  * then separate them with a short horizontal line. */
133  if (r+1 != rdlist.size()) {
134  html.append("<center><hr width=\"50%\"/></center>");
135  }
136  }
137  html.append("</body></html>");
138  setHtml(html);
139 }
140 
141 /** Displays the given router descriptor. */
142 void
144 {
145  display(QList<RouterDescriptor>() << rd);
146 }
147