kdeconnect-kde/core/backends/lan/lanpairinghandler.cpp

142 lines
4.3 KiB
C++
Raw Normal View History

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
{
m_pairingTimeout.setSingleShot(true);
m_pairingTimeout.setInterval(30 * 1000); //30 seconds of timeout
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
{
m_pairingTimeout.stop();
2015-07-27 16:28:58 +01: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-02 19:04:35 +00:00
Daemon::instance()->askPairingConfirmation(this);
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
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
}
setInternalPairStatus(NotPaired);
2015-07-09 22:51:08 +01:00
}
2015-07-05 14:23:53 +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
;
}
NetworkPackage np(PACKAGE_TYPE_PAIR, {{"pair", true}});
const bool success = deviceLink()->sendPackage(np);
if (success) {
2015-12-01 18:45:14 +00:00
setInternalPairStatus(Requested);
m_pairingTimeout.start();
}
2015-07-09 22:51:08 +01:00
return success;
2015-07-05 14:23:53 +01:00
}
bool LanPairingHandler::acceptPairing()
2015-07-14 13:04:04 +01:00
{
2015-11-30 18:36:01 +00:00
m_pairingTimeout.stop(); // Just in case it is started
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-09 22:51:08 +01:00
return success;
2015-07-05 14:23:53 +01:00
}
void LanPairingHandler::rejectPairing()
2015-07-14 13:04:04 +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
void LanPairingHandler::unpair() {
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);
}
void LanPairingHandler::pairingTimeout()
{
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)
{
m_status = status;
if (status == Paired) {
deviceLink()->setPairStatus(DeviceLink::Paired);
} else {
deviceLink()->setPairStatus(DeviceLink::NotPaired);
}
2015-07-05 14:23:53 +01:00
}