RouterDescriptorView.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00034 RouterDescriptorView::RouterDescriptorView(QWidget *parent)
00035 : QTextEdit(parent)
00036 {
00037
00038
00039 QShortcut *shortcut = new QShortcut(QKeySequence::Copy, this,
00040 SLOT(copySelectedText()));
00041 Q_UNUSED(shortcut);
00042 }
00043
00044
00045
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
00064 void
00065 RouterDescriptorView::copySelectedText()
00066 {
00067 QString selectedText = textCursor().selection().toPlainText();
00068 selectedText.replace(":\n", ": ");
00069 vApp->clipboard()->setText(selectedText);
00070 }
00071
00072
00073
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
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
00098 html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")"));
00099
00100
00101 html.append("<table>");
00102
00103
00104 if (!rd.location().isEmpty()) {
00105 html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location())));
00106 }
00107
00108
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
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
00122 html.append(trow(tcol(b(tr("Last Updated:"))) +
00123 tcol(string_format_datetime(rd.published()) + " GMT")));
00124
00125 html.append("</table>");
00126
00127
00128
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
00138 void
00139 RouterDescriptorView::display(RouterDescriptor rd)
00140 {
00141 display(QList<RouterDescriptor>() << rd);
00142 }
00143