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
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2013-06-06 04:57:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "daemon.h"
|
2013-07-24 17:42:33 +01:00
|
|
|
|
2013-06-25 17:08:34 +01:00
|
|
|
#include <QDBusConnection>
|
2018-03-24 11:52:20 +00:00
|
|
|
#include <QDBusMetaType>
|
2014-08-11 17:55:38 +01:00
|
|
|
#include <QNetworkAccessManager>
|
2015-03-02 04:16:07 +00:00
|
|
|
#include <QDebug>
|
2015-05-04 23:41:39 +01:00
|
|
|
#include <QPointer>
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2015-03-02 04:16:07 +00:00
|
|
|
#include "kdeconnectconfig.h"
|
2018-03-04 19:48:51 +00:00
|
|
|
#include "networkpacket.h"
|
2017-05-24 22:52:18 +01:00
|
|
|
|
|
|
|
#ifdef KDECONNECT_BLUETOOTH
|
|
|
|
#include "backends/bluetooth/bluetoothlinkprovider.h"
|
|
|
|
#endif
|
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "backends/lan/lanlinkprovider.h"
|
|
|
|
#include "backends/loopback/loopbacklinkprovider.h"
|
2014-06-14 14:22:40 +01:00
|
|
|
#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"
|
|
|
|
|
2016-11-26 14:55:18 +00:00
|
|
|
static Daemon* s_instance = nullptr;
|
2015-03-24 11:26:37 +00:00
|
|
|
|
2014-06-14 14:22:40 +01:00
|
|
|
struct DaemonPrivate
|
|
|
|
{
|
|
|
|
//Different ways to find devices and connect to them
|
2017-09-03 20:39:44 +01:00
|
|
|
QSet<LinkProvider*> m_linkProviders;
|
2014-06-14 14:22:40 +01:00
|
|
|
|
|
|
|
//Every known device
|
2017-09-03 20:39:44 +01:00
|
|
|
QMap<QString, Device*> m_devices;
|
2015-04-05 00:32:15 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QSet<QString> m_discoveryModeAcquisitions;
|
2014-06-14 14:22:40 +01:00
|
|
|
};
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2015-03-24 11:26:37 +00:00
|
|
|
Daemon* Daemon::instance()
|
|
|
|
{
|
2016-11-26 14:55:18 +00:00
|
|
|
Q_ASSERT(s_instance != nullptr);
|
|
|
|
return s_instance;
|
2015-03-24 11:26:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +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;
|
2014-11-04 18:12:29 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "KdeConnect daemon starting";
|
2014-08-11 17:53:08 +01:00
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
//Load backends
|
2015-09-07 13:54:33 +01:00
|
|
|
if (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());
|
2017-05-24 22:52:18 +01:00
|
|
|
#ifdef KDECONNECT_BLUETOOTH
|
2017-09-16 13:03:10 +01:00
|
|
|
d->m_linkProviders.insert(new BluetoothLinkProvider());
|
2017-05-24 22:52:18 +01:00
|
|
|
#endif
|
2019-03-13 00:30:24 +00:00
|
|
|
#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
|
|
|
|
2018-10-07 19:23:20 +01:00
|
|
|
//Read remembered paired devices
|
2015-03-02 04:16:07 +00:00
|
|
|
const QStringList& list = KdeConnectConfig::instance()->trustedDevices();
|
2017-07-20 15:14:07 +01:00
|
|
|
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
|
|
|
|
2014-08-11 17:55:38 +01:00
|
|
|
//Listen to new devices
|
2017-09-03 20:39:44 +01:00
|
|
|
for (LinkProvider* a : qAsConst(d->m_linkProviders)) {
|
2016-11-26 14:12:38 +00:00
|
|
|
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
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
//Register on DBus
|
2018-03-24 11:52:20 +00:00
|
|
|
qDBusRegisterMetaType< QMap<QString,QString> >();
|
2016-11-26 14:38:08 +00:00
|
|
|
QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kdeconnect"));
|
|
|
|
QDBusConnection::sessionBus().registerObject(QStringLiteral("/modules/kdeconnect"), this, QDBusConnection::ExportScriptableContents);
|
2015-03-02 04:16:07 +00:00
|
|
|
|
2014-11-04 18:12:29 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "KdeConnect daemon started";
|
2013-06-17 11:23:08 +01:00
|
|
|
}
|
2013-09-26 16:49:40 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
void Daemon::acquireDiscoveryMode(const QString& key)
|
2013-06-27 01:26:06 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
bool oldState = d->m_discoveryModeAcquisitions.isEmpty();
|
2015-09-08 08:30:55 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_discoveryModeAcquisitions.insert(key);
|
2015-09-09 19:09:04 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (oldState != d->m_discoveryModeAcquisitions.isEmpty()) {
|
2015-09-08 08:30:55 +01:00
|
|
|
forceOnNetworkChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
void Daemon::releaseDiscoveryMode(const QString& key)
|
2015-09-08 08:30:55 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
bool oldState = d->m_discoveryModeAcquisitions.isEmpty();
|
2015-09-09 19:09:04 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_discoveryModeAcquisitions.remove(key);
|
2015-09-09 19:09:04 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (oldState != d->m_discoveryModeAcquisitions.isEmpty()) {
|
2015-09-09 19:09:04 +01:00
|
|
|
cleanDevices();
|
|
|
|
}
|
2015-09-08 08:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Daemon::removeDevice(Device* device)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Daemon::cleanDevices()
|
|
|
|
{
|
2019-04-14 18:25:02 +01:00
|
|
|
const auto devs = d->m_devices;
|
|
|
|
for (Device* device : devs) {
|
2016-01-10 15:12:13 +00:00
|
|
|
if (device->isTrusted()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
device->cleanUnneededLinks();
|
|
|
|
//If there are no links remaining
|
|
|
|
if (!device->isReachable()) {
|
2015-09-08 08:30:55 +01:00
|
|
|
removeDevice(device);
|
|
|
|
}
|
2013-06-27 01:26:06 +01:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 16:49:40 +01:00
|
|
|
|
2013-08-07 10:29:56 +01:00
|
|
|
void Daemon::forceOnNetworkChange()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Sending onNetworkChange to " << d->m_linkProviders.size() << " LinkProviders";
|
|
|
|
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
|
|
|
|
2016-07-07 00:09:07 +01:00
|
|
|
Device*Daemon::getDevice(const QString& deviceId)
|
|
|
|
{
|
2017-09-03 20:39:44 +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
|
|
|
}
|
|
|
|
|
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;
|
2017-09-03 20:39:44 +01:00
|
|
|
for (Device* device : qAsConst(d->m_devices)) {
|
2014-01-16 11:14:05 +00:00
|
|
|
if (onlyReachable && !device->isReachable()) continue;
|
2015-12-01 18:45:14 +00:00
|
|
|
if (onlyTrusted && !device->isTrusted()) 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;
|
|
|
|
for (Device* device : qAsConst(d->m_devices)) {
|
|
|
|
if (onlyReachable && !device->isReachable()) continue;
|
|
|
|
if (onlyTrusted && !device->isTrusted()) continue;
|
|
|
|
ret[device->id()] = device->name();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void Daemon::onNewDeviceLink(const NetworkPacket& identityPacket, DeviceLink* dl)
|
2013-06-18 01:14:22 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
const QString& id = identityPacket.get<QString>(QStringLiteral("deviceId"));
|
2013-07-03 02:52:44 +01:00
|
|
|
|
2015-07-14 13:04:04 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via" << dl->provider()->name();
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (d->m_devices.contains(id)) {
|
2018-03-04 19:48:51 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "It is a known device" << identityPacket.get<QString>(QStringLiteral("deviceName"));
|
2017-09-03 20:39:44 +01:00
|
|
|
Device* device = d->m_devices[id];
|
2015-06-22 03:39:04 +01:00
|
|
|
bool wasReachable = device->isReachable();
|
2018-03-04 19:48:51 +00:00
|
|
|
device->addLink(identityPacket, dl);
|
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 {
|
2018-03-04 19:48:51 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "It is a new device" << identityPacket.get<QString>(QStringLiteral("deviceName"));
|
|
|
|
Device* device = new Device(this, identityPacket, dl);
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2015-09-09 11:30:39 +01:00
|
|
|
//we discard the connections that we created but it's not paired.
|
2016-01-10 15:12:13 +00:00
|
|
|
if (!isDiscoveringDevices() && !device->isTrusted() && !dl->linkShouldBeKeptAlive()) {
|
2015-09-09 12:42:28 +01:00
|
|
|
device->deleteLater();
|
2015-09-08 08:30:55 +01:00
|
|
|
} else {
|
2017-01-24 23:22:22 +00:00
|
|
|
addDevice(device);
|
2015-09-08 08:30:55 +01:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
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();
|
2013-08-13 22:23:32 +01:00
|
|
|
|
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();
|
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
|
|
|
}
|
2015-06-22 03:39:04 +01:00
|
|
|
|
2013-06-18 01:14:22 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
void Daemon::setAnnouncedName(const QString& name)
|
2015-03-02 05:23:05 +00:00
|
|
|
{
|
2015-04-05 00:32:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE()) << "Announcing name";
|
2015-03-02 05:23:05 +00:00
|
|
|
KdeConnectConfig::instance()->setName(name);
|
|
|
|
forceOnNetworkChange();
|
2016-08-21 18:38:15 +01:00
|
|
|
Q_EMIT announcedNameChanged(name);
|
2015-03-02 05:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Daemon::announcedName()
|
|
|
|
{
|
|
|
|
return KdeConnectConfig::instance()->name();
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:41:39 +01:00
|
|
|
QNetworkAccessManager* Daemon::networkAccessManager()
|
|
|
|
{
|
|
|
|
static QPointer<QNetworkAccessManager> manager;
|
|
|
|
if (!manager) {
|
|
|
|
manager = new QNetworkAccessManager(this);
|
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2015-09-07 13:54:33 +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
|
|
|
|
2015-09-09 19:09:04 +01:00
|
|
|
bool Daemon::isDiscoveringDevices() const
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
return !d->m_discoveryModeAcquisitions.isEmpty();
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QString Daemon::deviceIdByName(const QString& name) const
|
2016-06-05 22:31:13 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
for (Device* device : qAsConst(d->m_devices)) {
|
2017-07-20 15:14:07 +01:00
|
|
|
if (device->name() == name && device->isTrusted())
|
|
|
|
return device->id();
|
2016-06-05 22:31:13 +01:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-01-24 23:22:22 +00:00
|
|
|
void Daemon::addDevice(Device* device)
|
|
|
|
{
|
|
|
|
const QString id = device->id();
|
|
|
|
connect(device, &Device::reachableChanged, this, &Daemon::onDeviceStatusChanged);
|
|
|
|
connect(device, &Device::trustedChanged, this, &Daemon::onDeviceStatusChanged);
|
2017-01-25 00:18:14 +00:00
|
|
|
connect(device, &Device::hasPairingRequestsChanged, this, &Daemon::pairingRequestsChanged);
|
|
|
|
connect(device, &Device::hasPairingRequestsChanged, this, [this, device](bool hasPairingRequests) {
|
|
|
|
if (hasPairingRequests)
|
|
|
|
askPairingConfirmation(device);
|
|
|
|
} );
|
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;
|
2018-11-25 22:58:24 +00:00
|
|
|
for(Device* dev: qAsConst(d->m_devices)) {
|
2017-01-24 23:22:22 +00:00
|
|
|
if (dev->hasPairingRequests())
|
|
|
|
ret += dev->id();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
Daemon::~Daemon()
|
|
|
|
{
|
2013-07-24 17:42:33 +01:00
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
2017-02-24 21:37:00 +00:00
|
|
|
|
|
|
|
QString Daemon::selfId() const
|
|
|
|
{
|
|
|
|
return KdeConnectConfig::instance()->deviceId();
|
|
|
|
}
|