2015-07-05 14:23:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-07-27 16:28:58 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
#include "core_debug.h"
|
|
|
|
#include "daemon.h"
|
|
|
|
#include "kdeconnectconfig.h"
|
|
|
|
#include "landevicelink.h"
|
2015-07-05 14:23:53 +01:00
|
|
|
#include "lanpairinghandler.h"
|
2015-07-09 22:51:08 +01:00
|
|
|
#include "networkpackagetypes.h"
|
2015-07-05 14:23:53 +01:00
|
|
|
|
2015-12-17 13:54:24 +00:00
|
|
|
LanPairingHandler::LanPairingHandler(DeviceLink* deviceLink)
|
|
|
|
: PairingHandler(deviceLink)
|
2015-12-01 18:45:14 +00:00
|
|
|
, m_status(NotPaired)
|
2015-07-14 13:04:04 +01:00
|
|
|
{
|
2015-07-25 12:45:19 +01:00
|
|
|
m_pairingTimeout.setSingleShot(true);
|
2017-01-03 21:13:02 +00:00
|
|
|
m_pairingTimeout.setInterval(pairingTimeoutMsec());
|
2015-12-06 01:11:50 +00:00
|
|
|
connect(&m_pairingTimeout, &QTimer::timeout, this, &LanPairingHandler::pairingTimeout);
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|
|
|
|
|
2015-07-27 16:28:58 +01:00
|
|
|
void LanPairingHandler::packageReceived(const NetworkPackage& np)
|
2015-07-14 13:04:04 +01:00
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
bool wantsPair = np.get<bool>(QStringLiteral("pair"));
|
2015-07-27 16:28:58 +01:00
|
|
|
|
|
|
|
if (wantsPair) {
|
|
|
|
|
2015-11-30 18:36:01 +00:00
|
|
|
if (isPairRequested()) { //We started pairing
|
2015-07-27 16:28:58 +01:00
|
|
|
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Pair answer";
|
2015-12-17 14:54:29 +00:00
|
|
|
setInternalPairStatus(Paired);
|
2015-11-30 18:36:01 +00:00
|
|
|
|
2015-07-27 16:28:58 +01:00
|
|
|
} else {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Pair request";
|
|
|
|
|
2015-12-17 14:54:29 +00:00
|
|
|
if (isPaired()) { //I'm already paired, but they think I'm not
|
2015-08-13 10:26:28 +01:00
|
|
|
acceptPairing();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:45:14 +00:00
|
|
|
setInternalPairStatus(RequestedByPeer);
|
2015-07-27 16:28:58 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 18:36:01 +00:00
|
|
|
} else { //wantsPair == false
|
2015-07-27 16:28:58 +01:00
|
|
|
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Unpair request";
|
|
|
|
|
2015-12-17 14:54:29 +00:00
|
|
|
setInternalPairStatus(NotPaired);
|
|
|
|
if (isPairRequested()) {
|
2015-12-01 18:45:14 +00:00
|
|
|
Q_EMIT pairingError(i18n("Canceled by other peer"));
|
2015-07-27 16:28:58 +01:00
|
|
|
}
|
2015-07-09 22:51:08 +01:00
|
|
|
}
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 12:45:19 +01:00
|
|
|
bool LanPairingHandler::requestPairing()
|
2015-07-14 13:04:04 +01:00
|
|
|
{
|
2015-12-01 18:45:14 +00:00
|
|
|
switch (m_status) {
|
|
|
|
case Paired:
|
2015-12-02 19:04:35 +00:00
|
|
|
Q_EMIT pairingError(i18n("%1: Already paired", deviceLink()->name()));
|
2015-07-27 16:28:58 +01:00
|
|
|
return false;
|
2015-12-01 18:45:14 +00:00
|
|
|
case Requested:
|
2015-12-02 19:04:35 +00:00
|
|
|
Q_EMIT pairingError(i18n("%1: Pairing already requested for this device", deviceLink()->name()));
|
2015-07-27 16:28:58 +01:00
|
|
|
return false;
|
2015-12-01 18:45:14 +00:00
|
|
|
case RequestedByPeer:
|
2015-11-30 18:36:01 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request.";
|
2015-07-27 16:28:58 +01:00
|
|
|
acceptPairing();
|
|
|
|
return false;
|
2015-12-01 18:45:14 +00:00
|
|
|
case NotPaired:
|
2015-07-27 16:28:58 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:07:12 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR, {{"pair", true}});
|
|
|
|
const bool success = deviceLink()->sendPackage(np);
|
2015-09-12 21:15:19 +01:00
|
|
|
if (success) {
|
2015-12-01 18:45:14 +00:00
|
|
|
setInternalPairStatus(Requested);
|
2015-09-12 21:15:19 +01:00
|
|
|
}
|
2015-07-09 22:51:08 +01:00
|
|
|
return success;
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 12:45:19 +01:00
|
|
|
bool LanPairingHandler::acceptPairing()
|
2015-07-14 13:04:04 +01:00
|
|
|
{
|
2016-06-21 19:07:12 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR, {{"pair", true}});
|
2015-12-17 14:54:29 +00:00
|
|
|
bool success = deviceLink()->sendPackage(np);
|
2015-07-27 16:28:58 +01:00
|
|
|
if (success) {
|
2015-12-01 18:45:14 +00:00
|
|
|
setInternalPairStatus(Paired);
|
2015-07-25 12:45:19 +01:00
|
|
|
}
|
2015-07-09 22:51:08 +01:00
|
|
|
return success;
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 12:45:19 +01:00
|
|
|
void LanPairingHandler::rejectPairing()
|
2015-07-14 13:04:04 +01:00
|
|
|
{
|
2016-06-21 19:07:12 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR, {{"pair", false}});
|
2015-11-30 18:36:01 +00:00
|
|
|
deviceLink()->sendPackage(np);
|
2015-12-01 18:45:14 +00:00
|
|
|
setInternalPairStatus(NotPaired);
|
2015-07-09 22:51:08 +01:00
|
|
|
}
|
2015-07-05 14:23:53 +01:00
|
|
|
|
2015-07-25 12:45:19 +01:00
|
|
|
void LanPairingHandler::unpair() {
|
2016-06-21 19:07:12 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR, {{"pair", false}});
|
2015-11-30 18:36:01 +00:00
|
|
|
deviceLink()->sendPackage(np);
|
2015-12-01 18:45:14 +00:00
|
|
|
setInternalPairStatus(NotPaired);
|
2015-07-25 12:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanPairingHandler::pairingTimeout()
|
|
|
|
{
|
2016-06-21 19:07:12 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR, {{"pair", false}});
|
2015-11-30 18:36:01 +00:00
|
|
|
deviceLink()->sendPackage(np);
|
2015-12-01 18:45:14 +00:00
|
|
|
setInternalPairStatus(NotPaired); //Will emit the change as well
|
|
|
|
Q_EMIT pairingError(i18n("Timed out"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanPairingHandler::setInternalPairStatus(LanPairingHandler::InternalPairStatus status)
|
|
|
|
{
|
2017-01-24 23:22:22 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:45:14 +00:00
|
|
|
m_status = status;
|
|
|
|
if (status == Paired) {
|
|
|
|
deviceLink()->setPairStatus(DeviceLink::Paired);
|
|
|
|
} else {
|
|
|
|
deviceLink()->setPairStatus(DeviceLink::NotPaired);
|
|
|
|
}
|
2015-07-05 14:23:53 +01:00
|
|
|
}
|