/*
    Copyright 2021 Ivailo Monev <xakepa10@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include "devinfonetworkinterface.h"
#include "devinfodevice.h"

#include <QDebug>

using namespace Solid::Backends::Devinfo;

NetworkInterface::NetworkInterface(DevinfoDevice *device)
    : DeviceInterface(device)
{
    m_iface = QNetworkInterface::interfaceFromName(device->deviceProperty(DevinfoDevice::DeviceName));
}

NetworkInterface::~NetworkInterface()
{
}

QString NetworkInterface::ifaceName() const
{
    return m_iface.name();
}

bool NetworkInterface::isWireless() const
{
    // for reference:
    // freebsd-src/sbin/ifconfig/ifconfig.8
    return ifaceName().startsWith("wlan");
}

bool NetworkInterface::isLoopback() const
{
    return (m_iface.flags() & QNetworkInterface::IsLoopBack);
}

QString NetworkInterface::hwAddress() const
{
    const QString result = m_iface.hardwareAddress();
    if (result.isEmpty()) {
        return QLatin1String("00:00:00:00:00:00");
    }
    return result;
}

qulonglong NetworkInterface::macAddress() const
{
    const QString hwAddr(hwAddress());
    if (hwAddr != QLatin1String("00:00:00:00:00:00")) {
        // there is no documentation on what the return value should be
        return qHash(hwAddr);
    }
    return 0;
}

#include "backends/devinfo/moc_devinfonetworkinterface.cpp"
