kdeconnect-kde/core/daemon.cpp

248 lines
7.2 KiB
C++
Raw Normal View History

2013-06-06 04:57:06 +01:00
/**
* Copyright 2013 Albert Vaca <albertvaka@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/>.
*/
#include "daemon.h"
2013-06-25 17:08:34 +01:00
#include <QDBusConnection>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QPointer>
2013-06-06 04:57:06 +01:00
#include "core_debug.h"
#include "kdeconnectconfig.h"
#include "networkpackage.h"
#include "backends/lan/lanlinkprovider.h"
#include "backends/loopback/loopbacklinkprovider.h"
#include "device.h"
#include "backends/devicelink.h"
#include "backends/linkprovider.h"
Q_GLOBAL_STATIC(Daemon*, s_instance)
struct DaemonPrivate
{
//Different ways to find devices and connect to them
QSet<LinkProvider*> mLinkProviders;
//Every known device
QMap<QString, Device*> mDevices;
QSet<QString> mDiscoveryModeAcquisitions;
};
Daemon* Daemon::instance()
{
Q_ASSERT(s_instance.exists());
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.exists());
*s_instance = this;
qCDebug(KDECONNECT_CORE) << "KdeConnect daemon starting";
2014-08-11 17:53:08 +01:00
//Load backends
if (testMode)
d->mLinkProviders.insert(new LoopbackLinkProvider());
else
d->mLinkProviders.insert(new LanLinkProvider());
2013-07-02 01:46:41 +01:00
//Read remebered paired devices
const QStringList& list = KdeConnectConfig::instance()->trustedDevices();
Q_FOREACH(const QString& id, list) {
2014-01-22 22:31:27 +00:00
Device* device = new Device(this, id);
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged()));
2015-12-06 00:18:27 +00:00
connect(device, SIGNAL(trustedChanged(bool)), this, SLOT(onDeviceStatusChanged()));
d->mDevices[id] = device;
Q_EMIT deviceAdded(id);
2013-07-02 01:46:41 +01:00
}
//Listen to new devices
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
connect(a, SIGNAL(onConnectionReceived(NetworkPackage,DeviceLink*)),
this, SLOT(onNewDeviceLink(NetworkPackage,DeviceLink*)));
a->onStart();
2013-06-06 04:57:06 +01:00
}
2013-06-17 11:23:08 +01:00
//Register on DBus
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect", this, QDBusConnection::ExportScriptableContents);
qCDebug(KDECONNECT_CORE) << "KdeConnect 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->mDiscoveryModeAcquisitions.isEmpty();
d->mDiscoveryModeAcquisitions.insert(key);
if (oldState != d->mDiscoveryModeAcquisitions.isEmpty()) {
forceOnNetworkChange();
}
}
void Daemon::releaseDiscoveryMode(const QString &key)
{
bool oldState = d->mDiscoveryModeAcquisitions.isEmpty();
d->mDiscoveryModeAcquisitions.remove(key);
if (oldState != d->mDiscoveryModeAcquisitions.isEmpty()) {
cleanDevices();
}
}
void Daemon::removeDevice(Device* device)
{
d->mDevices.remove(device->id());
device->deleteLater();
Q_EMIT deviceRemoved(device->id());
}
void Daemon::cleanDevices()
{
Q_FOREACH(Device* device, d->mDevices) {
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->mLinkProviders.size() << " LinkProviders";
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
a->onNetworkChange();
2013-08-07 10:29:56 +01:00
}
}
2015-07-09 22:51:08 +01:00
// I hate this, but not able to find an alternative now
Device *Daemon::getDevice(QString deviceId) {
Q_FOREACH(Device* device, d->mDevices) {
if (device->id() == deviceId) {
return device;
}
}
return Q_NULLPTR;
}
2015-12-01 18:45:14 +00:00
QStringList Daemon::devices(bool onlyReachable, bool onlyTrusted) const
{
QStringList ret;
Q_FOREACH(Device* device, d->mDevices) {
if (onlyReachable && !device->isReachable()) continue;
2015-12-01 18:45:14 +00:00
if (onlyTrusted && !device->isTrusted()) continue;
ret.append(device->id());
}
return ret;
}
void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* dl)
{
const QString& id = identityPackage.get<QString>("deviceId");
2015-07-14 13:04:04 +01:00
//qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via" << dl->provider()->name();
if (d->mDevices.contains(id)) {
2015-07-05 14:23:53 +01:00
qCDebug(KDECONNECT_CORE) << "It is a known device " << identityPackage.get<QString>("deviceName");
Device* device = d->mDevices[id];
2015-06-22 03:39:04 +01:00
bool wasReachable = device->isReachable();
device->addLink(identityPackage, dl);
2015-06-22 03:39:04 +01:00
if (!wasReachable) {
Q_EMIT deviceVisibilityChanged(id, true);
}
} else {
2015-07-05 14:23:53 +01:00
qCDebug(KDECONNECT_CORE) << "It is a new device " << identityPackage.get<QString>("deviceName");
2014-01-22 22:31:27 +00:00
Device* device = new Device(this, identityPackage, dl);
//we discard the connections that we created but it's not paired.
if (!isDiscoveringDevices() && !device->isTrusted() && !dl->linkShouldBeKeptAlive()) {
device->deleteLater();
} else {
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged()));
2015-12-17 13:54:37 +00:00
connect(device, SIGNAL(trustedChanged(bool)), this, SLOT(onDeviceStatusChanged()));
d->mDevices[id] = device;
Q_EMIT deviceAdded(id);
}
}
}
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());
}
2015-06-22 03:39:04 +01:00
}
2015-09-12 08:45:59 +01:00
void Daemon::setAnnouncedName(const QString &name)
{
qCDebug(KDECONNECT_CORE()) << "Announcing name";
KdeConnectConfig::instance()->setName(name);
forceOnNetworkChange();
}
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->mDevices.values();
}
bool Daemon::isDiscoveringDevices() const
{
return !d->mDiscoveryModeAcquisitions.isEmpty();
2013-06-06 04:57:06 +01:00
}
2013-06-17 11:23:08 +01:00
2013-06-06 04:57:06 +01:00
Daemon::~Daemon()
{
2013-06-06 04:57:06 +01:00
}