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

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