routerlistitem.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 routerlistitem.cpp
00013 ** \version $Id: routerlistitem.cpp 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief Item representing a single router and status in a RouterListWidget
00015 */
00016 
00017 #include <QHeaderView>
00018 
00019 #include "routerlistwidget.h"
00020 #include "routerlistitem.h"
00021 
00022 #define STATUS_COLUMN   (RouterListWidget::StatusColumn)
00023 #define COUNTRY_COLUMN  (RouterListWidget::CountryColumn)
00024 #define NAME_COLUMN     (RouterListWidget::NameColumn)
00025 
00026 #define IMG_NODE_OFFLINE    ":/images/icons/node-unresponsive.png"
00027 #define IMG_NODE_SLEEPING   ":/images/icons/node-hibernating.png"
00028 #define IMG_NODE_NO_BW      ":/images/icons/node-bw-none.png"
00029 #define IMG_NODE_LOW_BW     ":/images/icons/node-bw-low.png"
00030 #define IMG_NODE_MED_BW     ":/images/icons/node-bw-med.png"
00031 #define IMG_NODE_HIGH_BW    ":/images/icons/node-bw-high.png"
00032 #define IMG_FLAG_UNKNOWN    ":/images/flags/unknown.png"
00033 
00034 
00035 /** Default constructor. */
00036 RouterListItem::RouterListItem(RouterListWidget *list, RouterDescriptor rd)
00037 : QTreeWidgetItem()
00038 {
00039   _list = list;
00040   _rd   = 0;
00041   _country = "~"; /* Force items with no country to the bottom */
00042   setIcon(COUNTRY_COLUMN, QIcon(IMG_FLAG_UNKNOWN));
00043   update(rd);
00044 }
00045 
00046 /** Destructor. */
00047 RouterListItem::~RouterListItem()
00048 {
00049   if (_rd)
00050     delete _rd;
00051 }
00052 
00053 /** Updates the router descriptor for this item. */
00054 void
00055 RouterListItem::update(const RouterDescriptor &rd)
00056 {
00057   QIcon statusIcon;
00058   if (_rd) {
00059     delete _rd;
00060   }
00061   _rd = new RouterDescriptor(rd);
00062   
00063   /* Determine the status value (used for sorting) and icon */
00064   if (_rd->offline()) {
00065     _statusValue = -1;
00066     statusIcon = QIcon(IMG_NODE_OFFLINE);
00067     setToolTip(STATUS_COLUMN, tr("Offline"));
00068   } else if (_rd->hibernating()) {
00069     _statusValue = 0;
00070     statusIcon = QIcon(IMG_NODE_SLEEPING);
00071     setToolTip(STATUS_COLUMN, tr("Hibernating"));
00072   } else {
00073     _statusValue = (qint64)_rd->observedBandwidth();
00074     if (_statusValue >= 400*1024) {
00075       statusIcon = QIcon(IMG_NODE_HIGH_BW);
00076     } else if (_statusValue >= 60*1024) {
00077       statusIcon = QIcon(IMG_NODE_MED_BW);
00078     } else if (_statusValue >= 20*1024) {
00079       statusIcon = QIcon(IMG_NODE_LOW_BW);
00080     } else {
00081       statusIcon = QIcon(IMG_NODE_NO_BW);
00082     }
00083     setToolTip(STATUS_COLUMN, tr("%1 KB/s").arg(_statusValue/1024));
00084   }
00085   
00086   /* Make the new information visible */
00087   setIcon(STATUS_COLUMN, statusIcon);
00088   setText(NAME_COLUMN, _rd->name());
00089   setToolTip(NAME_COLUMN, QString(_rd->name() + "\r\n" + _rd->platform()));
00090 }
00091 
00092 /** Sets the location information for this item's router descriptor. */
00093 void
00094 RouterListItem::setLocation(const GeoIp &geoip)
00095 {
00096   QPixmap flag(":/images/flags/" + geoip.country().toLower() + ".png");
00097   if (!flag.isNull()) {
00098     setIcon(COUNTRY_COLUMN, QIcon(flag));
00099   }
00100   setToolTip(COUNTRY_COLUMN, geoip.toLocation());
00101   
00102   if (_rd)
00103     _rd->setLocation(geoip.toLocation());
00104   _country = geoip.country();
00105 }
00106 
00107 /** Overload the comparison operator. */
00108 bool
00109 RouterListItem::operator<(const QTreeWidgetItem &other) const
00110 {
00111   const RouterListItem *a = this;
00112   const RouterListItem *b = (RouterListItem *)&other;
00113  
00114   if (_list) {
00115     Qt::SortOrder order = _list->header()->sortIndicatorOrder();
00116     switch (_list->sortColumn()) {
00117       case RouterListWidget::StatusColumn:
00118         /* Numeric comparison based on status and/or bandwidth */
00119         if (a->_statusValue == b->_statusValue) {
00120           if (order == Qt::AscendingOrder)
00121             return (a->name().toLower() > b->name().toLower());
00122           else
00123             return (a->name().toLower() < b->name().toLower());
00124         }
00125         return (a->_statusValue < b->_statusValue);
00126       case RouterListWidget::CountryColumn:
00127         /* Compare based on country code */
00128         if (a->_country == b->_country) {
00129           if (order == Qt::AscendingOrder)
00130             return (a->_statusValue > b->_statusValue);
00131           else
00132             return (a->_statusValue < b->_statusValue);
00133         }
00134         return (a->_country < b->_country);
00135       case RouterListWidget::NameColumn:
00136         /* Case-insensitive comparison based on router name */
00137         if (a->name().toLower() == b->name().toLower()) {
00138           if (order == Qt::AscendingOrder)
00139             return (a->_statusValue > b->_statusValue);
00140           else
00141             return (a->_statusValue < b->_statusValue);
00142         }
00143         return (a->name().toLower() < b->name().toLower());
00144       default:
00145         break;
00146     }
00147   }
00148   return QTreeWidgetItem::operator<(other);
00149 }
00150 

Generated on Sat Aug 16 17:38:35 2008 for Vidalia by  doxygen 1.5.6