kdeconnect-kde/core/daemon.cpp

315 lines
8.5 KiB
C++
Raw Normal View History

2013-06-06 04:57:06 +01:00
/**
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
2013-06-06 04:57:06 +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"
#include <QDBusMetaType>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QPointer>
2013-06-06 04:57:06 +01:00
#include "core_debug.h"
#include "kdeconnectconfig.h"
#include "networkpacket.h"
2019-06-09 16:28:49 +01:00
#include "dbushelper.h"
#include "notificationserverinfo.h"
#ifdef KDECONNECT_BLUETOOTH
#include "backends/bluetooth/bluetoothlinkprovider.h"
#endif
#include "backends/lan/lanlinkprovider.h"
#include "backends/loopback/loopbacklinkprovider.h"
#include "device.h"
#include "backends/devicelink.h"
#include "backends/linkprovider.h"
Build kdeconnect on sailfish and port some simple plugins Summary: Below is a lost of the commits, but, in summary Port the build system for Sailfish, which means selectively building only the bits we need/can, and only against the KF5 libs that are available. Allow to build on Qt 5.6 Switch from knotification to nemo notification (not complete!) Add a very simple example sailfish app. Note, there is still much missing functionality. Notifications dont work, pairing sort of works but not really, but when it is paired you can send a ping to the desktop client Dont build kio for Sailfish Port core build system Port daemon buld system Require CoreAddons on Sailfish Port plugins build for sailfish and include the ping plugin for now Final build changes for sailfish. Disable tests and other not needed parts Add includes for QCA Fix build errors on sailfish Get core/ to build on sailfish Get interfaces/ to build on sailfish Build daemon on sailfish On sailfish, dont install the kcm file Start port plugin to sailfish Fixup installed files Add sfos app Hack declarative plugin to give a public interface Build sfos app Compile declarativeplugin into the sfos app for now Redefine qAsConst for qt 5.6 Packaging fixes Use official icon Package .desktop Reviewers: #kde_connect, apol, nicolasfella, albertvaka Reviewed By: #kde_connect, apol, nicolasfella, albertvaka Subscribers: kdeconnect, andyholmes, albertvaka, kossebau, mtijink, vonreth, apol, #kde_connect, nicolasfella Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D10703
2018-08-02 20:10:59 +01:00
//In older Qt released, qAsConst isnt available
#include "qtcompat_p.h"
static Daemon* s_instance = nullptr;
struct DaemonPrivate
{
//Different ways to find devices and connect to them
QSet<LinkProvider*> m_linkProviders;
//Every known device
QMap<QString, Device*> m_devices;
QSet<QString> m_discoveryModeAcquisitions;
2019-05-13 23:35:25 +01:00
bool m_testMode;
};
Daemon* Daemon::instance()
{
Q_ASSERT(s_instance != nullptr);
return s_instance;
}
Daemon::Daemon(QObject* parent, bool testMode)
: QObject(parent)
, d(new DaemonPrivate)
2013-06-06 04:57:06 +01: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
//Load backends
2019-05-13 23:35:25 +01:00
if (d->m_testMode)
d->m_linkProviders.insert(new LoopbackLinkProvider());
2016-11-11 14:55:16 +00:00
else {
d->m_linkProviders.insert(new LanLinkProvider());
#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
}
2018-10-07 19:23:20 +01:00
//Read remembered paired devices
const QStringList& list = KdeConnectConfig::instance().trustedDevices();
for (const QString& id : list) {
addDevice(new Device(this, id));
2013-07-02 01:46:41 +01:00
}
//Listen to new devices
for (LinkProvider* a : qAsConst(d->m_linkProviders)) {
2016-11-26 14:12:38 +00:00
connect(a, &LinkProvider::onConnectionReceived,
this, &Daemon::onNewDeviceLink);
a->onStart();
2013-06-06 04:57:06 +01:00
}
2013-06-17 11:23:08 +01:00
//Register on DBus
qDBusRegisterMetaType< QMap<QString,QString> >();
2019-08-14 16:36:19 +01:00
DBusHelper::sessionBus().registerService(QStringLiteral("org.kde.kdeconnect"));
DBusHelper::sessionBus().registerObject(QStringLiteral("/modules/kdeconnect"), this, QDBusConnection::ExportScriptableContents);
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
void Daemon::acquireDiscoveryMode(const QString& key)
{
bool oldState = d->m_discoveryModeAcquisitions.isEmpty();
d->m_discoveryModeAcquisitions.insert(key);
if (oldState != d->m_discoveryModeAcquisitions.isEmpty()) {
forceOnNetworkChange();
}
}
void Daemon::releaseDiscoveryMode(const QString& key)
{
bool oldState = d->m_discoveryModeAcquisitions.isEmpty();
d->m_discoveryModeAcquisitions.remove(key);
if (oldState != d->m_discoveryModeAcquisitions.isEmpty()) {
cleanDevices();
}
}
void Daemon::removeDevice(Device* device)
{
d->m_devices.remove(device->id());
device->deleteLater();
Q_EMIT deviceRemoved(device->id());
Q_EMIT deviceListChanged();
}
void Daemon::cleanDevices()
{
2019-04-14 18:25:02 +01:00
const auto devs = d->m_devices;
for (Device* device : devs) {
if (device->isTrusted()) {
continue;
}
device->cleanUnneededLinks();
//If there are no links remaining
if (!device->isReachable()) {
removeDevice(device);
}
}
}
2013-09-26 16:49:40 +01:00
2013-08-07 10:29:56 +01:00
void Daemon::forceOnNetworkChange()
{
qCDebug(KDECONNECT_CORE) << "Sending onNetworkChange to" << d->m_linkProviders.size() << "LinkProviders";
for (LinkProvider* a : qAsConst(d->m_linkProviders)) {
a->onNetworkChange();
2013-08-07 10:29:56 +01:00
}
}
2020-04-10 22:42:40 +01:00
Device* Daemon::getDevice(const QString& deviceId)
2016-07-07 00:09:07 +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
}
const QSet<LinkProvider*>& Daemon::getLinkProviders() const
{
return d->m_linkProviders;
}
2015-12-01 18:45:14 +00:00
QStringList Daemon::devices(bool onlyReachable, bool onlyTrusted) const
{
QStringList ret;
for (Device* device : qAsConst(d->m_devices)) {
if (onlyReachable && !device->isReachable()) continue;
2015-12-01 18:45:14 +00:00
if (onlyTrusted && !device->isTrusted()) continue;
ret.append(device->id());
}
return ret;
}
QMap<QString, QString> Daemon::deviceNames(bool onlyReachable, bool onlyTrusted) const
{
QMap<QString, QString> ret;
for (Device* device : qAsConst(d->m_devices)) {
if (onlyReachable && !device->isReachable()) continue;
if (onlyTrusted && !device->isTrusted()) continue;
ret[device->id()] = device->name();
}
return ret;
}
void Daemon::onNewDeviceLink(const NetworkPacket& identityPacket, DeviceLink* dl)
{
const QString& id = identityPacket.get<QString>(QStringLiteral("deviceId"));
2015-07-14 13:04:04 +01:00
//qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via" << dl->provider()->name();
if (d->m_devices.contains(id)) {
qCDebug(KDECONNECT_CORE) << "It is a known device" << identityPacket.get<QString>(QStringLiteral("deviceName"));
Device* device = d->m_devices[id];
2015-06-22 03:39:04 +01:00
bool wasReachable = device->isReachable();
device->addLink(identityPacket, dl);
2015-06-22 03:39:04 +01:00
if (!wasReachable) {
Q_EMIT deviceVisibilityChanged(id, true);
Q_EMIT deviceListChanged();
2015-06-22 03:39:04 +01:00
}
} else {
qCDebug(KDECONNECT_CORE) << "It is a new device" << identityPacket.get<QString>(QStringLiteral("deviceName"));
Device* device = new Device(this, identityPacket, dl);
//we discard the connections that we created but it's not paired.
if (!isDiscoveringDevices() && !device->isTrusted() && !dl->linkShouldBeKeptAlive()) {
device->deleteLater();
} else {
addDevice(device);
}
}
}
void Daemon::onDeviceStatusChanged()
{
Device* device = (Device*)sender();
2015-09-11 09:28:58 +01:00
//qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "status changed. Reachable:" << device->isReachable() << ". Paired: " << device->isPaired();
2015-12-01 18:45:14 +00:00
if (!device->isReachable() && !device->isTrusted()) {
2015-09-11 09:28:58 +01:00
//qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name();
removeDevice(device);
2015-06-22 03:39:04 +01:00
} else {
Q_EMIT deviceVisibilityChanged(device->id(), device->isReachable());
Q_EMIT deviceListChanged();
}
2015-06-22 03:39:04 +01:00
}
void Daemon::setAnnouncedName(const QString& name)
{
qCDebug(KDECONNECT_CORE()) << "Announcing name";
KdeConnectConfig::instance().setName(name);
forceOnNetworkChange();
Q_EMIT announcedNameChanged(name);
}
QString Daemon::announcedName()
{
return KdeConnectConfig::instance().name();
}
QNetworkAccessManager* Daemon::networkAccessManager()
{
static QPointer<QNetworkAccessManager> manager;
if (!manager) {
manager = new QNetworkAccessManager(this);
}
return manager;
}
QList<Device*> Daemon::devicesList() const
2013-06-06 04:57:06 +01:00
{
return d->m_devices.values();
}
bool Daemon::isDiscoveringDevices() const
{
return !d->m_discoveryModeAcquisitions.isEmpty();
2013-06-06 04:57:06 +01:00
}
2013-06-17 11:23:08 +01:00
QString Daemon::deviceIdByName(const QString& name) const
{
for (Device* device : qAsConst(d->m_devices)) {
if (device->name() == name && device->isTrusted())
return device->id();
}
return {};
}
void Daemon::addDevice(Device* device)
{
const QString id = device->id();
connect(device, &Device::reachableChanged, this, &Daemon::onDeviceStatusChanged);
connect(device, &Device::trustedChanged, this, &Daemon::onDeviceStatusChanged);
connect(device, &Device::hasPairingRequestsChanged, this, &Daemon::pairingRequestsChanged);
connect(device, &Device::hasPairingRequestsChanged, this, [this, device](bool hasPairingRequests) {
if (hasPairingRequests)
askPairingConfirmation(device);
} );
d->m_devices[id] = device;
Q_EMIT deviceAdded(id);
Q_EMIT deviceListChanged();
}
QStringList Daemon::pairingRequests() const
{
QStringList ret;
for(Device* dev: qAsConst(d->m_devices)) {
if (dev->hasPairingRequests())
ret += dev->id();
}
return ret;
}
2013-06-06 04:57:06 +01:00
Daemon::~Daemon()
{
2013-06-06 04:57:06 +01:00
}
QString Daemon::selfId() const
{
return KdeConnectConfig::instance().deviceId();
}