kdeconnect-kde/core/daemon.cpp
Lukáš Tinkl eb38ad5330 turn critical debugs into qCCritical()
just in case, for the future... :)
2014-11-10 15:53:01 +01:00

244 lines
8.4 KiB
C++

/**
* 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"
#include <QDir>
#include <QUuid>
#include <QFile>
#include <QFileInfo>
#include <QDBusConnection>
#include <QNetworkSession>
#include <QNetworkConfigurationManager>
#include <QtCrypto>
#include <QStandardPaths>
#include <QNetworkAccessManager>
#include <KConfig>
#include <KConfigGroup>
#include <KSharedConfig>
#include "core_debug.h"
#include "dbushelper.h"
#include "networkpackage.h"
#include "backends/lan/lanlinkprovider.h"
#include "backends/loopback/loopbacklinkprovider.h"
#include "device.h"
#include "networkpackage.h"
#include "backends/devicelink.h"
#include "backends/linkprovider.h"
#include <QDebug>
struct DaemonPrivate
{
//Different ways to find devices and connect to them
QSet<LinkProvider*> mLinkProviders;
//Every known device
QMap<QString, Device*> mDevices;
// The Initializer object sets things up, and also does cleanup when it goes out of scope
// Note it's not being used anywhere. That's inteneded
QCA::Initializer mQcaInitializer;
};
Daemon::Daemon(QObject *parent)
: QObject(parent)
, d(new DaemonPrivate)
{
qCDebug(KDECONNECT_CORE) << "KdeConnect daemon starting";
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
if (!config->group("myself").hasKey("id")) {
QString uuid = QUuid::createUuid().toString();
DbusHelper::filterNonExportableCharacters(uuid);
config->group("myself").writeEntry("id", uuid);
config->sync();
qCDebug(KDECONNECT_CORE) << "My id:" << uuid;
}
//qCDebug(KDECONNECT_CORE) << "QCA supported capabilities:" << QCA::supportedFeatures().join(",");
if(!QCA::isSupported("rsa")) {
//TODO: Display this in a notification or another visible way
qCCritical(KDECONNECT_CORE) << "Error: KDE Connect could not find support for RSA in your QCA installation, if your distribution provides"
<< "separate packages for QCA-ossl and QCA-gnupg plugins, make sure you have them installed and try again";
return;
}
const QFile::Permissions strict = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser;
if (!config->group("myself").hasKey("privateKeyPath"))
{
QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
const QString privateKeyPath = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QStringLiteral("/key.pem");
QFile privKey(privateKeyPath);
if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate))
{
qCCritical(KDECONNECT_CORE) << "Error: KDE Connect could not create private keys file: " << privateKeyPath;
return;
}
if (!privKey.setPermissions(strict))
{
qCDebug(KDECONNECT_CORE) << "Error: KDE Connect could not set permissions for private file: " << privateKeyPath;
}
//http://delta.affinix.com/docs/qca/rsatest_8cpp-example.html
if (config->group("myself").hasKey("privateKey")) {
//Migration from older versions of KDE Connect
privKey.write(config->group("myself").readEntry<QString>("privateKey",QCA::KeyGenerator().createRSA(2048).toPEM()).toLatin1());
} else {
privKey.write(QCA::KeyGenerator().createRSA(2048).toPEM().toLatin1());
}
privKey.close();
//TODO: This should not store an absolute path: it will cause problems if the home folder changes, .kde4 becomes .kde (debian?), or similar...
config->group("myself").writeEntry("privateKeyPath", privateKeyPath);
config->sync();
}
if (QFile::permissions(config->group("myself").readEntry("privateKeyPath")) != strict)
{
qCDebug(KDECONNECT_CORE) << "Error: KDE Connect detects wrong permissions for private file " << config->group("myself").readEntry("privateKeyPath");
}
//Register on DBus
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect", this, QDBusConnection::ExportScriptableContents);
//Load backends (hardcoded by now, should be plugins in a future)
d->mLinkProviders.insert(new LanLinkProvider());
//d->mLinkProviders.insert(new LoopbackLinkProvider());
//Read remebered paired devices
const KConfigGroup& known = config->group("trusted_devices");
const QStringList& list = known.groupList();
Q_FOREACH(const QString& id, list) {
Device* device = new Device(this, id);
connect(device, SIGNAL(reachableStatusChanged()),
this, SLOT(onDeviceReachableStatusChanged()));
d->mDevices[id] = device;
Q_EMIT deviceAdded(id);
}
//Listen to new devices
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
connect(a, SIGNAL(onConnectionReceived(NetworkPackage, DeviceLink*)),
this, SLOT(onNewDeviceLink(NetworkPackage, DeviceLink*)));
}
setDiscoveryEnabled(true);
//Listen to connectivity changes
QNetworkConfigurationManager* manager = new QNetworkConfigurationManager();
QNetworkSession* network = new QNetworkSession(manager->defaultConfiguration());
connect(manager, SIGNAL(configurationAdded(QNetworkConfiguration)),
this, SLOT(forceOnNetworkChange()));
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
connect(network, SIGNAL(stateChanged(QNetworkSession::State)),
a, SLOT(onNetworkChange(QNetworkSession::State)));
}
qCDebug(KDECONNECT_CORE) << "KdeConnect daemon started";
}
void Daemon::setDiscoveryEnabled(bool b)
{
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
if (b)
a->onStart();
else
a->onStop();
}
}
void Daemon::forceOnNetworkChange()
{
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
a->onNetworkChange(QNetworkSession::Connected);
}
}
QStringList Daemon::devices(bool onlyReachable, bool onlyVisible) const
{
QStringList ret;
Q_FOREACH(Device* device, d->mDevices) {
if (onlyReachable && !device->isReachable()) continue;
if (onlyVisible && !device->isPaired()) continue;
ret.append(device->id());
}
return ret;
}
void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* dl)
{
const QString& id = identityPackage.get<QString>("deviceId");
//qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via" << dl->provider()->name();
if (d->mDevices.contains(id)) {
//qCDebug(KDECONNECT_CORE) << "It is a known device";
Device* device = d->mDevices[id];
device->addLink(identityPackage, dl);
} else {
//qCDebug(KDECONNECT_CORE) << "It is a new device";
Device* device = new Device(this, identityPackage, dl);
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceReachableStatusChanged()));
d->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());
//qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "reachable status changed:" << device->isReachable();
if (!device->isReachable()) {
if (!device->isPaired()) {
qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name();
Q_EMIT deviceRemoved(id);
d->mDevices.remove(id);
device->deleteLater();
}
}
}
Daemon::~Daemon()
{
}