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 3029 2008-09-02 01:39:36Z edmanm $
00014 ** \brief Formats and displays a router descriptor as HTML
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 /** Default constructor. */
00032 RouterDescriptorView::RouterDescriptorView(QWidget *parent)
00033 : QTextEdit(parent)
00034 {
00035   /* Steal QTextEdit's default "Copy" shortcut, since we want to do some
00036    * tweaking of the selected text before putting it on the clipboard. */
00037   QShortcut *shortcut = new QShortcut(QKeySequence::Copy, this,
00038                                       SLOT(copySelectedText()));
00039 }
00040 
00041 /** Displays a context menu for the user when they right-click on the
00042  * widget. */
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 /** Copies any selected text to the clipboard. */
00061 void
00062 RouterDescriptorView::copySelectedText()
00063 { 
00064   QString selectedText = textCursor().selection().toPlainText();
00065   selectedText.replace(":\n", ": ");
00066   vApp->clipboard()->setText(selectedText);
00067 }
00068 
00069 /** Format the date the descriptor was published. */
00070 QString
00071 RouterDescriptorView::formatPublished(QDateTime date)
00072 {
00073   return date.toString(DATE_FORMAT) + " GMT";
00074 }
00075 
00076 /** Adjusts the displayed uptime to include time since the router's descriptor
00077  * was last published. */
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 /** Format the uptime for this router in a readable format. */
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 /** Format the bandwidth into KB/s. */
00115 QString
00116 RouterDescriptorView::formatBandwidth(quint64 bandwidth)
00117 {
00118   return QString::number(bandwidth/1024);
00119 }
00120 
00121 /** Displays all router descriptors in the given list. */
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     
00131     /* Router name and status */
00132     html.append(p(b(rd.name()) + " (" + i(rd.status()) + ")"));
00133 
00134     /* IP and platform */
00135     html.append("<table>");
00136     
00137     /* If we have location information, show that first. */
00138     if (!rd.location().isEmpty()) {
00139       html.append(trow(tcol(b(tr("Location:"))) + tcol(rd.location())));
00140     }
00141     
00142     /* Add the IP address and router platform information */
00143     html.append(trow(tcol(b(tr("IP Address:"))) + tcol(rd.ip().toString())));
00144     html.append(trow(tcol(b(tr("Platform:")))   + tcol(rd.platform())));
00145 
00146     /* If the router is online, then show the uptime and bandwidth stats. */
00147     if (!rd.offline()) {
00148       html.append(trow(tcol(b(tr("Bandwidth:")))  + 
00149                        tcol(formatBandwidth(rd.observedBandwidth()) + " KB/s")));
00150       html.append(trow(tcol(b(tr("Uptime:")))   + 
00151                        tcol(formatUptime(
00152                               adjustUptime(rd.uptime(), rd.published())))));
00153     }
00154     
00155     /* Date the router was published */
00156     html.append(trow(tcol(b(tr("Last Updated:")))  +
00157                      tcol(formatPublished(rd.published()))));
00158     
00159     html.append("</table>");
00160     
00161     /* If there are multiple descriptors, and this isn't is the last one 
00162      * then separate them with a short horizontal line. */
00163     if (r+1 != rdlist.size()) {
00164       html.append("<center><hr width=\"50%\"/></center>");
00165     }
00166   }
00167   html.append("</body></html>");
00168   setHtml(html); 
00169 }
00170 
00171 /** Displays the given router descriptor. */
00172 void
00173 RouterDescriptorView::display(RouterDescriptor rd)
00174 {
00175   display(QList<RouterDescriptor>() << rd);
00176 }
00177 

Generated on Wed Nov 26 21:03:58 2008 for Vidalia by  doxygen 1.5.6