LinkProviders cleanup

This commit is contained in:
Albert Vaca 2013-08-28 23:47:39 +02:00
parent eef2bea161
commit 520d776a47
15 changed files with 59 additions and 248 deletions

View file

@ -2,12 +2,11 @@ set(kded_kdeconnect_SRCS
linkproviders/linkprovider.cpp
linkproviders/loopbacklinkprovider.cpp
#linkproviders/avahitcplinkprovider.cpp
linkproviders/broadcasttcplinkprovider.cpp
linkproviders/lanlinkprovider.cpp
devicelinks/devicelink.cpp
devicelinks/echodevicelink.cpp
devicelinks/tcpdevicelink.cpp
devicelinks/loopbackdevicelink.cpp
devicelinks/landevicelink.cpp
plugins/kdeconnectplugin.cpp
plugins/pluginloader.cpp

View file

@ -22,8 +22,7 @@
#include "networkpackage.h"
#include "linkproviders/broadcasttcplinkprovider.h"
#include "linkproviders/avahitcplinkprovider.h"
#include "linkproviders/lanlinkprovider.h"
#include "linkproviders/loopbacklinkprovider.h"
#include <QUuid>
@ -55,9 +54,8 @@ Daemon::Daemon(QObject *parent, const QList<QVariant>&)
qDebug() << "Starting KdeConnect daemon";
//Load backends (hardcoded by now, should be plugins in a future)
mLinkProviders.insert(new BroadcastTcpLinkProvider());
//mLinkProviders.insert(new AvahiTcpLinkProvider());
//mLinkProviders.insert(new LoopbackLinkProvider());
mLinkProviders.insert(new LanLinkProvider());
mLinkProviders.insert(new LoopbackLinkProvider());
//Read remebered paired devices
const KConfigGroup& known = config->group("devices").group("paired");

View file

@ -67,7 +67,7 @@ public:
Q_SIGNALS:
void receivedPackage(const NetworkPackage& np);
public Q_SLOTS:
bool sendPackage(const NetworkPackage& np) const;
virtual bool sendPackage(const NetworkPackage& np) const;
//Dbus operations called from kcm
public Q_SLOTS:
@ -77,7 +77,7 @@ public Q_SLOTS:
private Q_SLOTS:
void linkDestroyed(QObject* o = 0);
void privateReceivedPackage(const NetworkPackage& np);
virtual void privateReceivedPackage(const NetworkPackage& np);
Q_SIGNALS:
Q_SCRIPTABLE void reachableStatusChanged();

View file

@ -18,12 +18,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tcpdevicelink.h"
#include "landevicelink.h"
#include "linkproviders/linkprovider.h"
#include "networkpackage.h"
TcpDeviceLink::TcpDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* socket)
LanDeviceLink::LanDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* socket)
: DeviceLink(d, a)
{
mSocket = socket;
@ -33,15 +33,15 @@ TcpDeviceLink::TcpDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* sock
this, SLOT(dataReceived()));
}
bool TcpDeviceLink::sendPackage(const NetworkPackage& np) const
bool LanDeviceLink::sendPackage(const NetworkPackage& np) const
{
int written = mSocket->write(np.serialize());
return written != -1;
}
void TcpDeviceLink::dataReceived()
void LanDeviceLink::dataReceived()
{
qDebug() << "TcpDeviceLink dataReceived";
qDebug() << "LanDeviceLink dataReceived";
QByteArray data = mSocket->readAll();
QList<QByteArray> packages = data.split('\n');

View file

@ -18,8 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TCPDEVICELINK_H
#define TCPDEVICELINK_H
#ifndef LANDEVICELINK_H
#define LANDEVICELINK_H
#include <QObject>
#include <QString>
@ -30,13 +30,13 @@
class AvahiTcpLinkProvider;
class TcpDeviceLink
class LanDeviceLink
: public DeviceLink
{
Q_OBJECT
public:
TcpDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* socket);
LanDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* socket);
bool sendPackage(const NetworkPackage& np) const;

View file

