#include "networkinterface.h"

NetworkInterface::NetworkInterface(int interfaceIndex, QObject *parent)
    : QObject{parent},
    m_qni{QNetworkInterface::interfaceFromIndex(interfaceIndex)}
{
    setName(m_qni.name());
    auto adresses = m_qni.addressEntries();
    if ( adresses.size() == 0 )
        setAddress("NO ADRESS");
    else
        setAddress(adresses.first().ip().toString());
}

NetworkInterface::NetworkInterface(QNetworkInterface iface, QObject *parent)
    : QObject{parent},
    m_qni{iface}
{
    setName(m_qni.name());
    auto adresses = m_qni.addressEntries();
    if ( adresses.size() == 0 )
        setAddress("NO ADRESS");
    else
        setAddress(adresses.first().ip().toString());
}

QString NetworkInterface::name() const
{
    return m_name;
}

void NetworkInterface::setName(const QString &newName)
{
    if (m_name == newName)
        return;
    m_name = newName;
    emit nameChanged();
}

QString NetworkInterface::address() const
{
    return m_address;
}

void NetworkInterface::setAddress(const QString &newAddress)
{
    if (m_address == newAddress)
        return;
    m_address = newAddress;
    emit addressChanged();
}

void NetworkInterface::shutdown()
{
    qWarning() << "Shutting down interface";
}
