kdeconnect-kde/daemon/daemon.cpp

184 lines
5.4 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-06 04:57:06 +01:00
#include "networkpackage.h"
2013-08-28 22:47:39 +01:00
#include "linkproviders/lanlinkprovider.h"
#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>
#include <QDebug>
2013-07-02 01:46:41 +01:00
#include <KConfig>
2013-07-02 01:46:41 +01:00
#include <KConfigGroup>
#include <KNotification>
#include <KIcon>
2013-06-06 04:57:06 +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
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
if (!config->group("myself").hasKey("id")) {
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;
}
if (!config->group("myself").hasKey("privateKey") || !config->group("myself").hasKey("publicKey")) {
//http://delta.affinix.com/docs/qca/rsatest_8cpp-example.html
QCA::PrivateKey privateKey = QCA::KeyGenerator().createRSA(1024);
2013-08-31 16:43:21 +01:00
config->group("myself").writeEntry("privateKey", privateKey.toDER());
QCA::PublicKey publicKey = privateKey.toPublicKey();
2013-08-31 16:43:21 +01:00
config->group("myself").writeEntry("publicKey", publicKey.toDER());
//TODO: Store key in a PEM file instead (KStandardDirs::locate("appdata", "private.pem"))
}
//Debugging
qDebug() << "Starting KdeConnect daemon";
2013-06-06 04:57:06 +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 01:46:41 +01:00
//Read remebered paired devices
const KConfigGroup& known = config->group("devices");
2013-07-02 01:46:41 +01:00
const QStringList& list = known.groupList();
Q_FOREACH(const QString& id, list) {
Device* device = new Device(id);
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceReachableStatusChanged()));
mDevices[id] = device;
Q_EMIT deviceAdded(id);
2013-07-02 01:46:41 +01:00
}
2013-08-07 10:29:56 +01:00
QNetworkSession* network = new QNetworkSession(QNetworkConfigurationManager().defaultConfiguration());
//Listen to incomming connections
Q_FOREACH (LinkProvider* a, mLinkProviders) {
2013-08-07 10:29:56 +01:00
connect(network, SIGNAL(stateChanged(QNetworkSession::State)),
a, SLOT(onNetworkChange(QNetworkSession::State)));
connect(a, SIGNAL(onConnectionReceived(NetworkPackage, DeviceLink*)),
this, SLOT(onNewDeviceLink(NetworkPackage, DeviceLink*)));
2013-06-06 04:57:06 +01:00
}
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
2013-06-06 04:57:06 +01:00
setDiscoveryEnabled(true);
2013-06-17 11:23:08 +01:00
}
void Daemon::setDiscoveryEnabled(bool b)
{
//Listen to incomming connections
Q_FOREACH (LinkProvider* a, mLinkProviders) {
2013-08-07 10:29:56 +01:00
if (b)
a->onStart();
else
a->onStop();
}
}
2013-08-07 10:29:56 +01:00
void Daemon::forceOnNetworkChange()
{
Q_FOREACH (LinkProvider* a, mLinkProviders) {
a->onNetworkChange(QNetworkSession::Connected);
}
}
QStringList Daemon::visibleDevices()
{
QStringList ret;
Q_FOREACH(Device* device, mDevices) {
if (device->isReachable()) {
ret.append(device->id());
}
}
return ret;
}
QStringList Daemon::devices()
2013-07-02 14:22:05 +01:00
{
return mDevices.keys();
2013-06-06 04:57:06 +01:00
}
void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* dl)
{
const QString& id = identityPackage.get<QString>("deviceId");
qDebug() << "Device discovered" << id << "via" << dl->provider()->name();
if (mDevices.contains(id)) {
qDebug() << "It is a known device";
Device* device = mDevices[id];
device->addLink(dl);
} else {
qDebug() << "It is a new device";
Device* device = new Device(identityPackage, dl);
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceReachableStatusChanged()));
mDevices[id] = device;
Q_EMIT deviceAdded(id);
}
Q_EMIT deviceVisibilityChanged(id, true);
}
void Daemon::onDeviceReachableStatusChanged()
{
Device* device = (Device*)sender();
QString id = device->id();
Q_EMIT deviceVisibilityChanged(id, device->isReachable());
if (!device->isReachable()) {
if (!device->isPaired()) {
2013-08-16 08:27:32 +01:00
qDebug() << "Destroying device";
Q_EMIT deviceRemoved(id);
mDevices.remove(id);
device->deleteLater();
}
}
}
2013-06-06 04:57:06 +01:00
Daemon::~Daemon()
{
2013-06-06 04:57:06 +01:00
}
2013-06-17 11:23:08 +01:00