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 1238 2006-09-25 17:50:57Z edmanm $ 00025 * \brief Item representing a single router and status in a RouterListWidget 00026 */ 00027 00028 #include "routerlistwidget.h" 00029 #include "routerlistitem.h" 00030 00031 #define STATUS_COLUMN (RouterListWidget::StatusColumn) 00032 #define NAME_COLUMN (RouterListWidget::NameColumn) 00033 00034 #define IMG_NODE_OFFLINE ":/images/icons/node-unresponsive.png" 00035 #define IMG_NODE_SLEEPING ":/images/icons/node-hibernating.png" 00036 #define IMG_NODE_NO_BW ":/images/icons/node-bw-none.png" 00037 #define IMG_NODE_LOW_BW ":/images/icons/node-bw-low.png" 00038 #define IMG_NODE_MED_BW ":/images/icons/node-bw-med.png" 00039 #define IMG_NODE_HIGH_BW ":/images/icons/node-bw-high.png" 00040 00041 00042 /** Default constructor. */ 00043 RouterListItem::RouterListItem(RouterListWidget *list, RouterDescriptor rd) 00044 : QTreeWidgetItem() 00045 { 00046 _list = list; 00047 _rd = 0; 00048 update(rd); 00049 } 00050 00051 /** Destructor. */ 00052 RouterListItem::~RouterListItem() 00053 { 00054 delete _rd; 00055 } 00056 00057 /** Updates the router descriptor for this item. */ 00058 void 00059 RouterListItem::update(RouterDescriptor rd) 00060 { 00061 QIcon statusIcon; 00062 if (_rd) { 00063 delete _rd; 00064 } 00065 _rd = new RouterDescriptor(rd); 00066 00067 /* Determine the status value (used for sorting) and icon */ 00068 if (_rd->offline()) { 00069 _statusValue = -1; 00070 statusIcon = QIcon(IMG_NODE_OFFLINE); 00071 setToolTip(STATUS_COLUMN, tr("Offline")); 00072 } else if (_rd->hibernating()) { 00073 _statusValue = 0; 00074 statusIcon = QIcon(IMG_NODE_SLEEPING); 00075 setToolTip(STATUS_COLUMN, tr("Hibernating")); 00076 } else { 00077 _statusValue = (qint64)_rd->observedBandwidth(); 00078 if (_statusValue >= 400*1024) { 00079 statusIcon = QIcon(IMG_NODE_HIGH_BW); 00080 } else if (_statusValue >= 60*1024) { 00081 statusIcon = QIcon(IMG_NODE_MED_BW); 00082 } else if (_statusValue >= 20*1024) { 00083 statusIcon = QIcon(IMG_NODE_LOW_BW); 00084 } else { 00085 statusIcon = QIcon(IMG_NODE_NO_BW); 00086 } 00087 setToolTip(STATUS_COLUMN, tr("%1 KB/s").arg(_statusValue/1024)); 00088 } 00089 00090 /* Make the new information visible */ 00091 setIcon(STATUS_COLUMN, statusIcon); 00092 setText(NAME_COLUMN, _rd->name()); 00093 setToolTip(NAME_COLUMN, QString(_rd->name() + "\r\n" + _rd->platform())); 00094 } 00095 00096 /** Sets the location information for this item's router descriptor. */ 00097 void 00098 RouterListItem::setLocation(QString location) 00099 { 00100 _rd->setLocation(location); 00101 } 00102 00103 /** Overload the comparison operator. */ 00104 bool 00105 RouterListItem::operator<(const QTreeWidgetItem &other) const 00106 { 00107 const RouterListItem *a = this; 00108 const RouterListItem *b = (RouterListItem *)&other; 00109 00110 if (_list) { 00111 if (_list->sortColumn() == RouterListWidget::StatusColumn) { 00112 /* Numeric comparison based on status and/or bandwidth */ 00113 return (a->_statusValue > b->_statusValue); 00114 } else if (_list->sortColumn() == RouterListWidget::NameColumn) { 00115 /* Perform a case-insensitive comparison based on router name */ 00116 return (a->name().toLower() > b->name().toLower()); 00117 } 00118 } 00119 return QTreeWidgetItem::operator<(other); 00120 } 00121