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