[OpenDocString] kdeconnect-kde (cpp)
bluetoothpairinghandler.cpp
BluetoothPairingHandler::BluetoothPairingHandler(DeviceLink *deviceLink)
    : PairingHandler(deviceLink)
    , m_status(NotPaired)
{
    m_pairingTimeout.setSingleShot(true);
    m_pairingTimeout.setInterval(pairingTimeoutMsec());
    connect(&m_pairingTimeout, &QTimer::timeout, this, &BluetoothPairingHandler::pairingTimeout);
}
This constructor builds a pairing handler object and sets the pairing timeout to single shot mode and sets the interval and timer to run the pairing timeoutMsec.
void BluetoothPairingHandler::packetReceived(const NetworkPacket &np)
{
    qCDebug(KDECONNECT_CORE) << "Pairing packet received!" << np.serialize();

    m_pairingTimeout.stop();

    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";

        setInternalPairStatus(NotPaired);
        if (isPairRequested()) {
            Q_EMIT pairingError(i18n("Canceled by other peer"));
        }
    }
}
This method receives a NetworkPacket object np and sets the internal pair status to paired or not paired. It checks if the packet wants pairing, if it wants, and sets the internal pair status to paired. If the packet wants pairing, the peer wants pairing and sets the internal pair status to requestedByPeer.
bool BluetoothPairingHandler::requestPairing()
{
    switch (m_status) {
    case Paired:
        Q_EMIT pairingError(i18n("%1: Already paired", deviceLink()->name()));
        return false;
    case Requested:
        Q_EMIT pairingError(i18n("%1: Pairing already requested for this device", deviceLink()->name()));
        return false;
    case RequestedByPeer:
        qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request.";
        acceptPairing();
        return false;
    case NotPaired:;
    }

    NetworkPacket np(PACKET_TYPE_PAIR);
    np.set(QStringLiteral("pair"), true);
    bool success;
    success = deviceLink()->sendPacket(np);
    if (success) {
        setInternalPairStatus(Requested);
        m_pairingTimeout.start();
    }
    return success;
}
This requests that the device be paired. It returns true if the device is paired, false otherwise.
bool BluetoothPairingHandler::acceptPairing()
{
    qCDebug(KDECONNECT_CORE) << "User accepts pairing";
    m_pairingTimeout.stop(); // Just in case it is started
    NetworkPacket np(PACKET_TYPE_PAIR);
    np.set(QStringLiteral("pair"), true);
    bool success = deviceLink()->sendPacket(np);
    if (success) {
        setInternalPairStatus(Paired);
    }
    return success;
}
This sends a pairing network packet and sets the internal pair status to paired. It returns true on the first successful pairing. It returns false if the pairing failed.
void BluetoothPairingHandler::rejectPairing()
{
    qCDebug(KDECONNECT_CORE) << "User rejects pairing";
    NetworkPacket np(PACKET_TYPE_PAIR);
    np.set(QStringLiteral("pair"), false);
    deviceLink()->sendPacket(np);
    setInternalPairStatus(NotPaired);
}
This sends a pairing network packet and sets the internal pair status to not paired.
void BluetoothPairingHandler::unpair()
{
    NetworkPacket np(PACKET_TYPE_PAIR);
    np.set(QStringLiteral("pair"), false);
    deviceLink()->sendPacket(np);
    setInternalPairStatus(NotPaired);
}
This sends a pair network packet and marks the device as not paired. Finally it sets the internal pair status to not paired.
void BluetoothPairingHandler::pairingTimeout()
{
    NetworkPacket np(PACKET_TYPE_PAIR);
    np.set(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 change signal.
void BluetoothPairingHandler::setInternalPairStatus(BluetoothPairingHandler::InternalPairStatus status)
{
    m_status = status;
    if (status == Paired) {
        deviceLink()->setPairStatus(DeviceLink::Paired);
    } else if (status == NotPaired) {
        deviceLink()->setPairStatus(DeviceLink::NotPaired);
    } else if (status == RequestedByPeer) {
        Q_EMIT deviceLink()->pairingRequest(this);
    }
}
Sets the internal pair status of the device link. It sets the status of the device link and requests it if it is paired or not paired. It also sets the status of the device link to not paired.