routerdescriptorview.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file routerdescriptorview.cpp
00013 ** \version $Id: routerdescriptorview.cpp 2733 2008-06-15 04:20:08Z edmanm $
00014 ** \brief Formats and displays a router descriptor as HTML
00015 */
00016 
00017 #include <html.h>
00018 #include "routerdescriptorview.h"
00019 
00020 #define DATE_FORMAT   "yyyy-MM-dd HH:mm:ss"
00021 
00022 
00023 /** Default constructor. */
00024 RouterDescriptorView::RouterDescriptorView(QWidget *parent)
00025 : QTextEdit(parent)
00026 {
00027 }
00028 
00029 /** Format the date the descriptor was published. */
00030 QString
00031 RouterDescriptorView::formatPublished(QDateTime date)
00032 {
00033   return date.toString(DATE_FORMAT) + " GMT";
00034 }
00035 
00036 /** Adjusts the displayed uptime to include time since the router's descriptor
00037  * was last published. */
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 /** Format the uptime for this router in a readable format. */
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 /** Format the bandwidth into KB/s. */
00075 QString
00076 RouterDescriptorView::formatBandwidth(quint64 bandwidth)
00077 {
00078   return QString::number(bandwidth/1024);
00079 }
00080 
00081 /** Displays all router descriptors in the given list. */
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     /* Router name and status */
00092     html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")"));
00093 
00094     /* IP and platform */
00095     html.append("<table>");
00096     
00097     /* If we have location information, show that first. */
00098     if (!rd.location().isEmpty()) {
00099       html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location())));
00100     }
00101     
00102     /* Add the IP address and router platform information */
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     /* If the router is online, then show the uptime and bandwidth stats. */
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     /* Date the router was published */
00116     html.append(trow(tcol(b(tr("Last Updated:")))  +
00117                      tcol(formatPublished(rd.published()))));
00118     
00119     html.append("</table>");
00120     
00121     /* If there are multiple descriptors, and this isn't is the last one 
00122      * then separate them with a short horizontal line. */
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 /** Displays the given router descriptor. */
00132 void
00133 RouterDescriptorView::display(RouterDescriptor rd)
00134 {
00135   display(QList<RouterDescriptor>() << rd);
00136 }
00137 

Generated on Sat Aug 16 17:31:48 2008 for Vidalia by  doxygen 1.5.6