Vidalia  0.3.1
RouterListItem.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file RouterListItem.cpp
13 ** \brief Item representing a single router and status in a RouterListWidget
14 */
15 
16 #include "RouterListItem.h"
17 #include "RouterListWidget.h"
18 
19 #include <QHeaderView>
20 
21 #define STATUS_COLUMN (RouterListWidget::StatusColumn)
22 #define COUNTRY_COLUMN (RouterListWidget::CountryColumn)
23 #define NAME_COLUMN (RouterListWidget::NameColumn)
24 
25 #define IMG_NODE_OFFLINE ":/images/icons/node-unresponsive.png"
26 #define IMG_NODE_SLEEPING ":/images/icons/node-hibernating.png"
27 #define IMG_NODE_NO_BW ":/images/icons/node-bw-none.png"
28 #define IMG_NODE_LOW_BW ":/images/icons/node-bw-low.png"
29 #define IMG_NODE_MED_BW ":/images/icons/node-bw-med.png"
30 #define IMG_NODE_HIGH_BW ":/images/icons/node-bw-high.png"
31 #define IMG_FLAG_UNKNOWN ":/images/flags/unknown.png"
32 
33 
34 /** Default constructor. */
36  : QTreeWidgetItem()
37 {
38  _list = list;
39  _rd = 0;
40  _countryCode = "~"; /* Force items with no country to the bottom */
41  setIcon(COUNTRY_COLUMN, QIcon(IMG_FLAG_UNKNOWN));
42  update(rd);
43 }
44 
45 /** Destructor. */
47 {
48  if (_rd)
49  delete _rd;
50 }
51 
52 /** Updates the router descriptor for this item. */
53 void
55 {
56  QIcon statusIcon;
57  if (_rd) {
58  delete _rd;
59  }
60  _rd = new RouterDescriptor(rd);
61 
62  /* Determine the status value (used for sorting) and icon */
63  if (_rd->offline()) {
64  _statusValue = -1;
65  statusIcon = QIcon(IMG_NODE_OFFLINE);
66  setToolTip(STATUS_COLUMN, tr("Offline"));
67  } else if (_rd->hibernating()) {
68  _statusValue = 0;
69  statusIcon = QIcon(IMG_NODE_SLEEPING);
70  setToolTip(STATUS_COLUMN, tr("Hibernating"));
71  } else {
72  _statusValue = (qint64)qMin(_rd->observedBandwidth(),
73  qMin(_rd->averageBandwidth(),
74  _rd->burstBandwidth()));
75  if (_statusValue >= 400*1024) {
76  statusIcon = QIcon(IMG_NODE_HIGH_BW);
77  } else if (_statusValue >= 60*1024) {
78  statusIcon = QIcon(IMG_NODE_MED_BW);
79  } else if (_statusValue >= 20*1024) {
80  statusIcon = QIcon(IMG_NODE_LOW_BW);
81  } else {
82  statusIcon = QIcon(IMG_NODE_NO_BW);
83  }
84  setToolTip(STATUS_COLUMN, tr("%1 KB/s").arg(_statusValue/1024));
85  }
86 
87  /* Make the new information visible */
88  setIcon(STATUS_COLUMN, statusIcon);
89  setText(NAME_COLUMN, _rd->name());
90  setToolTip(NAME_COLUMN, QString(_rd->name() + "\r\n" + _rd->platform()));
91 }
92 
93 /** Sets the location information for this item's router descriptor. */
94 void
96 {
97  QPixmap flag(":/images/flags/" + geoip.countryCode().toLower() + ".png");
98  if (!flag.isNull()) {
99  setIcon(COUNTRY_COLUMN, QIcon(flag));
100  }
101  setToolTip(COUNTRY_COLUMN, geoip.toString());
102 
103  if (_rd)
104  _rd->setLocation(geoip.toString());
105  _countryCode = geoip.countryCode();
106 }
107 
108 /** Overload the comparison operator. */
109 bool
110 RouterListItem::operator<(const QTreeWidgetItem &other) const
111 {
112  const RouterListItem *a = this;
113  const RouterListItem *b = (RouterListItem *)&other;
114 
115  if (_list) {
116  Qt::SortOrder order = _list->header()->sortIndicatorOrder();
117  switch (_list->sortColumn()) {
119  /* Numeric comparison based on status and/or bandwidth */
120  if (a->_statusValue == b->_statusValue) {
121  if (order == Qt::AscendingOrder)
122  return (a->name().toLower() > b->name().toLower());
123  else
124  return (a->name().toLower() < b->name().toLower());
125  }
126  return (a->_statusValue < b->_statusValue);
128  /* Compare based on country code */
129  if (a->_countryCode == b->_countryCode) {
130  if (order == Qt::AscendingOrder)
131  return (a->_statusValue > b->_statusValue);
132  else
133  return (a->_statusValue < b->_statusValue);
134  }
135  return (a->_countryCode < b->_countryCode);
137  /* Case-insensitive comparison based on router name */
138  if (a->name().toLower() == b->name().toLower()) {
139  if (order == Qt::AscendingOrder)
140  return (a->_statusValue > b->_statusValue);
141  else
142  return (a->_statusValue < b->_statusValue);
143  }
144  return (a->name().toLower() < b->name().toLower());
145  default:
146  break;
147  }
148  }
149  return QTreeWidgetItem::operator<(other);
150 }
151