routerdescriptorview.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file routerdescriptorview.cpp
00024  * \version $Id: routerdescriptorview.cpp 1563 2006-12-26 06:06:04Z edmanm $
00025  * \brief Formats and displays a router descriptor as HTML
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 /** Default constructor. */
00035 RouterDescriptorView::RouterDescriptorView(QWidget *parent)
00036 : QTextEdit(parent)
00037 {
00038 }
00039 
00040 /** Format the date the descriptor was published. */
00041 QString
00042 RouterDescriptorView::formatPublished(QDateTime date)
00043 {
00044   return date.toString(DATE_FORMAT) + " GMT";
00045 }
00046 
00047 /** Adjusts the displayed uptime to include time since the router's descriptor
00048  * was last published. */
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 /** Format the uptime for this router in a readable format. */
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 /** Format the bandwidth into KB/s. */
00086 QString
00087 RouterDescriptorView::formatBandwidth(quint64 bandwidth)
00088 {
00089   return QString::number(bandwidth/1024);
00090 }
00091 
00092 /** Displays all router descriptors in the given list. */
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     /* Router name and status */
00103     html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")"));
00104 
00105     /* IP and platform */
00106     html.append("<table>");
00107     
00108     /* If we have location information, show that first. */
00109     if (!rd.location().isEmpty()) {
00110       html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location())));
00111     }
00112     
00113     /* Add the IP address and router platform information */
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     /* If the router is online, then show the uptime and bandwidth stats. */
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     /* Date the router was published */
00127     html.append(trow(tcol(b(tr("Last Updated:")))  +
00128                      tcol(formatPublished(rd.published()))));
00129     
00130     html.append("</table>");
00131     
00132     /* If there are multiple descriptors, and this isn't is the last one 
00133      * then separate them with a short horizontal line. */
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 /** Displays the given router descriptor. */
00143 void
00144 RouterDescriptorView::display(RouterDescriptor rd)
00145 {
00146   display(QList<RouterDescriptor>() << rd);
00147 }
00148 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3