lanpairinghandler.cpp 4.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * Copyright 2015 Vineet Garg <grg.vineet@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 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 14 of version 3 of the license.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20
 */

Nicolas Fella's avatar
Nicolas Fella committed
21 22
#include "lanpairinghandler.h"

Vineet Garg's avatar
Vineet Garg committed
23 24 25 26 27 28
#include <KLocalizedString>

#include "core_debug.h"
#include "daemon.h"
#include "kdeconnectconfig.h"
#include "landevicelink.h"
29
#include "networkpackettypes.h"
30

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
31 32
LanPairingHandler::LanPairingHandler(DeviceLink* deviceLink)
    : PairingHandler(deviceLink)
Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
33
    , m_status(NotPaired)
Vineet Garg's avatar
Vineet Garg committed
34
{
35
    m_pairingTimeout.setSingleShot(true);
36
    m_pairingTimeout.setInterval(pairingTimeoutMsec());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
37
    connect(&m_pairingTimeout, &QTimer::timeout, this, &LanPairingHandler::pairingTimeout);
38 39
}

40
void LanPairingHandler::packetReceived(const NetworkPacket& np)
Vineet Garg's avatar
Vineet Garg committed
41
{
42
    bool wantsPair = np.get<bool>(QStringLiteral("pair"));
Vineet Garg's avatar
Vineet Garg committed
43 44 45

    if (wantsPair) {

Albert Vaca Cintora's avatar
WIP  
Albert Vaca Cintora committed
46
        if (isPairRequested())  { //We started pairing
Vineet Garg's avatar
Vineet Garg committed
47 48

            qCDebug(KDECONNECT_CORE) << "Pair answer";
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
49
            setInternalPairStatus(Paired);
Albert Vaca Cintora's avatar
WIP  
Albert Vaca Cintora committed
50
            
Vineet Garg's avatar
Vineet Garg committed
51 52 53
        } else {
            qCDebug(KDECONNECT_CORE) << "Pair request";

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
54
            if (isPaired()) { //I'm already paired, but they think I'm not
55 56 57 58
                acceptPairing();
                return;
            }

Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
59
            setInternalPairStatus(RequestedByPeer);
Vineet Garg's avatar
Vineet Garg committed
60 61
        }

Albert Vaca Cintora's avatar
WIP  
Albert Vaca Cintora committed
62
    } else { //wantsPair == false
Vineet Garg's avatar
Vineet Garg committed
63 64 65

        qCDebug(KDECONNECT_CORE) << "Unpair request";

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
66
         if (isPairRequested()) {
Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
67
            Q_EMIT pairingError(i18n("Canceled by other peer"));
Vineet Garg's avatar
Vineet Garg committed
68
        }
69
        setInternalPairStatus(NotPaired);
70
    }
71 72
}

73
bool LanPairingHandler::requestPairing()
Vineet Garg's avatar
Vineet Garg committed
74
{
75 76 77 78 79 80 81
    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();
Vineet Garg's avatar
Vineet Garg committed
82 83
    }

84
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), true}});
85
    const bool success = deviceLink()->sendPacket(np);
Vineet Garg's avatar
Vineet Garg committed
86
    if (success) {
Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
87
        setInternalPairStatus(Requested);
Vineet Garg's avatar
Vineet Garg committed
88
    }
89
    return success;
90 91
}

92
bool LanPairingHandler::acceptPairing()
Vineet Garg's avatar
Vineet Garg committed
93
{
94
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), true}});
95
    bool success = deviceLink()->sendPacket(np);
Vineet Garg's avatar
Vineet Garg committed
96
    if (success) {
Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
97
        setInternalPairStatus(Paired);
98
    }
99
    return success;
100 101
}

102
void LanPairingHandler::rejectPairing()
Vineet Garg's avatar
Vineet Garg committed
103
{
104
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), false}});
105
    deviceLink()->sendPacket(np);
Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
106
    setInternalPairStatus(NotPaired);
107
}
108

109
void LanPairingHandler::unpair() {
110
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), false}});
111
    deviceLink()->sendPacket(np);
Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
112
    setInternalPairStatus(NotPaired);
113 114 115 116
}

void LanPairingHandler::pairingTimeout()
{
117
    NetworkPacket np(PACKET_TYPE_PAIR, {{QStringLiteral("pair"), false}});
118
    deviceLink()->sendPacket(np);
Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
119 120 121 122 123 124
    setInternalPairStatus(NotPaired); //Will emit the change as well
    Q_EMIT pairingError(i18n("Timed out"));
}

void LanPairingHandler::setInternalPairStatus(LanPairingHandler::InternalPairStatus status)
{
125 126 127 128 129 130 131 132 133 134 135 136
    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);
    }

Albert Vaca Cintora's avatar
WIPx3!  
Albert Vaca Cintora committed
137 138 139 140 141 142
    m_status = status;
    if (status == Paired) {
        deviceLink()->setPairStatus(DeviceLink::Paired);
    } else {
        deviceLink()->setPairStatus(DeviceLink::NotPaired);
    }
143
}