Vidalia 0.2.10
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.vidalia-project.net/. No part of Vidalia, including this file, 00007 ** may be copied, modified, propagated, or distributed except according to the 00008 ** terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file RouterDescriptorView.cpp 00013 ** \version $Id: RouterDescriptorView.cpp 3770 2009-05-14 02:41:24Z edmanm $ 00014 ** \brief Formats and displays a router descriptor as HTML 00015 */ 00016 00017 #include "RouterDescriptorView.h" 00018 #include "Vidalia.h" 00019 00020 #include "html.h" 00021 #include "stringutil.h" 00022 00023 #include <QMenu> 00024 #include <QIcon> 00025 #include <QTextCursor> 00026 #include <QClipboard> 00027 #include <QShortcut> 00028 #include <QTextDocumentFragment> 00029 00030 #define IMG_COPY ":/images/22x22/edit-copy.png" 00031 00032 00033 /** Default constructor. */ 00034 RouterDescriptorView::RouterDescriptorView(QWidget *parent) 00035 : QTextEdit(parent) 00036 { 00037 /* Steal QTextEdit's default "Copy" shortcut, since we want to do some 00038 * tweaking of the selected text before putting it on the clipboard. */ 00039 QShortcut *shortcut = new QShortcut(QKeySequence::Copy, this, 00040 SLOT(copySelectedText())); 00041 Q_UNUSED(shortcut); 00042 } 00043 00044 /** Displays a context menu for the user when they right-click on the 00045 * widget. */ 00046 void 00047 RouterDescriptorView::contextMenuEvent(QContextMenuEvent *event) 00048 { 00049 QMenu *menu = new QMenu(); 00050 00051 QAction *copyAction = new QAction(QIcon(IMG_COPY), tr("Copy"), menu); 00052 copyAction->setShortcut(QKeySequence::Copy); 00053 connect(copyAction, SIGNAL(triggered()), this, SLOT(copySelectedText())); 00054 00055 if (textCursor().selectedText().isEmpty()) 00056 copyAction->setEnabled(false); 00057 00058 menu->addAction(copyAction); 00059 menu->exec(event->globalPos()); 00060 delete menu; 00061 } 00062 00063 /** Copies any selected text to the clipboard. */ 00064 void 00065 RouterDescriptorView::copySelectedText() 00066 { 00067 QString selectedText = textCursor().selection().toPlainText(); 00068 selectedText.replace(":\n", ": "); 00069 vApp->clipboard()->setText(selectedText); 00070 } 00071 00072 /** Adjusts the displayed uptime to include time since the router's descriptor 00073 * was last published. */ 00074 quint64 00075 RouterDescriptorView::adjustUptime(quint64 uptime, QDateTime published) 00076 { 00077 QDateTime now = QDateTime::currentDateTime().toUTC(); 00078 00079 if (now < published) { 00080 return uptime; 00081 } 00082 return (uptime + (now.toTime_t() - published.toTime_t())); 00083 } 00084 00085 /** Displays all router descriptors in the given list. */ 00086 void 00087 RouterDescriptorView::display(QList<RouterDescriptor> rdlist) 00088 { 00089 RouterDescriptor rd; 00090 QString html = "<html><body>"; 00091 00092 for (int r = 0; r < rdlist.size(); r++) { 00093 rd = rdlist.at(r); 00094 if (rd.isEmpty()) 00095 continue; 00096 00097 /* Router name and status */ 00098 html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")")); 00099 00100 /* IP and platform */ 00101 html.append("<table>"); 00102 00103 /* If we have location information, show that first. */ 00104 if (!rd.location().isEmpty()) { 00105 html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location()))); 00106 } 00107 00108 /* Add the IP address and router platform information */ 00109 html.append(trow(tcol(b(tr("IP Address:"))) + tcol(rd.ip().toString()))); 00110 html.append(trow(tcol(b(tr("Platform:"))) + tcol(rd.platform()))); 00111 00112 /* If the router is online, then show the uptime and bandwidth stats. */ 00113 if (!rd.offline()) { 00114 html.append(trow(tcol(b(tr("Bandwidth:"))) + 00115 tcol(string_format_bandwidth(rd.observedBandwidth())))); 00116 html.append(trow(tcol(b(tr("Uptime:"))) + 00117 tcol(string_format_uptime( 00118 adjustUptime(rd.uptime(), rd.published()))))); 00119 } 00120 00121 /* Date the router was published */ 00122 html.append(trow(tcol(b(tr("Last Updated:"))) + 00123 tcol(string_format_datetime(rd.published()) + " GMT"))); 00124 00125 html.append("</table>"); 00126 00127 /* If there are multiple descriptors, and this isn't is the last one 00128 * then separate them with a short horizontal line. */ 00129 if (r+1 != rdlist.size()) { 00130 html.append("<center><hr width=\"50%\"/></center>"); 00131 } 00132 } 00133 html.append("</body></html>"); 00134 setHtml(html); 00135 } 00136 00137 /** Displays the given router descriptor. */ 00138 void 00139 RouterDescriptorView::display(RouterDescriptor rd) 00140 { 00141 display(QList<RouterDescriptor>() << rd); 00142 } 00143