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-07-24 17:42:33 +01:00
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
#include "networkpackage.h"
|
2013-07-24 17:42:33 +01:00
|
|
|
|
2013-08-28 22:47:39 +01:00
|
|
|
#include "linkproviders/lanlinkprovider.h"
|
2013-07-24 17:42:33 +01:00
|
|
|
#include "linkproviders/loopbacklinkprovider.h"
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-08-07 10:29:56 +01:00
|
|
|
#include <QUuid>
|
2013-06-25 17:08:34 +01:00
|
|
|
#include <QDBusConnection>
|
2013-08-07 10:29:56 +01:00
|
|
|
#include <QNetworkSession>
|
|
|
|
#include <QNetworkConfigurationManager>
|
2013-08-30 18:10:43 +01:00
|
|
|
#include <QSslKey>
|
2013-07-02 01:46:41 +01:00
|
|
|
|
|
|
|
#include <KIcon>
|
|
|
|
#include <KConfigGroup>
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-07-02 00:50:32 +01:00
|
|
|
K_PLUGIN_FACTORY(KdeConnectFactory, registerPlugin<Daemon>();)
|
|
|
|
K_EXPORT_PLUGIN(KdeConnectFactory("kdeconnect", "kdeconnect"))
|
2013-06-06 04:57:06 +01:00
|
|
|
|
|
|
|
Daemon::Daemon(QObject *parent, const QList<QVariant>&)
|
|
|
|
: KDEDModule(parent)
|
|
|
|
{
|
2013-08-07 10:29:56 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
if (!config->group("myself").hasKey("id")) {
|
2013-08-12 15:09:52 +01:00
|
|
|
QString uuid = QUuid::createUuid().toString();
|
|
|
|
//uuids contain charcaters that are not exportable in dbus paths
|
|
|
|
uuid = uuid.mid(1, uuid.length() - 2).replace("-", "_");
|
|
|
|
config->group("myself").writeEntry("id", uuid);
|
|
|
|
qDebug() << "My id:" << uuid;
|
2013-07-23 15:11:54 +01:00
|
|
|
}
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
if (!config->group("myself").hasKey("privateKey")) {
|
|
|
|
|
|
|
|
//TODO: Generate
|
|
|
|
|
|
|
|
QByteArray key = QByteArray(
|
|
|
|
"-----BEGIN RSA PRIVATE KEY-----\n"
|
|
|
|
"MIICXAIBAAKBgQCnKxy6aZrABVvbxuWqMPbohH4KRDBGqyO/OwxvUD1qHpqZ9cJT\n"
|
|
|
|
"bgttiIaXzdQny5esf6brI6Di/ssIp9awdLBlMT+eR6zR7g446tbxaCFuUiL0QIei\n"
|
|
|
|
"izEveTDNRbson/8DPJrn8/81doTeXsuV7YbqmtUGwdZ5kiocAW92ZZukdQIDAQAB\n"
|
|
|
|
"AoGBAI18yuLoMQdnQblBne8vZDumsDsmPaoCfc4EP2ETi/d+kaHPxTryABAkJq7j\n"
|
|
|
|
"kjZgdi6VGIUacbjOqK/Zxrcw/H460EwOUzh97Z4t9CDtDhz6t3ddT8CfbG2TUgbx\n"
|
|
|
|
"Vv3mSYSUDBdNBV6YY4fyLtZl6oI2V+rBaFIT48+vAK9doKlhAkEA2ZKm9dc80IjU\n"
|
|
|
|
"c/Wwn8ij+6ALs4Mpa0dPYivgZ2QhXiX5TfMymal2dDufkOH4wIUO+8vV8CSmmTRU\n"
|
|
|
|
"8Lv/B3pY7QJBAMSxeJtTSFwBcGRaZKRMIqeuZ/yMMT4EqqIh1DjBpujCRKApVpkO\n"
|
|
|
|
"kVx3Yu7xyOfniXBwujiYNSL6LrWdKykEsKkCQEr2UDgbtIRU4H4jhHtI8dbcSavL\n"
|
|
|
|
"4RVpOFymqWZ2BVke1EqbJC/1Ry687DlK4h3Sulre3BMlTZEziqB25WN6L/ECQBJv\n"
|
|
|
|
"B3yXG4rz35KoHhJ/yCeq4rf6c4r6aPt07Cy9iWT6/+96sFD72oet8KmwI0IIowrU\n"
|
|
|
|
"pb80FJbIl6QRrL/VXrECQBDdeCAG6J3Cwm4ozQiDQyiNd1qJqWc4co9savJxLtEU\n"
|
|
|
|
"s5L4Qwfrexm16oCJimGmsa5q6Y0n4f5gY+MRh3n+nQo=\n"
|
|
|
|
"-----END RSA PRIVATE KEY-----\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
//Test for validity
|
|
|
|
//QSslKey privateKey(key.toAscii(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
|
|
|
|
//qDebug() << "Valid private key:" << !privateKey.isNull();
|
|
|
|
|
|
|
|
config->group("myself").writeEntry("privateKey", key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config->group("myself").hasKey("publicKey")) {
|
|
|
|
|
|
|
|
//TODO: Generate
|
|
|
|
|
|
|
|
QByteArray key = QByteArray(
|
|
|
|
"-----BEGIN PUBLIC KEY-----\n"
|
|
|
|
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnKxy6aZrABVvbxuWqMPbohH4K\n"
|
|
|
|
"RDBGqyO/OwxvUD1qHpqZ9cJTbgttiIaXzdQny5esf6brI6Di/ssIp9awdLBlMT+e\n"
|
|
|
|
"R6zR7g446tbxaCFuUiL0QIeiizEveTDNRbson/8DPJrn8/81doTeXsuV7YbqmtUG\n"
|
|
|
|
"wdZ5kiocAW92ZZukdQIDAQAB\n"
|
|
|
|
"-----END PUBLIC KEY-----\n"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
//Test for validity
|
|
|
|
//QSslKey publicKey(key.toAscii(), QSsl::Rsa, QSsl::Pem, QSsl::PublicKey);
|
|
|
|
//qDebug() << "Valid public key:" << !publicKey.isNull();
|
|
|
|
|
|
|
|
config->group("myself").writeEntry("publicKey", key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
//Debugging
|
2013-07-24 17:42:33 +01:00
|
|
|
qDebug() << "Starting KdeConnect daemon";
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
//Load backends (hardcoded by now, should be plugins in a future)
|
2013-08-28 22:47:39 +01:00
|
|
|
mLinkProviders.insert(new LanLinkProvider());
|
|
|
|
mLinkProviders.insert(new LoopbackLinkProvider());
|
2013-07-02 00:50:32 +01:00
|
|
|
|
2013-07-02 01:46:41 +01:00
|
|
|
//Read remebered paired devices
|
2013-08-30 18:10:43 +01:00
|
|
|
const KConfigGroup& known = config->group("devices");
|
2013-07-02 01:46:41 +01:00
|
|
|
const QStringList& list = known.groupList();
|
2013-07-03 02:52:44 +01:00
|
|
|
Q_FOREACH(const QString& id, list) {
|
2013-08-30 18:10:43 +01:00
|
|
|
Device* device = new Device(id);
|
2013-08-13 22:23:32 +01:00
|
|
|
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceReachableStatusChanged()));
|
2013-07-24 17:42:33 +01:00
|
|
|
mDevices[id] = device;
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT deviceAdded(id);
|
2013-07-02 01:46:41 +01:00
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2013-08-07 10:29:56 +01:00
|
|
|
QNetworkSession* network = new QNetworkSession(QNetworkConfigurationManager().defaultConfiguration());
|
|
|
|
|
2013-06-18 19:04:53 +01:00
|
|
|
//Listen to incomming connections
|
2013-07-24 17:42:33 +01:00
|
|
|
Q_FOREACH (LinkProvider* a, mLinkProviders) {
|
2013-08-07 10:29:56 +01:00
|
|
|
connect(network, SIGNAL(stateChanged(QNetworkSession::State)),
|
|
|
|
a, SLOT(onNetworkChange(QNetworkSession::State)));
|
2013-08-12 15:09:52 +01:00
|
|
|
connect(a, SIGNAL(onConnectionReceived(NetworkPackage, DeviceLink*)),
|
|
|
|
this, SLOT(onNewDeviceLink(NetworkPackage, DeviceLink*)));
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
2013-07-04 18:17:22 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
setDiscoveryEnabled(true);
|
2013-06-17 11:23:08 +01:00
|
|
|
|
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
void Daemon::setDiscoveryEnabled(bool b)
|
2013-06-27 01:26:06 +01:00
|
|
|
{
|
|
|
|
//Listen to incomming connections
|
2013-07-24 17:42:33 +01:00
|
|
|
Q_FOREACH (LinkProvider* a, mLinkProviders) {
|
2013-08-07 10:29:56 +01:00
|
|
|
if (b)
|
|
|
|
a->onStart();
|
|
|
|
else
|
|
|
|
a->onStop();
|
2013-06-27 01:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-08-07 10:29:56 +01:00
|
|
|
void Daemon::forceOnNetworkChange()
|
|
|
|
{
|
|
|
|
Q_FOREACH (LinkProvider* a, mLinkProviders) {
|
|
|
|
a->onNetworkChange(QNetworkSession::Connected);
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 01:26:06 +01:00
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
QStringList Daemon::visibleDevices()
|
|
|
|
{
|
|
|
|
QStringList ret;
|
|
|
|
Q_FOREACH(Device* device, mDevices) {
|
|
|
|
if (device->reachable()) {
|
|
|
|
ret.append(device->id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
QStringList Daemon::devices()
|
2013-07-02 14:22:05 +01:00
|
|
|
{
|
2013-07-24 17:42:33 +01:00
|
|
|
return mDevices.keys();
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* dl)
|
2013-06-18 01:14:22 +01:00
|
|
|
{
|
2013-07-23 15:11:54 +01:00
|
|
|
const QString& id = identityPackage.get<QString>("deviceId");
|
2013-07-03 02:52:44 +01:00
|
|
|
|
2013-07-24 17:42:33 +01:00
|
|
|
qDebug() << "Device discovered" << id << "via" << dl->provider()->name();
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2013-07-24 17:42:33 +01:00
|
|
|
if (mDevices.contains(id)) {
|
2013-07-04 18:17:22 +01:00
|
|
|
qDebug() << "It is a known device";
|
2013-07-24 17:42:33 +01:00
|
|
|
Device* device = mDevices[id];
|
2013-07-03 02:52:44 +01:00
|
|
|
device->addLink(dl);
|
|
|
|
} else {
|
2013-07-04 18:17:22 +01:00
|
|
|
qDebug() << "It is a new device";
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
Device* device = new Device(identityPackage, dl);
|
2013-08-13 22:23:32 +01:00
|
|
|
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceReachableStatusChanged()));
|
2013-07-24 17:42:33 +01:00
|
|
|
mDevices[id] = device;
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT deviceAdded(id);
|
|
|
|
}
|
|
|
|
|
2013-08-15 21:45:33 +01:00
|
|
|
Q_EMIT deviceVisibilityChanged(id, true);
|
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Daemon::onDeviceReachableStatusChanged()
|
|
|
|
{
|
|
|
|
|
|
|
|
Device* device = (Device*)sender();
|
|
|
|
QString id = device->id();
|
|
|
|
|
2013-08-15 21:45:33 +01:00
|
|
|
Q_EMIT deviceVisibilityChanged(id, device->reachable());
|
2013-08-13 22:23:32 +01:00
|
|
|
|
2013-08-15 21:45:33 +01:00
|
|
|
if (!device->reachable()) {
|
2013-08-13 22:23:32 +01:00
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
if (!device->isPaired()) {
|
2013-08-16 08:27:32 +01:00
|
|
|
qDebug() << "Destroying device";
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT deviceRemoved(id);
|
|
|
|
mDevices.remove(id);
|
|
|
|
device->deleteLater();
|
|
|
|
}
|
|
|
|
|
2013-06-18 01:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2013-06-17 11:23:08 +01:00
|
|
|
|