2013-06-25 17:06:51 +01:00
|
|
|
#include "device.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
|
|
|
|
#include <KSharedPtr>
|
|
|
|
#include <KSharedConfig>
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <KConfigGroup>
|
2013-08-13 04:07:32 +01:00
|
|
|
#include <KStandardDirs>
|
|
|
|
#include <KPluginSelector>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KPluginInfo>
|
2013-08-30 18:10:43 +01:00
|
|
|
#include <KNotification>
|
|
|
|
#include <KIcon>
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <QDebug>
|
2013-06-25 17:06:51 +01:00
|
|
|
|
2013-08-13 04:14:46 +01:00
|
|
|
#include "plugins/kdeconnectplugin.h"
|
2013-08-13 04:07:32 +01:00
|
|
|
#include "plugins/pluginloader.h"
|
2013-09-09 17:35:56 +01:00
|
|
|
#include "backends/devicelink.h"
|
|
|
|
#include "backends/linkprovider.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
#include "networkpackage.h"
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
Device::Device(const QString& id)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2013-08-16 08:27:32 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
m_deviceId = id;
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
2013-09-03 15:01:28 +01:00
|
|
|
const KConfigGroup& data = config->group("trusted_devices").group(id);
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-09-03 15:01:28 +01:00
|
|
|
m_deviceName = data.readEntry<QString>("deviceName", QString("unnamed"));
|
2013-07-26 15:21:19 +01:00
|
|
|
|
2013-09-03 15:01:28 +01:00
|
|
|
const QString& key = data.readEntry<QString>("publicKey",QString());
|
|
|
|
m_publicKey = QCA::RSAPublicKey::fromPEM(key);
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
m_pairStatus = Device::Paired;
|
2013-08-16 05:26:40 +01:00
|
|
|
|
2013-07-26 15:21:19 +01:00
|
|
|
//Register in bus
|
2013-08-20 12:52:25 +01:00
|
|
|
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
|
2013-07-26 15:21:19 +01:00
|
|
|
|
2013-06-25 17:06:51 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
Device::Device(const NetworkPackage& identityPackage, DeviceLink* dl)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2013-08-30 18:10:43 +01:00
|
|
|
m_deviceId = identityPackage.get<QString>("deviceId");
|
|
|
|
m_deviceName = identityPackage.get<QString>("deviceName");
|
2013-08-16 08:27:32 +01:00
|
|
|
|
2013-09-03 01:14:27 +01:00
|
|
|
int protocolVersion = identityPackage.get<int>("protocolVersion");
|
|
|
|
if (protocolVersion != NetworkPackage::ProtocolVersion) {
|
2013-09-16 14:21:22 +01:00
|
|
|
qDebug() << m_deviceName << "- warning, device uses a different protocol version" << protocolVersion << "expected" << NetworkPackage::ProtocolVersion;
|
2013-09-03 01:14:27 +01:00
|
|
|
//TODO: Do something
|
|
|
|
}
|
2013-09-02 12:26:26 +01:00
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
addLink(dl);
|
2013-07-26 15:21:19 +01:00
|
|
|
|
2013-09-03 01:14:27 +01:00
|
|
|
m_pairStatus = Device::NotPaired;
|
2013-08-16 05:26:40 +01:00
|
|
|
|
|
|
|
//Register in bus
|
2013-08-20 12:52:25 +01:00
|
|
|
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-08-16 08:27:32 +01:00
|
|
|
Device::~Device()
|
|
|
|
{
|
2013-07-03 02:52:44 +01:00
|
|
|
|
|
|
|
}
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
bool Device::hasPlugin(const QString& name) const
|
2013-08-13 22:23:32 +01:00
|
|
|
{
|
|
|
|
return m_plugins.contains(name);
|
|
|
|
}
|
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
QStringList Device::loadedPlugins() const
|
2013-08-18 19:27:25 +01:00
|
|
|
{
|
|
|
|
return m_plugins.keys();
|
|
|
|
}
|
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
void Device::reloadPlugins()
|
|
|
|
{
|
2013-08-14 00:35:12 +01:00
|
|
|
QMap< QString, KdeConnectPlugin* > newPluginMap;
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
if (isPaired() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
QString path = KStandardDirs().resourceDirs("config").first()+"kdeconnect/";
|
|
|
|
QMap<QString,QString> pluginStates = KSharedConfig::openConfig(path + id())->group("Plugins").entryMap();
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
PluginLoader* loader = PluginLoader::instance();
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
//Code borrowed from KWin
|
|
|
|
foreach (const QString& pluginName, loader->getPluginList()) {
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
const QString value = pluginStates.value(pluginName + QString::fromLatin1("Enabled"), QString());
|
|
|
|
KPluginInfo info = loader->getPluginInfo(pluginName);
|
|
|
|
bool enabled = (value.isNull() ? info.isPluginEnabledByDefault() : QVariant(value).toBool());
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
if (enabled) {
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
if (m_plugins.contains(pluginName)) {
|
|
|
|
//Already loaded, reuse it
|
|
|
|
newPluginMap[pluginName] = m_plugins[pluginName];
|
|
|
|
m_plugins.remove(pluginName);
|
|
|
|
} else {
|
|
|
|
KdeConnectPlugin* plugin = loader->instantiatePluginForDevice(pluginName, this);
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
connect(this, SIGNAL(receivedPackage(NetworkPackage)),
|
|
|
|
plugin, SLOT(receivePackage(NetworkPackage)));
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
newPluginMap[pluginName] = plugin;
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 00:35:12 +01:00
|
|
|
//Erase all the plugins left in the original map (it means that we don't want
|
|
|
|
//them anymore, otherways they would have been moved to the newPluginMap)
|
|
|
|
qDeleteAll(m_plugins);
|
|
|
|
m_plugins.clear();
|
|
|
|
|
|
|
|
m_plugins = newPluginMap;
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) {
|
|
|
|
plugin->connected();
|
|
|
|
}
|
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT pluginsChanged();
|
2013-08-13 04:07:32 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
void Device::requestPair()
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus == Device::Paired) {
|
2013-08-30 18:10:43 +01:00
|
|
|
Q_EMIT pairingFailed(i18n("Already paired"));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus == Device::Requested) {
|
2013-09-01 21:13:03 +01:00
|
|
|
Q_EMIT pairingFailed(i18n("Pairing already requested for this device"));
|
|
|
|
return;
|
|
|
|
}
|
2013-08-31 12:04:00 +01:00
|
|
|
if (!isReachable()) {
|
2013-08-30 18:10:43 +01:00
|
|
|
Q_EMIT pairingFailed(i18n("Device not reachable"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
m_pairStatus = Device::Requested;
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
//Send our own public key
|
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR);
|
|
|
|
np.set("pair", true);
|
2013-07-03 02:52:44 +01:00
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
2013-09-03 15:01:28 +01:00
|
|
|
const QString& key = config->group("myself").readEntry<QString>("publicKey",QString());
|
2013-08-30 18:10:43 +01:00
|
|
|
np.set("publicKey",key);
|
|
|
|
bool success = sendPackage(np);
|
|
|
|
|
|
|
|
if (!success) {
|
2013-09-03 21:11:13 +01:00
|
|
|
m_pairStatus = Device::NotPaired;
|
2013-08-30 18:10:43 +01:00
|
|
|
Q_EMIT pairingFailed(i18n("Error contacting device"));
|
|
|
|
return;
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus == Device::Paired) return;
|
|
|
|
pairingTimer.setSingleShot(true);
|
2013-08-30 18:10:43 +01:00
|
|
|
pairingTimer.start(20 * 1000);
|
|
|
|
connect(&pairingTimer, SIGNAL(timeout()),
|
|
|
|
this, SLOT(pairingTimeout()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::unpair()
|
|
|
|
{
|
|
|
|
if (!isPaired()) return;
|
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
m_pairStatus = Device::NotPaired;
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
2013-09-03 15:01:28 +01:00
|
|
|
config->group("trusted_devices").deleteGroup(id());
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-09-03 18:14:11 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR);
|
|
|
|
np.set("pair", false);
|
|
|
|
sendPackage(np);
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
reloadPlugins(); //Will unload the plugins
|
|
|
|
|
2013-09-16 15:52:40 +01:00
|
|
|
Q_EMIT unpaired();
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::pairingTimeout()
|
|
|
|
{
|
2013-09-03 21:11:13 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR);
|
|
|
|
np.set("pair", false);
|
|
|
|
sendPackage(np);
|
|
|
|
m_pairStatus = Device::NotPaired;
|
2013-09-04 20:19:02 +01:00
|
|
|
Q_EMIT pairingFailed(i18n("Timed out"));
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool lessThan(DeviceLink* p1, DeviceLink* p2)
|
|
|
|
{
|
2013-07-24 17:42:33 +01:00
|
|
|
return p1->provider()->priority() > p2->provider()->priority();
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::addLink(DeviceLink* link)
|
|
|
|
{
|
2013-09-09 17:28:52 +01:00
|
|
|
//qDebug() << "Adding link to" << id() << "via" << link->provider();
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2013-08-16 05:26:40 +01:00
|
|
|
connect(link, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(linkDestroyed(QObject*)));
|
2013-07-04 18:17:22 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
m_deviceLinks.append(link);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2013-09-13 22:27:16 +01:00
|
|
|
//TODO: Do not read the key every time
|
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
|
|
|
const QString& key = config->group("myself").readEntry<QString>("privateKey",QString());
|
|
|
|
QCA::PrivateKey privateKey = QCA::PrivateKey::fromPEM(key);
|
|
|
|
link->setPrivateKey(privateKey);
|
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
//Theoretically we will never add two links from the same provider (the provider should destroy
|
|
|
|
//the old one before this is called), so we do not have to worry about destroying old links.
|
|
|
|
//Actually, we should not destroy them or the provider will store an invalid ref!
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
connect(link, SIGNAL(receivedPackage(NetworkPackage)),
|
|
|
|
this, SLOT(privateReceivedPackage(NetworkPackage)));
|
2013-07-04 18:17:22 +01:00
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
qSort(m_deviceLinks.begin(), m_deviceLinks.end(), lessThan);
|
2013-07-28 21:00:45 +01:00
|
|
|
|
|
|
|
if (m_deviceLinks.size() == 1) {
|
2013-08-22 02:21:08 +01:00
|
|
|
reloadPlugins(); //Will load the plugins
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT reachableStatusChanged();
|
2013-08-22 02:21:08 +01:00
|
|
|
} else {
|
|
|
|
Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) {
|
|
|
|
plugin->connected();
|
|
|
|
}
|
2013-07-28 21:00:45 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-07-04 18:17:22 +01:00
|
|
|
void Device::linkDestroyed(QObject* o)
|
|
|
|
{
|
|
|
|
removeLink(static_cast<DeviceLink*>(o));
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void Device::removeLink(DeviceLink* link)
|
|
|
|
{
|
2013-07-04 18:17:22 +01:00
|
|
|
m_deviceLinks.removeOne(link);
|
2013-07-28 21:00:45 +01:00
|
|
|
|
2013-09-09 17:28:52 +01:00
|
|
|
//qDebug() << "RemoveLink" << m_deviceLinks.size() << "links remaining";
|
2013-07-28 21:00:45 +01:00
|
|
|
|
|
|
|
if (m_deviceLinks.empty()) {
|
2013-08-16 00:01:58 +01:00
|
|
|
reloadPlugins();
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT reachableStatusChanged();
|
2013-07-28 21:00:45 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
bool Device::sendPackage(NetworkPackage& np)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2013-09-03 15:01:28 +01:00
|
|
|
if (np.type() != PACKAGE_TYPE_PAIR && isPaired()) {
|
2013-09-13 22:27:16 +01:00
|
|
|
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
|
|
|
|
//TODO: Actually detect if a package is received or not, now we keep TCP
|
|
|
|
//"ESTABLISHED" connections that look legit (return true when we use them),
|
|
|
|
//but that are actually broken
|
|
|
|
if (dl->sendPackageEncrypted(m_publicKey, np)) return true;
|
|
|
|
}
|
2013-09-01 21:13:03 +01:00
|
|
|
} else {
|
|
|
|
//Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data
|
2013-09-13 22:27:16 +01:00
|
|
|
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
|
|
|
|
//TODO: Actually detect if a package is received or not, now we keep TCP
|
|
|
|
//"ESTABLISHED" connections that look legit (return true when we use them),
|
|
|
|
//but that are actually broken
|
|
|
|
if (dl->sendPackage(np)) return true;
|
|
|
|
}
|
2013-08-13 04:40:39 +01:00
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
return false;
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::privateReceivedPackage(const NetworkPackage& np)
|
|
|
|
{
|
2013-08-30 18:10:43 +01:00
|
|
|
if (np.type() == PACKAGE_TYPE_PAIR) {
|
|
|
|
|
2013-09-09 17:28:52 +01:00
|
|
|
//qDebug() << "Pair package";
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
bool wantsPair = np.get<bool>("pair");
|
2013-09-01 21:13:03 +01:00
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
if (wantsPair == isPaired()) {
|
|
|
|
qDebug() << "Already" << (wantsPair? "paired":"unpaired");
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus == Device::Requested) {
|
2013-09-01 21:13:03 +01:00
|
|
|
m_pairStatus = Device::NotPaired;
|
|
|
|
pairingTimer.stop();
|
|
|
|
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
|
|
|
|
}
|
2013-08-30 18:10:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:04:00 +01:00
|
|
|
if (wantsPair) {
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-09-03 15:01:28 +01:00
|
|
|
//Retrieve their public key
|
|
|
|
const QString& key = np.get<QString>("publicKey");
|
|
|
|
m_publicKey = QCA::RSAPublicKey::fromPEM(key);
|
|
|
|
if (m_publicKey.isNull()) {
|
|
|
|
qDebug() << "ERROR decoding key";
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus == Device::Requested) {
|
|
|
|
m_pairStatus = Device::NotPaired;
|
2013-09-03 15:01:28 +01:00
|
|
|
pairingTimer.stop();
|
|
|
|
}
|
|
|
|
Q_EMIT pairingFailed(i18n("Received incorrect key"));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-01 21:13:03 +01:00
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus == Device::Requested) { //We started pairing
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
qDebug() << "Pair answer";
|
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
m_pairStatus = Device::Paired;
|
2013-08-30 18:10:43 +01:00
|
|
|
pairingTimer.stop();
|
|
|
|
|
|
|
|
//Store as trusted device
|
2013-09-01 21:13:03 +01:00
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
2013-09-03 15:01:28 +01:00
|
|
|
config->group("trusted_devices").group(id()).writeEntry("publicKey",key);
|
|
|
|
config->group("trusted_devices").group(id()).writeEntry("deviceName",name());
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
reloadPlugins();
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
Q_EMIT pairingSuccesful();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
qDebug() << "Pair request";
|
|
|
|
|
|
|
|
KNotification* notification = new KNotification("pingReceived"); //KNotification::Persistent
|
|
|
|
notification->setPixmap(KIcon("dialog-information").pixmap(48, 48));
|
|
|
|
notification->setComponentData(KComponentData("kdeconnect", "kdeconnect"));
|
|
|
|
notification->setTitle("KDE Connect");
|
|
|
|
notification->setText(i18n("Pairing request from %1", m_deviceName));
|
|
|
|
notification->setActions(QStringList() << i18n("Accept") << i18n("Reject"));
|
|
|
|
connect(notification, SIGNAL(action1Activated()), this, SLOT(acceptPairing()));
|
|
|
|
connect(notification, SIGNAL(action2Activated()), this, SLOT(rejectPairing()));
|
|
|
|
notification->sendEvent();
|
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
m_pairStatus = Device::RequestedByPeer;
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
qDebug() << "Unpair request";
|
2013-09-03 15:01:28 +01:00
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus == Device::Requested) {
|
2013-08-30 18:10:43 +01:00
|
|
|
pairingTimer.stop();
|
|
|
|
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
|
2013-09-03 21:11:13 +01:00
|
|
|
} else if (m_pairStatus == Device::Paired) {
|
2013-09-03 15:01:28 +01:00
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
|
|
|
config->group("trusted_devices").deleteGroup(id());
|
|
|
|
reloadPlugins();
|
2013-09-16 15:52:40 +01:00
|
|
|
Q_EMIT unpaired();
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:14:33 +01:00
|
|
|
m_pairStatus = Device::NotPaired;
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!isPaired()) {
|
|
|
|
|
2013-09-03 15:01:28 +01:00
|
|
|
//TODO: Notify the other side that we don't trust them
|
2013-08-30 18:10:43 +01:00
|
|
|
qDebug() << "device" << name() << "not paired, ignoring package" << np.type();
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
} else {
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-09-13 22:27:16 +01:00
|
|
|
//Forward package
|
|
|
|
Q_EMIT receivedPackage(np);
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::acceptPairing()
|
|
|
|
{
|
2013-09-03 21:11:13 +01:00
|
|
|
if (m_pairStatus != Device::RequestedByPeer) return;
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
qDebug() << "Accepted pairing";
|
|
|
|
|
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
|
|
|
|
|
|
|
//Send our own public key
|
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR);
|
|
|
|
np.set("pair", true);
|
2013-09-03 15:01:28 +01:00
|
|
|
const QString& key = config->group("myself").readEntry<QString>("publicKey",QString());
|
2013-08-30 18:10:43 +01:00
|
|
|
np.set("publicKey",key);
|
|
|
|
bool success = sendPackage(np);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Store as trusted device
|
2013-09-03 15:01:28 +01:00
|
|
|
config->group("trusted_devices").group(id()).writeEntry("publicKey", m_publicKey.toPEM());
|
|
|
|
config->group("trusted_devices").group(id()).writeEntry("deviceName", name());
|
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
m_pairStatus = Device::Paired;
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
reloadPlugins(); //This will load plugins
|
2013-09-03 21:11:13 +01:00
|
|
|
|
|
|
|
Q_EMIT pairingSuccesful();
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::rejectPairing()
|
|
|
|
{
|
|
|
|
qDebug() << "Rejected pairing";
|
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
m_pairStatus = Device::NotPaired;
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_PAIR);
|
|
|
|
np.set("pair", false);
|
|
|
|
sendPackage(np);
|
|
|
|
|
2013-09-03 21:11:13 +01:00
|
|
|
Q_EMIT pairingFailed(i18n("Canceled by the user"));
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
QStringList Device::availableLinks() const
|
|
|
|
{
|
|
|
|
QStringList sl;
|
|
|
|
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
|
2013-07-24 17:42:33 +01:00
|
|
|
sl.append(dl->provider()->name());
|
2013-07-23 15:11:54 +01:00
|
|
|
}
|
|
|
|
return sl;
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void Device::sendPing()
|
|
|
|
{
|
2013-07-04 00:09:49 +01:00
|
|
|
NetworkPackage np("kdeconnect.ping");
|
|
|
|
bool success = sendPackage(np);
|
|
|
|
qDebug() << "sendPing:" << success;
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|