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

83 lines
2.6 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-09 22:51:08 +01:00
#include <kdeconnectconfig.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-07-14 13:04:04 +01:00
LanPairingHandler::LanPairingHandler()
{
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
void LanPairingHandler::createPairPackage(NetworkPackage& np)
{
2015-07-09 22:51:08 +01:00
np.set("pair", true);
np.set("publicKey", KdeConnectConfig::instance()->publicKey().toPEM());
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
bool LanPairingHandler::packageReceived(Device *device,const NetworkPackage& np)
{
2015-07-09 22:51:08 +01:00
//Retrieve their public key
2015-07-14 13:04:04 +01:00
QString keyString = np.get<QString>("publicKey");
2015-07-09 22:51:08 +01:00
device->setPublicKey(QCA::RSAPublicKey::fromPEM(keyString));
if (device->publicKey().isNull()) {
return false;
}
return true;
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
bool LanPairingHandler::requestPairing(Device *device)
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
2015-07-09 22:51:08 +01:00
bool success = device->sendPackage(np);
return success;
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
bool LanPairingHandler::acceptPairing(Device *device)
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
2015-07-09 22:51:08 +01:00
bool success = device->sendPackage(np);
return success;
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
void LanPairingHandler::rejectPairing(Device *device)
{
2015-07-09 22:51:08 +01:00
// TODO : check status of reject pairing
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
device->sendPackage(np);
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
void LanPairingHandler::pairingDone(Device *device)
{
2015-07-09 22:51:08 +01:00
// No need to worry, if either of certificate or public key is null an empty qstring will be returned
KdeConnectConfig::instance()->setDeviceProperty(device->id(), "key", device->publicKey().toPEM());
KdeConnectConfig::instance()->setDeviceProperty(device->id(), "certificate", QString(device->certificate().toPem()));
}
2015-07-05 14:23:53 +01:00
2015-07-09 22:51:08 +01:00
void LanPairingHandler::unpair(Device *device) {
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
bool success = device->sendPackage(np);
2015-07-14 13:04:04 +01:00
Q_UNUSED(success);
2015-07-05 14:23:53 +01:00
}