1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*
Copyright (C) 2016 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "devicemodel.h"
#include "arpstatusmodel.h"
#include "ndpstatusmodel.h"
#include "port.h"
#include "emulproto.pb.h"
#include "uint128.h"
#include <QBrush>
#include <QColor>
#include <QFont>
#include <QHostAddress>
enum {
kMacAddress,
kVlans,
kIp4Address,
kIp4Gateway,
kIp6Address,
kIp6Gateway,
kArpInfo,
kNdpInfo,
kFieldCount
};
static QStringList columns_ = QStringList()
<< "Mac"
<< "Vlans"
<< "IPv4 Address"
<< "IPv4 Gateway"
<< "IPv6 Address"
<< "IPv6 Gateway"
<< "ARP"
<< "NDP";
DeviceModel::DeviceModel(QObject *parent)
: QAbstractTableModel(parent)
{
port_ = NULL;
arpStatusModel_ = new ArpStatusModel(this);
ndpStatusModel_ = new NdpStatusModel(this);
}
int DeviceModel::rowCount(const QModelIndex &parent) const
{
if (!port_ || parent.isValid())
return 0;
return port_->numDevices();
}
int DeviceModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return columns_.size();
}
QVariant DeviceModel::headerData(
int section,
Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
switch (orientation) {
case Qt::Horizontal:
return columns_[section];
case Qt::Vertical:
return QString("%1").arg(section + 1);
default:
Q_ASSERT(false); // Unreachable
}
return QVariant();
}
QVariant DeviceModel::data(const QModelIndex &index, int role) const
{
QString str;
if (!port_ || !index.isValid())
return QVariant();
int devIdx = index.row();
int field = index.column();
Q_ASSERT(devIdx < port_->numDevices());
Q_ASSERT(field < kFieldCount);
const OstEmul::Device *dev = port_->deviceByIndex(devIdx);
Q_ASSERT(dev);
switch (field) {
case kMacAddress:
switch (role) {
case Qt::DisplayRole:
return QString("%1").arg(dev->mac(), 6*2, 16, QChar('0'))
.replace(QRegExp("([0-9a-fA-F]{2}\\B)"), "\\1:")
.toUpper();
default:
break;
}
return QVariant();
case kVlans:
switch (role) {
case Qt::DisplayRole:
if (!dev->vlan_size())
return QString("None");
for (int i = 0; i < dev->vlan_size(); i++)
str.append(i == 0 ? "" : ", ")
.append(QString::number(dev->vlan(i) & 0xfff));
return str;
default:
break;
}
return QVariant();
case kIp4Address:
switch (role) {
case Qt::DisplayRole:
if (dev->has_ip4_prefix_length())
return QString("%1/%2")
.arg(QHostAddress(dev->ip4()).toString())
.arg(dev->ip4_prefix_length());
else
return QString("--");
default:
break;
}
return QVariant();
case kIp4Gateway:
switch (role) {
case Qt::DisplayRole:
if (dev->has_ip4_prefix_length())
return QHostAddress(dev->ip4_default_gateway())
.toString();
else
return QString("--");
default:
break;
}
return QVariant();
case kIp6Address:
switch (role) {
case Qt::DisplayRole:
if (dev->has_ip6_prefix_length()) {
OstEmul::Ip6Address ip = dev->ip6();
return QString("%1/%2")
.arg(QHostAddress(UInt128(ip.hi(), ip.lo())
.toArray()).toString())
.arg(dev->ip6_prefix_length());
}
else
return QString("--");
default:
break;
}
return QVariant();
case kIp6Gateway:
switch (role) {
case Qt::DisplayRole:
if (dev->has_ip6_prefix_length()) {
OstEmul::Ip6Address ip = dev->ip6_default_gateway();
return QHostAddress(
UInt128(ip.hi(), ip.lo()).toArray())
.toString();
}
else
return QString("--");
default:
break;
}
return QVariant();
case kArpInfo:
switch (role) {
case Qt::DisplayRole:
if (dev->has_ip4_prefix_length())
return QString("%1/%2")
.arg(port_->numArpResolved(devIdx))
.arg(port_->numArp(devIdx));
else
return QString("--");
default:
if (dev->has_ip4_prefix_length())
return drillableStyle(role);
break;
}
return QVariant();
case kNdpInfo:
switch (role) {
case Qt::DisplayRole:
if (dev->has_ip6_prefix_length())
return QString("%1/%2")
.arg(port_->numNdpResolved(devIdx))
.arg(port_->numNdp(devIdx));
else
return QString("--");
default:
if (dev->has_ip6_prefix_length())
return drillableStyle(role);
break;
}
return QVariant();
default:
Q_ASSERT(false); // unreachable!
break;
}
qWarning("%s: Unsupported field #%d", __FUNCTION__, field);
return QVariant();
}
Qt::DropActions DeviceModel::supportedDropActions() const
{
return Qt::IgnoreAction; // read-only model, doesn't accept any data
}
void DeviceModel::setPort(Port *port)
{
beginResetModel();
port_ = port;
if (port_)
connect(port_, SIGNAL(deviceInfoChanged()), SLOT(updateDeviceList()));
endResetModel();
}
QAbstractItemModel* DeviceModel::detailModel(const QModelIndex &index)
{
if (!index.isValid())
return NULL;
switch(index.column()) {
case kArpInfo:
arpStatusModel_->setDeviceIndex(port_, index.row());
return arpStatusModel_;
case kNdpInfo:
ndpStatusModel_->setDeviceIndex(port_, index.row());
return ndpStatusModel_;
default:
return NULL;
}
}
void DeviceModel::updateDeviceList()
{
beginResetModel();
endResetModel();
}
// Style roles for drillable fields
QVariant DeviceModel::drillableStyle(int role) const
{
QFont f;
switch (role) {
case Qt::ToolTipRole:
return QString("Click for details ...");
case Qt::ForegroundRole:
return QBrush(QColor(Qt::blue));
case Qt::FontRole:
f.setUnderline(true);
return f;
default:
break;
}
return QVariant();
}
|