2014-06-09 10:52:16 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2014-06-09 10:52:16 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2014-06-09 10:52:16 +01:00
|
|
|
*/
|
|
|
|
|
2013-06-25 17:06:51 +01:00
|
|
|
#include "device.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2018-05-15 23:15:05 +01:00
|
|
|
#include <QSet>
|
2016-07-05 13:13:48 +01:00
|
|
|
#include <QSslCertificate>
|
2020-11-26 10:28:32 +00:00
|
|
|
#include <QSslKey>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QVector>
|
2014-06-09 10:52:16 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <KConfigGroup>
|
2014-09-22 01:37:10 +01:00
|
|
|
#include <KLocalizedString>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KSharedConfig>
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-09-09 17:35:56 +01:00
|
|
|
#include "backends/devicelink.h"
|
2017-07-22 10:13:21 +01:00
|
|
|
#include "backends/lan/landevicelink.h"
|
2013-09-09 17:35:56 +01:00
|
|
|
#include "backends/linkprovider.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "core_debug.h"
|
2015-03-24 11:26:37 +00:00
|
|
|
#include "daemon.h"
|
2019-06-09 16:28:49 +01:00
|
|
|
#include "dbushelper.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "kdeconnectconfig.h"
|
|
|
|
#include "kdeconnectplugin.h"
|
|
|
|
#include "networkpacket.h"
|
|
|
|
#include "pluginloader.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2018-05-15 23:15:05 +01:00
|
|
|
class Device::DevicePrivate
|
|
|
|
{
|
|
|
|
public:
|
2023-06-27 12:10:59 +01:00
|
|
|
DevicePrivate(const DeviceInfo &deviceInfo)
|
|
|
|
: m_deviceInfo(deviceInfo)
|
2018-05-15 23:15:05 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DevicePrivate()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_deviceLinks);
|
|
|
|
m_deviceLinks.clear();
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceInfo m_deviceInfo;
|
2018-05-15 23:15:05 +01:00
|
|
|
|
|
|
|
QVector<DeviceLink *> m_deviceLinks;
|
|
|
|
QHash<QString, KdeConnectPlugin *> m_plugins;
|
|
|
|
|
|
|
|
QMultiMap<QString, KdeConnectPlugin *> m_pluginsByIncomingCapability;
|
|
|
|
QSet<QString> m_supportedPlugins;
|
2023-07-16 15:20:34 +01:00
|
|
|
PairingHandler *m_pairingHandler;
|
2018-05-15 23:15:05 +01:00
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static void warn(const QString &info)
|
2015-12-17 14:53:29 +00:00
|
|
|
{
|
|
|
|
qWarning() << "Device pairing error" << info;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Device::Device(QObject *parent, const QString &id)
|
2014-01-22 22:31:27 +00:00
|
|
|
: QObject(parent)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceInfo info = KdeConnectConfig::instance().getTrustedDevice(id);
|
|
|
|
d = new Device::DevicePrivate(info);
|
2013-07-26 15:21:19 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
d->m_pairingHandler = new PairingHandler(this, PairState::Paired);
|
2023-04-17 20:20:51 +01:00
|
|
|
const auto supported = PluginLoader::instance()->getPluginList();
|
|
|
|
d->m_supportedPlugins = QSet(supported.begin(), supported.end()); // Assume every plugin is supported until we get the capabilities
|
2015-03-13 23:39:13 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Register in bus
|
2022-04-12 06:40:03 +01:00
|
|
|
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
|
2015-12-17 14:53:29 +00:00
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
connect(d->m_pairingHandler, &PairingHandler::incomingPairRequest, this, &Device::pairingHandler_incomingPairRequest);
|
|
|
|
connect(d->m_pairingHandler, &PairingHandler::pairingFailed, this, &Device::pairingHandler_pairingFailed);
|
|
|
|
connect(d->m_pairingHandler, &PairingHandler::pairingSuccessful, this, &Device::pairingHandler_pairingSuccessful);
|
|
|
|
connect(d->m_pairingHandler, &PairingHandler::unpaired, this, &Device::pairingHandler_unpaired);
|
2013-06-25 17:06:51 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
Device::Device(QObject *parent, DeviceLink *dl)
|
2014-01-22 22:31:27 +00:00
|
|
|
: QObject(parent)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
d = new Device::DevicePrivate(dl->deviceInfo());
|
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
d->m_pairingHandler = new PairingHandler(this, PairState::NotPaired);
|
2023-04-17 20:20:51 +01:00
|
|
|
const auto supported = PluginLoader::instance()->getPluginList();
|
|
|
|
d->m_supportedPlugins = QSet(supported.begin(), supported.end()); // Assume every plugin is supported until we get the capabilities
|
2019-05-10 23:20:12 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
addLink(dl);
|
2015-09-08 16:28:47 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Register in bus
|
2022-04-12 06:40:03 +01:00
|
|
|
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
|
2015-12-17 14:53:29 +00:00
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
connect(this, &Device::pairingFailed, this, &warn);
|
2019-07-21 16:53:06 +01:00
|
|
|
|
|
|
|
connect(this, &Device::reachableChanged, this, &Device::statusIconNameChanged);
|
2023-06-01 01:25:37 +01:00
|
|
|
connect(this, &Device::pairStateChanged, this, &Device::statusIconNameChanged);
|
|
|
|
|
|
|
|
connect(d->m_pairingHandler, &PairingHandler::incomingPairRequest, this, &Device::pairingHandler_incomingPairRequest);
|
|
|
|
connect(d->m_pairingHandler, &PairingHandler::pairingFailed, this, &Device::pairingHandler_pairingFailed);
|
|
|
|
connect(d->m_pairingHandler, &PairingHandler::pairingSuccessful, this, &Device::pairingHandler_pairingSuccessful);
|
|
|
|
connect(d->m_pairingHandler, &PairingHandler::unpaired, this, &Device::pairingHandler_unpaired);
|
2023-06-27 12:10:59 +01:00
|
|
|
|
|
|
|
if (protocolVersion() != NetworkPacket::s_protocolVersion) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << name() << " uses a different protocol version" << protocolVersion() << "expected" << NetworkPacket::s_protocolVersion;
|
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-08-16 08:27:32 +01:00
|
|
|
Device::~Device()
|
|
|
|
{
|
2018-05-15 23:15:05 +01:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Device::id() const
|
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
return d->m_deviceInfo.id;
|
2018-05-15 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Device::name() const
|
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
return d->m_deviceInfo.name;
|
2018-05-15 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceType Device::type() const
|
2018-05-15 23:15:05 +01:00
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
return d->m_deviceInfo.type;
|
2018-05-15 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Device::isReachable() const
|
|
|
|
{
|
|
|
|
return !d->m_deviceLinks.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Device::protocolVersion()
|
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
return d->m_deviceInfo.protocolVersion;
|
2018-05-15 23:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Device::supportedPlugins() const
|
|
|
|
{
|
2023-04-17 20:20:51 +01:00
|
|
|
return QList(d->m_supportedPlugins.cbegin(), d->m_supportedPlugins.cend());
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool Device::hasPlugin(const QString &name) const
|
2013-08-13 22:23:32 +01:00
|
|
|
{
|
2018-05-15 23:15:05 +01:00
|
|
|
return d->m_plugins.contains(name);
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
QStringList Device::loadedPlugins() const
|
2013-08-18 19:27:25 +01:00
|
|
|
{
|
2018-05-15 23:15:05 +01:00
|
|
|
return d->m_plugins.keys();
|
2013-08-18 19:27:25 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
void Device::reloadPlugins()
|
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << name() << "- reload plugins";
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QHash<QString, KdeConnectPlugin *> newPluginMap, oldPluginMap = d->m_plugins;
|
|
|
|
QMultiMap<QString, KdeConnectPlugin *> newPluginsByIncomingCapability;
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2023-06-01 01:25:37 +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
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
PluginLoader *loader = PluginLoader::instance();
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const QString &pluginName : qAsConst(d->m_supportedPlugins)) {
|
2015-09-08 19:03:44 +01:00
|
|
|
const KPluginMetaData service = loader->getPluginInfo(pluginName);
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2015-09-08 19:03:44 +01:00
|
|
|
const bool pluginEnabled = isPluginEnabled(pluginName);
|
2023-04-17 20:20:51 +01:00
|
|
|
const QStringList incomingCapabilities = service.rawData().value(QStringLiteral("X-KdeConnect-SupportedPacketType")).toVariant().toStringList();
|
2015-09-08 19:03:44 +01:00
|
|
|
|
|
|
|
if (pluginEnabled) {
|
2022-09-10 22:23:52 +01:00
|
|
|
KdeConnectPlugin *plugin = d->m_plugins.take(pluginName);
|
2014-07-11 00:54:19 +01:00
|
|
|
|
|
|
|
if (!plugin) {
|
|
|
|
plugin = loader->instantiatePluginForDevice(pluginName, this);
|
|
|
|
}
|
2016-07-06 16:37:22 +01:00
|
|
|
Q_ASSERT(plugin);
|
2014-07-11 00:54:19 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const QString &interface : incomingCapabilities) {
|
2016-07-06 16:37:22 +01:00
|
|
|
newPluginsByIncomingCapability.insert(interface, plugin);
|
2013-08-14 00:35:12 +01:00
|
|
|
}
|
2015-09-12 16:48:24 +01:00
|
|
|
|
2013-10-29 16:29:31 +00:00
|
|
|
newPluginMap[pluginName] = plugin;
|
2023-06-24 19:34:15 +01:00
|
|
|
|
|
|
|
plugin->connected();
|
2013-08-14 00:35:12 +01:00
|
|
|
}
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 12:21:40 +01:00
|
|
|
const bool differentPlugins = oldPluginMap != newPluginMap;
|
2016-07-06 16:37:22 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Erase all left plugins in the original map (meaning that we don't want
|
|
|
|
// them anymore, otherwise they would have been moved to the newPluginMap)
|
2018-05-15 23:15:05 +01:00
|
|
|
qDeleteAll(d->m_plugins);
|
|
|
|
d->m_plugins = newPluginMap;
|
|
|
|
d->m_pluginsByIncomingCapability = newPluginsByIncomingCapability;
|
2013-08-14 00:35:12 +01:00
|
|
|
|
2023-06-24 19:34:15 +01:00
|
|
|
// Recreate dbus paths for all plugins (new and existing)
|
2022-04-12 06:40:03 +01:00
|
|
|
QDBusConnection bus = QDBusConnection::sessionBus();
|
2022-09-10 22:23:52 +01:00
|
|
|
for (KdeConnectPlugin *plugin : qAsConst(d->m_plugins)) {
|
2016-12-30 15:38:12 +00:00
|
|
|
const QString dbusPath = plugin->dbusPath();
|
|
|
|
if (!dbusPath.isEmpty()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
bus.registerObject(dbusPath,
|
|
|
|
plugin,
|
|
|
|
QDBusConnection::ExportAllProperties | QDBusConnection::ExportScriptableInvokables | QDBusConnection::ExportScriptableSignals
|
|
|
|
| QDBusConnection::ExportScriptableSlots);
|
2016-12-30 15:38:12 +00:00
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
}
|
2016-07-06 16:37:22 +01:00
|
|
|
if (differentPlugins) {
|
|
|
|
Q_EMIT pluginsChanged();
|
2015-09-08 16:28:47 +01:00
|
|
|
}
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
QString Device::pluginsConfigFile() const
|
|
|
|
{
|
2019-09-08 16:09:52 +01:00
|
|
|
return KdeConnectConfig::instance().deviceConfigDir(id()).absoluteFilePath(QStringLiteral("config"));
|
2015-03-02 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
void Device::requestPairing()
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2023-06-01 01:25:37 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Request pairing";
|
|
|
|
d->m_pairingHandler->requestPairing();
|
|
|
|
Q_EMIT pairStateChanged(pairStateAsInt());
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::unpair()
|
|
|
|
{
|
2023-06-01 01:25:37 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Request unpairing";
|
|
|
|
d->m_pairingHandler->unpair();
|
2015-03-16 02:13:54 +00:00
|
|
|
}
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
void Device::acceptPairing()
|
2015-03-16 02:13:54 +00:00
|
|
|
{
|
2023-06-01 01:25:37 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Accept pairing";
|
|
|
|
d->m_pairingHandler->acceptPairing();
|
|
|
|
}
|
2013-08-30 18:10:43 +01:00
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
void Device::cancelPairing()
|
|
|
|
{
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Cancel pairing";
|
|
|
|
d->m_pairingHandler->cancelPairing();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::pairingHandler_incomingPairRequest()
|
|
|
|
{
|
|
|
|
Q_ASSERT(d->m_pairingHandler->pairState() == PairState::RequestedByPeer);
|
|
|
|
Q_EMIT pairStateChanged(pairStateAsInt());
|
|
|
|
}
|
2015-11-30 18:36:01 +00:00
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
void Device::pairingHandler_pairingSuccessful()
|
|
|
|
{
|
|
|
|
Q_ASSERT(d->m_pairingHandler->pairState() == PairState::Paired);
|
2023-06-27 12:10:59 +01:00
|
|
|
KdeConnectConfig::instance().addTrustedDevice(d->m_deviceInfo);
|
2022-09-10 22:23:52 +01:00
|
|
|
reloadPlugins(); // Will load/unload plugins
|
2023-06-01 01:25:37 +01:00
|
|
|
Q_EMIT pairStateChanged(pairStateAsInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::pairingHandler_pairingFailed(const QString &errorMessage)
|
|
|
|
{
|
|
|
|
Q_ASSERT(d->m_pairingHandler->pairState() == PairState::NotPaired);
|
|
|
|
Q_EMIT pairingFailed(errorMessage);
|
|
|
|
Q_EMIT pairStateChanged(pairStateAsInt());
|
|
|
|
}
|
2015-11-30 18:36:01 +00:00
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
void Device::pairingHandler_unpaired()
|
|
|
|
{
|
|
|
|
Q_ASSERT(d->m_pairingHandler->pairState() == PairState::NotPaired);
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Unpaired";
|
|
|
|
KdeConnectConfig::instance().removeTrustedDevice(id());
|
|
|
|
reloadPlugins(); // Will load/unload plugins
|
|
|
|
Q_EMIT pairStateChanged(pairStateAsInt());
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
void Device::addLink(DeviceLink *link)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2019-07-04 23:13:55 +01:00
|
|
|
if (d->m_deviceLinks.contains(link)) {
|
2017-07-14 02:30:49 +01:00
|
|
|
return;
|
2019-07-04 23:13:55 +01:00
|
|
|
}
|
2015-12-17 15:44:29 +00:00
|
|
|
|
2018-05-15 23:15:05 +01:00
|
|
|
d->m_deviceLinks.append(link);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
connect(link, &QObject::destroyed, this, &Device::linkDestroyed);
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(link, &DeviceLink::receivedPacket, this, &Device::privateReceivedPacket);
|
2013-07-04 18:17:22 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
bool hasChanges = updateDeviceInfo(link->deviceInfo());
|
2016-07-06 16:37:22 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
if (d->m_deviceLinks.size() == 1) {
|
|
|
|
Q_EMIT reachableChanged(true);
|
|
|
|
hasChanges = true;
|
2016-07-06 16:37:22 +01:00
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
if (hasChanges) {
|
|
|
|
reloadPlugins();
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 16:37:22 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
bool Device::updateDeviceInfo(const DeviceInfo &newDeviceInfo)
|
|
|
|
{
|
|
|
|
bool hasChanges = false;
|
|
|
|
if (d->m_deviceInfo.name != newDeviceInfo.name || d->m_deviceInfo.type != newDeviceInfo.type) {
|
|
|
|
hasChanges = true;
|
|
|
|
d->m_deviceInfo.name = newDeviceInfo.name;
|
|
|
|
d->m_deviceInfo.type = newDeviceInfo.type;
|
|
|
|
Q_EMIT typeChanged(d->m_deviceInfo.type.toString());
|
|
|
|
Q_EMIT nameChanged(d->m_deviceInfo.name);
|
|
|
|
if (isPaired()) {
|
|
|
|
KdeConnectConfig::instance().updateTrustedDeviceInfo(d->m_deviceInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->m_deviceInfo.outgoingCapabilities != newDeviceInfo.outgoingCapabilities
|
|
|
|
|| d->m_deviceInfo.incomingCapabilities != newDeviceInfo.incomingCapabilities) {
|
|
|
|
if (!newDeviceInfo.incomingCapabilities.isEmpty() && !newDeviceInfo.outgoingCapabilities.isEmpty()) {
|
|
|
|
hasChanges = true;
|
|
|
|
d->m_supportedPlugins = PluginLoader::instance()->pluginsForCapabilities(newDeviceInfo.incomingCapabilities, newDeviceInfo.outgoingCapabilities);
|
|
|
|
qDebug() << "new capabilities for " << name();
|
|
|
|
}
|
2013-07-28 21:00:45 +01:00
|
|
|
}
|
2023-06-27 12:10:59 +01:00
|
|
|
|
|
|
|
return hasChanges;
|
2017-01-24 23:22:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Device::linkDestroyed(QObject *o)
|
2013-07-04 18:17:22 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
removeLink(static_cast<DeviceLink *>(o));
|
2013-07-04 18:17:22 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Device::removeLink(DeviceLink *link)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2018-05-15 23:15:05 +01:00
|
|
|
d->m_deviceLinks.removeAll(link);
|
2013-07-28 21:00:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_CORE) << "RemoveLink" << m_deviceLinks.size() << "links remaining";
|
2013-07-28 21:00:45 +01:00
|
|
|
|
2018-05-15 23:15:05 +01:00
|
|
|
if (d->m_deviceLinks.isEmpty()) {
|
2013-08-16 00:01:58 +01:00
|
|
|
reloadPlugins();
|
2016-11-23 16:24:03 +00:00
|
|
|
Q_EMIT reachableChanged(false);
|
2013-07-28 21:00:45 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool Device::sendPacket(NetworkPacket &np)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2023-06-01 01:25:37 +01:00
|
|
|
Q_ASSERT(isPaired() || np.type() == PACKET_TYPE_PAIR);
|
2015-12-02 19:04:35 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Maybe we could block here any packet that is not an identity or a pairing packet to prevent sending non encrypted data
|
|
|
|
for (DeviceLink *dl : qAsConst(d->m_deviceLinks)) {
|
|
|
|
if (dl->sendPacket(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
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Device::privateReceivedPacket(const NetworkPacket &np)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2023-06-01 01:25:37 +01:00
|
|
|
if (np.type() == PACKET_TYPE_PAIR) {
|
|
|
|
d->m_pairingHandler->packetReceived(np);
|
|
|
|
} else if (isPaired()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
const QList<KdeConnectPlugin *> plugins = d->m_pluginsByIncomingCapability.values(np.type());
|
2016-06-03 14:51:05 +01:00
|
|
|
if (plugins.isEmpty()) {
|
2018-03-04 19:48:51 +00:00
|
|
|
qWarning() << "discarding unsupported packet" << np.type() << "for" << name();
|
2016-06-03 14:51:05 +01:00
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
for (KdeConnectPlugin *plugin : plugins) {
|
2018-03-04 19:48:51 +00:00
|
|
|
plugin->receivePacket(np);
|
2013-10-29 16:29:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-04 19:48:51 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "device" << name() << "not paired, ignoring packet" << np.type();
|
2015-07-27 16:28:58 +01:00
|
|
|
unpair();
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
PairState Device::pairState() const
|
|
|
|
{
|
|
|
|
return d->m_pairingHandler->pairState();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Device::pairStateAsInt() const
|
2015-12-01 15:25:34 +00:00
|
|
|
{
|
2023-06-01 01:25:37 +01:00
|
|
|
return (int)pairState();
|
2013-11-06 20:34:06 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
bool Device::isPaired() const
|
|
|
|
{
|
|
|
|
return d->m_pairingHandler->pairState() == PairState::Paired;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Device::isPairRequested() const
|
|
|
|
{
|
|
|
|
return d->m_pairingHandler->pairState() == PairState::Requested;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Device::isPairRequestedByPeer() const
|
|
|
|
{
|
|
|
|
return d->m_pairingHandler->pairState() == PairState::RequestedByPeer;
|
|
|
|
}
|
|
|
|
|
2017-07-22 10:13:21 +01:00
|
|
|
QHostAddress Device::getLocalIpAddress() const
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
for (DeviceLink *dl : qAsConst(d->m_deviceLinks)) {
|
|
|
|
LanDeviceLink *ldl = dynamic_cast<LanDeviceLink *>(dl);
|
2017-07-22 10:13:21 +01:00
|
|
|
if (ldl) {
|
|
|
|
return ldl->hostAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QHostAddress::Null;
|
|
|
|
}
|
|
|
|
|
2014-06-14 18:09:31 +01:00
|
|
|
QString Device::iconName() const
|
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
return d->m_deviceInfo.type.icon();
|
2014-06-14 18:09:31 +01:00
|
|
|
}
|
2015-03-14 03:28:54 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
QString Device::statusIconName() const
|
2020-08-02 12:57:58 +01:00
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
return d->m_deviceInfo.type.iconForStatus(isReachable(), isPaired());
|
2020-08-02 12:57:58 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KdeConnectPlugin *Device::plugin(const QString &pluginName) const
|
2015-09-07 13:54:33 +01:00
|
|
|
{
|
2018-05-15 23:15:05 +01:00
|
|
|
return d->m_plugins[pluginName];
|
2015-09-07 13:54:33 +01:00
|
|
|
}
|
2015-09-08 16:28:47 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Device::setPluginEnabled(const QString &pluginName, bool enabled)
|
2015-09-08 16:28:47 +01:00
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
if (!PluginLoader::instance()->doesPluginExist(pluginName)) {
|
|
|
|
qWarning() << "Tried to enable a plugin that doesn't exist" << pluginName;
|
2019-05-10 23:20:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:28:47 +01:00
|
|
|
KConfigGroup pluginStates = KSharedConfig::openConfig(pluginsConfigFile())->group("Plugins");
|
|
|
|
|
|
|
|
const QString enabledKey = pluginName + QStringLiteral("Enabled");
|
|
|
|
pluginStates.writeEntry(enabledKey, enabled);
|
|
|
|
reloadPlugins();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool Device::isPluginEnabled(const QString &pluginName) const
|
2015-09-08 16:28:47 +01:00
|
|
|
{
|
|
|
|
const QString enabledKey = pluginName + QStringLiteral("Enabled");
|
|
|
|
KConfigGroup pluginStates = KSharedConfig::openConfig(pluginsConfigFile())->group("Plugins");
|
|
|
|
|
|
|
|
return (pluginStates.hasKey(enabledKey) ? pluginStates.readEntry(enabledKey, false)
|
|
|
|
: PluginLoader::instance()->getPluginInfo(pluginName).isEnabledByDefault());
|
|
|
|
}
|
2015-11-30 18:36:01 +00:00
|
|
|
|
|
|
|
QString Device::encryptionInfo() const
|
|
|
|
{
|
|
|
|
QString result;
|
2020-11-26 10:28:32 +00:00
|
|
|
const QCryptographicHash::Algorithm digestAlgorithm = QCryptographicHash::Algorithm::Sha256;
|
2015-11-30 18:36:01 +00:00
|
|
|
|
2020-11-26 10:28:32 +00:00
|
|
|
QString localChecksum = QString::fromLatin1(KdeConnectConfig::instance().certificate().digest(digestAlgorithm).toHex());
|
2022-09-10 22:23:52 +01:00
|
|
|
for (int i = 2; i < localChecksum.size(); i += 3) {
|
2020-11-26 10:28:32 +00:00
|
|
|
localChecksum.insert(i, QStringLiteral(":")); // Improve readability
|
2015-11-30 18:36:01 +00:00
|
|
|
}
|
2020-11-26 10:28:32 +00:00
|
|
|
result += i18n("SHA256 fingerprint of your device certificate is: %1\n", localChecksum);
|
2015-11-30 18:36:01 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
QString remoteChecksum = QString::fromLatin1(certificate().digest(digestAlgorithm).toHex());
|
2020-11-26 10:28:32 +00:00
|
|
|
for (int i = 2; i < remoteChecksum.size(); i += 3) {
|
|
|
|
remoteChecksum.insert(i, QStringLiteral(":")); // Improve readability
|
2015-11-30 18:36:01 +00:00
|
|
|
}
|
2020-11-26 10:28:32 +00:00
|
|
|
result += i18n("SHA256 fingerprint of remote device certificate is: %1\n", remoteChecksum);
|
2015-11-30 18:36:01 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-26 10:28:32 +00:00
|
|
|
QSslCertificate Device::certificate() const
|
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
return d->m_deviceInfo.certificate;
|
2020-11-26 10:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Device::verificationKey() const
|
|
|
|
{
|
|
|
|
auto a = KdeConnectConfig::instance().certificate().publicKey().toDer();
|
|
|
|
auto b = certificate().publicKey().toDer();
|
|
|
|
if (a < b) {
|
|
|
|
std::swap(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha256);
|
|
|
|
hash.addData(a);
|
|
|
|
hash.addData(b);
|
|
|
|
return hash.result().toHex();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString Device::pluginIconName(const QString &pluginName)
|
2018-08-03 00:53:21 +01:00
|
|
|
{
|
|
|
|
if (hasPlugin(pluginName)) {
|
|
|
|
return d->m_plugins[pluginName]->iconName();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|