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
|
/*
networkinterfacemodel.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "networkinterfacemodel.h"
#include <core/metaenum.h>
#include <QNetworkInterface>
#include <limits>
using namespace GammaRay;
#define F(x) \
{ \
QNetworkInterface::x, #x \
}
static const MetaEnum::Value<QNetworkInterface::InterfaceFlag> interface_flag_table[] = {
F(IsUp),
F(IsRunning),
F(CanBroadcast),
F(IsLoopBack),
F(IsPointToPoint),
F(CanMulticast)
};
#undef F
static const quintptr IFACE_ID = std::numeric_limits<quintptr>::max();
NetworkInterfaceModel::NetworkInterfaceModel(QObject *parent)
: QAbstractItemModel(parent)
{
m_interfaces = QNetworkInterface::allInterfaces();
}
NetworkInterfaceModel::~NetworkInterfaceModel() = default;
int NetworkInterfaceModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
int NetworkInterfaceModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
if (parent.internalId() == IFACE_ID)
return m_interfaces.at(parent.row()).addressEntries().size();
return 0;
}
return m_interfaces.size();
}
QVariant NetworkInterfaceModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole) {
if (index.internalId() == IFACE_ID) {
const auto &iface = m_interfaces.at(index.row());
switch (index.column()) {
case 0:
if (iface.name() == iface.humanReadableName())
return iface.name();
return QStringLiteral("%1 (%2)").arg(iface.name(), iface.humanReadableName());
case 1:
return iface.hardwareAddress();
case 2:
return MetaEnum::flagsToString(iface.flags(), interface_flag_table);
}
} else if (index.column() == 0) {
const auto iface = m_interfaces.at(index.internalId());
const auto addr = iface.addressEntries().at(index.row());
return QString(addr.ip().toString() + QLatin1Char('/') + addr.netmask().toString());
}
}
return QVariant();
}
QVariant NetworkInterfaceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case 0:
return tr("Interface");
case 1:
return tr("Hardware Address");
case 2:
return tr("Flags");
}
}
return QAbstractItemModel::headerData(section, orientation, role);
}
QModelIndex NetworkInterfaceModel::parent(const QModelIndex &child) const
{
if (!child.isValid() || child.internalId() == IFACE_ID)
return {};
return createIndex(child.internalId(), 0, IFACE_ID);
}
QModelIndex NetworkInterfaceModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid())
return createIndex(row, column, IFACE_ID);
if (parent.internalId() == IFACE_ID)
return createIndex(row, column, parent.row());
return {};
}
|