00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <QMenu>
00018 #include <QIcon>
00019 #include <QTextCursor>
00020 #include <QClipboard>
00021 #include <QShortcut>
00022 #include <QTextDocumentFragment>
00023 #include <html.h>
00024 #include <vidalia.h>
00025 #include "routerdescriptorview.h"
00026
00027 #define DATE_FORMAT "yyyy-MM-dd HH:mm:ss"
00028 #define IMG_COPY ":/images/22x22/edit-copy.png"
00029
00030
00031
00032 RouterDescriptorView::RouterDescriptorView(QWidget *parent)
00033 : QTextEdit(parent)
00034 {
00035
00036
00037 QShortcut *shortcut = new QShortcut(QKeySequence::Copy, this,
00038 SLOT(copySelectedText()));
00039 }
00040
00041
00042
00043 void
00044 RouterDescriptorView::contextMenuEvent(QContextMenuEvent *event)
00045 {
00046 QMenu *menu = new QMenu();
00047
00048 QAction *copyAction = new QAction(QIcon(IMG_COPY), tr("Copy"), menu);
00049 copyAction->setShortcut(QKeySequence::Copy);
00050 connect(copyAction, SIGNAL(triggered()), this, SLOT(copySelectedText()));
00051
00052 if (textCursor().selectedText().isEmpty())
00053 copyAction->setEnabled(false);
00054
00055 menu->addAction(copyAction);
00056 menu->exec(event->globalPos());
00057 delete menu;
00058 }
00059
00060
00061 void
00062 RouterDescriptorView::copySelectedText()
00063 {
00064 QString selectedText = textCursor().selection().toPlainText();
00065 selectedText.replace(":\n", ": ");
00066 vApp->clipboard()->setText(selectedText);
00067 }
00068
00069
00070 QString
00071 RouterDescriptorView::formatPublished(QDateTime date)
00072 {
00073 return date.toString(DATE_FORMAT) + " GMT";
00074 }
00075
00076
00077
00078 quint64
00079 RouterDescriptorView::adjustUptime(quint64 uptime, QDateTime published)
00080 {
00081 QDateTime now = QDateTime::currentDateTime().toUTC();
00082
00083 if (now < published) {
00084 return uptime;
00085 }
00086 return (uptime + (now.toTime_t() - published.toTime_t()));
00087 }
00088
00089
00090 QString
00091 RouterDescriptorView::formatUptime(quint64 seconds)
00092 {
00093 QString uptime;
00094 int secs = (seconds % 60);
00095 int mins = (seconds / 60 % 60);
00096 int hours = (seconds / 3600 % 24);
00097 int days = (seconds / 86400);
00098
00099 if (days) {
00100 uptime += tr("%1 days ").arg(days);
00101 }
00102 if (hours) {
00103 uptime += tr("%1 hours ").arg(hours);
00104 }
00105 if (mins) {
00106 uptime += tr("%1 mins ").arg(mins);
00107 }
00108 if (secs) {
00109 uptime += tr("%1 secs").arg(secs);
00110 }
00111 return uptime;
00112 }
00113
00114
00115 QString
00116 RouterDescriptorView::formatBandwidth(quint64 bandwidth)
00117 {
00118 return QString::number(bandwidth/1024);
00119 }
00120
00121
00122 void
00123 RouterDescriptorView::display(QList<RouterDescriptor> rdlist)
00124 {
00125 RouterDescriptor rd;
00126 QString html = "<html><body>";
00127
00128 for (int r = 0; r < rdlist.size(); r++) {
00129 rd = rdlist.at(r);
00130 if (rd.isEmpty())
00131 continue;
00132
00133
00134 html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")"));
00135
00136
00137 html.append("<table>");
00138
00139
00140 if (!rd.location().isEmpty()) {
00141 html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location())));
00142 }
00143
00144
00145 html.append(trow(tcol(b(tr("IP Address:"))) + tcol(rd.ip().toString())));
00146 html.append(trow(tcol(b(tr("Platform:"))) + tcol(rd.platform())));
00147
00148
00149 if (!rd.offline()) {
00150 html.append(trow(tcol(b(tr("Bandwidth:"))) +
00151 tcol(formatBandwidth(rd.observedBandwidth()) + " KB/s")));
00152 html.append(trow(tcol(b(tr("Uptime:"))) +
00153 tcol(formatUptime(
00154 adjustUptime(rd.uptime(), rd.published())))));
00155 }
00156
00157
00158 html.append(trow(tcol(b(tr("Last Updated:"))) +
00159 tcol(formatPublished(rd.published()))));
00160
00161 html.append("</table>");
00162
00163
00164
00165 if (r+1 != rdlist.size()) {
00166 html.append("<center><hr width=\"50%\"/></center>");
00167 }
00168 }
00169 html.append("</body></html>");
00170 setHtml(html);
00171 }
00172
00173
00174 void
00175 RouterDescriptorView::display(RouterDescriptor rd)
00176 {
00177 display(QList<RouterDescriptor>() << rd);
00178 }
00179