2013-06-06 04:57:06 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-06-06 04:57:06 +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
|
2013-06-06 04:57:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "daemon.h"
|
2013-07-24 17:42:33 +01:00
|
|
|
|
2018-03-24 11:52:20 +00:00
|
|
|
#include <QDBusMetaType>
|
2015-03-02 04:16:07 +00:00
|
|
|
#include <QDebug>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QNetworkAccessManager>
|
2015-05-04 23:41:39 +01:00
|
|
|
#include <QPointer>
|
2020-08-22 15:12:46 +01:00
|
|
|
#include <QProcess>
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "dbushelper.h"
|
2015-03-02 04:16:07 +00:00
|
|
|
#include "kdeconnectconfig.h"
|
2018-03-04 19:48:51 +00:00
|
|
|
#include "networkpacket.h"
|
2019-06-02 15:03:11 +01:00
|
|
|
#include "notificationserverinfo.h"
|
2017-05-24 22:52:18 +01:00
|
|
|
|
|
|
|
#ifdef KDECONNECT_BLUETOOTH
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "backends/bluetooth/bluetoothlinkprovider.h"
|
2017-05-24 22:52:18 +01:00
|
|
|
#endif
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "backends/devicelink.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "backends/lan/lanlinkprovider.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "backends/linkprovider.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "backends/loopback/loopbacklinkprovider.h"
|
2014-06-14 14:22:40 +01:00
|
|
|
#include "device.h"
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
static Daemon *s_instance = nullptr;
|
2015-03-24 11:26:37 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
struct DaemonPrivate {
|
|
|
|
// Different ways to find devices and connect to them
|
|
|
|
QSet<LinkProvider *> m_linkProviders;
|
2014-06-14 14:22:40 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Every known device
|
|
|
|
QMap<QString, Device *> m_devices;
|
2015-04-05 00:32:15 +01:00
|
|
|
|
2019-05-13 23:35:25 +01:00
|
|
|
bool m_testMode;
|
2014-06-14 14:22:40 +01:00
|
|
|
};
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Daemon *Daemon::instance()
|
2015-03-24 11:26:37 +00:00
|
|
|
{
|
2016-11-26 14:55:18 +00:00
|
|
|
Q_ASSERT(s_instance != nullptr);
|
|
|
|
return s_instance;
|
2015-03-24 11:26:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Daemon::Daemon(QObject *parent, bool testMode)
|
2014-06-14 14:22:40 +01:00
|
|
|
: QObject(parent)
|
|
|
|
, d(new DaemonPrivate)
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
2016-11-26 14:55:18 +00:00
|
|
|
Q_ASSERT(!s_instance);
|
|
|
|
s_instance = this;
|
2019-05-13 23:35:25 +01:00
|
|
|
d->m_testMode = testMode;
|
|
|
|
|
|
|
|
// HACK init may call pure virtual functions from this class so it can't be called directly from the ctor
|
|
|
|
QTimer::singleShot(0, this, &Daemon::init);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Daemon::init()
|
|
|
|
{
|
2019-05-26 19:30:39 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Daemon starting";
|
2014-08-11 17:53:08 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Load backends
|
2019-05-13 23:35:25 +01:00
|
|
|
if (d->m_testMode)
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_linkProviders.insert(new LoopbackLinkProvider());
|
2016-11-11 14:55:16 +00:00
|
|
|
else {
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_linkProviders.insert(new LanLinkProvider());
|
2022-09-10 22:23:52 +01:00
|
|
|
#ifdef KDECONNECT_BLUETOOTH
|
|
|
|
d->m_linkProviders.insert(new BluetoothLinkProvider());
|
|
|
|
#endif
|
|
|
|
#ifdef KDECONNECT_LOOPBACK
|
|
|
|
d->m_linkProviders.insert(new LoopbackLinkProvider());
|
|
|
|
#endif
|
2016-11-11 14:55:16 +00:00
|
|
|
}
|
2013-07-02 00:50:32 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Register on DBus
|
|
|
|
qDBusRegisterMetaType<QMap<QString, QString>>();
|
2022-04-12 06:40:03 +01:00
|
|
|
QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kdeconnect"));
|
|
|
|
QDBusConnection::sessionBus().registerObject(QStringLiteral("/modules/kdeconnect"), this, QDBusConnection::ExportScriptableContents);
|
2020-09-24 22:13:55 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Read remembered paired devices
|
|
|
|
const QStringList &list = KdeConnectConfig::instance().trustedDevices();
|
|
|
|
for (const QString &id : list) {
|
2017-01-24 23:22:22 +00:00
|
|
|
addDevice(new Device(this, id));
|
2013-07-02 01:46:41 +01:00
|
|
|
}
|
2014-09-23 18:45:29 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Listen to new devices
|
|
|
|
for (LinkProvider *a : qAsConst(d->m_linkProviders)) {
|
|
|
|
connect(a, &LinkProvider::onConnectionReceived, this, &Daemon::onNewDeviceLink);
|
2015-09-08 08:30:55 +01:00
|
|
|
a->onStart();
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2019-06-02 15:03:11 +01:00
|
|
|
NotificationServerInfo::instance().init();
|
|
|
|
|
2019-05-26 19:30:39 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Daemon started";
|
2013-06-17 11:23:08 +01:00
|
|
|
}
|
2013-09-26 16:49:40 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Daemon::removeDevice(Device *device)
|
2015-09-08 08:30:55 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_devices.remove(device->id());
|
2015-09-08 08:30:55 +01:00
|
|
|
device->deleteLater();
|
|
|
|
Q_EMIT deviceRemoved(device->id());
|
2018-05-29 19:51:35 +01:00
|
|
|
Q_EMIT deviceListChanged();
|
2015-09-08 08:30:55 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 10:29:56 +01:00
|
|
|
void Daemon::forceOnNetworkChange()
|
|
|
|
{
|
2019-05-26 19:36:08 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Sending onNetworkChange to" << d->m_linkProviders.size() << "LinkProviders";
|
2022-09-10 22:23:52 +01:00
|
|
|
for (LinkProvider *a : qAsConst(d->m_linkProviders)) {
|
2015-04-05 00:32:15 +01:00
|
|
|
a->onNetworkChange();
|
2013-08-07 10:29:56 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-27 01:26:06 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Device *Daemon::getDevice(const QString &deviceId)
|
2016-07-07 00:09:07 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
for (Device *device : qAsConst(d->m_devices)) {
|
2015-07-09 22:51:08 +01:00
|
|
|
if (device->id() == deviceId) {
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
}
|
2019-05-05 14:45:50 +01:00
|
|
|
return nullptr;
|
2015-07-09 22:51:08 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
const QSet<LinkProvider *> &Daemon::getLinkProviders() const
|
2019-06-02 22:26:47 +01:00
|
|
|
{
|
|
|
|
return d->m_linkProviders;
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:45:14 +00:00
|
|
|
QStringList Daemon::devices(bool onlyReachable, bool onlyTrusted) const
|
2013-08-13 22:23:32 +01:00
|
|
|
{
|
|
|
|
QStringList ret;
|
2022-09-10 22:23:52 +01:00
|
|
|
for (Device *device : qAsConst(d->m_devices)) {
|
|
|
|
if (onlyReachable && !device->isReachable())
|
|
|
|
continue;
|
2023-06-01 01:25:37 +01:00
|
|
|
if (onlyTrusted && !device->isPaired())
|
2022-09-10 22:23:52 +01:00
|
|
|
continue;
|
2014-01-16 11:14:05 +00:00
|
|
|
ret.append(device->id());
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-24 11:52:20 +00:00
|
|
|
QMap<QString, QString> Daemon::deviceNames(bool onlyReachable, bool onlyTrusted) const
|
|
|
|
{
|
|
|
|
QMap<QString, QString> ret;
|
2022-09-10 22:23:52 +01:00
|
|
|
for (Device *device : qAsConst(d->m_devices)) {
|
|
|
|
if (onlyReachable && !device->isReachable())
|
|
|
|
continue;
|
2023-06-01 01:25:37 +01:00
|
|
|
if (onlyTrusted && !device->isPaired())
|
2022-09-10 22:23:52 +01:00
|
|
|
continue;
|
2018-03-24 11:52:20 +00:00
|
|
|
ret[device->id()] = device->name();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
void Daemon::onNewDeviceLink(DeviceLink *link)
|
2013-06-18 01:14:22 +01:00
|
|
|
{
|
2023-07-26 20:40:52 +01:00
|
|
|
QString id = link->deviceId();
|
2013-07-03 02:52:44 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
// qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via" << dl->name();
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2019-07-04 23:58:33 +01:00
|
|
|
if (d->m_devices.contains(id)) {
|
2023-06-27 12:10:59 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "It is a known device" << link->deviceInfo().name;
|
2022-09-10 22:23:52 +01:00
|
|
|
Device *device = d->m_devices[id];
|
2015-06-22 03:39:04 +01:00
|
|
|
bool wasReachable = device->isReachable();
|
2023-06-27 12:10:59 +01:00
|
|
|
device->addLink(link);
|
2015-06-22 03:39:04 +01:00
|
|
|
if (!wasReachable) {
|
|
|
|
Q_EMIT deviceVisibilityChanged(id, true);
|
2018-05-29 19:51:35 +01:00
|
|
|
Q_EMIT deviceListChanged();
|
2015-06-22 03:39:04 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
} else {
|
2023-06-27 12:10:59 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "It is a new device" << link->deviceInfo().name;
|
|
|
|
Device *device = new Device(this, link);
|
2023-06-01 01:25:37 +01:00
|
|
|
addDevice(device);
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 03:40:05 +01:00
|
|
|
void Daemon::onDeviceStatusChanged()
|
2013-08-13 22:23:32 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
Device *device = (Device *)sender();
|
2013-08-13 22:23:32 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "status changed. Reachable:" << device->isReachable() << ". Paired: " << device->isPaired();
|
2013-08-13 22:23:32 +01:00
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
if (!device->isReachable() && !device->isPaired()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name();
|
2015-09-08 08:30:55 +01:00
|
|
|
removeDevice(device);
|
2015-06-22 03:39:04 +01:00
|
|
|
} else {
|
2015-09-08 08:30:55 +01:00
|
|
|
Q_EMIT deviceVisibilityChanged(device->id(), device->isReachable());
|
2018-05-29 19:51:35 +01:00
|
|
|
Q_EMIT deviceListChanged();
|
2013-06-18 01:14:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Daemon::setAnnouncedName(const QString &name)
|
2015-03-02 05:23:05 +00:00
|
|
|
{
|
2020-08-18 15:55:48 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Announcing name";
|
2019-09-08 16:09:52 +01:00
|
|
|
KdeConnectConfig::instance().setName(name);
|
2015-03-02 05:23:05 +00:00
|
|
|
forceOnNetworkChange();
|
2016-08-21 18:38:15 +01:00
|
|
|
Q_EMIT announcedNameChanged(name);
|
2015-03-02 05:23:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Daemon::setCustomDevices(const QStringList &addresses)
|
2020-08-18 15:55:48 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
auto &config = KdeConnectConfig::instance();
|
2020-08-18 15:55:48 +01:00
|
|
|
|
|
|
|
auto customDevices = config.customDevices();
|
|
|
|
if (customDevices != addresses) {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Changed list of custom device addresses:" << addresses;
|
|
|
|
config.setCustomDevices(addresses);
|
|
|
|
Q_EMIT customDevicesChanged(addresses);
|
|
|
|
|
|
|
|
forceOnNetworkChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Daemon::customDevices() const
|
|
|
|
{
|
|
|
|
return KdeConnectConfig::instance().customDevices();
|
|
|
|
}
|
|
|
|
|
2015-03-02 05:23:05 +00:00
|
|
|
QString Daemon::announcedName()
|
|
|
|
{
|
2019-09-08 16:09:52 +01:00
|
|
|
return KdeConnectConfig::instance().name();
|
2015-03-02 05:23:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QNetworkAccessManager *Daemon::networkAccessManager()
|
2015-05-04 23:41:39 +01:00
|
|
|
{
|
|
|
|
static QPointer<QNetworkAccessManager> manager;
|
|
|
|
if (!manager) {
|
|
|
|
manager = new QNetworkAccessManager(this);
|
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QList<Device *> Daemon::devicesList() const
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
return d->m_devices.values();
|
2015-09-07 13:54:33 +01:00
|
|
|
}
|
2013-07-24 17:42:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString Daemon::deviceIdByName(const QString &name) const
|
2016-06-05 22:31:13 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
for (Device *device : qAsConst(d->m_devices)) {
|
2023-06-01 01:25:37 +01:00
|
|
|
if (device->name() == name && device->isPaired())
|
2017-07-20 15:14:07 +01:00
|
|
|
return device->id();
|
2016-06-05 22:31:13 +01:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Daemon::addDevice(Device *device)
|
2017-01-24 23:22:22 +00:00
|
|
|
{
|
|
|
|
const QString id = device->id();
|
|
|
|
connect(device, &Device::reachableChanged, this, &Daemon::onDeviceStatusChanged);
|
2023-06-01 01:25:37 +01:00
|
|
|
connect(device, &Device::pairStateChanged, this, &Daemon::onDeviceStatusChanged);
|
|
|
|
connect(device, &Device::pairStateChanged, this, &Daemon::pairingRequestsChanged);
|
|
|
|
connect(device, &Device::pairStateChanged, this, [this, device](int pairStateAsInt) {
|
|
|
|
PairState pairState = (PairState)pairStateAsInt;
|
|
|
|
if (pairState == PairState::RequestedByPeer)
|
2017-01-25 00:18:14 +00:00
|
|
|
askPairingConfirmation(device);
|
2022-09-10 22:23:52 +01:00
|
|
|
});
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_devices[id] = device;
|
2017-01-24 23:22:22 +00:00
|
|
|
|
|
|
|
Q_EMIT deviceAdded(id);
|
2018-05-29 19:51:35 +01:00
|
|
|
Q_EMIT deviceListChanged();
|
2017-01-24 23:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Daemon::pairingRequests() const
|
|
|
|
{
|
|
|
|
QStringList ret;
|
2022-09-10 22:23:52 +01:00
|
|
|
for (Device *dev : qAsConst(d->m_devices)) {
|
2023-06-01 01:25:37 +01:00
|
|
|
if (dev->isPairRequestedByPeer())
|
2017-01-24 23:22:22 +00:00
|
|
|
ret += dev->id();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
Daemon::~Daemon()
|
|
|
|
{
|
|
|
|
}
|
2017-02-24 21:37:00 +00:00
|
|
|
|
|
|
|
QString Daemon::selfId() const
|
|
|
|
{
|
2019-09-08 16:09:52 +01:00
|
|
|
return KdeConnectConfig::instance().deviceId();
|
2017-02-24 21:37:00 +00:00
|
|
|
}
|