@ -18,11 +18,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "echodevicelink.h"
#include "loopbackdevicelink.h"
#include "linkproviders/loopbacklinkprovider.h"
EchoDeviceLink::EchoDeviceLink(const QString& deviceId, LoopbackLinkProvider* provider)
LoopbackDeviceLink::LoopbackDeviceLink(const QString& deviceId, LoopbackLinkProvider* provider)
: DeviceLink(deviceId, provider)
{

View file

@ -24,12 +24,12 @@
class LoopbackLinkProvider;
class EchoDeviceLink
class LoopbackDeviceLink
: public DeviceLink
{
Q_OBJECT
public:
EchoDeviceLink(const QString& d, LoopbackLinkProvider* a);
LoopbackDeviceLink(const QString& d, LoopbackLinkProvider* a);
bool sendPackage(const NetworkPackage& np) const {
Q_EMIT receivedPackage(np);

View file

@ -1,126 +0,0 @@
/**
* 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 "avahitcplinkprovider.h"
#include "devicelinks/tcpdevicelink.h"
#include <QHostInfo>
#include <QTcpServer>
AvahiTcpLinkProvider::AvahiTcpLinkProvider()
{
QString serviceType = "_kdeconnect._tcp";
//http://api.kde.org/4.x-api/kdelibs-apidocs/dnssd/html/index.html
service = new DNSSD::PublicService(QHostInfo::localHostName(), serviceType, port);
mServer = new QTcpServer(this);
connect(mServer,SIGNAL(newConnection()),this, SLOT(newConnection()));
}
void AvahiTcpLinkProvider::onStart()
{
mServer->listen(QHostAddress::Any, port);
service->publishAsync();
}
void AvahiTcpLinkProvider::onStop()
{
mServer->close();
service->stop();
}
void AvahiTcpLinkProvider::onNetworkChange(QNetworkSession::State state)
{
Q_UNUSED(state);
//Nothing to do, Avahi will handle it
}
void AvahiTcpLinkProvider::newConnection()
{
qDebug() << "AvahiTcpLinkProvider newConnection";
QTcpSocket* socket = mServer->nextPendingConnection();
socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
connect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
NetworkPackage np(PACKAGE_TYPE_IDENTITY);
NetworkPackage::createIdentityPackage(&np);
int written = socket->write(np.serialize());
qDebug() << "AvahiTcpLinkProvider sent package." << written << " bytes written, waiting for reply";
}
void AvahiTcpLinkProvider::dataReceived()
{
QTcpSocket* socket = (QTcpSocket*) QObject::sender();
QByteArray data = socket->readLine();
qDebug() << "AvahiTcpLinkProvider received reply:" << data;
NetworkPackage np("");
NetworkPackage::unserialize(data,&np);
if (np.version() > 0 && np.type() == PACKAGE_TYPE_IDENTITY) {
const QString& id = np.get<QString>("deviceId");
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*)));
if (links.contains(id)) {
//Delete old link if we already know it, probably it is down if this happens.
qDebug() << "Destroying old link";
delete links[id];
}
links[id] = dl;
qDebug() << "AvahiTcpLinkProvider creating link to device" << id << "(" << socket->peerAddress() << ")";
Q_EMIT onConnectionReceived(np, dl);
disconnect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
} else {
qDebug() << "AvahiTcpLinkProvider/newConnection: Not an identification package (wuh?)";
}
}
void AvahiTcpLinkProvider::deviceLinkDestroyed(QObject* uncastedDeviceLink)
{
DeviceLink* deviceLink = (DeviceLink*)uncastedDeviceLink;
Q_EMIT onConnectionLost(deviceLink);
const QString& id = deviceLink->deviceId();
if (links.contains(id)) links.remove(id);
}
AvahiTcpLinkProvider::~AvahiTcpLinkProvider()
{
delete service;
}

View file

@ -1,64 +0,0 @@
/**
* 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/>.
*/
#ifndef AVAHITCPLINKPROVIDER_H
#define AVAHITCPLINKPROVIDER_H
#include <QObject>
#include <QTcpServer>
#include <KDE/DNSSD/PublicService>
#include "linkprovider.h"
#include "netaddress.h"
class AvahiTcpLinkProvider
: public LinkProvider
{
Q_OBJECT
public:
AvahiTcpLinkProvider();
~AvahiTcpLinkProvider();
QString name() { return "AvahiTcpLinkProvider"; }
int priority() { return PRIORITY_HIGH + 1; }
public Q_SLOTS:
virtual void onNetworkChange(QNetworkSession::State state);
virtual void onStart();
virtual void onStop();
private Q_SLOTS:
void newConnection();
void deviceLinkDestroyed(QObject*);
void dataReceived();
private:
DNSSD::PublicService* service;
QTcpServer* mServer;
static const quint16 port = 10602;
QMap<QString, DeviceLink*> links;
};
#endif

View file

@ -18,16 +18,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "broadcasttcplinkprovider.h"
#include "lanlinkprovider.h"
#include <QHostInfo>
#include <QTcpServer>
#include <QUdpSocket>
#include "devicelinks/tcpdevicelink.h"
#include "devicelinks/landevicelink.h"
#include "networkpackage.h"
BroadcastTcpLinkProvider::BroadcastTcpLinkProvider()
LanLinkProvider::LanLinkProvider()
{
mUdpServer = new QUdpSocket(this);
@ -38,7 +38,7 @@ BroadcastTcpLinkProvider::BroadcastTcpLinkProvider()
}
void BroadcastTcpLinkProvider::onStart()
void LanLinkProvider::onStart()
{
mUdpServer->bind(QHostAddress::Broadcast, port, QUdpSocket::ShareAddress);
@ -48,14 +48,14 @@ void BroadcastTcpLinkProvider::onStart()
onNetworkChange(QNetworkSession::Connected);
}
void BroadcastTcpLinkProvider::onStop()
void LanLinkProvider::onStop()
{
mUdpServer->close();
mTcpServer->close();
}
//I'm in a new network, let's be polite and introduce myself
void BroadcastTcpLinkProvider::onNetworkChange(QNetworkSession::State state)
void LanLinkProvider::onNetworkChange(QNetworkSession::State state)
{
qDebug() << "onNetworkChange" << state;
NetworkPackage np("");
@ -65,7 +65,7 @@ void BroadcastTcpLinkProvider::onNetworkChange(QNetworkSession::State state)
}
//I'm the existing device, a new device is kindly introducing itself (I will create a TcpSocket)
void BroadcastTcpLinkProvider::newUdpConnection()
void LanLinkProvider::newUdpConnection()
{
while (mUdpServer->hasPendingDatagrams()) {
QByteArray datagram;
@ -109,7 +109,7 @@ void BroadcastTcpLinkProvider::newUdpConnection()
}
void BroadcastTcpLinkProvider::connectError()
void LanLinkProvider::connectError()
{
QTcpSocket* socket = (QTcpSocket*)sender();
@ -124,7 +124,7 @@ void BroadcastTcpLinkProvider::connectError()
}
void BroadcastTcpLinkProvider::connected()
void LanLinkProvider::connected()
{
QTcpSocket* socket = (QTcpSocket*)sender();
@ -136,7 +136,7 @@ void BroadcastTcpLinkProvider::connected()
const QString& id = np->get<QString>("deviceId");
//qDebug() << "Connected" << socket->isWritable();
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
LanDeviceLink* dl = new LanDeviceLink(id, this, socket);
NetworkPackage np2("");
NetworkPackage::createIdentityPackage(&np2);
@ -178,9 +178,9 @@ void BroadcastTcpLinkProvider::connected()
}
//I'm the new device and this is the answer to my UDP introduction (no data received yet)
void BroadcastTcpLinkProvider::newConnection()
void LanLinkProvider::newConnection()
{
qDebug() << "BroadcastTcpLinkProvider newConnection";
qDebug() << "LanLinkProvider newConnection";
QTcpSocket* socket = mTcpServer->nextPendingConnection();
socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
@ -192,18 +192,18 @@ void BroadcastTcpLinkProvider::newConnection()
NetworkPackage::createIdentityPackage(&np);
int written = socket->write(np.serialize());
qDebug() << "BroadcastTcpLinkProvider sent package." << written << " bytes written, waiting for reply";
qDebug() << "LanLinkProvider sent package." << written << " bytes written, waiting for reply";
*/
}
//I'm the new device and this is the answer to my UDP introduction (data received)
void BroadcastTcpLinkProvider::dataReceived()
void LanLinkProvider::dataReceived()
{
QTcpSocket* socket = (QTcpSocket*) QObject::sender();
QByteArray data = socket->readLine();
qDebug() << "BroadcastTcpLinkProvider received reply:" << data;
qDebug() << "LanLinkProvider received reply:" << data;
NetworkPackage np("");
NetworkPackage::unserialize(data,&np);
@ -211,7 +211,7 @@ void BroadcastTcpLinkProvider::dataReceived()
if (np.version() > 0 && np.type() == PACKAGE_TYPE_IDENTITY) {
const QString& id = np.get<QString>("deviceId");
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
LanDeviceLink* dl = new LanDeviceLink(id, this, socket);
qDebug() << "Handshaking done (i'm the new device)";
@ -234,12 +234,12 @@ void BroadcastTcpLinkProvider::dataReceived()
disconnect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
} else {
qDebug() << "BroadcastTcpLinkProvider/newConnection: Not an identification package (wuh?)";
qDebug() << "LanLinkProvider/newConnection: Not an identification package (wuh?)";
}
}
void BroadcastTcpLinkProvider::deviceLinkDestroyed(QObject* uncastedDeviceLink)
void LanLinkProvider::deviceLinkDestroyed(QObject* uncastedDeviceLink)
{
qDebug() << "deviceLinkDestroyed";
@ -253,7 +253,7 @@ void BroadcastTcpLinkProvider::deviceLinkDestroyed(QObject* uncastedDeviceLink)
}
BroadcastTcpLinkProvider::~BroadcastTcpLinkProvider()
LanLinkProvider::~LanLinkProvider()
{
}

View file

@ -18,8 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BROADCASTTCPLINKPROVIDER_H
#define BROADCASTTCPLINKPROVIDER_H
#ifndef LANLINKPROVIDER_H
#define LANLINKPROVIDER_H
#include <QObject>
#include <QTcpServer>
@ -28,18 +28,17 @@
#include "linkprovider.h"
#include "netaddress.h"
class BroadcastTcpLinkProvider
class LanLinkProvider
: public LinkProvider
{
Q_OBJECT
public:
BroadcastTcpLinkProvider();
~BroadcastTcpLinkProvider();
LanLinkProvider();
~LanLinkProvider();
QString name() { return "BroadcastTcpLinkProvider"; }
int priority() { return PRIORITY_HIGH + 5; }
QString name() { return "LanLinkProvider"; }
int priority() { return PRIORITY_HIGH + 10; }
public Q_SLOTS:
virtual void onNetworkChange(QNetworkSession::State state);

View file

@ -19,7 +19,6 @@
*/
#include "loopbacklinkprovider.h"
#include "devicelinks/echodevicelink.h"
#include <QDebug>
@ -38,17 +37,17 @@ void LoopbackLinkProvider::onNetworkChange(QNetworkSession::State state)
{
Q_UNUSED(state);
qDebug() << "Echo Device discovery emitted";
Q_EMIT onConnectionReceived(identityPackage, echoDeviceLink);
Q_EMIT onConnectionReceived(identityPackage, loopbackDeviceLink);
}
void LoopbackLinkProvider::onStart()
{
echoDeviceLink = new EchoDeviceLink("loopback", this);
loopbackDeviceLink = new LoopbackDeviceLink("loopback", this);
onNetworkChange(QNetworkSession::Connected);
}
void LoopbackLinkProvider::onStop()
{
delete echoDeviceLink;
delete loopbackDeviceLink;
}

View file

@ -23,6 +23,7 @@
#include "linkprovider.h"
#include "networkpackage.h"
#include "devicelinks/loopbackdevicelink.h"
class LoopbackLinkProvider
: public LinkProvider
@ -40,7 +41,7 @@ public:
virtual void onNetworkChange(QNetworkSession::State state);
private:
DeviceLink* echoDeviceLink;
LoopbackDeviceLink* loopbackDeviceLink;
NetworkPackage identityPackage;
};

View file

@ -22,11 +22,15 @@
#define NETWORKPACKAGE_H
#include "networkpackagetypes.h"
#include <QObject>
#include <QString>
#include <QVariant>
#include <QStringList>
#include <QSsl>
#include <qjson/parser.h>
#include "default_args.h"
class NetworkPackage : public QObject
@ -44,6 +48,9 @@ public:
static void unserialize(const QByteArray&, NetworkPackage*);
QByteArray serialize() const;
static void rsaUnserialize(const QByteArray&, NetworkPackage*, Qssl );
QByteArray rsaSerialize() const;
static void createIdentityPackage(NetworkPackage*);
long id() const { return mId; }

View file

@ -188,11 +188,9 @@ bool NotificationsModel::isAnyDimissable()
{
Q_FOREACH(NotificationDbusInterface* notification, m_notificationList) {
if (notification->dismissable()) {
qDebug() << "Dismisable true";
return true;
}
}
qDebug() << "Dismisable false";
return false;
}