[OpenDocString] kdeconnect-kde (cpp)
lanpairinghandler.cpp
LanPairingHandler::LanPairingHandler(DeviceLink *deviceLink)
    : PairingHandler(deviceLink)
    , m_status(NotPaired)
{
    m_pairingTimeout.setSingleShot(true);
    m_pairingTimeout.setInterval(pairingTimeoutMsec());
    connect(&m_pairingTimeout, &QTimer::timeout, this, &LanPairingHandler::pairingTimeout);
}
This constructor builds a pairing handler. It sets single shot mode and sets interval and sets signal pairingTimeout.
void LanPairingHandler::packetReceived(const NetworkPacket &np)
{
    bool wantsPair = np.get(QStringLiteral("pair"));

    if (wantsPair) {
        if (isPairRequested()) { // We started pairing

            qCDebug(KDECONNECT_CORE) << "Pair answer";
            setInternalPairStatus(Paired);

        } else {
            qCDebug(KDECONNECT_CORE) << "Pair request";

            if (isPaired()) { // I'm already paired, but they think I'm not
                acceptPairing();
                return;
            }

            setInternalPairStatus(RequestedByPeer);
        }

    } else { // wantsPair == false

        qCDebug(KDECONNECT_CORE) << "Unpair request";

        if (isPairRequested()) {
            Q_EMIT pairingError(i18n("Canceled by other peer"));
        }
        setInternalPairStatus(NotPaired);
    }
}
This code receives a NetworkPacket object np and checks if the packet wants pairing. It checks if the packet wants pairing, if it is requested, and sets the internal pair status to paired. If it doesn't wants pairing, it logs a warning.
bool LanPairingHandler::requestPairing()
{
    if (m_status == Paired) {
        Q_EMIT pairingError(i18n("%1: Already paired", deviceLink()->name()));
        return false;
    }
    if (m_status == RequestedByPeer) {
        qCDebug(KDECONNECT_CORE) << deviceLink()->name() << ": Pairing already started by the other end, accepting their request.";
        return acceptPairing();
    }

    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), true}});
    const bool success = deviceLink()->sendPacket(np);
    if (success) {
        setInternalPairStatus(Requested);
    }
    return success;
}
This requests that the device be paired. It returns true if the device is paired, false otherwise.
bool LanPairingHandler::acceptPairing()
{
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), true}});
    bool success = deviceLink()->sendPacket(np);
    if (success) {
        setInternalPairStatus(Paired);
    }
    return success;
}
This sends a pair network packet and sets the pair status to paired. It returns true on the first successful pair.
void LanPairingHandler::rejectPairing()
{
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), false}});
    deviceLink()->sendPacket(np);
    setInternalPairStatus(NotPaired);
}
This sends a pairing network packet and sets the internal pair status to not paired.
void LanPairingHandler::unpair()
{
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), false}});
    deviceLink()->sendPacket(np);
    setInternalPairStatus(NotPaired);
}
This sends a pair network packet and marks the device as not paired.
void LanPairingHandler::pairingTimeout()
{
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), false}});
    deviceLink()->sendPacket(np);
    setInternalPairStatus(NotPaired); // Will emit the change as well
    Q_EMIT pairingError(i18n("Timed out"));
}
This sends a pair network packet and sets the pair status to not paired. It also emits the pairing error signal.
void LanPairingHandler::setInternalPairStatus(LanPairingHandler::InternalPairStatus status)
{
    if (status == Requested || status == RequestedByPeer) {
        m_pairingTimeout.start();
    } else {
        m_pairingTimeout.stop();
    }

    if (m_status == RequestedByPeer && (status == NotPaired || status == Paired)) {
        Q_EMIT deviceLink()->pairingRequestExpired(this);
    } else if (status == RequestedByPeer) {
        Q_EMIT deviceLink()->pairingRequest(this);
    }

    m_status = status;
    if (status == Paired) {
        deviceLink()->setPairStatus(DeviceLink::Paired);
    } else {
        deviceLink()->setPairStatus(DeviceLink::NotPaired);
    }
}
This sets the internal pair status of the device link. It starts pairing timeout, sets the pair status to requested, and stops pairing timeout. If the status is not paired, it requests the pairing request by the peer.