00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <util/html.h>
00029 #include "routerdescriptorview.h"
00030
00031 #define DATE_FORMAT "yyyy-MM-dd HH:mm:ss"
00032
00033
00034
00035 RouterDescriptorView::RouterDescriptorView(QWidget *parent)
00036 : QTextEdit(parent)
00037 {
00038 }
00039
00040
00041 QString
00042 RouterDescriptorView::formatPublished(QDateTime date)
00043 {
00044 return date.toString(DATE_FORMAT) + " GMT";
00045 }
00046
00047
00048
00049 quint64
00050 RouterDescriptorView::adjustUptime(quint64 uptime, QDateTime published)
00051 {
00052 QDateTime now = QDateTime::currentDateTime().toUTC();
00053
00054 if (now < published) {
00055 return uptime;
00056 }
00057 return (uptime + (now.toTime_t() - published.toTime_t()));
00058 }
00059
00060
00061 QString
00062 RouterDescriptorView::formatUptime(quint64 seconds)
00063 {
00064 QString uptime;
00065 int secs = (seconds % 60);
00066 int mins = (seconds / 60 % 60);
00067 int hours = (seconds / 3600 % 24);
00068 int days = (seconds / 86400);
00069
00070 if (days) {
00071 uptime += QString("%1 days ").arg(days);
00072 }
00073 if (hours) {
00074 uptime += QString("%1 hours ").arg(hours);
00075 }
00076 if (mins) {
00077 uptime += QString("%1 mins ").arg(mins);
00078 }
00079 if (secs) {
00080 uptime += QString("%1 secs").arg(secs);
00081 }
00082 return uptime;
00083 }
00084
00085
00086 QString
00087 RouterDescriptorView::formatBandwidth(quint64 bandwidth)
00088 {
00089 return QString::number(bandwidth/1024);
00090 }
00091
00092
00093 void
00094 RouterDescriptorView::display(QList<RouterDescriptor> rdlist)
00095 {
00096 RouterDescriptor rd;
00097 QString html = "<html><body>";
00098
00099 for (int r = 0; r < rdlist.size(); r++) {
00100 rd = rdlist.at(r);
00101
00102
00103 html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")"));
00104
00105
00106 html.append("<table>");
00107
00108
00109 if (!rd.location().isEmpty()) {
00110 html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location())));
00111 }
00112
00113
00114 html.append(trow(tcol(b(tr("IP Address:"))) + tcol(rd.ip().toString())));
00115 html.append(trow(tcol(b(tr("Platform:"))) + tcol(rd.platform())));
00116
00117
00118 if (!rd.offline()) {
00119 html.append(trow(tcol(b(tr("Bandwidth:"))) +
00120 tcol(formatBandwidth(rd.observedBandwidth()) + " KB/s")));
00121 html.append(trow(tcol(b(tr("Uptime:"))) +
00122 tcol(formatUptime(
00123 adjustUptime(rd.uptime(), rd.published())))));
00124 }
00125
00126
00127 html.append(trow(tcol(b(tr("Last Updated:"))) +
00128 tcol(formatPublished(rd.published()))));
00129
00130 html.append("</table>");
00131
00132
00133
00134 if (r+1 != rdlist.size()) {
00135 html.append("<center><hr width=\"50%\"/></center>");
00136 }
00137 }
00138 html.append("</body></html>");
00139 setHtml(html);
00140 }
00141
00142
00143 void
00144 RouterDescriptorView::display(RouterDescriptor rd)
00145 {
00146 display(QList<RouterDescriptor>() << rd);
00147 }
00148